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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -26
app.py CHANGED
@@ -5,51 +5,32 @@ 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!"
 
5
 
6
  # --- MediaPipe setup ---
7
  mp_hands = mp.solutions.hands
8
+ hands = mp_hands.Hands(min_detection_confidence=0.7)
 
 
 
 
 
9
  mp_draw = mp.solutions.drawing_utils
10
 
11
  # --- Global canvas ---
12
  canvas = None
13
 
14
  def paint(frame: np.ndarray) -> np.ndarray:
 
 
 
 
15
  global canvas
16
+ h, w, _ = frame.shape
17
 
 
 
 
 
 
18
  if canvas is None:
19
  canvas = np.zeros((h, w, 3), dtype=np.uint8)
20
 
21
+ rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
22
  results = hands.process(rgb)
23
  if results.multi_hand_landmarks:
24
+ lm = results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
 
 
25
  x, y = int(lm.x * w), int(lm.y * h)
 
26
  cv2.circle(canvas, (x, y), 8, (0, 0, 255), -1)
27
 
28
+ return cv2.addWeighted(frame, 0.5, canvas, 0.5, 0)
 
 
29
 
 
30
  demo = gr.Interface(
31
  fn=paint,
32
+ inputs=gr.inputs.Camera(type="numpy", streaming=True),
33
+ outputs=gr.outputs.Image(type="numpy"),
34
  live=True,
35
  title="🖌️ Virtual Painter",
36
  description="Move your index finger in front of the camera to draw!"