Repository navigation
Expand file tree
/
Copy pathHandTrackingLibrary.py
More file actions
247 lines (179 loc) · 9.38 KB
/
Copy pathHandTrackingLibrary.py
File metadata and controls
247 lines (179 loc) · 9.38 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# This is Basic hand tracking library made using the mediapipe framework.
# Packages
import cv2
import mediapipe as mp
import time
import math
class HandTracker():
def __init__(self, mode=False, MaxHands=2, modelComplexity=1, DetectionConfidence=0.5, TrackingConfidence=0.5):
self.mode = mode
self.MaxHands = MaxHands
self.modelComplex = modelComplexity
self.DetectionConfidence = DetectionConfidence
self.TrackingConfidence = TrackingConfidence
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(
self.mode, self.MaxHands, self.modelComplex, self.DetectionConfidence, self.TrackingConfidence)
self.mpDraw = mp.solutions.drawing_utils
# Creating a list of all the finger tip indices
self.FingerTipIndices = [4, 8, 12, 16, 20]
def Detect_hands(self, frame, draw=True):
# Passing the rgb frame to the hand object since it only uses rgb format
RgbFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Processing our rgb image using the hands object
self.results = self.hands.process(RgbFrame)
# executes if there is a detection
if self.results.multi_hand_landmarks:
# Checking for multiple hands in the frame
for HandLandMarks in self.results.multi_hand_landmarks:
if draw: # draws the hand connections if the draw flag is set to true
self.mpDraw.draw_landmarks(
frame, HandLandMarks, self.mpHands.HAND_CONNECTIONS)
return frame
def Find_Landmark_Position(self, frame, HandNumber=0, draw=True):
self.LandmarkList = [] # this list will store all the landmarks position
# These lists are going to store all the X and Y Positions
XCoordListAll = []
YCoordListAll = []
# Variable to store the coordinate values of the bounding box
BoundingBox = []
# executes if there is a detection
if self.results.multi_hand_landmarks:
# picks the specified hand's landmark
ThisHand = self.results.multi_hand_landmarks[HandNumber]
# id/index corresponding to the landmark
for id, landmark in enumerate(ThisHand.landmark):
# the landmark variable stores the ratio of the image. So, we will multiply the image with the width and height to get the pixel locations
height, width, _ = frame.shape
# gets the pixel locations of the all the landmarks
center_x, center_y = int(
landmark.x*width), int(landmark.y*height)
# Appending the X and Y values to the above 2 lists
XCoordListAll.append(center_x)
YCoordListAll.append(center_y)
# Appending the landmarks to the landmark's list
self.LandmarkList.append([id, center_x, center_y])
# if draw:
# # Drawing a circle in the pixel location of the first landmark
# if id == 4 or id == 8:
# cv2.circle(frame, (center_x, center_y),
# 20, (255, 0, 255), cv2.FILLED)
# Getting the Mininum & Maximum X and Y Coordinates to Draw the Bounding Box Around a Detected Hand
MinXPos, MaxXPos, MinYPos, MaxYPos = min(XCoordListAll), max(
XCoordListAll), min(YCoordListAll), max(YCoordListAll)
BoundingBox = MinXPos, MinYPos, MaxXPos, MaxYPos
# Draws the Bounding Box if the draw flag is set to true. Since the Points are exactly on the fingers, we add & sub a constant value of 20 so that there's a little more gap
if draw:
# Variables to store the BGR Color Code for the drawings
BGRColor0 = (167, 99, 246)
BGRColor = (0, 0, 0)
# Rectangle Around The Detected Hand
cv2.rectangle(frame, (BoundingBox[0] - 20, BoundingBox[1] - 20), (
BoundingBox[2] + 20, BoundingBox[3] + 20), BGRColor0, 2)
# Top Left Corner Lines
cv2.line(frame, (BoundingBox[0] - 20, BoundingBox[1] - 20),
(BoundingBox[0] - 20, BoundingBox[1] + 20), BGRColor, 5)
cv2.line(frame, (BoundingBox[0] - 20, BoundingBox[1] - 20),
(BoundingBox[0] + 20, BoundingBox[1] - 20), BGRColor, 5)
# Top Right Corner Lines
cv2.line(frame, (BoundingBox[2] + 20, BoundingBox[1] - 20),
(BoundingBox[2] + 20, BoundingBox[1] + 20), BGRColor, 5)
cv2.line(frame, (BoundingBox[2] - 20, BoundingBox[1] - 20),
(BoundingBox[2] + 20, BoundingBox[1] - 20), BGRColor, 5)
# Bottom Left Corner Lines
cv2.line(frame, (BoundingBox[0] - 20, BoundingBox[3] + 20),
(BoundingBox[0] - 20, BoundingBox[3] - 20), BGRColor, 5)
cv2.line(frame, (BoundingBox[0] - 20, BoundingBox[3] + 20),
(BoundingBox[0] + 20, BoundingBox[3] + 20), BGRColor, 5)
# Bottom Right Corner Lines
cv2.line(frame, (BoundingBox[2] + 20, BoundingBox[3] + 20),
(BoundingBox[2] + 20, BoundingBox[3] - 20), BGRColor, 5)
cv2.line(frame, (BoundingBox[2] + 20, BoundingBox[3] + 20),
(BoundingBox[2] - 20, BoundingBox[3] + 20), BGRColor, 5)
return self.LandmarkList, BoundingBox
# This Method returns the list of the fingers that are UP
def Get_Fingers_Up(self):
# Creating a list to store all the fingers that are up
FingersUp = []
# This statement acts as a fail check when the Landmark List is empty
if len(self.LandmarkList) != 0:
if self.LandmarkList[self.FingerTipIndices[0]][1] < self.LandmarkList[self.FingerTipIndices[0] - 1][1]:
FingersUp.append(1)
else:
FingersUp.append(0)
# We use FingerTipIndices[index] - 2 to get the value 2 indices below the tip.
for index in range(1, 5):
if self.LandmarkList[self.FingerTipIndices[index]][2] < self.LandmarkList[self.FingerTipIndices[index] - 2][2]:
FingersUp.append(1)
else:
FingersUp.append(0)
return FingersUp
else:
print("No Landmarks Detected! The landmark list is empty.")
# This returns an empty list
return []
# This Method will be used to get the distance between any two finger points
def Get_Finger_Distance(self, frame, Point1, Point2, draw=True):
# The center coords of both the thumb and the index finger
center_x1 = self.LandmarkList[Point1][1]
center_y1 = self.LandmarkList[Point1][2]
center_x2 = self.LandmarkList[Point2][1]
center_y2 = self.LandmarkList[Point2][2]
# Calculating the center of the line between the fingers
LineCenter_x = (center_x1 + center_x2) // 2
LineCenter_y = (center_y1 + center_y2) // 2
if draw:
# Drawing the filled circles around the center of the tips of the fingers
cv2.circle(frame, (center_x1, center_y1),
8, (255, 0, 0), cv2.FILLED)
cv2.circle(frame, (center_x2, center_y2),
8, (255, 0, 0), cv2.FILLED)
# Drawing a line between the fingers
cv2.line(frame, (center_x1, center_y1),
(center_x2, center_y2), (255, 0, 0), 3)
# Drawing a circle in the center of the line
cv2.circle(frame, (LineCenter_x, LineCenter_y),
8, (255, 0, 0), cv2.FILLED)
# Calculating the length of the line
LineLength = math.hypot(
center_x2 - center_x1, center_y2 - center_y1)
# Stores all the center values calculated above
CenterList = [center_x1, center_y1, center_x2,
center_y2, LineCenter_x, LineCenter_y]
return frame, LineLength, CenterList
def HandTracking():
# Creating the video object and setting the window size
video = cv2.VideoCapture(0)
video.set(3, 640)
video.set(4, 480)
# Using the Ip Webcam app on the phone for better camera feed
# address = 'http://192.168.2.101:8080/video'
# video.open(address)
# Variable to store the current time and the previous time
PrevTime = 0
CurrTime = 0
Tracker = HandTracker() # creating an object of the class
run = True
while run:
success, frame = video.read()
# calling the function of the class
frame = Tracker.Detect_hands(frame)
LndmrkList, BndBox = Tracker.Find_Landmark_Position(frame)
# Only executes is the list isn't empty
# if len(LndmrkList) != 0:
# print(LndmrkList[8])
# Calculationg the frame rate
CurrTime = time.time()
Fps = 1/(CurrTime - PrevTime)
PrevTime = CurrTime
# Displaying the frame rate
cv2.putText(frame, "FPS: " + str(int(Fps)), (10, 70),
cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3)
frame = cv2.resize(frame, (640, 480))
cv2.imshow("Image", frame)
key = cv2.waitKey(1)
if key == 81 or key == 113:
run = False
break
if __name__ == '__main__':
HandTracking()