-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv.py
More file actions
235 lines (179 loc) · 7.15 KB
/
cv.py
File metadata and controls
235 lines (179 loc) · 7.15 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import cv2
import mediapipe as mp
import math
import threading
class WebCam:
__instance = None
__initialized = False
def __new__(cls):
if not cls.__instance:
cls.__instance = super(WebCam, cls).__new__(cls)
return cls.__instance
def __init__(self):
if not self.__initialized:
self.cap = None
self.__initialized = True
def startWebcam(self):
if not self.cap:
self.cap = cv2.VideoCapture(0)
def stopWebcam(self):
if self.cap:
self.cap.release()
self.cap = None
def returnFrame(self):
if not self.cap:
return None
ret, frame = self.cap.read()
if not ret:
return None
return frame
class PoseEstimator:
__instance = None
__initialized = False
__tracking = False
__points = dict.fromkeys(range(0,33))
__points["read"] = False
def __new__(cls):
if not cls.__instance:
cls.__instance = super(PoseEstimator, cls).__new__(cls)
return cls.__instance
def __init__(self):
if not self.__initialized:
self.mp_pose = mp.solutions.pose
self.pose = self.mp_pose.Pose()
self.__initialized = True
def getPoints(self, points_to_display=list(range(0, 33))) -> dict:
if self.__points["read"]:
return {point: self.__points[point] for point in points_to_display if 0 <= point <= 32}
def pointsVisible(self, points_to_check) -> bool:
if not points_to_check:
return True
for idx in points_to_check:
landmark = self.__points[idx]
if landmark.visibility < 0.5:
return False
return True
def getAngle(self, point1, point2, point3) -> float:
if not self.__points["read"]:
return None
point1 = self.__points[point1]
point2 = self.__points[point2]
point3 = self.__points[point3]
if not point1 or not point2 or not point3:
return None
angle = math.degrees(math.atan2(point3.y - point2.y, point3.x - point2.x) - math.atan2(point1.y - point2.y, point1.x - point2.x))
if angle < 0:
angle = 360 + angle
return angle
def __track(self, webcam):
while self.__tracking:
try:
pose_results = self.pose.process(cv2.cvtColor(webcam.returnFrame(), cv2.COLOR_BGR2RGB))
if pose_results:
for landmark in range(0, 33):
self.__points[landmark] = pose_results.pose_landmarks.landmark[landmark]
self.__points[landmark].x = 1 - self.__points[landmark].x
self.__points["read"] = True
except:
self.__points["read"] = False
def start_tracking(self):
self.__tracking = True
WebCam().startWebcam()
threading.Thread(target=lambda: self.__track(WebCam()), daemon=True).start()
def stop_tracking(self):
self.__tracking = False
WebCam().stopWebcam()
class HandEstimator:
_instance = None
_initialized = False
def __new__(cls):
if not cls._instance:
cls._instance = super(HandEstimator, cls).__new__(cls)
return cls._instance
def __init__(self):
if not self._initialized:
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands()
self._initialized = True
# For all below methods:
# n = 0 -> left
# n = 1 -> right
# n = 2 -> both
def getPoints(self, frame, points_to_display=list(range(0, 21)), n=2) -> list:
hand_results = self.hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if hand_results.multi_hand_landmarks:
hands = []
for landmarks, hand_info in zip(hand_results.multi_hand_landmarks, hand_results.multi_handedness):
landmarks_to_display = dict.fromkeys(points_to_display, None)
for idx in landmarks_to_display:
landmark_point = landmarks.landmark[idx]
landmark_point.x = 1 - landmark_point.x
landmarks_to_display[idx] = landmark_point
if n == 0 and hand_info.classification[0].label == "Right":
return ["left", landmarks_to_display]
elif n == 1 and hand_info.classification[0].label == "Left":
return ["right", landmarks_to_display]
elif n == 2:
hands.append(["left" if hand_info.classification[0].label == "Right" else "right", landmarks_to_display])
return hands
def pointsVisible(self, frame, points, n=2) -> bool:
if not points:
return True
landmarks = self.getPoints(frame, points)
if landmarks:
c = 0
for i in landmarks:
if i[0] == "left" and n == 0:
return True
if i[0] == "right" and n == 1:
return True
c += 1
if n == 2 and c == 2:
return True
return False
def isHandOpen(self, frame, n=2) -> bool:
if n == 2:
return self.isHandOpen(frame, 0) and self.isHandOpen(frame, 1)
hand = self.getPoints(frame, [5, 8, 9, 12, 13, 16, 17, 20], n)
if hand:
hand = hand[1]
if hand[4].y < hand[0].y and hand[8].y < hand[5].y and hand[12].y < hand[9].y and hand[16].y < hand[13].y and hand[20].y < hand[17].y:
return True
return False
def isFist(self, frame, n=2) -> bool:
if n == 2:
return self.isFist(frame, 0) and self.isFist(frame, 1)
hand = self.getPoints(frame, [0, 4, 5, 8, 9, 12, 13, 16, 17, 20], n)
if hand:
hand = hand[1]
if hand[8].y > hand[5].y and hand[12].y > hand[9].y and hand[16].y > hand[13].y and hand[20].y > hand[17].y:
return True
return False
if __name__ == "__main__":
wc1 = WebCam()
pe = PoseEstimator()
he = HandEstimator()
wc1.startWebcam()
wc = WebCam()
while True:
frame = wc.returnFrame()
print(he.isFist(frame, 2))
# hands = he.getPoints(frame)
# pose = pe.getPoints(frame)
# if hands:
# for hand in hands:
# landmarks = hand[2]
# hand_type = hand[0]
# for idx in landmarks:
# landmark = landmarks[idx]
# cv2.circle(frame, (int((1 - landmark.x) * frame.shape[1]), int(landmark.y * frame.shape[0])), 5, (0, 0, 255), -1)
# if pose:
# for idx in pose:
# landmark = pose[idx]
# if landmark.visibility > 0.5:
# cv2.circle(frame, (int((1 - landmark.x) * frame.shape[1]), int(landmark.y * frame.shape[0])), 5, (0, 0, 255), -1)
cv2.imshow('Pose and Hand Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
wc.stopWebcam()
cv2.destroyAllWindows()