-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacerec.py
57 lines (43 loc) · 1.91 KB
/
facerec.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
from flask import Flask, request, jsonify
import face_recognition
import sys
import logging
app = Flask(__name__)
# Set up logging
logging.basicConfig(filename='api.log', level=logging.ERROR)
def perform_face_recognition(user_profile_image_path, captured_image_path):
try:
# Load images
user_profile_image = face_recognition.load_image_file(user_profile_image_path)
captured_image = face_recognition.load_image_file(captured_image_path)
# Encode faces
user_profile_encodings = face_recognition.face_encodings(user_profile_image)
captured_encodings = face_recognition.face_encodings(captured_image)
if not user_profile_encodings:
raise ValueError("No face detected in the user profile image")
if not captured_encodings:
raise ValueError("No face detected in the captured image")
user_profile_encoding = user_profile_encodings[0]
captured_encoding = captured_encodings[0]
# Compare faces
results = face_recognition.compare_faces([user_profile_encoding], captured_encoding)
# Return result (match or not)
return {'match': bool(results[0])}
except Exception as e:
error_message = f"Error performing face recognition: {e}"
logging.error(error_message)
return {'error': error_message}
@app.route('/perform-face-recognition', methods=['POST'])
def handle_face_recognition():
try:
data = request.json
user_profile_image_path = data['user_profile_image_path']
captured_image_path = data['captured_image_path']
result = perform_face_recognition(user_profile_image_path, captured_image_path)
return jsonify(result)
except Exception as e:
error_message = f"Error processing request: {e}"
logging.error(error_message)
return jsonify({'error': error_message}), 500
if __name__ == "__main__":
app.run(debug=True)