-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (56 loc) · 2.18 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# import libraries and hand detector from another file
import cv2
from handmodule import handDetector
import time
from pynput.keyboard import Controller
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
detector = handDetector(detectionConf=0.8)
finalText = ''
#make a keyboard
keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
keyboard = Controller()
def drawAll(img, buttonList):
for b in buttonList:
x,y = b.pos
w,h = b.size
cv2.rectangle(img, b.pos, (x+w, y+h), (255,0,255), cv2.FILLED)
cv2.putText(img, b.text, (x+20, y+75), cv2.FONT_HERSHEY_PLAIN, 4, (255,255,255), 4)
return img
class Button():
def __init__(self, pos, text, size=[85,85]):
self.pos = pos
self.text = text
self.size = size
buttonList = []
for i in range(len(keys)):
for j, key in enumerate(keys[i]):
buttonList.append(Button([100*j+50, 100 * i +50], key))
# run
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPos(img)
img = drawAll(img, buttonList)
if lmList:
for b in buttonList:
x,y = b.pos
w,h = b.size
if x<lmList[8][1]<x+w and y<lmList[8][2]<y+h:
cv2.rectangle(img, b.pos, (x+w, y+h), (175,0,175), cv2.FILLED)
cv2.putText(img, b.text, (x+20, y+75), cv2.FONT_HERSHEY_PLAIN, 4, (255,255,255), 4)
l = detector.findDistance(img, 12, 8)
print(l)
if l[0] <= 40:
keyboard.press(b.text)
cv2.rectangle(img, b.pos, (x+w, y+h), (0,255,0), cv2.FILLED)
cv2.putText(img, b.text, (x+20, y+75), cv2.FONT_HERSHEY_PLAIN, 4, (255,255,255), 4)
finalText += b.text
time.sleep(0.5)
cv2.rectangle(img, (50, 350), (700, 450), (175, 0 , 175), cv2.FILLED)
cv2.putText(img, finalText, (60, 425), cv2.FONT_HERSHEY_PLAIN, 5, (0,255,0), 5)
cv2.imshow("Image", img)
cv2.waitKey(1)