face-recognition-app/face_recognition/app/app.py

114 lines
3.7 KiB
Python
Raw Normal View History

2019-06-13 11:42:20 +00:00
from flask import Flask, request
2019-06-08 19:08:20 +00:00
from minio import Minio
2019-06-10 09:59:39 +00:00
from minio.error import (ResponseError, BucketAlreadyOwnedByYou, BucketAlreadyExists)
2019-06-08 18:59:15 +00:00
import os
2019-06-10 10:31:07 +00:00
import uuid
2019-06-11 10:17:19 +00:00
import pickle
2019-06-13 12:11:56 +00:00
import face_recognition
from werkzeug.datastructures import ImmutableMultiDict
2019-06-23 17:18:35 +00:00
import base64
2019-06-24 12:28:17 +00:00
import cv2
import numpy as np
2019-06-08 17:11:05 +00:00
app = Flask(__name__)
2019-06-11 09:44:32 +00:00
minioClient = Minio('minio:9000', access_key=os.environ['s3-name'], secret_key=os.environ['s3-password'], secure=False)
2019-06-08 17:11:05 +00:00
@app.route('/')
def hello_docker():
return 'Hello, I run in a docker container'
2019-06-08 18:59:15 +00:00
@app.route('/acces_minio')
def access_minio():
2019-06-10 09:46:47 +00:00
for bucket in minioClient.list_buckets():
2019-06-10 10:35:09 +00:00
print(bucket.name, bucket.creation_date, flush=True)
2019-06-10 09:41:07 +00:00
return('Connection Succesfull')
2019-06-11 09:24:06 +00:00
@app.route('/new_user_id')
def new_user_id():
2019-06-11 11:04:42 +00:00
#demo objekt speichern um id zu 'speichern'. Später mit if (type(object)==list oä überschreiben mit face_encoding.pkl
2019-06-11 09:24:06 +00:00
id = None
for limited_try in range(0,5):
2019-06-11 09:41:28 +00:00
id = str(uuid.uuid4())
2019-06-11 09:38:30 +00:00
if check_id(id) == False:
2019-06-11 09:24:06 +00:00
break
2019-06-11 10:17:19 +00:00
demo_object = ['test']
2019-06-11 10:42:46 +00:00
with open('/tmp/demo_object.pkl', 'wb') as f:
2019-06-11 10:28:25 +00:00
pickle.dump(demo_object, f)
2019-06-11 10:43:39 +00:00
minioClient.fput_object('users', str(id), '/tmp/demo_object.pkl')
2019-06-11 09:24:06 +00:00
return(id)
2019-06-13 11:31:17 +00:00
2019-06-23 16:57:04 +00:00
@app.route('/init_face')
#call like https://face.sguba.de/init_face?id=123&encoded_string=abc
2019-06-13 11:38:40 +00:00
def new_face():
id = request.args.get('id', None)
2019-06-23 16:54:49 +00:00
img = request.args.get('encoded_string', None)
2019-06-24 12:52:02 +00:00
img += "=" * ((4 - len(img) % 4) % 4)
img = bytes(img, encoding='utf-8')
2019-06-24 12:28:17 +00:00
#print(img, flush=True)
#imgdata = base64.b64decode(img)
#with open('/tmp/'+str(id), 'wb') as file:
# file.write(bytearray(imgdata))
2019-06-24 12:52:02 +00:00
save_img(img, '/tmp/'+str(id)+'.jpg')
2019-06-24 12:28:17 +00:00
#to debug send img
2019-06-24 12:45:09 +00:00
#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]
#with open('/tmp/'+str(id)+'.pkl', 'wb') as file:
# pickle.dump(face_encoding, file)
#minioClient.fput_object('users', str(id), '/tmp/'+str(id)+'.pkl')
return str(id)
2019-06-13 12:40:33 +00:00
2019-06-23 16:57:04 +00:00
@app.route('/check_face')
#call like https://face.sguba.de/check_face?id=123&encoded_string=abc
2019-06-13 12:40:33 +00:00
def check_face():
id = request.args.get('id', None)
2019-06-23 16:54:49 +00:00
img = request.args.get('encoded_string', None)
imgdata = base64.b64decode(img)
with open('/tmp/'+str(id), 'wb') as file:
file.write(imgdata)
face = face_recognition.load_image_file('/tmp/'+str(id))
2019-06-13 12:40:33 +00:00
face_encoding = face_recognition.face_encodings(face)[0]
2019-06-13 13:12:51 +00:00
minioClient.fget_object('users', str(id), '/tmp/'+str(id))
with open('/tmp/'+str(id), 'rb') as file:
2019-06-13 13:25:56 +00:00
face2_encoding = pickle.load(file)
2019-06-13 13:27:04 +00:00
return str(face_recognition.compare_faces([face_encoding], face2_encoding))
2019-06-13 13:12:51 +00:00
2019-06-11 09:24:06 +00:00
def check_id(id):
2019-06-11 10:17:19 +00:00
#return True -> id bereits verwendet
2019-06-11 09:24:06 +00:00
#return False -> id noch nicht verwendet
users = minioClient.list_objects('users')
known = False
for user in users:
2019-06-11 10:54:43 +00:00
if id == user.object_name:
2019-06-11 09:24:06 +00:00
known = True
else:
pass
return known
2019-06-24 12:28:17 +00:00
def save_img(encoded_data, filename):
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
return cv2.imwrite(filename, img)
2019-06-10 10:31:07 +00:00
2019-06-10 09:59:39 +00:00
def setup():
try:
minioClient.make_bucket("users")
2019-06-10 10:37:12 +00:00
except BucketAlreadyOwnedByYou as err:
2019-06-10 09:59:39 +00:00
pass
2019-06-10 10:37:12 +00:00
except BucketAlreadyExists as err:
2019-06-10 09:59:39 +00:00
pass
2019-06-10 10:37:12 +00:00
except ResponseError as err:
2019-06-10 09:59:39 +00:00
raise
2019-06-13 12:11:56 +00:00
try:
minioClient.make_bucket("uploads")
except BucketAlreadyOwnedByYou as err:
pass
except BucketAlreadyExists as err:
pass
except ResponseError as err:
raise
2019-06-10 09:59:39 +00:00
2019-06-08 17:11:05 +00:00
if __name__ == '__main__':
2019-06-11 09:07:04 +00:00
setup()
2019-06-08 17:11:05 +00:00
app.run(host='0.0.0.0')