abubasith86 commited on
Commit
3ca982b
ยท
verified ยท
1 Parent(s): a3c0886

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +81 -38
src/streamlit_app.py CHANGED
@@ -7,6 +7,8 @@ import mediapipe as mp
7
  import time
8
  import os
9
  from mediapipe.python.solutions import hands
 
 
10
 
11
  st.set_page_config(page_title="๐Ÿ–๏ธ Hand Tracking Demo", layout="wide")
12
 
@@ -54,52 +56,93 @@ st.title("๐Ÿ–๏ธ Hand Tracking Demo")
54
  stop_button = st.button("Stop")
55
  frame_placeholder = st.empty()
56
  finger_count_text = st.empty()
57
- # Initialize webcam
58
- cap = cv2.VideoCapture(0)
59
- # Initialize finger count
60
- current_finger_count = 0
61
-
62
- # Initialize MediaPipe Hands
63
- with hands.Hands(
64
- max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5
65
- ) as hands_model:
66
- while not stop_button:
67
- ret, frame = cap.read()
68
- if not ret:
69
- st.error("Failed to capture video from webcam")
70
- break
71
- frame = cv2.flip(frame, 1)
72
- frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
73
  mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame_rgb)
74
  result = landmarker.detect(mp_image)
75
- # Reset finger count for each frame
76
- current_finger_count = 0
77
  if result.hand_landmarks:
78
  for hand_landmarks in result.hand_landmarks:
79
- # Draw landmarks & connections
80
- draw_hand_landmarks(frame, hand_landmarks)
81
- # Calculate finger count for this hand
82
  hand_finger_count = 0
83
- if hand_landmarks[4].y < hand_landmarks[3].y: # Thumb
84
  hand_finger_count += 1
85
- for i in [8, 12, 16, 20]: # Index, middle, ring, pinky
86
  if hand_landmarks[i].y < hand_landmarks[i - 2].y:
87
  hand_finger_count += 1
88
- # Add this hand's fingers to the total count
89
- current_finger_count += hand_finger_count
90
- # Display finger count
91
- finger_count_text.markdown(f"### Fingers detected: {current_finger_count}")
92
- # Convert BGR to RGB for displaying in Streamlit
93
- frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
94
- frame_placeholder.image(frame_rgb, channels="RGB")
95
- # Add a small delay to simulate real-time processing
96
- time.sleep(0.05)
97
- # Rerun to check if stop button was pressed
98
- if stop_button:
99
- break
100
- # Release resources
101
- cap.release()
102
- st.success("Camera released. Application stopped.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
 
105
  # """
 
7
  import time
8
  import os
9
  from mediapipe.python.solutions import hands
10
+ from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
11
+
12
 
13
  st.set_page_config(page_title="๐Ÿ–๏ธ Hand Tracking Demo", layout="wide")
14
 
 
56
  stop_button = st.button("Stop")
57
  frame_placeholder = st.empty()
58
  finger_count_text = st.empty()
59
+
60
+
61
+ class VideoProcessor(VideoTransformerBase):
62
+ def __init__(self):
63
+ self.finger_count = 0
64
+
65
+ def transform(self, frame):
66
+ img = frame.to_ndarray(format="bgr24")
67
+ frame_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
 
68
  mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame_rgb)
69
  result = landmarker.detect(mp_image)
70
+
71
+ self.finger_count = 0
72
  if result.hand_landmarks:
73
  for hand_landmarks in result.hand_landmarks:
74
+ draw_hand_landmarks(img, hand_landmarks)
 
 
75
  hand_finger_count = 0
76
+ if hand_landmarks[4].y < hand_landmarks[3].y:
77
  hand_finger_count += 1
78
+ for i in [8, 12, 16, 20]:
79
  if hand_landmarks[i].y < hand_landmarks[i - 2].y:
80
  hand_finger_count += 1
81
+ self.finger_count += hand_finger_count
82
+
83
+ return img
84
+
85
+ # Start webcam capture
86
+ ctx = webrtc_streamer(
87
+ key="hand-tracker",
88
+ video_processor_factory=VideoProcessor,
89
+ media_stream_constraints={"video": True, "audio": False},
90
+ async_processing=True,
91
+ )
92
+
93
+ # Display finger count
94
+ if ctx.state.playing:
95
+ st.markdown("### Live tracking active...")
96
+ while ctx.video_processor:
97
+ finger_count_text.markdown(f"### Fingers detected: {ctx.video_processor.finger_count}")
98
+ time.sleep(0.1)
99
+ else:
100
+ st.info("Click 'Start' to begin camera tracking.")
101
+
102
+ # Initialize finger count
103
+ # current_finger_count = 0
104
+
105
+ # # Initialize MediaPipe Hands
106
+ # with hands.Hands(
107
+ # max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5
108
+ # ) as hands_model:
109
+ # while not stop_button:
110
+ # ret, frame = cap.read()
111
+ # if not ret:
112
+ # st.error("Failed to capture video from webcam")
113
+ # break
114
+ # frame = cv2.flip(frame, 1)
115
+ # frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
116
+ # mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame_rgb)
117
+ # result = landmarker.detect(mp_image)
118
+ # # Reset finger count for each frame
119
+ # current_finger_count = 0
120
+ # if result.hand_landmarks:
121
+ # for hand_landmarks in result.hand_landmarks:
122
+ # # Draw landmarks & connections
123
+ # draw_hand_landmarks(frame, hand_landmarks)
124
+ # # Calculate finger count for this hand
125
+ # hand_finger_count = 0
126
+ # if hand_landmarks[4].y < hand_landmarks[3].y: # Thumb
127
+ # hand_finger_count += 1
128
+ # for i in [8, 12, 16, 20]: # Index, middle, ring, pinky
129
+ # if hand_landmarks[i].y < hand_landmarks[i - 2].y:
130
+ # hand_finger_count += 1
131
+ # # Add this hand's fingers to the total count
132
+ # current_finger_count += hand_finger_count
133
+ # # Display finger count
134
+ # finger_count_text.markdown(f"### Fingers detected: {current_finger_count}")
135
+ # # Convert BGR to RGB for displaying in Streamlit
136
+ # frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
137
+ # frame_placeholder.image(frame_rgb, channels="RGB")
138
+ # # Add a small delay to simulate real-time processing
139
+ # time.sleep(0.05)
140
+ # # Rerun to check if stop button was pressed
141
+ # if stop_button:
142
+ # break
143
+ # # Release resources
144
+ # cap.release()
145
+ # st.success("Camera released. Application stopped.")
146
 
147
 
148
  # """