-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_tracking_module.py
More file actions
74 lines (53 loc) · 2.07 KB
/
hand_tracking_module.py
File metadata and controls
74 lines (53 loc) · 2.07 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
import cv2
import mediapipe as mp
import time
class HandDetector:
def __init__(self, mode=False, max_hands=2, complexity=1, detection_con=0.5, tracking_con=0.5):
self.mode = mode
self.max_hands = max_hands
self.complexity = complexity
self.detection_con = detection_con
self.tracking_con = tracking_con
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(self.mode, self.max_hands, self.complexity, self.detection_con,
self.tracking_con)
self.mp_draw = mp.solutions.drawing_utils
def find_hands(self, img, draw=True):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(img_rgb)
if self.results.multi_hand_landmarks:
for hand_lms in self.results.multi_hand_landmarks:
if draw:
self.mp_draw.draw_landmarks(img, hand_lms, self.mp_hands.HAND_CONNECTIONS)
return img
def find_position(self, img, hand_no=0, draw=True):
lm_list = []
if self.results.multi_hand_landmarks:
my_hand = self.results.multi_hand_landmarks[hand_no]
for id, lm in enumerate(my_hand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lm_list.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
return lm_list
def main():
p_time = 0
c_time = 0
cap = cv2.VideoCapture(0)
detector = HandDetector()
while True:
success, img = cap.read()
img = detector.find_hands(img)
lm_list = detector.find_position(img)
if len(lm_list) != 0:
print(lm_list)
c_time = time.time()
fps = 1 / (c_time - p_time)
p_time = c_time
cv2.putText(img, str(int(fps)), (10, 70),
cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()