Pratyush101 commited on
Commit
6b85062
·
verified ·
1 Parent(s): ab7606f

Update app.py

Browse files

I have included the result queue

Files changed (1) hide show
  1. app.py +51 -39
app.py CHANGED
@@ -199,6 +199,9 @@ from cvzone.HandTrackingModule import HandDetector
199
  from cvzone.SelfiSegmentationModule import SelfiSegmentation
200
  import os
201
  import time
 
 
 
202
  from sample_utils.turn import get_ice_servers
203
 
204
  logger = logging.getLogger(__name__)
@@ -224,6 +227,13 @@ class Button:
224
  self.size = size
225
  self.text = text
226
 
 
 
 
 
 
 
 
227
  listImg = os.listdir('model/street') if os.path.exists('model/street') else []
228
  if not listImg:
229
  st.error("Error: 'street' directory is missing or empty. Please add background images.")
@@ -238,50 +248,51 @@ output_text = ""
238
  if "output_text" not in st.session_state:
239
  st.session_state["output_text"] = ""
240
 
241
- def video_frame_callback(frame):
242
  global indexImg, output_text
243
 
244
  img = frame.to_ndarray(format="bgr24")
245
  imgOut = segmentor.removeBG(img, imgList[indexImg])
246
- hands, img = detector.findHands(imgOut, flipType=False)
247
-
248
- keyboard_canvas = np.zeros_like(img)
249
- buttonList = []
250
-
251
- for key in keys[0]:
252
- buttonList.append(Button([30 + keys[0].index(key) * 105, 30], key))
253
- for key in keys[1]:
254
- buttonList.append(Button([30 + keys[1].index(key) * 105, 150], key))
255
- for key in keys[2]:
256
- buttonList.append(Button([30 + keys[2].index(key) * 105, 260], key))
257
-
258
- for i, hand in enumerate(hands):
259
- lmList = hand['lmList']
260
- if lmList:
261
- x4, y4 = lmList[4][0], lmList[4][1]
262
- x8, y8 = lmList[8][0], lmList[8][1]
263
- distance = np.sqrt((x8 - x4) ** 2 + (y8 - y4) ** 2)
264
- click_threshold = 10
265
-
266
- for button in buttonList:
267
- x, y = button.pos
268
- w, h = button.size
269
- if x < x8 < x + w and y < y8 < y + h:
270
- cv2.rectangle(img, button.pos, (x + w, y + h), (0, 255, 160), -1)
271
- cv2.putText(img, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
272
-
273
- if (distance / np.sqrt((hand['bbox'][2]) ** 2 + (hand['bbox'][3]) ** 2)) * 100 < click_threshold:
274
- if time.time() - prev_key_time[i] > 2:
275
- prev_key_time[i] = time.time()
276
- if button.text != 'BS' and button.text != 'SPACE':
277
- output_text += button.text
278
- elif button.text == 'BS':
279
- output_text = output_text[:-1]
280
- else:
281
- output_text += ' '
282
-
 
283
  st.session_state["output_text"] = output_text
284
- return frame.from_ndarray(img, format="bgr24")
285
 
286
  webrtc_streamer(
287
  key="virtual-keyboard",
@@ -295,3 +306,4 @@ webrtc_streamer(
295
  st.subheader("Output Text")
296
  st.text_area("Live Input:", value=st.session_state["output_text"], height=200)
297
 
 
 
199
  from cvzone.SelfiSegmentationModule import SelfiSegmentation
200
  import os
201
  import time
202
+ import av
203
+ import queue
204
+ from typing import List, NamedTuple
205
  from sample_utils.turn import get_ice_servers
206
 
207
  logger = logging.getLogger(__name__)
 
227
  self.size = size
228
  self.text = text
229
 
230
+ class Detection(NamedTuple):
231
+ label: str
232
+ score: float
233
+ box: np.ndarray
234
+
235
+ result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
236
+
237
  listImg = os.listdir('model/street') if os.path.exists('model/street') else []
238
  if not listImg:
239
  st.error("Error: 'street' directory is missing or empty. Please add background images.")
 
248
  if "output_text" not in st.session_state:
249
  st.session_state["output_text"] = ""
250
 
251
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
252
  global indexImg, output_text
253
 
254
  img = frame.to_ndarray(format="bgr24")
255
  imgOut = segmentor.removeBG(img, imgList[indexImg])
256
+ hands, imgOut = detector.findHands(imgOut, flipType=False)
257
+
258
+ buttonList = [Button([30 + col * 105, 30 + row * 120], key) for row, line in enumerate(keys) for col, key in enumerate(line)]
259
+
260
+ detections = []
261
+ if hands:
262
+ for i, hand in enumerate(hands):
263
+ lmList = hand['lmList']
264
+ bbox = hand['bbox']
265
+ label = "Hand"
266
+ score = hand['score']
267
+ box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
268
+ detections.append(Detection(label=label, score=score, box=box))
269
+
270
+ if lmList:
271
+ x4, y4 = lmList[4][0], lmList[4][1]
272
+ x8, y8 = lmList[8][0], lmList[8][1]
273
+ distance = np.sqrt((x8 - x4) ** 2 + (y8 - y4) ** 2)
274
+ click_threshold = 10
275
+
276
+ for button in buttonList:
277
+ x, y = button.pos
278
+ w, h = button.size
279
+ if x < x8 < x + w and y < y8 < y + h:
280
+ cv2.rectangle(imgOut, button.pos, (x + w, y + h), (0, 255, 160), -1)
281
+ cv2.putText(imgOut, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
282
+
283
+ if (distance / np.sqrt(bbox[2] ** 2 + bbox[3] ** 2)) * 100 < click_threshold:
284
+ if time.time() - prev_key_time[i] > 2:
285
+ prev_key_time[i] = time.time()
286
+ if button.text != 'BS' and button.text != 'SPACE':
287
+ output_text += button.text
288
+ elif button.text == 'BS':
289
+ output_text = output_text[:-1]
290
+ else:
291
+ output_text += ' '
292
+
293
+ result_queue.put(detections)
294
  st.session_state["output_text"] = output_text
295
+ return av.VideoFrame.from_ndarray(imgOut, format="bgr24")
296
 
297
  webrtc_streamer(
298
  key="virtual-keyboard",
 
306
  st.subheader("Output Text")
307
  st.text_area("Live Input:", value=st.session_state["output_text"], height=200)
308
 
309
+