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

Update app.py

Browse files

I have changed the code to previous version i.e. detect hands

Files changed (1) hide show
  1. app.py +60 -40
app.py CHANGED
@@ -248,51 +248,71 @@ output_text = ""
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",
 
248
  if "output_text" not in st.session_state:
249
  st.session_state["output_text"] = ""
250
 
 
 
251
 
252
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
253
  img = frame.to_ndarray(format="bgr24")
254
+ hands, img = detector.findHands(img, flipType=False)
 
255
 
256
+ # Render hand detection results
257
 
 
258
  if hands:
259
+ hand = hands[0]
260
+ bbox = hand["bbox"]
261
+ cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (255, 0, 0), 2)
262
+
263
+ cv2.putText(img, 'OpenCV', (50,50), font,
264
+ fontScale, color, thickness, cv2.LINE_AA)
265
+ cv2.putText(img, 'OpenCV', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 1, cv2.LINE_AA)
266
+
267
+ result_queue.put(hands)
268
+
269
+ # def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
270
+ # global indexImg, output_text
271
+
272
+ # img = frame.to_ndarray(format="bgr24")
273
+ # imgOut = segmentor.removeBG(img, imgList[indexImg])
274
+ # hands, imgOut = detector.findHands(imgOut, flipType=False)
275
+
276
+ # buttonList = [Button([30 + col * 105, 30 + row * 120], key) for row, line in enumerate(keys) for col, key in enumerate(line)]
277
+
278
+ # detections = []
279
+ # if hands:
280
+ # for i, hand in enumerate(hands):
281
+ # lmList = hand['lmList']
282
+ # bbox = hand['bbox']
283
+ # label = "Hand"
284
+ # score = hand['score']
285
+ # box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
286
+ # detections.append(Detection(label=label, score=score, box=box))
287
+
288
+ # if lmList:
289
+ # x4, y4 = lmList[4][0], lmList[4][1]
290
+ # x8, y8 = lmList[8][0], lmList[8][1]
291
+ # distance = np.sqrt((x8 - x4) ** 2 + (y8 - y4) ** 2)
292
+ # click_threshold = 10
293
+
294
+ # for button in buttonList:
295
+ # x, y = button.pos
296
+ # w, h = button.size
297
+ # if x < x8 < x + w and y < y8 < y + h:
298
+ # cv2.rectangle(imgOut, button.pos, (x + w, y + h), (0, 255, 160), -1)
299
+ # cv2.putText(imgOut, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
300
+
301
+ # if (distance / np.sqrt(bbox[2] ** 2 + bbox[3] ** 2)) * 100 < click_threshold:
302
+ # if time.time() - prev_key_time[i] > 2:
303
+ # prev_key_time[i] = time.time()
304
+ # if button.text != 'BS' and button.text != 'SPACE':
305
+ # output_text += button.text
306
+ # elif button.text == 'BS':
307
+ # output_text = output_text[:-1]
308
+ # else:
309
+ # output_text += ' '
310
+
311
+ # result_queue.put(detections)
312
+ # st.session_state["output_text"] = output_text
313
+ # return av.VideoFrame.from_ndarray(imgOut, format="bgr24")
314
+
315
+
316
 
317
  webrtc_streamer(
318
  key="virtual-keyboard",