-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_tracking_mod.py
More file actions
158 lines (132 loc) · 6.39 KB
/
Copy pathhand_tracking_mod.py
File metadata and controls
158 lines (132 loc) · 6.39 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
import cv2
import numpy as np
import mediapipe as mp
import time as time
import math
class HandDetector():
def __init__(self, mode=False, max_num_hands=2, detectioncon=0.7, trackcon=0.5):
self.mode = mode
self.max_num_hands = max_num_hands
self.detectioncon = detectioncon
self.trackcon = trackcon
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(min_detection_confidence=self.detectioncon,
min_tracking_confidence=self.trackcon)
self.mpdraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20] # List of tip finger IDs
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.mp_hands.HAND_CONNECTIONS)
return img
def findposition(self, img, hand=0, draw=True):
self.lmlist = []
if self.results.multi_hand_landmarks:
myhand = self.results.multi_hand_landmarks[hand]
for id, lm in enumerate(myhand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
self.lmlist.append([id, cx, cy]) # Store [ID, x, y] for each landmark
if draw:
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
return self.lmlist
def fingersup(self):
fingers = []
if len(self.lmlist) > 0:
# Thumb: Compare x-coordinates to check if the thumb is up
if self.lmlist[self.tipIds[0]][1] < self.lmlist[self.tipIds[0] - 1][1]:
fingers.append(1) # Thumb is open
else:
fingers.append(0) # Thumb is closed
# Other fingers: Compare y-coordinates to check if the fingers are up
for id in range(1, 5):
if self.lmlist[self.tipIds[id]][2] < self.lmlist[self.tipIds[id] - 2][2]:
fingers.append(1) # Finger is open
else:
fingers.append(0) # Finger is closed
else:
fingers = [0, 0, 0, 0, 0] # Return all 0 if no hand is detected
return fingers
def findDistance(self, p1, p2, img=None):
if isinstance(p1, tuple) and len(p1) == 2 and isinstance(p2, tuple) and len(p2) == 2:
x1, y1 = p1
x2, y2 = p2
# Euclidean distance formula
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
if img is not None:
# Optionally, draw a line between the points on the image
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.circle(img, (x1, y1), 5, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (x2, y2), 5, (0, 255, 0), cv2.FILLED)
return distance, img # Returning the distance along with the image
else:
raise ValueError("p1 and p2 must be tuples of (x, y) coordinates")
import cv2
import numpy as np
import mediapipe as mp
import time as time
import math
class HandDetector():
def __init__(self, mode=False, max_num_hands=2, detectioncon=0.7, trackcon=0.5):
self.mode = mode
self.max_num_hands = max_num_hands
self.detectioncon = detectioncon
self.trackcon = trackcon
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(min_detection_confidence=self.detectioncon,
min_tracking_confidence=self.trackcon)
self.mpdraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20] # List of tip finger IDs
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.mp_hands.HAND_CONNECTIONS)
return img
def findposition(self, img, hand=0, draw=True):
self.lmlist = []
if self.results.multi_hand_landmarks:
myhand = self.results.multi_hand_landmarks[hand]
for id, lm in enumerate(myhand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
self.lmlist.append([id, cx, cy]) # Store [ID, x, y] for each landmark
if draw:
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
return self.lmlist
def fingersup(self):
fingers = []
if len(self.lmlist) > 0:
# Thumb: Compare x-coordinates to check if the thumb is up
if self.lmlist[self.tipIds[0]][1] < self.lmlist[self.tipIds[0] - 1][1]:
fingers.append(1) # Thumb is open
else:
fingers.append(0) # Thumb is closed
# Other fingers: Compare y-coordinates to check if the fingers are up
for id in range(1, 5):
if self.lmlist[self.tipIds[id]][2] < self.lmlist[self.tipIds[id] - 2][2]:
fingers.append(1) # Finger is open
else:
fingers.append(0) # Finger is closed
else:
fingers = [0, 0, 0, 0, 0] # Return all 0 if no hand is detected
return fingers
def findDistance(self, p1, p2, img=None):
if isinstance(p1, tuple) and len(p1) == 2 and isinstance(p2, tuple) and len(p2) == 2:
x1, y1 = p1
x2, y2 = p2
# Euclidean distance formula
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
if img is not None:
# Optionally, draw a line between the points on the image
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.circle(img, (x1, y1), 5, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (x2, y2), 5, (0, 255, 0), cv2.FILLED)
return distance, img # Returning the distance along with the image
else:
raise ValueError("p1 and p2 must be tuples of (x, y) coordinates")