Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from mtcnn import MTCNN
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import time
|
| 6 |
+
import concurrent.futures
|
| 7 |
+
|
| 8 |
+
# loading haar
|
| 9 |
+
ff = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 10 |
+
ff_alt = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml')
|
| 11 |
+
ff_alt2 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
|
| 12 |
+
pf = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_profileface.xml')
|
| 13 |
+
|
| 14 |
+
# loading mtcnn
|
| 15 |
+
mtcnn = MTCNN()
|
| 16 |
+
|
| 17 |
+
global_start = time.perf_counter()
|
| 18 |
+
haar_start = 0
|
| 19 |
+
mtcnn_start = 0
|
| 20 |
+
|
| 21 |
+
def get_unique_face_locations(all_face_locations):
|
| 22 |
+
unique_detected_faces = []
|
| 23 |
+
for (x1, y1, w1, h1) in all_face_locations:
|
| 24 |
+
unique = True
|
| 25 |
+
for (x2, y2, w2, h2) in unique_detected_faces:
|
| 26 |
+
if abs(x1 - x2) < 50 and abs(y1 - y2) < 50:
|
| 27 |
+
unique = False
|
| 28 |
+
break
|
| 29 |
+
if unique:
|
| 30 |
+
unique_detected_faces.append((x1, y1, w1, h1))
|
| 31 |
+
|
| 32 |
+
return unique_detected_faces
|
| 33 |
+
|
| 34 |
+
def detect_haar(gray):
|
| 35 |
+
global haar_start
|
| 36 |
+
|
| 37 |
+
haar_start = time.perf_counter()
|
| 38 |
+
ff_faces = ff.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=10, minSize=(25, 25))
|
| 39 |
+
ff_alt2_faces = ff_alt2.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=10, minSize=(20, 20))
|
| 40 |
+
pf_faces = pf.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(20, 20))
|
| 41 |
+
|
| 42 |
+
return ff_faces, ff_alt2_faces, pf_faces
|
| 43 |
+
|
| 44 |
+
def detect_mtcnn(frame):
|
| 45 |
+
global mtcnn_start
|
| 46 |
+
|
| 47 |
+
mtcnn_start = time.perf_counter()
|
| 48 |
+
faces = mtcnn.detect_faces(frame)
|
| 49 |
+
mt_faces = [face['box'] for face in faces]
|
| 50 |
+
|
| 51 |
+
return mt_faces
|
| 52 |
+
|
| 53 |
+
def detect_faces(image):
|
| 54 |
+
frame = image
|
| 55 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
| 56 |
+
|
| 57 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 58 |
+
haar_detections = executor.submit(detect_haar, gray)
|
| 59 |
+
mtcnn_detections = executor.submit(detect_mtcnn, frame)
|
| 60 |
+
|
| 61 |
+
ff_faces, ff_alt2_faces, pf_faces = haar_detections.result()
|
| 62 |
+
mt_faces = mtcnn_detections.result()
|
| 63 |
+
|
| 64 |
+
all_faces = [*ff_faces, *ff_alt2_faces, *pf_faces, *mt_faces]
|
| 65 |
+
unique_detected_faces = get_unique_face_locations(all_faces)
|
| 66 |
+
|
| 67 |
+
for (x, y, w, h) in unique_detected_faces:
|
| 68 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
|
| 69 |
+
|
| 70 |
+
frame = cv2.putText(frame, f"{len(unique_detected_faces)} Faces", (20, 650), cv2.FONT_HERSHEY_SIMPLEX, 1.6, (0, 0, 0), 5)
|
| 71 |
+
|
| 72 |
+
print('\n\n')
|
| 73 |
+
print(f"Haar Took - {time.perf_counter() - haar_start:.2f}s")
|
| 74 |
+
print(f"MTCNN Took - {time.perf_counter() - mtcnn_start:.2f}s")
|
| 75 |
+
print(f"Total Time - {time.perf_counter() - global_start:.2f}s")
|
| 76 |
+
print('\n\n')
|
| 77 |
+
|
| 78 |
+
return frame
|
| 79 |
+
# Create a Gradio interface
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=detect_faces,
|
| 82 |
+
inputs=gr.components.Image(sources="webcam"),
|
| 83 |
+
outputs=[gr.components.Image(type="numpy", label="Processed Image")],
|
| 84 |
+
live=True
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Launch the application
|
| 88 |
+
iface.launch()
|