-
Notifications
You must be signed in to change notification settings - Fork 16
/
faceServer.py
363 lines (288 loc) · 11.7 KB
/
faceServer.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from flask import Response, Flask, send_file, make_response, request
from flask import render_template, jsonify
from io import BytesIO
from skimage.io import imsave
import cv2
import numpy as np
import os
os.environ['GLOG_minloglevel'] = '2'
import caffe
import sys
import json
import time
import pyw_hnswlib as hnswlib
from flask_cors import CORS, cross_origin
import urllib.request
import requests
import socket
sys.path.append("mtcnn")
import mtcnn
minsize = 40
threshold = [0.8, 0.8, 0.6]
factor = 0.709
caffe_model_path = "./mtcnn"
imgSavePath = "static/uploadImages"
max_elements = 10*10000
faceSearchData = "./faceSearchData"
hnswModel = hnswlib.Index(space='l2', dim=128)
dThreshold = 0.6
port = 9090
caffe.set_mode_cpu()
PNet = caffe.Net(caffe_model_path+"/det1.prototxt", caffe_model_path+"/det1.caffemodel", caffe.TEST)
RNet = caffe.Net(caffe_model_path+"/det2.prototxt", caffe_model_path+"/det2.caffemodel", caffe.TEST)
ONet = caffe.Net(caffe_model_path+"/det3.prototxt", caffe_model_path+"/det3.caffemodel", caffe.TEST)
caffePrototxt = 'InceptionResnet_Model/resnetInception-128.prototxt'
caffemodel = 'InceptionResnet_Model/inception_resnet_v1_conv1x1.caffemodel'
net = caffe.Net(caffePrototxt, caffemodel, caffe.TEST)
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def prewhiten(x):
mean = np.mean(x)
std = np.std(x)
std_adj = np.maximum(std, 1.0/np.sqrt(x.size))
y = np.multiply(np.subtract(x, mean), 1/std_adj)
return y
def normL2Vector(bottleNeck):
sum = 0
for v in bottleNeck:
sum += np.power(v, 2)
sqrt = np.max([np.sqrt(sum), 0.0000000001])
vector = np.zeros((bottleNeck.shape))
for (i, v) in enumerate(bottleNeck):
vector[i] = v/sqrt
return vector.astype(np.float32)
def calcCaffeVector(image):
image = cv2.resize(image, (160,160))
prewhitened = prewhiten(image)[np.newaxis]
inputCaffe = prewhitened.transpose((0,3,1,2)) #[1,3,160,160]
net.blobs['data'].data[...] = inputCaffe
net.forward()
vector = normL2Vector(net.blobs['flatten'].data.squeeze())
return vector
def mtcnnDetect(image):
if(image.shape[2]!=3 and image.shape[2]!=4):
return [],[],[]
if(image.shape[2]==4):
image = image[:,:,:-1]
img_matlab = image.copy()
tmp = img_matlab[:,:,2].copy()
img_matlab[:,:,2] = img_matlab[:,:,0]
img_matlab[:,:,0] = tmp
# boundingboxes: [None, 5] => the last dim is probability.
boundingboxes, points = mtcnn.detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor)
boundingboxes = boundingboxes.astype(np.int32)
vectors = []
for i in range(boundingboxes.shape[0]):
left = boundingboxes[i][0]
right = boundingboxes[i][2]
top = boundingboxes[i][1]
bottom = boundingboxes[i][3]
old_size = (right-left+bottom-top)/2.0
centerX = right - (right-left)/2.0
centerY = bottom - (bottom-top)/2 + old_size*0.1
size = int(old_size*1.15)
x1 = int(centerX-size/2)
y1 = int(centerY-size/2)
x2 = int(centerX+size/2)
y2 = int(centerY+size/2)
width = x2 - x1
height = y2 - y1
rectify_x1 = x1
rectify_y1 = y1
warped = img_matlab
if(x2>img_matlab.shape[1]):
warped = cv2.copyMakeBorder(img_matlab, 0, 0, 0, x2-img_matlab.shape[1], cv2.BORDER_CONSTANT)
if(x1<0):
warped = cv2.copyMakeBorder(img_matlab, 0, 0, -x1, 0, cv2.BORDER_CONSTANT)
rectify_x1 = 0
if(y2>img_matlab.shape[0]):
warped = cv2.copyMakeBorder(img_matlab, 0, y2-img_matlab.shape[0], 0, 0, cv2.BORDER_CONSTANT)
if(y1<0):
warped = cv2.copyMakeBorder(img_matlab, -y1, 0, 0, 0, cv2.BORDER_CONSTANT)
rectify_y1 = 0
warped = warped[rectify_y1:y2, rectify_x1:x2]
vector = calcCaffeVector(warped)
vectors.append(vector)
if(left<0):
boundingboxes[i][0] = 0
if(top<0):
boundingboxes[i][1] = 0
if(right>img_matlab.shape[1]):
boundingboxes[i][2] = img_matlab.shape[1]
if(bottom>img_matlab.shape[0]):
boundingboxes[i][3] = img_matlab.shape[0]
return boundingboxes, points, vectors
app = Flask(__name__)
@app.route("/faceRegister", methods=['GET', 'POST'])
def faceRegister():
if request.method == 'POST': # post by browser
names = request.form['names']
faceArr = []
for name in names[:-1].split("*"):
file = request.files[name]
img = np.fromstring(file.read(), np.uint8)
filename = imgSavePath +"/"+ str(time.time()) +".jpg"
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED)
cv2.imwrite(filename, img)
boundingboxes, points, vectors = mtcnnDetect(img)
filenames = []
faceDict = {}
boxPtArr = []
for i in range(boundingboxes.shape[0]):
filenames.append(filename)
boxPtDict = {}
box = []
box.append(int(boundingboxes[i][0]))
box.append(int(boundingboxes[i][1]))
box.append(int(boundingboxes[i][2]))
box.append(int(boundingboxes[i][3]))
boxPtDict["box"] = box
pts = []
for j in range(10):
pts.append(int(points[i][j]))
boxPtDict["pt"] = pts
boxPtArr.append(boxPtDict)
faceDict["BoxsPoints"] = boxPtArr
faceDict["name"] = file.filename
faceArr.append(faceDict)
# Save face vector to hnswModel
if(len(filenames)>0):
hnswModel.add_items(vectors, filenames)
hnswModel.save_index(faceSearchData)
faceJson = {}
faceJson["faces"] = faceArr
response = make_response(json.dumps(faceJson))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
# image = cv2.imread("38967138.jpg")
# b,g,r = cv2.split(image)
# image = cv2.merge([r,g,b])
# arr = cv2.rectangle(image, (10, 10), (200, 200), (255, 0, 0), 2)
# strIO = BytesIO()
# imsave(strIO, arr, plugin='pil', format_str='png')
# strIO.seek(0)
# response = make_response(send_file(strIO, mimetype="image/jpeg"))
response = make_response('{"faces":[{"name":"test1.jpg","box":[10,20,30,40],"pt":[10,20,30,40,50,10,20,30,40,50]},{"name":"test1.jpg","box":[101,20,30,40],"pt":[101,201,301,401,501,10,20,30,40,50]}]}')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route("/faceRegisterBatch", methods=['POST'])
@cross_origin()
def faceRegisterBatch():
obj = request.get_json(force=True)
urls = obj['urls']
if(len(urls)>100):
response = make_response("Limit Error: url items count > 100")
return response
faceArr = []
for url in urls:
try:
headers = {"Referer":"https://www.fanfiction.net/s/4873155/1/Uchiha-Riz", "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}
content = requests.get(url, timeout=5, headers=headers).content
# content = urllib.request.urlopen(url, timeout=5, headers=headers).read()
img = np.fromstring(content, np.uint8)
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED)
filename = imgSavePath +"/"+ str(time.time()) +".jpg"
with open(filename, "wb") as fp:
fp.write(content)
boundingboxes, points, vectors = mtcnnDetect(img)
filenames = []
faceDict = {}
boxPtArr = []
for i in range(boundingboxes.shape[0]):
filenames.append(filename)
boxPtDict = {}
box = []
box.append(int(boundingboxes[i][0]))
box.append(int(boundingboxes[i][1]))
box.append(int(boundingboxes[i][2]))
box.append(int(boundingboxes[i][3]))
boxPtDict["box"] = box
pts = []
for j in range(10):
pts.append(int(points[i][j]))
boxPtDict["pt"] = pts
boxPtArr.append(boxPtDict)
faceDict["BoxsPoints"] = boxPtArr
faceDict["name"] = url
faceArr.append(faceDict)
# Save face vector to hnswModel
if(len(filenames)>0):
hnswModel.add_items(vectors, filenames)
except:
response = make_response("Unfortunitely -- An Unknow Error Happened")
return response
hnswModel.save_index(faceSearchData)
faceJson = {}
faceJson["faces"] = faceArr
response = make_response(json.dumps(faceJson))
return response
@app.route("/faceRetrieve", methods=['GET', 'POST'])
def faceRetrieve():
if(hnswModel.cur_ind==0):
response = make_response('Database is null now.')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
if request.method == 'POST':
name = request.form['name']
file = request.files[name]
img = np.fromstring(file.read(), np.uint8)
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED)
boundingboxes, points, vectors = mtcnnDetect(img)
if(boundingboxes.shape[0]==0):
response = make_response('Not detect any face in this picture, please choose another.')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif(boundingboxes.shape[0]>1):
response = make_response('Detect more than 1 face in this picture, please choose another.')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
topN = int(hnswModel.cur_ind/100)
if(topN<=1):
topN = hnswModel.cur_ind
elif(topN<=10):
topN = int(hnswModel.cur_ind/10)
hnswModel.set_ef(topN)
labels, distances = hnswModel.knn_query(vectors[0], k = topN)
faceArr = []
for i, label in enumerate(labels[0]):
if(distances[0][i]>dThreshold):
break
faceDict = {}
faceDict["distance"] = distances[0][i].tolist()
faceDict["path"] = label
faceArr.append(faceDict)
faceJson = {}
faceJson["faces"] = faceArr
response = make_response(json.dumps(faceJson))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
response = make_response('Not support Get method')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/faceRegister.html')
def home():
faceRegisterURL = "http://" +get_host_ip()+ ":" +str(port)+ "/faceRegister"
faceRegisterBatchURL = "http://" +get_host_ip()+ ":" +str(port)+ "/faceRegisterBatch"
return render_template("faceRegister.html", faceRegister=faceRegisterURL, faceRegisterBatch=faceRegisterBatchURL)
@app.route('/faceRetrieve.html')
def home2():
faceRetrieveURL = "http://" +get_host_ip()+ ":" +str(port)+ "/faceRetrieve"
return render_template("faceRetrieve.html", faceRetrieve=faceRetrieveURL)
def main(argv):
global port
port = argv[1]
app.run(host='0.0.0.0', port=port, debug=True)
if __name__ == '__main__':
if(os.path.exists(faceSearchData)):
hnswModel.load_index(faceSearchData)
else:
hnswModel.init_index(max_elements = max_elements, ef_construction = 200, M = 16)
main(sys.argv)