-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
137 lines (113 loc) · 4.54 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Course: CST 205 Multimedia Design and Programming
# Title: Hide Yo Kids...
# Description:
# Input - Users upload images
# Process - Detect children in user's upload using an image classifier
# Output - Blurs and saves the region of interest if child is detected
# Authors: Michael Cwener, Dustin D'Avignon, Spencer Ortega
# Date: December 11th 2017
# Michael: OpenCV
# Dustin: Flask
# Spencer: OpenCV/Flask, marketing, and vision of "Hide Yo Kids..."
# Github link: https://github.com/ddavignon/image-detection
from flask import Flask, render_template, redirect, request, url_for, flash, jsonify, session
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter, current_user
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
# from flask_wtf import FlaskForm
from flask_dropzone import Dropzone
# from flask_wtf.file import FileField, FileRequired, FileAllowed
# from wtforms import SubmitField
from face_detection import check_image_for_minors
import uuid
import os
app = Flask(__name__)
Bootstrap(app)
dropzone = Dropzone(app)
app.config['SECRET_KEY'] = 'supersecretkeygoeshere'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['CSRF_ENABLED'] = True
app.config['USER_ENABLE_EMAIL'] = False
app.config['USER_APP_NAME'] = "Flask App"
# Dropzone setting
app.config['DROPZONE_UPLOAD_MULTIPLE'] = True
app.config['DROPZONE_ALLOWED_FILE_CUSTOM'] = True
app.config['DROPZONE_ALLOWED_FILE_TYPE'] = 'image/*'
app.config['DROPZONE_REDIRECT_VIEW'] = 'results'
# Uploads settings
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd() + '/static/tmp/uploads'
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app) # set maximum file size, default is 16MB
db = SQLAlchemy(app)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')
active = db.Column(db.Boolean(), nullable=False, server_default='0')
db_adapter = SQLAlchemyAdapter(db, User)
user_manager = UserManager(db_adapter, app)
@app.route('/profile')
@login_required
def profile():
return render_template('pages/user_profile.html')
@app.route('/')
@app.route('/home')
def home():
# grab users saved uploads and children detections
face_images = os.listdir(os.path.join(app.static_folder, "tmp/faces"))
face_images = filter(lambda x: x != '.keep', face_images)
uploaded_images = os.listdir(os.path.join(app.static_folder, "tmp/uploads"))
return render_template(
'pages/home.html',
active='home',
face_images=face_images,
uploaded_images=uploaded_images
)
@app.route('/about')
def about():
return render_template('pages/about.html', active='about')
@app.route('/upload', methods=['GET', 'POST'])
@login_required
def upload():
# set session for image results
if "file_urls" not in session:
session['file_urls'] = []
file_urls = session['file_urls']
# handle image upload from Dropszone
if request.method == 'POST':
directory_name=os.getcwd()+"/static/tmp/uploads"
raw_files = request.files
for raw_file in raw_files:
file = request.files.get(raw_file)
ext = os.path.splitext(file.filename)[1]
unique_name = uuid.uuid4().hex + ext
# initial save of photo to test
filename = photos.save(
file,
name=unique_name
)
# check image for minors / sanitize image
check_image_for_minors(directory_name + "/" + unique_name)
# append image urls
file_urls.append(photos.url(filename))
session['file_urls'] = file_urls
# return dropzone template on GET request
return render_template('pages/upload.html', active='upload', file_urls=file_urls)
@app.route('/results')
@login_required
def results():
if "file_urls" not in session or session['file_urls'] == []:
return redirect(url_for('upload'))
file_urls = session['file_urls']
session.pop('file_urls', None)
return render_template("pages/files.html", file_urls=file_urls)
if __name__ == '__main__':
db.create_all()
app.run(
host=os.getenv('IP', '0.0.0.0'),
port=int(os.getenv('PORT', 8080)),
debug=True
)