-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandDetector.py
More file actions
65 lines (50 loc) · 2.32 KB
/
HandDetector.py
File metadata and controls
65 lines (50 loc) · 2.32 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
import cv2
import mediapipe as mp
import math
import Utils as utils
class HandDetector():
max_hands : int
hand : mp.solutions.hands.Hands
click_distance : float
def __init__(self, detection_confidence=0.5, track_confidence=0.2, click_distance=25) -> None:
self.max_hands = 1
self.click_distance = click_distance
self.mp_hand_solution = mp.solutions.hands
self.hand = mp.solutions.hands.Hands(
max_num_hands=self.max_hands,
min_detection_confidence=detection_confidence,
min_tracking_confidence=track_confidence
)
def track_fingers(self, img, draw_line=True, draw_all_landmarks=True) -> tuple:
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = self.hand.process(img_rgb)
hand_landmark_list = []
h , w , c = img.shape
if results.multi_hand_landmarks:
hand = results.multi_hand_landmarks[0]
for id, lm in enumerate(hand.landmark):
cx, cy = int(lm.x*w), int(lm.y*h)
hand_landmark_list.append([id, cx, cy])
if (len(hand_landmark_list) != 0):
# thumb coordiantes
l_x1, l_y1 = hand_landmark_list[4][1], hand_landmark_list[4][2]
# pointer finger coordinates
l_x3, l_y3 = hand_landmark_list[8][1], hand_landmark_list[8][2]
# center between pointer finger and thumb
cx , cy = ((l_x1 + l_x3) // 2) , ((l_y1 + l_y3) // 2)
finger_distance = math.hypot(l_x1-l_x3, l_y1-l_y3)
if draw_all_landmarks:
utils.draw_hand_landmarks(img, results)
if draw_line:
utils.draw_circle(img, (l_x1,l_y1), utils.BLUE, 8)
utils.draw_circle(img, (l_x3,l_y3), utils.BLUE, 8)
utils.draw_line(img, (l_x1,l_y1), (l_x3,l_y3), utils.WHITE, 3)
if finger_distance < self.click_distance:
utils.draw_circle(img, (cx,cy), utils.RED, 8)
return (cx, cy, True)
else:
utils.draw_circle(img, (cx,cy), utils.GREEN, 8)
return (cx, cy, False)
else:
#print('No Handlandmars found')
return (None, None, False)