Spaces:
Sleeping
Sleeping
| 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 | |
| # Initialize canvas once | |
| if canvas is None: | |
| canvas = np.zeros((h, w, 3), dtype=np.uint8) | |
| # MediaPipe expects RGB | |
| 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) | |
| # Blend live frame and canvas | |
| return cv2.addWeighted(frame, 0.5, canvas, 0.5, 0) | |
| # --- Gradio interface using gr.components --- | |
| demo = gr.Interface( | |
| fn=paint, | |
| inputs=gr.components.Image(source="webcam", type="numpy"), | |
| outputs=gr.components.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() | |