RP-Azul commited on
Commit
a95228d
·
verified ·
1 Parent(s): 0151130

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -55
app.py CHANGED
@@ -1,66 +1,57 @@
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.")
 
 
 
1
  import streamlit as st
2
+ from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
3
  import mediapipe as mp
4
+ import cv2
5
+ import av
6
 
7
  # Initialize MediaPipe Hands
8
  mp_hands = mp.solutions.hands
9
  mp_drawing = mp.solutions.drawing_utils
10
 
11
+ class VideoProcessor(VideoTransformerBase):
12
+ def __init__(self):
13
+ # Initialize MediaPipe Hands
14
+ self.hands = mp_hands.Hands(
15
+ max_num_hands=2,
16
+ min_detection_confidence=0.5,
17
+ min_tracking_confidence=0.5,
18
+ )
19
+
20
+ def recv(self, frame):
21
+ img = frame.to_ndarray(format="bgr24")
22
+ img = cv2.flip(img, 1) # Flip for a mirror effect
23
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
24
+
25
+ # Process the frame for hand landmarks
26
+ result = self.hands.process(img_rgb)
27
+
28
+ # Draw hand landmarks
29
+ if result.multi_hand_landmarks:
30
+ for hand_landmarks in result.multi_hand_landmarks:
31
+ mp_drawing.draw_landmarks(
32
+ img, hand_landmarks, mp_hands.HAND_CONNECTIONS
33
+ )
34
+
35
+ return av.VideoFrame.from_ndarray(img, format="bgr24")
36
+
37
+ # Streamlit UI
38
  st.title("Gesture & Hand Landmark Detection 🚀")
39
+ st.write("This app uses MediaPipe and Streamlit to detect hand landmarks in real-time from your webcam.")
 
 
 
40
 
41
+ # WebRTC streamer for live video
42
+ webrtc_streamer(
43
+ key="gesture-detection",
44
+ video_processor_factory=VideoProcessor,
45
+ rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
46
  )
47
 
48
+ # Footer
49
+ st.markdown(
50
+ """
51
+ <style>
52
+ .footer {text-align: center; font-size: 12px; color: grey; margin-top: 20px;}
53
+ </style>
54
+ <p class="footer"></p>
55
+ """,
56
+ unsafe_allow_html=True,
57
+ )