1+ import cv2
2+ import mediapipe as mp
3+ import time
4+ import math
5+ import numpy as np
6+
7+ class handDetector ():
8+ def __init__ (self , mode = False , maxHands = 2 , detectionCon = 0.5 ,modelComplexity = 1 , trackCon = 0.5 ):
9+ self .mode = mode
10+ self .maxHands = maxHands
11+ self .detectionCon = detectionCon
12+ self .modelComplex = modelComplexity
13+ self .trackCon = trackCon
14+
15+ self .mpHands = mp .solutions .hands
16+ self .hands = self .mpHands .Hands (self .mode , self .maxHands ,self .modelComplex , self .detectionCon , self .trackCon )
17+ self .mpDraw = mp .solutions .drawing_utils
18+ self .tipIds = [4 , 8 , 12 , 16 , 20 ]
19+
20+ def findHands (self , img , draw = True ):
21+ imgRGB = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
22+ self .results = self .hands .process (imgRGB )
23+ # print(results.multi_hand_landmarks)
24+
25+ if self .results .multi_hand_landmarks :
26+ for handLms in self .results .multi_hand_landmarks :
27+ if draw :
28+ self .mpDraw .draw_landmarks (img , handLms , self .mpHands .HAND_CONNECTIONS )
29+ return img
30+
31+ def findPosition (self , img , handNo = 0 , draw = True ):
32+ xList = []
33+ yList = []
34+ bbox = []
35+ self .lmList = []
36+ if self .results .multi_hand_landmarks :
37+ myHand = self .results .multi_hand_landmarks [handNo ]
38+ for id , lm in enumerate (myHand .landmark ):
39+ # print(id, lm)
40+ h , w , c = img .shape
41+ cx , cy = int (lm .x * w ), int (lm .y * h )
42+ xList .append (cx )
43+ yList .append (cy )
44+ # print(id, cx, cy)
45+ self .lmList .append ([id , cx , cy ])
46+ if draw :
47+ cv2 .circle (img , (cx , cy ), 5 , (0 , 0 , 255 ), cv2 .FILLED )
48+
49+ xmin , xmax = min (xList ), max (xList )
50+ ymin , ymax = min (yList ), max (yList )
51+ bbox = xmin , ymin , xmax , ymax
52+
53+ if draw :
54+ cv2 .rectangle (img , (xmin - 20 , ymin - 20 ), (xmax + 20 , ymax + 20 ), (0 , 255 , 0 ), 2 )
55+
56+ return self .lmList , bbox
57+
58+ def fingersUp (self ):
59+ fingers = []
60+ # Thumb
61+ if self .lmList [self .tipIds [0 ]][1 ] > self .lmList [self .tipIds [0 ] - 1 ][1 ]:
62+ fingers .append (1 )
63+ else :
64+ fingers .append (0 )
65+
66+ # Fingers
67+ for id in range (1 , 5 ):
68+
69+ if self .lmList [self .tipIds [id ]][2 ] < self .lmList [self .tipIds [id ] - 2 ][2 ]:
70+ fingers .append (1 )
71+ else :
72+ fingers .append (0 )
73+
74+ # totalFingers = fingers.count(1)
75+ return fingers
76+
77+ def findDistance (self , p1 , p2 , img , draw = True , r = 15 , t = 3 ):
78+ x1 , y1 = self .lmList [p1 ][1 :]
79+ x2 , y2 = self .lmList [p2 ][1 :]
80+ cx , cy = (x1 + x2 ) // 2 , (y1 + y2 ) // 2
81+
82+ if draw :
83+ cv2 .line (img , (x1 , y1 ), (x2 , y2 ), (255 , 0 , 255 ), t )
84+ cv2 .circle (img , (x1 , y1 ), r , (255 , 0 , 255 ), cv2 .FILLED )
85+ cv2 .circle (img , (x2 , y2 ), r , (255 , 0 , 255 ), cv2 .FILLED )
86+ cv2 .circle (img , (cx , cy ), r , (0 , 0 , 255 ), cv2 .FILLED )
87+ length = math .hypot (x2 - x1 , y2 - y1 )
88+
89+ return length , img , [x1 , y1 , x2 , y2 , cx , cy ]
90+
91+ def main ():
92+ pTime = 0
93+ cTime = 0
94+ try :
95+ cap = cv2 .VideoCapture (0 )
96+ except :
97+ cap = cv2 .VideoCapture (1 )
98+ detector = handDetector ()
99+ while True :
100+ success , img = cap .read ()
101+ img = detector .findHands (img )
102+ lmList , bbox = detector .findPosition (img )
103+ if len (lmList ) != 0 :
104+ print (lmList [4 ])
105+
106+ cTime = time .time ()
107+ fps = 1 / (cTime - pTime )
108+ pTime = cTime
109+
110+ cv2 .putText (img , str (int (fps )), (10 , 70 ), cv2 .FONT_HERSHEY_PLAIN , 3 , (255 , 0 , 255 ), 3 )
111+
112+ cv2 .imshow ("Image" , img )
113+ cv2 .waitKey (1 )
114+
115+ if __name__ == "__main__" :
116+ main ()
0 commit comments