import cv2 import numpy as np import streamlit as st from cvzone.HandTrackingModule import HandDetector # Streamlit app title st.title("Hand Tracking Drawing App") # Initialize the webcam cap = cv2.VideoCapture(0) # Initialize the hand detector detector = HandDetector(detectionCon=0.8, maxHands=2) # Streamlit placeholder for the video feed video_placeholder = st.empty() # Initialize drawing canvas height, width = 500, 800 background_color = (0, 0, 0) img = np.full((height, width, 3), background_color, dtype=np.uint8) img_show = img.copy() # Function to draw shapes def draw(shape, x1, x2, y1, y2, color, thickness): if shape == "line": cv2.line(img, (x1, y1), (x2, y2), color, thickness) elif shape == "circle": cv2.circle(img, ((x2 + x1) // 2, (y2 + y1) // 2), int((np.sqrt((x2 - x1)**2 + (y2 - y1)**2)) / 2), color, thickness) elif shape == "rectangle": cv2.rectangle(img, (x1, y1), (x2, y2), color, thickness) # Main loop while True: # Read a frame from the webcam success, frame = cap.read() if not success: st.error("Failed to capture video from the webcam.") break # Detect hands in the frame hands, frame = detector.findHands(frame) # Display the frame in the Streamlit app video_placeholder.image(frame, channels="BGR", use_column_width=True) # Check for hand gestures and draw on the canvas if hands: # Example: Draw a line if two fingers are detected if len(hands) == 2: x1, y1 = hands[0]['center'] x2, y2 = hands[1]['center'] draw("line", x1, x2, y1, y2, (0, 255, 0), 5) # Display the drawing canvas st.image(img, channels="BGR", use_column_width=True) # Release the webcam cap.release()