Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
import pickle # For loading the face embeddings database
|
| 7 |
+
|
| 8 |
+
# Import your face detection and recognition functions
|
| 9 |
+
from face_detection import detect_faces
|
| 10 |
+
from face_recognition import recognize_face # Assuming you have this
|
| 11 |
+
|
| 12 |
+
# --- Configuration ---
|
| 13 |
+
DATABASE_PATH = "face_database.pkl" # Path to your stored face embeddings
|
| 14 |
+
THRESHOLD = 0.6 # Similarity threshold for recognition
|
| 15 |
+
|
| 16 |
+
# --- Load the face embeddings database (if it exists) ---
|
| 17 |
+
face_embeddings_db = {}
|
| 18 |
+
if os.path.exists(DATABASE_PATH):
|
| 19 |
+
with open(DATABASE_PATH, 'rb') as f:
|
| 20 |
+
face_embeddings_db = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
# --- Function to process an input image ---
|
| 23 |
+
def recognize_faces_in_image(image):
|
| 24 |
+
img_array = np.array(image)
|
| 25 |
+
detected_faces = detect_faces(img_array) # Returns a list of (box, confidence)
|
| 26 |
+
|
| 27 |
+
output_image = img_array.copy()
|
| 28 |
+
results = []
|
| 29 |
+
|
| 30 |
+
for box, _ in detected_faces:
|
| 31 |
+
x1, y1, w, h = box
|
| 32 |
+
x2, y2 = x1 + w, y1 + h
|
| 33 |
+
|
| 34 |
+
face_roi = img_array[y1:y2, x1:x2]
|
| 35 |
+
|
| 36 |
+
if face_roi.shape[0] > 0 and face_roi.shape[1] > 0:
|
| 37 |
+
identity = recognize_face(face_roi, face_embeddings_db, THRESHOLD)
|
| 38 |
+
|
| 39 |
+
if identity:
|
| 40 |
+
color = (0, 255, 0) # Green for recognized
|
| 41 |
+
text = identity
|
| 42 |
+
else:
|
| 43 |
+
color = (255, 0, 0) # Red for unknown
|
| 44 |
+
text = "Unknown"
|
| 45 |
+
|
| 46 |
+
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
|
| 47 |
+
cv2.putText(output_image, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
| 48 |
+
results.append(f"Face detected at ({x1}, {y1}) - {text}")
|
| 49 |
+
else:
|
| 50 |
+
results.append(f"Small or invalid face detected.")
|
| 51 |
+
|
| 52 |
+
return [Image.fromarray(output_image), "\n".join(results)]
|
| 53 |
+
|
| 54 |
+
# --- Gradio Interface ---
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=recognize_faces_in_image,
|
| 57 |
+
inputs=gr.Image(label="Upload an Image"),
|
| 58 |
+
outputs=[gr.Image(label="Detected Faces"), gr.Textbox(label="Recognition Results")],
|
| 59 |
+
title="Face Recognition App",
|
| 60 |
+
description="Upload an image and see the detected and recognized faces."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
iface.launch()
|