File size: 1,106 Bytes
f0105aa
 
 
 
 
 
 
b6d78be
f0105aa
 
 
 
 
 
 
b6d78be
f0105aa
 
 
 
b6d78be
f0105aa
 
b6d78be
f0105aa
 
 
b6d78be
f0105aa
 
 
b6d78be
 
f0105aa
 
 
 
 
 
 
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
import cv2
import mediapipe as mp
import numpy as np
import gradio as gr

# --- MediaPipe setup ---
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.7)
mp_draw = mp.solutions.drawing_utils

# --- Global canvas ---
canvas = None

def paint(frame: np.ndarray) -> np.ndarray:
    global canvas
    h, w, _ = frame.shape

    if canvas is None:
        canvas = np.zeros((h, w, 3), dtype=np.uint8)

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(rgb)
    if results.multi_hand_landmarks:
        lm = results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
        x, y = int(lm.x * w), int(lm.y * h)
        cv2.circle(canvas, (x, y), 8, (0, 0, 255), -1)

    return cv2.addWeighted(frame, 0.5, canvas, 0.5, 0)

demo = gr.Interface(
    fn=paint,
    inputs=gr.inputs.Camera(type="numpy", streaming=True),
    outputs=gr.outputs.Image(type="numpy"),
    live=True,
    title="๐Ÿ–Œ๏ธ Virtual Painter",
    description="Move your index finger in front of the camera to draw!"
)

if __name__ == "__main__":
    demo.launch()