-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·83 lines (82 loc) · 3.79 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
81
82
83
#!/usr/bin/env python3
import cv2
from cvzone.HandTrackingModule import HandDetector
from time import sleep
cap = cv2.VideoCapture(0)
detector = HandDetector(detectionCon=0.8)
keysText = [["A","Z","E","R","T","Y","U","I","O","P"],["Q","S","D","F","G","H","J","K","L","M"],["W","X","C","V","B","N"]]
buttonList = []
class Key():
def __init__(self,pos,text,size=52):
self.pos = pos
self.text = text
self.size = size
y = 50
for row in keysText:
l = []
pos1 = 15
for text in row:
if text=="W":
pos1+=55
l.append(Key((pos1,y),text))
pos1+=55
y+=60
buttonList.append(l)
def drawAll(img,list):
for row in list:
for key in row:
pos,size,text = key.pos,key.size,key.text
cv2.rectangle(img,pos,(pos[0] +size,pos[1]+size),(255,0,255),cv2.FILLED)
cv2.putText(img,text,(pos[0]+5,pos[1]+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
return img
pressedKeys = []
while 1:
ret,img = cap.read()
img = cv2.flip(img,1)
img = detector.findHands(img)
lmList, bboxInfo = detector.findPosition(img)
img = drawAll(img,buttonList)
if len(lmList) !=0 :
for row in buttonList:
for key in row:
x,y = key.pos
w = h = key.size
if x < lmList[8][0] < x+w and y < lmList[8][1] < y + h:
cv2.rectangle(img,(x,y),(x +w,y+h),(0,255,0),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
l = detector.findDistance(8,12,img,draw=False)[0]
l2 = detector.findDistance(8,16,img,draw=False)[0]
if l < 30 and l2>45:
try:
if pressedKeys[-1] != key.text:
cv2.rectangle(img,(x,y),(x + w,y+h),(0,0,255),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
pressedKeys.append(key.text)
print(pressedKeys)
except IndexError:
cv2.rectangle(img,(x,y),(x + w,y+h),(0,0,255),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
pressedKeys.append(key.text)
print(pressedKeys)
elif l2<45:
try :
if (pressedKeys[-1] == pressedKeys[-2] and pressedKeys[-1] == key.text):
pass
elif (pressedKeys[-1] == key.text):
cv2.rectangle(img,(x,y),(x +w,y+h),(0,0,255),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
pressedKeys.append(key.text)
else :
cv2.rectangle(img,(x,y),(x +w,y+h),(0,0,255),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
pressedKeys.append(key.text)
pressedKeys.append(key.text)
print(pressedKeys)
except IndexError:
cv2.rectangle(img,(x,y),(x +w,y+h),(0,0,255),cv2.FILLED)
cv2.putText(img,key.text,(x+5,y+50),cv2.FONT_HERSHEY_PLAIN,4,(255,255,255),3)
pressedKeys.append(key.text)
print(pressedKeys)
sleep(0.15)
cv2.imshow("Camera",img)
cv2.waitKey(1)