Rasleen commited on
Commit
814589f
Β·
verified Β·
1 Parent(s): 7d51cef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -15,11 +15,10 @@ def paint(frame: np.ndarray) -> np.ndarray:
15
  global canvas
16
  h, w, _ = frame.shape
17
 
18
- # Initialize canvas once
19
  if canvas is None:
20
  canvas = np.zeros((h, w, 3), dtype=np.uint8)
21
 
22
- # MediaPipe expects RGB
23
  rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
24
  results = hands.process(rgb)
25
  if results.multi_hand_landmarks:
@@ -30,15 +29,18 @@ def paint(frame: np.ndarray) -> np.ndarray:
30
  # Blend live frame and canvas
31
  return cv2.addWeighted(frame, 0.5, canvas, 0.5, 0)
32
 
33
- # --- Gradio interface using gr.components ---
34
- demo = gr.Interface(
35
- fn=paint,
36
- inputs=gr.components.Image(source="webcam", type="numpy"),
37
- outputs=gr.components.Image(type="numpy"),
38
- live=True,
39
- title="πŸ–ŒοΈ Virtual Painter",
40
- description="Move your index finger in front of the camera to draw!"
41
- )
42
-
43
- if __name__ == "__main__":
44
- demo.launch()
 
 
 
 
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
+ # Convert to RGB for MediaPipe
22
  rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
23
  results = hands.process(rgb)
24
  if results.multi_hand_landmarks:
 
29
  # Blend live frame and canvas
30
  return cv2.addWeighted(frame, 0.5, canvas, 0.5, 0)
31
 
32
+ def clear_canvas():
33
+ global canvas
34
+ canvas = None
35
+
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("## πŸ–ŒοΈ Virtual Painter\nMove your index finger in front of the camera to draw!")
38
+ webcam = gr.Webcam(type="numpy", streaming=True)
39
+ output = gr.Image(type="numpy")
40
+ clear_btn = gr.Button("Clear Canvas")
41
+
42
+ # Stream webcam β†’ paint() β†’ output
43
+ webcam.stream(fn=paint, inputs=webcam, outputs=output)
44
+ clear_btn.click(fn=clear_canvas, inputs=None, outputs=None)
45
+
46
+ demo.launch()