-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandTracker.py
147 lines (116 loc) · 5.56 KB
/
handTracker.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
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
import numpy as np
import cv2
import mediapipe as mp
import matplotlib.pyplot as plt
from skimage.transform import resize
from skimage.color import rgb2gray
class HandTracker:
def __init__(self, mode=False, max_hands=2, detection_con=0.5, model_complexity=1, track_con=0.5):
self.centers = None
self.results = None
self.mode = mode
self.maxHands = max_hands
self.detectionCon = detection_con
self.modelComplex = model_complexity
self.trackCon = track_con
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(static_image_mode=self.mode,
max_num_hands=self.maxHands,
model_complexity=self.modelComplex,
min_detection_confidence=self.detectionCon,
min_tracking_confidence=self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def find_hands(self, img, draw=True, BGR2RGB=True):
if BGR2RGB:
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
else:
imgRGB = img
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 find_positions(self, img):
lmlist = []
if self.results.multi_hand_landmarks:
for hand in range(len(self.results.multi_hand_landmarks)):
for finger_id, lm in enumerate(self.results.multi_hand_world_landmarks[hand].landmark):
# h, w = img.shape
# cx, cy = int(lm.x * w), int(lm.y * h)
lmlist.append([hand, finger_id, lm.x, lm.x, lm.z])
lmlist = np.array(lmlist)
return lmlist
# def draw_borders(self, img):
#
# rect_centers = []
# image_height, image_width, _ = img.shape
# crops = []
#
# if self.results.multi_hand_landmarks:
# for hand_landmarks in self.results.multi_hand_landmarks:
#
# x = [landmark.x for landmark in hand_landmarks.landmark]
# y = [landmark.y for landmark in hand_landmarks.landmark]
#
# center = np.array([np.mean(x) * image_width, np.mean(y) * image_height]).astype('int32')
# rect_centers.append(center)
# cv2.rectangle(img,
# (center[0] - 128, center[1] - 128),
# (center[0] + 128, center[1] + 128),
# (0, 0, 255), 2)
#
# cropped = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cropped = cropped[(center[0] - 128):(center[0] + 128), (center[1] - 128):(center[1] + 128)]
# if cropped.shape[0] >= 64 and cropped.shape[1] >= 64:
# cropped = resize(cropped, (64, 64))
# cropped = rgb2gray(cropped)
# cropped /= 255
#
# cropped = cropped.flatten()
# crops.append(cropped)
#
# rect_centers = np.array(rect_centers)
# # print(np.shape(rect_centers))
# self.centers = rect_centers
#
# crops = np.array(crops)
# return crops
def draw_borders(self, img):
rect_centers = []
image_height, image_width, _ = img.shape
if self.results.multi_hand_landmarks:
for hand_landmarks in self.results.multi_hand_landmarks:
x = [landmark.x for landmark in hand_landmarks.landmark]
y = [landmark.y for landmark in hand_landmarks.landmark]
center = np.array([np.mean(x) * image_width, np.mean(y) * image_height]).astype('int32')
rect_centers.append(center)
cv2.rectangle(img,
(center[0] - 120, center[1] - 120),
(center[0] + 120, center[1] + 120),
(0, 0, 255), 2)
center = np.array([np.mean(x) * image_width, np.mean(y) * image_height]).astype('int32')
rect_centers.append(center)
cv2.rectangle(img,
(center[0] - 120, center[1] - 120),
(center[0] + 120, center[1] + 120),
(0, 0, 255), 2)
rect_centers = np.array(rect_centers)
# print(np.shape(rect_centers))
self.centers = rect_centers
return self.centers
def slice_hand_imgs(self, img, index, radius, BGR2RGB=True):
if BGR2RGB:
cropped = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
else:
cropped = img
cropped = cropped[(self.centers[index, 1] - radius):(self.centers[index, 1] + radius),
(self.centers[index, 0] - radius):(self.centers[index, 0] + radius)]
if np.shape(cropped)[0] > 64 and np.shape(cropped)[1] > 64:
cropped = resize(cropped, (64, 64, 3))
return cropped
else:
return np.zeros((64, 64, 3))
def display_letters(self, img, index, letter, prob):
x, y = (self.centers[index, 0] - 128), (self.centers[index, 1] - 128)
cv2.putText(img, '{}: {}%'.format(letter, prob), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))