-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackHands.py
77 lines (64 loc) · 3.13 KB
/
TrackHands.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
import cv2
import time
import mediapipe as mp # library for detecting the hands
class HandDetector :
def __init__(self, image_mode= False, max_num_hands =3, min_detection_confidence =0.5, min_tracking_confidence =0.5):
self.image_mode = image_mode
self.max_num_hands = max_num_hands
self.min_detection_confidence = min_detection_confidence
self.min_tracking_confidence = min_tracking_confidence
self.mphands= mp.solutions.hands
self.hands = self.mphands.Hands(self.image_mode, self.max_num_hands, self.min_detection_confidence, self.min_tracking_confidence)
self.mpdraw = mp.solutions.drawing_utils
self.finger_tip_id= [4,8,12,16,20]
def findPosition(self, img, hand_num =0):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
self.lm_list=[]
if self.results.multi_hand_landmarks:
myHand= self.results.multi_hand_landmarks[hand_num]
for id, lm in enumerate(myHand.landmark):
#print(id, lm)
h, w, c = img.shape
cx,cy = int(lm.x*w), int(lm.y*h)
self.lm_list.append([id, cx, cy])
#if draw:
#cv2.circle(img, center=(cx,cy),radius=3, color=(255,255,255), thickness=1)
return self.lm_list
def fingerStatus(self):
#if len(self.lm_list)!=0:
fingers=[]
if (self.lm_list[self.finger_tip_id[0]][1] > self.lm_list[self.finger_tip_id[0] - 1][1]):
fingers.append(0)
else:
fingers.append(1)
for i in range(1,5):
if (self.lm_list[self.finger_tip_id[i]][2] > self.lm_list[self.finger_tip_id[i] - 2][2]):
fingers.append(0)
else:
fingers.append(1)
return fingers
i = 1
if __name__ == "__main__" :
cap = cv2.VideoCapture(0)
previousT = 0
currentT= 0
detector = HandDetector()
while True:
ret, img = cap.read()
landmark_list = detector.findPosition(img)
if len(landmark_list) == 0 :
cv2.putText(img, 'NOT Detected', (10,140), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(0,0,255))
else :
my_fingers = detector.fingerStatus()
if (my_fingers[1] and not my_fingers[2]):
cv2.circle(img, center=(landmark_list[8][1],landmark_list[8][2]),radius=20, color=(255,255,0), thickness=-1)
cv2.putText(img, 'Detected write mode', (10,140), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255,0,0))
else :
cv2.putText(img, 'Detected', (10,140), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255,0,0))
currentT = time.time()
fps = 1/(currentT- previousT)
previousT = currentT
cv2.putText(img, 'Client FPS:' + str(int(fps)), (10,70), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255,0,0))
cv2.imshow('img', img)
cv2.waitKey(1)