Commit ·
6b3451b
1
Parent(s): 574f329
demo
Browse files- app.py +69 -4
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -1,7 +1,72 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import tensorflow_hub as hub
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import tempfile
|
| 7 |
+
import json
|
| 8 |
+
import uuid
|
| 9 |
|
| 10 |
+
# Load face detector
|
| 11 |
+
detector = hub.load("https://tfhub.dev/tensorflow/ssd_mobilenet_v2/fpnlite_320x320/1")
|
| 12 |
|
| 13 |
+
def detect_faces(image):
|
| 14 |
+
"""Runs TensorFlow SSD model to detect faces in an image."""
|
| 15 |
+
input_tensor = tf.convert_to_tensor([image], dtype=tf.uint8)
|
| 16 |
+
detector_output = detector(input_tensor)
|
| 17 |
+
boxes = detector_output['detection_boxes'][0].numpy()
|
| 18 |
+
scores = detector_output['detection_scores'][0].numpy()
|
| 19 |
+
return boxes[scores > 0.5]
|
| 20 |
+
|
| 21 |
+
def euclidean_hash(box):
|
| 22 |
+
"""Simple hash based on box centroid"""
|
| 23 |
+
y1, x1, y2, x2 = box
|
| 24 |
+
cx = (x1 + x2) / 2
|
| 25 |
+
cy = (y1 + y2) / 2
|
| 26 |
+
return str(uuid.uuid5(uuid.NAMESPACE_DNS, f"{cx:.4f}-{cy:.4f}"))
|
| 27 |
+
|
| 28 |
+
def process_frame(frame):
|
| 29 |
+
h, w, _ = frame.shape
|
| 30 |
+
boxes = detect_faces(frame)
|
| 31 |
+
result = []
|
| 32 |
+
for box in boxes:
|
| 33 |
+
y1, x1, y2, x2 = box
|
| 34 |
+
x1a, y1a, x2a, y2a = int(x1 * w), int(y1 * h), int(x2 * w), int(y2 * h)
|
| 35 |
+
face_id = euclidean_hash(box)
|
| 36 |
+
result.append({
|
| 37 |
+
"id": face_id,
|
| 38 |
+
"coords": [x1a, y1a, x2a, y2a]
|
| 39 |
+
})
|
| 40 |
+
cv2.rectangle(frame, (x1a, y1a), (x2a, y2a), (0, 255, 0), 2)
|
| 41 |
+
cv2.putText(frame, face_id[:6], (x1a, y1a - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 42 |
+
return frame, json.dumps(result, indent=2)
|
| 43 |
+
|
| 44 |
+
def video_handler(video):
|
| 45 |
+
cap = cv2.VideoCapture(video)
|
| 46 |
+
frames = []
|
| 47 |
+
last_json = {}
|
| 48 |
+
while True:
|
| 49 |
+
ret, frame = cap.read()
|
| 50 |
+
if not ret:
|
| 51 |
+
break
|
| 52 |
+
tagged_frame, json_result = process_frame(frame)
|
| 53 |
+
frames.append(tagged_frame)
|
| 54 |
+
last_json = json_result
|
| 55 |
+
cap.release()
|
| 56 |
+
out_path = tempfile.mktemp(suffix=".mp4")
|
| 57 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 58 |
+
out = cv2.VideoWriter(out_path, fourcc, 15, (frames[0].shape[1], frames[0].shape[0]))
|
| 59 |
+
for f in frames:
|
| 60 |
+
out.write(f)
|
| 61 |
+
out.release()
|
| 62 |
+
return out_path, last_json
|
| 63 |
+
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=video_handler,
|
| 66 |
+
inputs=gr.Video(),
|
| 67 |
+
outputs=[gr.Video(label="Tagged Video"), gr.Textbox(label="Detected Faces JSON")],
|
| 68 |
+
title="Multi-Face Recognizer with Unique ID Hashes",
|
| 69 |
+
description="Upload or stream a video. Each detected face will be tagged with a short unique hash."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.29.0
|
| 2 |
+
opencv-python-headless==4.9.0.80
|
| 3 |
+
tensorflow==2.16.1
|
| 4 |
+
tensorflow-hub==0.16.1
|
| 5 |
+
numpy==1.26.4
|