| 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() |