Spaces:
Sleeping
Sleeping
import gradio as gr | |
from mtcnn.mtcnn import MTCNN | |
import cv2 | |
import numpy as np | |
import json | |
# Function to detect faces using MTCNN | |
def detect_faces(image): | |
# Convert image to RGB format | |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
# Detect faces using MTCNN | |
detector = MTCNN() | |
faces = detector.detect_faces(image_rgb) | |
# Extract and format face information | |
formatted_faces = [{"x": face['box'][0], "y": face['box'][1], "width": face['box'][2], "height": face['box'][3]} for face in faces] | |
# Draw bounding boxes around detected faces | |
for face in faces: | |
x, y, width, height = face['box'] | |
cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2) | |
return image, json.dumps(formatted_faces) | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=detect_faces, | |
inputs=gr.Image(type="numpy", label="Input Image"), | |
outputs=[gr.Image(type="numpy", label="Output Image"), gr.Textbox(label="Face Coordinates")], | |
live=True, | |
title="MTCNN Face Detection", | |
description="Detect faces in an image using MTCNN.", | |
) | |
# Launch the Gradio Interface | |
iface.launch() | |