File size: 1,145 Bytes
2273c07
 
 
 
b76f2d8
2273c07
366b72d
2273c07
366b72d
 
2273c07
366b72d
2273c07
366b72d
2273c07
b76f2d8
 
 
366b72d
2273c07
 
366b72d
2273c07
b76f2d8
2273c07
366b72d
2273c07
 
366b72d
4aa3d02
366b72d
 
 
2273c07
 
366b72d
ff7da15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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(share=True)