-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandTrackingModule.py
More file actions
136 lines (100 loc) · 4.36 KB
/
handTrackingModule.py
File metadata and controls
136 lines (100 loc) · 4.36 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# IMPORTS --------------------------------------------------------------------------------------------------------------
import cv2
import mediapipe as mp
import time
class handDetector():
# CONSTRUCTOR ----------------------------------------------------------------------------------------------------------
def __init__(self, mode = False, maxHands = 2, detectionCon = 0.5, trackCon = 0.5):
self.mode = mode
self.maxHands = maxHands
#self.modelComplex = modelComplex
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def findHands(self, img, draw = True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo = 0, draw = True):
lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for iD, lm in enumerate(myHand.landmark):
h, w, c = img.shape
cx, cy, cz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
lmList.append([iD, cx, cy, cz])
if draw:
cv2.circle(img, (cx, cy), 55, (255, 0, 255), cv2.FILLED)
return lmList
def findHandType(self):
handsType = []
if self.results.multi_hand_landmarks != None:
for hand in self.results.multi_handedness:
handType = hand.classification[0].label
handsType.append(handType)
return handsType
def fingerCombination(self, lmList):
#the fingers are represented in binary manner. If a finger is shown it is
#represented as 1 and if it is not it is represented as 0 starting from pinky.
#e.g. if only pinky is raised it will give the value 1(00001)
#if middle and index is raised, the value is 00110
#if thumb is raised, the value is 10000
fingerNum = 0
if len(lmList) != 0:
index = lmList[8][2] < lmList[6][2]
middle = lmList[12][2] < lmList[10][2]
ring = lmList[16][2] < lmList[14][2]
pinky = lmList[20][2] < lmList[18][2]
thumb = lmList[4][1] < lmList[3][1]
if pinky:
fingerNum += 10**0
if ring:
fingerNum += 10**1
if middle:
fingerNum += 10**2
if index:
fingerNum += 10**3
if thumb:
fingerNum += 10**4
return fingerNum
def main():
# Initialize webcam
cam = cv2.VideoCapture(0)
# Initialize time variables: Previous Time = pTime, Current Time = cTime
pTime = 0
cTime = 0
# Creating a handDetector object
detector = handDetector()
while True:
# Read frames from webcam
success, frame = cam.read()
# Send frames to detector object
frame = detector.findHands(frame)
# Creating list of hand landmark data
lmList = detector.findPosition(frame)
# Checking if the list is empty or not
if len(lmList) != 0:
print(lmList[4])
# Track Frames per Seconds (FPS)
cTime = time.time()
FPS = 1 / (cTime - pTime)
pTime = cTime
# Displays the FPS value onto the frame
cv2.putText(frame, str(int(FPS)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
# Display frames in a new window called Camera
cv2.imshow('Camera', frame)
# Exits or breaks when user presses the Q key, q key, or the Esc key
key = cv2.waitKey(1)
if key in [27, ord('Q'), ord('q')]:
break
# Close window
cam.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()