Update 'face_recognition/app/app.py'

This commit is contained in:
fabrice 2019-06-25 14:03:58 +00:00
parent 7408b83a54
commit 67c8854760

View File

@ -35,27 +35,21 @@ def new_user_id():
with open('/tmp/demo_object.pkl', 'wb') as f: with open('/tmp/demo_object.pkl', 'wb') as f:
pickle.dump(demo_object, f) pickle.dump(demo_object, f)
minioClient.fput_object('users', str(id), '/tmp/demo_object.pkl') minioClient.fput_object('users', str(id), '/tmp/demo_object.pkl')
return jsonify({'id':id, 'id2':'test2'}) return jsonify({'id':id})
@app.route('/init_face') @app.route('/init_face')
#call like https://face.sguba.de/init_face?id=123&encoded_string=abc #call like https://face.sguba.de/init_face?id=123&encoded_string=abc
def new_face(): def new_face():
id = request.args.get('id', None) id = request.args.get('id', None)
img = request.args.get('encoded_string', None) img = request.args.get('encoded_string', None)
img += "=" * ((4 - len(img) % 4) % 4) temporary_img_path = '/tmp/'+str(id)+'.jpg'
img = bytes(img, encoding='utf-8') temporary_pkl_path = '/tmp/'+str(id)+'.pkl'
#print(img, flush=True) save_img(img, temporary_img_path)
#imgdata = base64.b64decode(img) face = face_recognition.load_image_file(temporary_img_path)
#with open('/tmp/'+str(id), 'wb') as file:
# file.write(bytearray(imgdata))
save_img(img, '/tmp/'+str(id)+'.jpg')
#to debug send img
minioClient.fput_object('users', str(id)+'.jpg', '/tmp/'+str(id)+'.jpg')
face = face_recognition.load_image_file('/tmp/'+str(id)+'.jpg')
face_encoding = face_recognition.face_encodings(face)[0] face_encoding = face_recognition.face_encodings(face)[0]
with open('/tmp/'+str(id)+'.pkl', 'wb') as file: with open(temporary_pkl_path, 'wb') as file:
pickle.dump(face_encoding, file) pickle.dump(face_encoding, file)
minioClient.fput_object('users', str(id), '/tmp/'+str(id)+'.pkl') minioClient.fput_object('users', str(id), temporary_pkl_path)
return jsonify({'id':id}) return jsonify({'id':id})
@app.route('/check_face') @app.route('/check_face')
@ -63,13 +57,13 @@ def new_face():
def check_face(): def check_face():
id = request.args.get('id', None) id = request.args.get('id', None)
img = request.args.get('encoded_string', None) img = request.args.get('encoded_string', None)
imgdata = base64.b64decode(img) temporary_img_path = '/tmp/'+str(id)+'.jpg'
with open('/tmp/'+str(id), 'wb') as file: temporary_pkl_path = '/tmp/'+str(id)+'.pkl'
file.write(imgdata) save_img(img, temporary_img_path)
face = face_recognition.load_image_file('/tmp/'+str(id)) face = face_recognition.load_image_file(temporary_img_path)
face_encoding = face_recognition.face_encodings(face)[0] face_encoding = face_recognition.face_encodings(face)[0]
minioClient.fget_object('users', str(id), '/tmp/'+str(id)) minioClient.fget_object('users', str(id), temporary_pkl_path)
with open('/tmp/'+str(id), 'rb') as file: with open(temporary_pkl_path, 'rb') as file:
face2_encoding = pickle.load(file) face2_encoding = pickle.load(file)
return jsonify({'result':face_recognition.compare_faces([face_encoding], face2_encoding)}) return jsonify({'result':face_recognition.compare_faces([face_encoding], face2_encoding)})
@ -87,6 +81,8 @@ def check_id(id):
return known return known
def save_img(encoded_data, filename): def save_img(encoded_data, filename):
encoded_data += "=" * ((4 - len(encoded_data) % 4) % 4)
encoded_data = bytes(encoded_data, encoding='utf-8')
nparr = np.fromstring(base64.urlsafe_b64decode(encoded_data), np.uint8) nparr = np.fromstring(base64.urlsafe_b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR) img = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
img = imutils.rotate(img, 90) img = imutils.rotate(img, 90)
@ -101,14 +97,6 @@ def setup():
pass pass
except ResponseError as err: except ResponseError as err:
raise raise
try:
minioClient.make_bucket("uploads")
except BucketAlreadyOwnedByYou as err:
pass
except BucketAlreadyExists as err:
pass
except ResponseError as err:
raise
if __name__ == '__main__': if __name__ == '__main__':