Virtual_painter / app.py
Rasleen's picture
Update app.py
b6d78be verified
raw
history blame
1.11 kB
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()