| import os |
| import cv2 |
| import streamlit as st |
| import mediapipe as mp |
| from PIL import Image |
|
|
| |
| mp_hands = mp.solutions.hands |
| mp_drawing = mp.solutions.drawing_utils |
|
|
| |
| st.title("Gesture & Hand Landmark Detection 🚀") |
| st.write("This app uses MediaPipe and OpenCV to detect hand landmarks in real-time from your webcam.") |
|
|
| |
| run_webcam = st.button("Start Webcam") |
|
|
| |
| hands = mp_hands.Hands( |
| max_num_hands=2, |
| min_detection_confidence=0.5, |
| min_tracking_confidence=0.5, |
| ) |
|
|
| |
| if run_webcam: |
| stframe = st.empty() |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| st.error("Unable to access the webcam. Please ensure it's connected and accessible.") |
| else: |
| while run_webcam: |
| ret, frame = cap.read() |
| if not ret: |
| st.error("Failed to grab frame from webcam.") |
| break |
|
|
| |
| frame = cv2.flip(frame, 1) |
| frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
|
| |
| result = hands.process(frame_rgb) |
|
|
| |
| if result.multi_hand_landmarks: |
| for hand_landmarks in result.multi_hand_landmarks: |
| mp_drawing.draw_landmarks( |
| frame, |
| hand_landmarks, |
| mp_hands.HAND_CONNECTIONS, |
| mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4), |
| mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2, circle_radius=2), |
| ) |
|
|
| |
| frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) |
|
|
| |
| stframe.image(frame_bgr, channels="BGR", use_column_width=True) |
|
|
| cap.release() |
|
|
| hands.close() |
| st.write("Webcam stopped.") |
|
|