Pratyush101 commited on
Commit
0aaf03a
·
verified ·
1 Parent(s): 7e54a2e

Create webrtc_helpers.py

Browse files
Files changed (1) hide show
  1. sample_utils/webrtc_helpers.py +45 -0
sample_utils/webrtc_helpers.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from cvzone.HandTrackingModule import HandDetector
4
+
5
+ class Button:
6
+ def __init__(self, pos, text, size=[100, 100]):
7
+ self.pos = pos
8
+ self.size = size
9
+ self.text = text
10
+
11
+ def process_video_frame(frame, detector, segmentor, imgList, indexImg, keys, session_state):
12
+ image = frame.to_ndarray(format="bgr24")
13
+ imgOut = segmentor.removeBG(image, imgList[indexImg])
14
+
15
+ hands, img = detector.findHands(imgOut, flipType=False)
16
+ keyboard_canvas = np.zeros_like(img)
17
+ buttonList = []
18
+
19
+ for key in keys[0]:
20
+ buttonList.append(Button([30 + keys[0].index(key) * 105, 30], key))
21
+ for key in keys[1]:
22
+ buttonList.append(Button([30 + keys[1].index(key) * 105, 150], key))
23
+ for key in keys[2]:
24
+ buttonList.append(Button([30 + keys[2].index(key) * 105, 260], key))
25
+
26
+ for button in buttonList:
27
+ x, y = button.pos
28
+ cv2.rectangle(keyboard_canvas, (x, y), (x + button.size[0], y + button.size[1]), (255, 255, 255), -1)
29
+ cv2.putText(keyboard_canvas, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 0), 3)
30
+
31
+ # Handle input and gestures
32
+ if hands:
33
+ for hand in hands:
34
+ lmList = hand["lmList"]
35
+ if lmList:
36
+ x8, y8 = lmList[8][0], lmList[8][1]
37
+ for button in buttonList:
38
+ bx, by = button.pos
39
+ bw, bh = button.size
40
+ if bx < x8 < bx + bw and by < y8 < by + bh:
41
+ cv2.rectangle(img, (bx, by), (bx + bw, by + bh), (0, 255, 0), -1)
42
+ cv2.putText(img, button.text, (bx + 20, by + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
43
+ session_state["output_text"] += button.text
44
+
45
+ return frame.from_ndarray(img, format="bgr24")