| | import cv2 |
| | import face_recognition |
| | import numpy as np |
| | import gradio as gr |
| |
|
| | |
| | reference_image = face_recognition.load_image_file("reference.jpg") |
| | reference_encoding = face_recognition.face_encodings(reference_image)[0] |
| |
|
| | def detect_and_recognize(image_path): |
| | |
| | image = face_recognition.load_image_file(image_path) |
| | |
| | |
| | face_locations = face_recognition.face_locations(image) |
| | face_encodings = face_recognition.face_encodings(image, face_locations) |
| |
|
| | |
| | image_cv2 = cv2.imread(image_path) |
| | |
| | recognized_faces = [] |
| |
|
| | for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): |
| | |
| | matches = face_recognition.compare_faces([reference_encoding], face_encoding) |
| | label = "Match" if matches[0] else "Unknown" |
| | |
| | |
| | cv2.rectangle(image_cv2, (left, top), (right, bottom), (0, 255, 0), 2) |
| | cv2.putText(image_cv2, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
| |
|
| | recognized_faces.append(label) |
| | |
| | output_path = "output.jpg" |
| | cv2.imwrite(output_path, image_cv2) |
| |
|
| | return output_path, recognized_faces |
| |
|
| | |
| | iface = gr.Interface(fn=detect_and_recognize, inputs="file", outputs=["image", "text"], title="Face Recognition") |
| | iface.launch() |
| |
|