Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from mtcnn.mtcnn import MTCNN
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Function to perform face detection using MTCNN
|
7 |
+
def detect_faces(image):
|
8 |
+
# Load the image
|
9 |
+
img = cv2.imdecode(np.frombuffer(image.read(), np.uint8), -1)
|
10 |
+
|
11 |
+
# Create an MTCNN detector
|
12 |
+
detector = MTCNN()
|
13 |
+
|
14 |
+
# Detect faces in the image
|
15 |
+
faces = detector.detect_faces(img)
|
16 |
+
|
17 |
+
# Draw bounding boxes around the faces
|
18 |
+
for face in faces:
|
19 |
+
x, y, width, height = face['box']
|
20 |
+
cv2.rectangle(img, (x, y), (x + width, y + height), (0, 255, 0), 2)
|
21 |
+
|
22 |
+
# Convert the image to RGB format for Gradio
|
23 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
24 |
+
|
25 |
+
return img_rgb
|
26 |
+
|
27 |
+
# Create a Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=detect_faces,
|
30 |
+
inputs=gr.Image(),
|
31 |
+
outputs="image"
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the Gradio interface
|
35 |
+
iface.launch()
|