-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholistic.py
368 lines (279 loc) · 11.9 KB
/
holistic.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import cv2
import mediapipe as mp
import time
class mediapipe:
# ============================================ init =================================================
def __init__(self):
# mediapipe solutions variable
mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_detection = mp.solutions.face_detection
mp_face_mesh = mp.solutions.face_mesh
mp_hands = mp.solutions.hands
mp_pose = mp.solutions.pose
pTime = 0
self.mp_drawing = mp_drawing
self.mp_holistic = mp_holistic
self.mp_drawing_styles = mp_drawing_styles
self.mp_face_detection = mp_face_detection
self.mp_hands = mp_hands
self.mp_pose = mp_pose
self.mp_face_mesh = mp_face_mesh
self.pTime = pTime
# ===================================================================================================
def simple_holistic(self, show_fps=False):
# capturing webcam 0
cap = cv2.VideoCapture(0)
# Initiate holistic model
with self.mp_holistic.Holistic( min_detection_confidence=0.5, min_tracking_confidence=0.5 ) as holistic:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
# Recolor Feed
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Make Detections
results = holistic.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# face landmarks
self.mp_drawing.draw_landmarks(
image,
results.face_landmarks,
self.mp_holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(0, 167, 196), thickness=2, circle_radius=1)
)
# pose landmarks
self.mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
self.mp_holistic.POSE_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(0,0,0), thickness=2, circle_radius=2),
connection_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(255, 255, 255), thickness=3, circle_radius=2)
)
# fliping image
flip_image = cv2.flip(image, 1)
# fps
if show_fps==True:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
cv2.putText(flip_image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# Showimg image
cv2.imshow('MediaPipe Holistic', flip_image)
# quiting
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ===================================================================================================
def complex_holistic(self, show_fps=True):
# capturing webcam 0
cap = cv2.VideoCapture(0)
# Initiate holistic model
with self.mp_holistic.Holistic( min_detection_confidence=0.5, min_tracking_confidence=0.5 ) as holistic:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
# Recolor Feed
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Make Detections
results = holistic.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# face landmarks
self.mp_drawing.draw_landmarks(
image,
results.face_landmarks,
self.mp_holistic.FACEMESH_TESSELATION,
landmark_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=1, circle_radius=1),
connection_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=1, circle_radius=1)
)
# Right hand
self.mp_drawing.draw_landmarks(
image,
results.right_hand_landmarks,
self.mp_holistic.HAND_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing.DrawingSpec(color=(0, 0, 0), thickness=3, circle_radius=3),
connection_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
)
# Left Hand
self.mp_drawing.draw_landmarks(
image,
results.left_hand_landmarks,
self.mp_holistic.HAND_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing.DrawingSpec(color=(0, 0, 0), thickness=3, circle_radius=3),
connection_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
)
# Pose landmarks
self.mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
self.mp_holistic.POSE_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(0,0,0), thickness=2, circle_radius=2),
connection_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(255, 255, 255), thickness=3, circle_radius=2)
)
# fliping image
flip_image = cv2.flip(image, 1)
# fps
if show_fps==True:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
cv2.putText(flip_image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# showing fliped image
cv2.imshow('MediaPipe Holistic', flip_image)
# quiting
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# =========================================================================================================================
def face_mesh(self, show_fps=True, contours=True ):
# capturing webcam 0
cap = cv2.VideoCapture(0)
# Initiate face mesh
with self.mp_face_mesh.FaceMesh( min_detection_confidence=0.5, min_tracking_confidence=0.5 ) as face_mesh:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# Recolor Feed
image.flags.writeable = False
# Make Detections
results = face_mesh.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
# face mesh results
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
self.mp_drawing.draw_landmarks(
image=image,
landmark_list=face_landmarks,
connections=self.mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=1, circle_radius=1 )
)
if contours==True:
self.mp_drawing.draw_landmarks(
image=image,
landmark_list=face_landmarks,
connections=self.mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=self.mp_drawing_styles.get_default_face_mesh_contours_style()
)
# fliping image
flip_image = cv2.flip(image, 1)
# fps
if show_fps==True:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
cv2.putText(flip_image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# showing image
cv2.imshow('MediaPipe Holistic', flip_image)
# quiting
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ==================================================================================================
def hand_detector(self, show_fps=True):
# Capuring webcam 0
cap = cv2.VideoCapture(0)
with self.mp_hands.Hands( min_detection_confidence=0.5, min_tracking_confidence=0.5 ) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# Recolor Feed
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make Detections
results = hands.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# hand detector results
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
self.mp_drawing.draw_landmarks(
image,
hand_landmarks,
self.mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing.DrawingSpec(color=(0, 0, 0), thickness=3, circle_radius=3),
connection_drawing_spec=self.mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2)
)
# flipping image
flip_image = cv2.flip(image, 1)
# fps
if show_fps==True:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
cv2.putText(flip_image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# showing flipped image
cv2.imshow('MediaPipe Holistic', flip_image)
# quitting
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ==================================================================================================
def pose(self, show_fps=True):
# capuring webcam 0
cap = cv2.VideoCapture(0)
with self.mp_pose.Pose( min_detection_confidence=0.5, min_tracking_confidence=0.5 ) as pose:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# Recolor Feed
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make Detections
results = pose.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
self.mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
self.mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(0,0,0), thickness=2, circle_radius=2),
connection_drawing_spec=self.mp_drawing_styles.DrawingSpec(color=(255, 255, 255), thickness=3, circle_radius=2)
)
# flipping image
flip_image = cv2.flip(image, 1)
# fps
if show_fps==True:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
cv2.putText(flip_image, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
# showing flipped image
cv2.imshow('MediaPipe Holistic', flip_image)
# quitting
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# ================================================ class end ===============================================================
if __name__ == '__main__':
pipe = mediapipe()
pipe.simple_holistic()