-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandsTrackingModule.py
66 lines (64 loc) · 2.45 KB
/
HandsTrackingModule.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
import cv2 as c
import mediapipe as mp
import time
class HandDetect() :
def __init__(self, mode=False, maxhands=2, detectC=0.5, trackC=0.5):
self.mode = mode
self.maxhands = maxhands
self.detectC = detectC
self.trackC = trackC
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxhands, self.detectC, self.trackC)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds=[4,8,12,16,20]
def findhands(self,img,draw = True):
Rgb = c.cvtColor(img, c.COLOR_BGR2RGB)
self.result = self.hands.process(Rgb)
# print(result.multi_hand_landmarks)
if self.result.multi_hand_landmarks:
for HandsLms in self.result.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, HandsLms, self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self,img,handNo=0,draw=True):
self.lmlist = []
if self.result.multi_hand_landmarks:
myhand=self.result.multi_hand_landmarks[handNo]
for id, lm in enumerate(myhand.landmark):
# print(id,lm)
h, w, cen = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
#print(id, cx, cy)
self.lmlist.append([id,cx, cy])
#if id in (4, 8, 16, 12, 20) and draw==True:
#c.circle(img, (cx, cy), 15, (255, 0, 255), c.FILLED)return
return self.lmlist
def FingersUp(self):
finger = []
if self.lmlist[self.tipIds[0]][1] < self.lmlist[self.tipIds[0]-1][1]:
finger.append(1)
else:
finger.append(0)
for id in range(1,5):
if self.lmlist[self.tipIds[id]][2] < self.lmlist[self.tipIds[id]-2][2]:
finger.append(1)
else:
finger.append(0)
return finger
def main():#to write from this everytime for usuage
cap = c.VideoCapture(0)
ctime = 0
ptime = 0
HDT=HandDetect()
while True:
success, img = cap.read()
img = HDT.findhands(img,False)#put false fr noy showing the lines
HDT.findPosition(img)#false for not shoing dots as well
ctime = time.time()
fps = 1/(ctime-ptime)
ptime = ctime
c.putText(img, str(int(fps)), (10, 70), c.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
c.imshow("Image", img)
c.waitKey(1)
if __name__ == "__main__":
main()