RP-Azul commited on
Commit
1740e15
·
verified ·
1 Parent(s): 207414d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -58
app.py CHANGED
@@ -1,74 +1,66 @@
1
  import os
 
2
  import streamlit as st
3
  import mediapipe as mp
4
- import cv2
5
- from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
6
- import av
7
- import asyncio
8
-
9
- # Prevent asyncio event loop conflicts
10
- try:
11
- asyncio.get_event_loop().run_until_complete(asyncio.sleep(0))
12
- except RuntimeError:
13
- asyncio.set_event_loop(asyncio.new_event_loop())
14
 
15
  # Initialize MediaPipe Hands
16
  mp_hands = mp.solutions.hands
17
  mp_drawing = mp.solutions.drawing_utils
18
 
19
- # Path to the gesture recognizer model
20
- model_path = "model/gesture_recognizer.task"
21
- if not os.path.exists(model_path):
22
- st.error(f"Model file not found at {model_path}. Please upload it to the correct directory.")
23
- st.stop()
 
24
 
25
- class VideoProcessor(VideoTransformerBase):
26
- def __init__(self):
27
- # Initialize MediaPipe Hands
28
- self.hands = mp_hands.Hands(
29
- max_num_hands=2,
30
- model_complexity=0,
31
- min_detection_confidence=0.5,
32
- min_tracking_confidence=0.5,
33
- )
34
 
35
- def recv(self, frame):
36
- img = frame.to_ndarray(format="bgr24")
37
- img = cv2.flip(img, 1) # Flip for a mirror effect
38
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
 
 
 
39
 
40
- # Process the frame for hand landmarks
41
- result = self.hands.process(img_rgb)
 
42
 
43
- # Draw hand landmarks
44
- if result.multi_hand_landmarks:
45
- for hand_landmarks in result.multi_hand_landmarks:
46
- mp_drawing.draw_landmarks(
47
- img, hand_landmarks, mp_hands.HAND_CONNECTIONS
48
- )
49
 
50
- return av.VideoFrame.from_ndarray(img, format="bgr24")
 
 
 
 
 
 
 
 
 
51
 
52
- # Streamlit UI
53
- st.title("Gesture & Hand Landmark Detection 🚀")
54
- st.write("This app uses MediaPipe and Streamlit to detect hand landmarks in real-time.")
55
 
56
- # WebRTC streamer for live video
57
- webrtc_streamer(
58
- key="gesture-detection",
59
- video_processor_factory=VideoProcessor,
60
- rtc_configuration={
61
- "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
62
- },
63
- )
64
 
65
- # Footer
66
- st.markdown(
67
- """
68
- <style>
69
- .footer {text-align: center; font-size: 12px; color: grey; margin-top: 20px;}
70
- </style>
71
- <p class="footer">Made with ❤️ using Streamlit & MediaPipe</p>
72
- """,
73
- unsafe_allow_html=True,
74
- )
 
1
  import os
2
+ import cv2
3
  import streamlit as st
4
  import mediapipe as mp
5
+ from PIL import Image
 
 
 
 
 
 
 
 
 
6
 
7
  # Initialize MediaPipe Hands
8
  mp_hands = mp.solutions.hands
9
  mp_drawing = mp.solutions.drawing_utils
10
 
11
+ # Streamlit App Title
12
+ st.title("Gesture & Hand Landmark Detection 🚀")
13
+ st.write("This app uses MediaPipe and OpenCV to detect hand landmarks in real-time from your webcam.")
14
+
15
+ # Start Webcam
16
+ run_webcam = st.button("Start Webcam")
17
 
18
+ # MediaPipe Hands configuration
19
+ hands = mp_hands.Hands(
20
+ max_num_hands=2,
21
+ min_detection_confidence=0.5,
22
+ min_tracking_confidence=0.5,
23
+ )
 
 
 
24
 
25
+ # Webcam video feed
26
+ if run_webcam:
27
+ stframe = st.empty() # Placeholder for video frames
28
+ cap = cv2.VideoCapture(0) # Open the first webcam
29
+
30
+ if not cap.isOpened():
31
+ st.error("Unable to access the webcam. Please ensure it's connected and accessible.")
32
+ else:
33
+ while run_webcam:
34
+ ret, frame = cap.read()
35
+ if not ret:
36
+ st.error("Failed to grab frame from webcam.")
37
+ break
38
 
39
+ # Convert frame to RGB
40
+ frame = cv2.flip(frame, 1) # Flip horizontally
41
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
42
 
43
+ # Process the frame with MediaPipe Hands
44
+ result = hands.process(frame_rgb)
 
 
 
 
45
 
46
+ # Draw hand landmarks
47
+ if result.multi_hand_landmarks:
48
+ for hand_landmarks in result.multi_hand_landmarks:
49
+ mp_drawing.draw_landmarks(
50
+ frame,
51
+ hand_landmarks,
52
+ mp_hands.HAND_CONNECTIONS,
53
+ mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
54
+ mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2, circle_radius=2),
55
+ )
56
 
57
+ # Convert back to BGR for OpenCV display
58
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
 
59
 
60
+ # Display frame in Streamlit
61
+ stframe.image(frame_bgr, channels="BGR", use_column_width=True)
 
 
 
 
 
 
62
 
63
+ cap.release()
64
+
65
+ hands.close()
66
+ st.write("Webcam stopped.")