Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
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 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
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()
|