File size: 1,013 Bytes
c0b40ee
cc1b595
 
 
c0b40ee
cc1b595
 
 
 
c0b40ee
cc1b595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0b40ee
cc1b595
3b34f8b
 
c0b40ee
0224b9d
c0b40ee
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
import gradio as gr
import mediapipe as mp
import cv2
import numpy as np

# Initialize MediaPipe Face Detection
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)

def detect_faces(image):
    # Convert the image from BGR (Gradio's default) to RGB
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

    # Process the image and detect faces
    results = face_detection.process(image)

    # Draw face detections on the image
    if results.detections:
        for detection in results.detections:
            mp_drawing.draw_detection(image, detection)

    # Convert the image back to BGR
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    return image

# Define the Gradio interface
demo = gr.Interface(
    fn=detect_faces,
    inputs=gr.components.Image(shape=(480, 640), source="webcam"),
    outputs=gr.components.Image(type="numpy", label="Processed Image")
)

demo.launch()