Random / app.py
RP-Azul's picture
Update app.py
1740e15 verified
raw
history blame
2.12 kB
import os
import cv2
import streamlit as st
import mediapipe as mp
from PIL import Image
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# Streamlit App Title
st.title("Gesture & Hand Landmark Detection 🚀")
st.write("This app uses MediaPipe and OpenCV to detect hand landmarks in real-time from your webcam.")
# Start Webcam
run_webcam = st.button("Start Webcam")
# MediaPipe Hands configuration
hands = mp_hands.Hands(
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
)
# Webcam video feed
if run_webcam:
stframe = st.empty() # Placeholder for video frames
cap = cv2.VideoCapture(0) # Open the first webcam
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
# Convert frame to RGB
frame = cv2.flip(frame, 1) # Flip horizontally
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame with MediaPipe Hands
result = hands.process(frame_rgb)
# Draw hand landmarks
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),
)
# Convert back to BGR for OpenCV display
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# Display frame in Streamlit
stframe.image(frame_bgr, channels="BGR", use_column_width=True)
cap.release()
hands.close()
st.write("Webcam stopped.")