File size: 2,622 Bytes
ef2f26b
dff63c2
ef2f26b
dff63c2
ef2f26b
dff63c2
 
 
 
ef2f26b
dff63c2
 
 
 
ef2f26b
dff63c2
 
 
 
 
 
 
 
ef2f26b
dff63c2
 
 
 
 
 
 
 
ef2f26b
dff63c2
 
 
 
 
 
 
 
 
 
ef2f26b
dff63c2
 
 
 
 
ef2f26b
dff63c2
 
ef2f26b
dff63c2
 
 
ef2f26b
dff63c2
ef2f26b
dff63c2
2d6e948
dff63c2
 
 
ef2f26b
 
dff63c2
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import mediapipe as mp
import numpy as np
import gradio as gr

# Initialize Mediapipe solutions
mp_hands = mp.solutions.hands
mp_face_mesh = mp.solutions.face_mesh
mp_drawing = mp.solutions.drawing_utils

# Function to process and draw landmarks on the input image
def process_image(input_image):
    # Convert input image to RGB for Mediapipe
    rgb_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)

    # Initialize Hands and Face Mesh models
    with mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.5) as hands, \
         mp_face_mesh.FaceMesh(static_image_mode=True, min_detection_confidence=0.5) as face_mesh:
        
        # Process the image for hands
        hand_results = hands.process(rgb_image)
        # Process the image for face mesh
        face_results = face_mesh.process(rgb_image)

        # Draw Hand Landmarks
        if hand_results.multi_hand_landmarks:
            for hand_landmarks in hand_results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(
                    input_image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
                    mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
                    mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2, circle_radius=2)
                )

        # Draw Face Mesh Landmarks
        if face_results.multi_face_landmarks:
            for face_landmarks in face_results.multi_face_landmarks:
                mp_drawing.draw_landmarks(
                    input_image, face_landmarks, mp_face_mesh.FACEMESH_TESSELATION,
                    mp_drawing.DrawingSpec(color=(80, 110, 10), thickness=1, circle_radius=1),
                    mp_drawing.DrawingSpec(color=(80, 256, 121), thickness=1, circle_radius=1)
                )
    
    return input_image

# Gradio interface
def gradio_interface(image):
    # Convert Gradio PIL image to OpenCV format
    image = np.array(image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    # Process the image to detect landmarks
    processed_image = process_image(image)

    # Convert the processed image back to RGB for display in Gradio
    processed_image = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
    return processed_image

# Define Gradio app
iface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="numpy"),
    title="Face and Hand Landmarks Detection",
    description="Upload an image or take a photo to detect face and hand landmarks using Mediapipe and OpenCV."
)

# Launch Gradio app
if __name__ == "__main__":
    iface.launch()