Pratyush101 commited on
Commit
b61be7b
·
verified ·
1 Parent(s): 9289f95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -86
app.py CHANGED
@@ -25,8 +25,20 @@ class Button:
25
  self.size = size
26
  self.text = text
27
 
28
- # Function to process the video frame from the webcam
29
- # def process_video_frame(frame: av.VideoFrame, detector, segmentor, imgList, indexImg, keys, session_state)-> av.VideoFrame:
 
 
 
 
 
 
 
 
 
 
 
 
30
  # # Convert the frame to a numpy array (BGR format)
31
  # image = frame.to_ndarray(format="bgr24")
32
 
@@ -77,90 +89,21 @@ class Button:
77
 
78
 
79
 
80
- # Initialize components
81
- detector = HandDetector(maxHands=1, detectionCon=0.8)
82
- segmentor = SelfiSegmentation()
83
- keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
84
- ["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
85
- ["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
86
-
87
- listImg = os.listdir('model/street')
88
- imgList = [cv2.imread(f'model/street/{imgPath}') for imgPath in listImg]
89
- indexImg = 0
90
-
91
-
92
- # Function to process the video frame from the webcam
93
- def process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, session_state):
94
- # Convert the frame to a numpy array (BGR format)
95
- image = frame.to_ndarray(format="bgr24")
96
-
97
- # Remove background using SelfiSegmentation
98
- imgOut = segmentor.removeBG(image, imgList[indexImg])
99
-
100
- # Detect hands on the background-removed image
101
- hands, img = detector.findHands(imgOut, flipType=False)
102
-
103
- # Create a blank canvas for the keyboard
104
- keyboard_canvas = np.zeros_like(img)
105
- buttonList = []
106
-
107
- # Create buttons for the virtual keyboard based on the keys list
108
- for key in keys[0]:
109
- buttonList.append(Button([30 + keys[0].index(key) * 105, 30], key))
110
- for key in keys[1]:
111
- buttonList.append(Button([30 + keys[1].index(key) * 105, 150], key))
112
- for key in keys[2]:
113
- buttonList.append(Button([30 + keys[2].index(key) * 105, 260], key))
114
-
115
- # Draw the buttons on the keyboard canvas
116
- for button in buttonList:
117
- x, y = button.pos
118
- cv2.rectangle(keyboard_canvas, (x, y), (x + button.size[0], y + button.size[1]), (255, 255, 255), -1)
119
- cv2.putText(keyboard_canvas, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 0), 3)
120
-
121
- # Handle input and gestures from detected hands
122
- if hands:
123
- for hand in hands:
124
- lmList = hand["lmList"]
125
- if lmList:
126
- # Get the coordinates of the index finger tip (landmark 8)
127
- x8, y8 = lmList[8][0], lmList[8][1]
128
- for button in buttonList:
129
- bx, by = button.pos
130
- bw, bh = button.size
131
- # Check if the index finger is over a button
132
- if bx < x8 < bx + bw and by < y8 < by + bh:
133
- # Highlight the button and update the text
134
- cv2.rectangle(img, (bx, by), (bx + bw, by + bh), (0, 255, 0), -1)
135
- cv2.putText(img, button.text, (bx + 20, by + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
136
- # Update the output text in session_state
137
- session_state["output_text"] += button.text
138
-
139
- # Corrected return: Create a video frame from the ndarray image
140
- return av.VideoFrame.from_ndarray(img, format="bgr24")
141
-
142
 
143
 
144
 
145
 
 
 
 
146
 
 
 
 
 
 
147
 
148
-
149
-
150
-
151
-
152
-
153
- # def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
154
- # img = frame.to_ndarray(format="bgr24")
155
- # hands, img = detector.findHands(img, flipType=False)
156
-
157
- # # Render hand detection results
158
- # if hands:
159
- # hand = hands[0]
160
- # bbox = hand["bbox"]
161
- # cv2.rectangle(img, (bbox[0], bbox[1]), (255, 0, 0), 2)
162
-
163
- # return av.VideoFrame.from_ndarray(img, format="bgr24")
164
 
165
  # Shared state for output text
166
  if "output_text" not in st.session_state:
@@ -169,21 +112,23 @@ if "output_text" not in st.session_state:
169
  # Create a thread-safe queue for passing results from callback
170
  result_queue = queue.Queue()
171
 
172
- def video_frame_callback(frame):
173
- # Process the frame asynchronously
174
- processed_frame = process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, st.session_state)
175
- # Put the processed frame into the queue
176
- result_queue.put(processed_frame)
177
- return processed_frame
178
 
179
  webrtc_ctx = webrtc_streamer(
180
  key="keyboard-demo",
181
  mode=WebRtcMode.SENDRECV,
182
  rtc_configuration={
183
  "iceServers": get_ice_servers(),
 
184
  },
185
  video_frame_callback=video_frame_callback,
186
  media_stream_constraints={"video": True, "audio": False},
 
187
  )
188
 
189
  st.markdown("### Instructions")
 
25
  self.size = size
26
  self.text = text
27
 
28
+ # # Initialize components
29
+ # detector = HandDetector(maxHands=1, detectionCon=0.8)
30
+ # segmentor = SelfiSegmentation()
31
+ # keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
32
+ # ["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
33
+ # ["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
34
+
35
+ # listImg = os.listdir('model/street')
36
+ # imgList = [cv2.imread(f'model/street/{imgPath}') for imgPath in listImg]
37
+ # indexImg = 0
38
+
39
+
40
+ # # Function to process the video frame from the webcam
41
+ # def process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, session_state):
42
  # # Convert the frame to a numpy array (BGR format)
43
  # image = frame.to_ndarray(format="bgr24")
44
 
 
89
 
90
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
 
94
 
95
 
96
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
97
+ img = frame.to_ndarray(format="bgr24")
98
+ hands, img = detector.findHands(img, flipType=False)
99
 
100
+ # Render hand detection results
101
+ if hands:
102
+ hand = hands[0]
103
+ bbox = hand["bbox"]
104
+ cv2.rectangle(img, (bbox[0], bbox[1]), (255, 0, 0), 2)
105
 
106
+ return av.VideoFrame.from_ndarray(img, format="bgr24")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  # Shared state for output text
109
  if "output_text" not in st.session_state:
 
112
  # Create a thread-safe queue for passing results from callback
113
  result_queue = queue.Queue()
114
 
115
+ # def video_frame_callback(frame):
116
+ # # Process the frame asynchronously
117
+ # processed_frame = process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, st.session_state)
118
+ # # Put the processed frame into the queue
119
+ # result_queue.put(processed_frame)
120
+ # return processed_frame
121
 
122
  webrtc_ctx = webrtc_streamer(
123
  key="keyboard-demo",
124
  mode=WebRtcMode.SENDRECV,
125
  rtc_configuration={
126
  "iceServers": get_ice_servers(),
127
+ "iceTransportPolicy": "relay",
128
  },
129
  video_frame_callback=video_frame_callback,
130
  media_stream_constraints={"video": True, "audio": False},
131
+ async_processing=True,
132
  )
133
 
134
  st.markdown("### Instructions")