From 304ff945a7515822c14faf785284d77214382b4e Mon Sep 17 00:00:00 2001 From: Kar Date: Sun, 5 Jan 2025 15:21:24 +0000 Subject: [PATCH] multiple faces --- app.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app.py b/app.py index 7a45837..13a13f9 100644 --- a/app.py +++ b/app.py @@ -82,5 +82,39 @@ def recognize_image(): return jsonify({'error': 'Student not recognized'}), 404 +@app.route('/rec_multi_face', methods=['POST']) +def recognize_multiple_faces(): + if 'face' not in request.files: + return jsonify({'error': 'Image not provided'}), 400 + + image = request.files['face'] + # Load the uploaded image file into a numpy array + unknown_image = face_recognition.load_image_file(image) + + # Detect face locations in the image + face_locations = face_recognition.face_locations(unknown_image) + # Get the face encodings for each detected face + unknown_encodings = face_recognition.face_encodings(unknown_image, face_locations) + + if len(unknown_encodings) > 0: + matches = [] + + for unknown_encoding in unknown_encodings: + for student_id, known_encoding in students_db.items(): + # Calculate the distance between the known encoding and the uploaded image encoding + distance = face_recognition.face_distance([known_encoding], unknown_encoding)[0] + # Convert distance to accuracy score (the closer the distance, the higher the accuracy) + accuracy_score = (1 - distance) * 100 + + if accuracy_score > 50: # Threshold to filter out low-confidence matches + matches.append({'student_id': student_id, 'accuracy_score': accuracy_score}) + + # Sort matches by accuracy score in descending order + matches = sorted(matches, key=lambda x: x['accuracy_score'], reverse=True) + + return jsonify({'number_of_faces': len(unknown_encodings), 'matches': matches}), 200 + + return jsonify({'error': 'No recognizable faces found', 'number_of_faces': 0}), 404 + if __name__ == '__main__': app.run(debug=True, host="0.0.0.0", port=5005)