File size: 838 Bytes
2273c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()