Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import face_recognition
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load reference image and encode face
|
| 7 |
+
reference_image = face_recognition.load_image_file("reference.jpg")
|
| 8 |
+
reference_encoding = face_recognition.face_encodings(reference_image)[0]
|
| 9 |
+
|
| 10 |
+
def detect_and_recognize(image_path):
|
| 11 |
+
# Load input image
|
| 12 |
+
image = face_recognition.load_image_file(image_path)
|
| 13 |
+
|
| 14 |
+
# Detect faces
|
| 15 |
+
face_locations = face_recognition.face_locations(image)
|
| 16 |
+
face_encodings = face_recognition.face_encodings(image, face_locations)
|
| 17 |
+
|
| 18 |
+
# Convert image for OpenCV visualization
|
| 19 |
+
image_cv2 = cv2.imread(image_path)
|
| 20 |
+
|
| 21 |
+
recognized_faces = []
|
| 22 |
+
|
| 23 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
| 24 |
+
# Compare detected face with reference
|
| 25 |
+
matches = face_recognition.compare_faces([reference_encoding], face_encoding)
|
| 26 |
+
label = "Match" if matches[0] else "Unknown"
|
| 27 |
+
|
| 28 |
+
# Draw a rectangle around the face
|
| 29 |
+
cv2.rectangle(image_cv2, (left, top), (right, bottom), (0, 255, 0), 2)
|
| 30 |
+
cv2.putText(image_cv2, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 31 |
+
|
| 32 |
+
recognized_faces.append(label)
|
| 33 |
+
|
| 34 |
+
output_path = "output.jpg"
|
| 35 |
+
cv2.imwrite(output_path, image_cv2)
|
| 36 |
+
|
| 37 |
+
return output_path, recognized_faces
|
| 38 |
+
|
| 39 |
+
# Gradio Interface
|
| 40 |
+
iface = gr.Interface(fn=detect_and_recognize, inputs="file", outputs=["image", "text"], title="Face Recognition")
|
| 41 |
+
iface.launch()
|