-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_detection.py
79 lines (59 loc) · 2.74 KB
/
number_detection.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
import cv2
import mediapipe
import time
ctime=0
ptime=0
cap=cv2.VideoCapture(0)
medhands=mediapipe.solutions.hands
hands=medhands.Hands(max_num_hands=1,min_detection_confidence=0.7)
draw=mediapipe.solutions.drawing_utils
while True:
success, img=cap.read()
img = cv2.flip(img,1)
imgrgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
res = hands.process(imgrgb)
lmlist=[]
tipids=[4,8,12,16,20] #list of all landmarks of the tips of fingers
cv2.rectangle(img,(20,350),(90,440),(0,255,204),cv2.FILLED)
cv2.rectangle(img,(20,350),(90,440),(0,0,0),5)
if res.multi_hand_landmarks:
for handlms in res.multi_hand_landmarks:
for id,lm in enumerate(handlms.landmark):
h,w,c= img.shape
cx,cy=int(lm.x * w) , int(lm.y * h)
lmlist.append([id,cx,cy])
if len(lmlist) != 0 and len(lmlist)==21:
fingerlist=[]
#thumb and dealing with flipping of hands
if lmlist[12][1] > lmlist[20][1]:
if lmlist[tipids[0]][1] > lmlist[tipids[0]-1][1]:
fingerlist.append(1)
else:
fingerlist.append(0)
else:
if lmlist[tipids[0]][1] < lmlist[tipids[0]-1][1]:
fingerlist.append(1)
else:
fingerlist.append(0)
#others
for id in range (1,5):
if lmlist[tipids[id]][2] < lmlist[tipids[id]-2][2]:
fingerlist.append(1)
else:
fingerlist.append(0)
if len(fingerlist)!=0:
fingercount=fingerlist.count(1)
cv2.putText(img,str(fingercount),(25,430),cv2.FONT_HERSHEY_PLAIN,6,(0,0,0),5)
#change color of points and lines
draw.draw_landmarks(img,handlms,medhands.HAND_CONNECTIONS,draw.DrawingSpec(color=(0,255,204),thickness=2,circle_radius=2),draw.DrawingSpec(color=(0,0,0),thickness=2,circle_radius=3))
#fps counter
ctime = time.time()
fps=1/(ctime-ptime)
ptime=ctime
#fps display
cv2.putText(img,f'FPS:{str(int(fps))}',(0,12),cv2.FONT_HERSHEY_PLAIN,1,(0,255,0),1)
cv2.imshow("hand gestures",img)
#press q to quit
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()