forked from OpenShiftDemos/os-sample-python
/
wsgi.py
214 lines (161 loc) · 6.3 KB
/
wsgi.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import time
import os
import face_recognition
from flask import Flask, jsonify, request, redirect, g, render_template
from flask_basicauth import BasicAuth
import psycopg2
# You can change this to any folder on your system
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__, static_url_path='/static')
app.config['BASIC_AUTH_USERNAME'] = 'root'
app.config['BASIC_AUTH_PASSWORD'] = 'toor'
basic_auth = BasicAuth(app)
@app.before_request
def before_request():
g.request_start_time = time.time()
g.request_time = lambda: "%.5fs" % (time.time() - g.request_start_time)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/first', methods=['GET', 'POST'])
#@basic_auth.required
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return detect_faces_in_image(file)
# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Fissss</title>
<h1>reutn face 128d</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''
def detect_faces_in_image(file_stream):
# Load the uploaded image file
img = face_recognition.load_image_file(file_stream)
# Get face encodings for any faces in the uploaded image
face_encodings = face_recognition.face_encodings(img)
face_found = False
# Return the result as json
result = {
"encoding": str(face_encodings),
"r.time": g.request_time()
}
return jsonify(result)
def dbin(file_stream, filename):
file_name = filename
# Load the uploaded image file
img = face_recognition.load_image_file(file_stream)
# Get face encodings for any faces in the uploaded image
encodings = face_recognition.face_encodings(img)
if len(encodings) > 1:
return "Multiple faces not allowed"
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
db = conn.cursor()
if len(encodings) > 0:
query = "INSERT INTO vectors (file, vec_low, vec_high) VALUES ('{}', CUBE(array[{}]), CUBE(array[{}]))".format( file_name, ','.join(str(s) for s in encodings[0][0:64]), ','.join(str(s) for s in encodings[0][64:128]),)
db.execute(query)
conn.commit()
db.close()
conn.close()
return "Added into databse"
@app.route('/addface', methods=['GET', 'POST'])
@basic_auth.required
def addface():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
if 'firstname' not in request.form:
return redirect(request.url)
file = request.files['file']
filename = request.form['firstname']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return dbin(file,filename)
# If no valid image file was uploaded, show the file upload form:
return '''
<!doctype html>
<title>Fissss</title>
<h1>Addd Face (only one face in picture</h1>
<form method="POST" enctype="multipart/form-data" action="/addface" >
<input type="text" name="firstname">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''
# =================================== find face strt V ====================
def dbout(file_stream):
# Load the uploaded image file
img = face_recognition.load_image_file(file_stream)
# Get face encodings for any faces in the uploaded image
encodings = face_recognition.face_encodings(img)
if len(encodings) > 1:
return "Multiple faces not allowed"
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
db = conn.cursor()
threshold = 0.6
if len(encodings) > 0:
query = "SELECT file FROM vectors WHERE sqrt(power(CUBE(array[{}]) <-> vec_low, 2) + power(CUBE(array[{}]) <-> vec_high, 2)) <= {} ".format(
','.join(str(s) for s in encodings[0][0:64]),
','.join(str(s) for s in encodings[0][64:128]),
threshold,
) + \
"ORDER BY sqrt(power(CUBE(array[{}]) <-> vec_low, 2) + power(CUBE(array[{}]) <-> vec_high, 2)) ASC LIMIT 1".format(
','.join(str(s) for s in encodings[0][0:64]),
','.join(str(s) for s in encodings[0][64:128]),
)
db.execute(query)
res = db.fetchone()
db.close()
conn.close()
return res
@app.route('/findface', methods=['GET', 'POST'])
@basic_auth.required
def findface():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return dbout(file)
# If no valid image file was uploaded, show the file upload form:
return render_template('imgup.html') #render_template('findface.html')
# ===================================find face end==================
@app.route('/', methods=['GET', 'POST'])
def webcam():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'webcam' not in request.files:
return redirect(request.url)
file = request.files['webcam']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
# The image file seems valid! Detect faces and return the result.
return dbout(file)
# If no valid image file was uploaded, show the file upload form:
return render_template('webcambt2.html')
@app.route('/phone', methods=['GET', 'POST'])
def phone():
return render_template('phone.html')
if __name__ == "__main__":
application.run()