File size: 1,117 Bytes
0c66baa
 
 
 
d3e1c3a
 
0c66baa
 
 
d3e1c3a
 
0c66baa
d3e1c3a
0c66baa
 
 
 
d3e1c3a
0c66baa
 
 
 
 
d3e1c3a
 
0c66baa
d3e1c3a
 
 
 
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
import cv2
import numpy as np
from PIL import Image
import io
from mtcnn import MTCNN
import gradio as gr

detector = MTCNN()

def detect_faces_gradio(image):
    img_rgb = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    faces = detector.detect_faces(img_rgb)
    processed_img = np.array(image).copy()
    if faces:
        for face in faces:
            bounding_box = face['box']
            keypoints = face['keypoints']
            cv2.rectangle(processed_img,
                          (bounding_box[0], bounding_box[1]),
                          (bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]),
                          (0, 255, 0),
                          2)
            for key, value in keypoints.items():
                cv2.circle(processed_img, (int(value[0]), int(value[1])), 2, (0, 0, 255), 2)
    return processed_img

iface = gr.Interface(fn=detect_faces_gradio, inputs=gr.Image(type="pil"), outputs="image",
                     title="MTCNN Face Detection",
                     description="Upload an image and see the detected faces and landmarks.")
iface.launch()