Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update facerec_service.py #8

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions facerec_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import listdir
from os import listdir, remove
from os.path import isfile, join, splitext

import face_recognition
Expand All @@ -8,6 +8,7 @@

# Global storage for images
faces_dict = {}
persistent_faces = "/root/faces"

# Create flask app
app = Flask(__name__)
Expand Down Expand Up @@ -49,7 +50,7 @@ def calc_face_encoding(image):
def get_faces_dict(path):
image_files = get_all_picture_files(path)
return dict([(remove_file_ext(image), calc_face_encoding(image))
for image in image_files])
for image in image_files])


def detect_faces_in_image(file_stream):
Expand Down Expand Up @@ -112,6 +113,10 @@ def web_faces():
raise BadRequest("Identifier for the face was not given!")

if request.method == 'POST':
app.logger.info('%s loaded', file.filename)
# HINT jpg included just for the image check -> this is faster then passing boolean var through few methods
# TODO add method for extension persistence - do not forget abut the deletion
file.save("{0}/{1}.jpg".format(persistent_faces, request.args.get('id')))
try:
new_encoding = calc_face_encoding(file)
faces_dict.update({request.args.get('id'): new_encoding})
Expand All @@ -120,6 +125,7 @@ def web_faces():

elif request.method == 'DELETE':
faces_dict.pop(request.args.get('id'))
remove("{0}/{1}.jpg".format(persistent_faces, request.args.get('id')))

return jsonify(list(faces_dict.keys()))

Expand All @@ -140,7 +146,8 @@ def extract_image(request):
if __name__ == "__main__":
print("Starting by generating encodings for found images...")
# Calculate known faces
faces_dict = get_faces_dict("/root/faces")
faces_dict = get_faces_dict(persistent_faces)
print(faces_dict)

# Start app
print("Starting WebServer...")
Expand Down