-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandTrackModule.py
More file actions
107 lines (96 loc) · 4.35 KB
/
HandTrackModule.py
File metadata and controls
107 lines (96 loc) · 4.35 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
import cv2
import mediapipe as mp
import math
class handDetector():
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp. solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20]
self.inclination_data = 0
self.h_cut = 0
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.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo=0, draw=True):
xList = []
yList = []
bbox = []
self.lmList = []
if self.results.multi_hand_landmarks:
check = 1 if len(self.results.multi_hand_landmarks) > 2 else 0
myHand = self.results.multi_hand_landmarks[check]
for id, lm in enumerate(myHand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
xList.append(cx)
yList.append(cy)
self.lmList.append([id, cx, cy, lm.z])
if draw:
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
xmin, xmax = min(xList), max(xList)
ymin, ymax = min(yList), max(yList)
bbox = xmin, ymin, xmax, ymax
if draw:
cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
return self.lmList, bbox
def distense(self, a, b):
x1, y1 = self.lmList[a][1], self.lmList[a][2]
x2, y2 = self.lmList[b][1], self.lmList[b][2]
self.length = math.hypot(x2 - x1, y2 - y1)
def inclination(self, a, b):
x1, y1 = self.lmList[a][1], self.lmList[a][2]
x2, y2 = self.lmList[b][1], self.lmList[b][2]
self.inclination_data = int((y1-y2)/(x1-x2)) if x1-x2 != 0 else 0
def fingersUp(self):
fingers = 0
for id in range(1, 5):
if (self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 3][2]):
if id == 1:
fingers = 1
if id == 2:
fingers = 2 if fingers >= 1 else 0
self.distense(self.tipIds[1], self.tipIds[2])
if id == 3:
fingers = 3 if fingers >= 2 else 0
if fingers == 3:
self.alllength = 0
for i in range(1, 3):
self.distense(self.tipIds[i], self.tipIds[i + 1])
self.alllength += self.length
if (self.lmList[self.tipIds[3]][2] > self.lmList[self.tipIds[3] - 1][2]):
fingers = 32
if id == 4:
fingers = 4 if fingers >= 3 else 0
if fingers == 4:
self.alllength = 0
for i in range(1,4):
self.distense(self.tipIds[i], self.tipIds[i+1])
self.alllength += self.length
if fingers < 2 and (self.lmList[self.tipIds[1]][2] < self.lmList[self.tipIds[1] - 3][2]):
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
fingers = 21
if self.lmList[self.tipIds[1]][2] > self.lmList[self.tipIds[1] - 1][2]:
fingers = 22
self.distense(self.tipIds[1], self.tipIds[2])
if self.length < 50:
fingers = 23
elif fingers == 2:
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
fingers = 31
elif fingers == 4:
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
fingers = 51
elif self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 2][1]:
fingers = 5
return fingers