Spaces:
Build error
Build error
Commit ·
3d9de46
1
Parent(s): 20a6573
Deploying Gradio face recognition app
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ def load_trained_model():
|
|
| 10 |
data = pickle.load(f)
|
| 11 |
return data["encodings"], data["names"]
|
| 12 |
|
|
|
|
| 13 |
def recognize_face(frame):
|
| 14 |
encodings, names = load_trained_model()
|
| 15 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
@@ -31,9 +32,43 @@ def recognize_face(frame):
|
|
| 31 |
|
| 32 |
return frame
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Gradio Interface
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
iface.launch()
|
|
|
|
| 10 |
data = pickle.load(f)
|
| 11 |
return data["encodings"], data["names"]
|
| 12 |
|
| 13 |
+
# Face recognition function
|
| 14 |
def recognize_face(frame):
|
| 15 |
encodings, names = load_trained_model()
|
| 16 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
| 32 |
|
| 33 |
return frame
|
| 34 |
|
| 35 |
+
# Process video frame-by-frame
|
| 36 |
+
def process_video(video):
|
| 37 |
+
cap = cv2.VideoCapture(video)
|
| 38 |
+
frames = []
|
| 39 |
+
|
| 40 |
+
while cap.isOpened():
|
| 41 |
+
ret, frame = cap.read()
|
| 42 |
+
if not ret:
|
| 43 |
+
break
|
| 44 |
+
frame = recognize_face(frame)
|
| 45 |
+
frames.append(frame)
|
| 46 |
+
|
| 47 |
+
cap.release()
|
| 48 |
+
return frames
|
| 49 |
+
|
| 50 |
+
# Live webcam feed processing
|
| 51 |
+
def process_webcam():
|
| 52 |
+
cap = cv2.VideoCapture(0)
|
| 53 |
+
|
| 54 |
+
while cap.isOpened():
|
| 55 |
+
ret, frame = cap.read()
|
| 56 |
+
if not ret:
|
| 57 |
+
break
|
| 58 |
+
frame = recognize_face(frame)
|
| 59 |
+
yield frame # Stream processed frames
|
| 60 |
+
|
| 61 |
+
cap.release()
|
| 62 |
+
|
| 63 |
# Gradio Interface
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn={"Upload Video": process_video, "Live Webcam": process_webcam},
|
| 66 |
+
inputs=gr.Video(source="upload", format="mp4", optional=True),
|
| 67 |
+
outputs=gr.Video(),
|
| 68 |
+
title="Face Identification System",
|
| 69 |
+
description="Upload a video or use a live webcam for real-time face recognition.",
|
| 70 |
+
live=True
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
iface.launch()
|