Rasleen commited on
Commit
f0105aa
·
verified ·
1 Parent(s): 0003168

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # --- MediaPipe setup ---
7
+ mp_hands = mp.solutions.hands
8
+ hands = mp_hands.Hands(
9
+ static_image_mode=False,
10
+ max_num_hands=1,
11
+ min_detection_confidence=0.7,
12
+ min_tracking_confidence=0.5,
13
+ )
14
+ mp_draw = mp.solutions.drawing_utils
15
+
16
+ # --- Global canvas ---
17
+ canvas = None
18
+
19
+ def paint(frame: np.ndarray) -> np.ndarray:
20
+ """
21
+ Receives a BGR frame from the browser camera,
22
+ returns the frame overlaid with the drawing canvas.
23
+ """
24
+ global canvas
25
+
26
+ # Convert to RGB for MediaPipe, and capture dimensions
27
+ rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
28
+ h, w, _ = rgb.shape
29
+
30
+ # Initialize canvas once
31
+ if canvas is None:
32
+ canvas = np.zeros((h, w, 3), dtype=np.uint8)
33
+
34
+ # Detect hand landmarks
35
+ results = hands.process(rgb)
36
+ if results.multi_hand_landmarks:
37
+ hand_lms = results.multi_hand_landmarks[0]
38
+ # Get normalized index-finger tip coords
39
+ lm = hand_lms.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
40
+ x, y = int(lm.x * w), int(lm.y * h)
41
+ # Draw on the canvas: small circle or line from previous point
42
+ cv2.circle(canvas, (x, y), 8, (0, 0, 255), -1)
43
+
44
+ # Blend canvas with live frame
45
+ blended = cv2.addWeighted(frame, 0.5, canvas, 0.5, 0)
46
+ return blended
47
+
48
+ # --- Gradio interface ---
49
+ demo = gr.Interface(
50
+ fn=paint,
51
+ inputs=gr.Camera(type="numpy", streaming=True),
52
+ outputs=gr.Image(type="numpy"),
53
+ live=True,
54
+ title="🖌️ Virtual Painter",
55
+ description="Move your index finger in front of the camera to draw!"
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()