Spaces:
Sleeping
Sleeping
import gradio as gr | |
from mtcnn import MTCNN | |
import cv2 | |
import numpy as np | |
import time | |
import concurrent.futures | |
# loading haar | |
ff = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') | |
ff_alt = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml') | |
ff_alt2 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml') | |
pf = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_profileface.xml') | |
# loading mtcnn | |
mtcnn = MTCNN() | |
global_start = time.perf_counter() | |
haar_start = 0 | |
mtcnn_start = 0 | |
def get_unique_face_locations(all_face_locations): | |
unique_detected_faces = [] | |
for (x1, y1, w1, h1) in all_face_locations: | |
unique = True | |
for (x2, y2, w2, h2) in unique_detected_faces: | |
if abs(x1 - x2) < 50 and abs(y1 - y2) < 50: | |
unique = False | |
break | |
if unique: | |
unique_detected_faces.append((x1, y1, w1, h1)) | |
return unique_detected_faces | |
def detect_haar(gray): | |
global haar_start | |
haar_start = time.perf_counter() | |
ff_faces = ff.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=10, minSize=(25, 25)) | |
ff_alt2_faces = ff_alt2.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=10, minSize=(20, 20)) | |
pf_faces = pf.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5, minSize=(20, 20)) | |
return ff_faces, ff_alt2_faces, pf_faces | |
def detect_mtcnn(frame): | |
global mtcnn_start | |
mtcnn_start = time.perf_counter() | |
faces = mtcnn.detect_faces(frame) | |
mt_faces = [face['box'] for face in faces] | |
return mt_faces | |
def detect_faces(image): | |
frame = image | |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY) | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
haar_detections = executor.submit(detect_haar, gray) | |
mtcnn_detections = executor.submit(detect_mtcnn, frame) | |
ff_faces, ff_alt2_faces, pf_faces = haar_detections.result() | |
mt_faces = mtcnn_detections.result() | |
all_faces = [*ff_faces, *ff_alt2_faces, *pf_faces, *mt_faces] | |
unique_detected_faces = get_unique_face_locations(all_faces) | |
for (x, y, w, h) in unique_detected_faces: | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) | |
frame = cv2.putText(frame, f"{len(unique_detected_faces)} Faces", (20, 650), cv2.FONT_HERSHEY_SIMPLEX, 1.6, (0, 0, 0), 5) | |
print('\n\n') | |
print(f"Haar Took - {time.perf_counter() - haar_start:.2f}s") | |
print(f"MTCNN Took - {time.perf_counter() - mtcnn_start:.2f}s") | |
print(f"Total Time - {time.perf_counter() - global_start:.2f}s") | |
print('\n\n') | |
return frame | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=detect_faces, | |
inputs=gr.components.Image(sources="webcam"), | |
outputs=[gr.components.Image(type="numpy", label="Processed Image")], | |
live=True | |
) | |
# Launch the application | |
iface.launch() | |