-
Notifications
You must be signed in to change notification settings - Fork 0
/
hands.py
81 lines (63 loc) · 1.87 KB
/
hands.py
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
#!/usr/bin/env python3
"""
@Filename: HandCoords.py
@Author: dulanj
@Time: 2021-09-23 15.32
"""
import numpy as np
class Point:
def __init__(self, point):
self.x = point.x
self.y = point.y
self.z = point.z
def get(self):
return np.array([self.x, self.y, self.z])
def __sub__(self, other):
return np.linalg.norm(self.get() - other.get())
class Finger:
def __init__(self, landmarks):
self.tip = Point(landmarks[3])
self.dip = Point(landmarks[2])
self.pip = Point(landmarks[1])
self.mcp = Point(landmarks[0])
class Hand:
def __init__(self, hand_lmk):
landmarks = hand_lmk.landmark
self.thumb = Finger(landmarks[1:5])
self.index = Finger(landmarks[5:9])
self.middle = Finger(landmarks[9:13])
self.ring = Finger(landmarks[13:17])
self.pinky = Finger(landmarks[17:21])
self.palm = Point(landmarks[0])
x_coords = [lmk.x for lmk in landmarks]
y_coords = [lmk.y for lmk in landmarks]
self.width = np.max(x_coords) - np.min(x_coords)
self.height = np.max(y_coords) - np.min(y_coords)
class Hands:
def __init__(self, results):
self.left = None
self.right = None
self.areAvailable = False
if results.multi_hand_landmarks:
for handType, handLms in zip(results.multi_handedness, results.multi_hand_landmarks):
if handType.classification[0].label == "Right":
self.left = Hand(handLms)
else:
self.right = Hand(handLms)
self.areAvailable = True
if __name__ == '__main__':
class P:
x = 0
y = 0
z = 0
p1 = P
p1.x = 1
p1.y = 1
p1.z = 0
pt1 = Point(p1)
p2 = P
p2.x = 5
p2.y = 4
p2.z = 0
pt2 = Point(p2)
print(pt2-pt1)