Spaces:
Runtime error
Runtime error
import gradio as gr | |
from mtcnn.mtcnn import MTCNN | |
import cv2 | |
import numpy as np | |
# Function to perform face detection using MTCNN | |
def detect_faces(image): | |
# Load the image | |
img = cv2.imdecode(np.frombuffer(image.read(), np.uint8), -1) | |
# Create an MTCNN detector | |
detector = MTCNN() | |
# Detect faces in the image | |
faces = detector.detect_faces(img) | |
# Draw bounding boxes around the faces | |
for face in faces: | |
x, y, width, height = face['box'] | |
cv2.rectangle(img, (x, y), (x + width, y + height), (0, 255, 0), 2) | |
# Convert the image to RGB format for Gradio | |
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
return img_rgb | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=detect_faces, | |
inputs=gr.Image(), | |
outputs="image" | |
) | |
# Launch the Gradio interface | |
iface.launch() | |