-
Notifications
You must be signed in to change notification settings - Fork 2
/
03_pose_predictions.py
216 lines (171 loc) · 9.2 KB
/
03_pose_predictions.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
import cv2
import mediapipe as mp
import numpy as np
import pandas as pd
import joblib
import imutils
import argparse
import copy
DEFAULT_IMAGE_WIDTH = 1200
X_TRANSLATION_PIXELS = 200
Z_TRANSLATION_PIXELS = 100
mp_drawing = mp.solutions.drawing_utils # Drawing helpers
mp_pose = mp.solutions.pose
"""
Usage:
python 03_pose_predictions.py --model-name best_ymca_pose_model
python 03_pose_predictions.py
"""
def add_dancer(landmark_values, x_translation_pixels, z_translation_pixels=None):
landmarks_copy = copy.deepcopy(landmark_values)
if landmarks_copy:
for i, lm in enumerate(landmarks_copy.landmark):
lm.x = lm.x + x_translation_pixels / DEFAULT_IMAGE_WIDTH
if z_translation_pixels is not None:
lm.z = lm.z + z_translation_pixels / DEFAULT_IMAGE_WIDTH
mp_drawing.draw_landmarks(image, landmarks_copy, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("--model-name", type=str, required=False, default='best_ymca_pose_model',
help="name of the saved pickled model [no suffix]")
ap.add_argument("--suppress-landmarks", action='store_true',
help="[Optional: False] if present do not show landmarks on yourself ")
ap.add_argument("--image-width", type=int, required=False, default=1200,
help="Image width")
ap.add_argument("--add-dancers", action='store_true',
help="[Optional: False] add the rest of the virtual village people")
ap.add_argument("--add-counters", action='store_true',
help="[Optional: False] if present should the pose counts ")
args = vars(ap.parse_args())
DEFAULT_IMAGE_WIDTH = args['image_width']
add_dancers = args['add_dancers']
model_name = args['model_name']
suppress_landmarks = args['suppress_landmarks']
add_counters = args['add_counters']
with open(f'{model_name}.pkl', 'rb') as f:
model = joblib.load(f)
cap = cv2.VideoCapture(0)
# Initiate holistic model
y_counter = 0
m_counter = 0
c_counter = 0
a_counter = 0
last_detected_pose = None
number_of_new_pose_detections = 0
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
frame = imutils.resize(frame, width=DEFAULT_IMAGE_WIDTH)
# Recolor Feed
image = cv2.cvtColor(frame, 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)
if add_dancers:
if results:
if results.pose_landmarks:
add_dancer(results.pose_landmarks, X_TRANSLATION_PIXELS)
add_dancer(results.pose_landmarks, -X_TRANSLATION_PIXELS)
add_dancer(results.pose_landmarks, 2 * -X_TRANSLATION_PIXELS)
# 4. Pose Detections
if not suppress_landmarks:
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
)
# Export coordinates
try:
# Extract Pose landmarks
landmarks = results.pose_landmarks.landmark
arm_landmarks = []
pose_index = mp_pose.PoseLandmark.LEFT_SHOULDER.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
pose_index = mp_pose.PoseLandmark.RIGHT_SHOULDER.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
pose_index = mp_pose.PoseLandmark.LEFT_ELBOW.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
pose_index = mp_pose.PoseLandmark.RIGHT_ELBOW.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
pose_index = mp_pose.PoseLandmark.LEFT_WRIST.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
pose_index = mp_pose.PoseLandmark.RIGHT_WRIST.value
arm_landmarks += [landmarks[pose_index].x, landmarks[pose_index].y, landmarks[pose_index].z]
row = np.around(arm_landmarks, decimals=9).tolist()
# Make Detections
X = pd.DataFrame([row])
body_language_class = model.predict(X)[0]
body_language_prob = model.predict_proba(X)[0]
print(body_language_class, np.around(body_language_prob, decimals=3))
if add_counters:
if last_detected_pose != body_language_class and np.around(np.max(body_language_prob),
decimals=3) > 0.700:
print(last_detected_pose, body_language_class)
last_detected_pose = body_language_class
if body_language_class == 'Y':
y_counter += 1
elif body_language_class == 'M':
m_counter += 1
elif body_language_class == 'C':
c_counter += 1
elif body_language_class == 'A':
a_counter += 1
# Get status box
status_width = 250
if add_counters:
status_width = 500
cv2.rectangle(image, (0, 0), (status_width, 60), (245, 117, 16), -1)
# Display Class
cv2.putText(image, 'CLASS'
, (95, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, body_language_class.split(' ')[0]
, (90, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Display Probability
cv2.putText(image, 'PROB'
, (15, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, str(round(body_language_prob[np.argmax(body_language_prob)], 2))
, (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
if add_counters:
# Display Y count
count_x_start = 210
count_x_offset = 50
value_x_offset = 5
count_x = count_x_start
cv2.putText(image, 'Y'
, (count_x, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, str(y_counter)
, (count_x - value_x_offset, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
cv2.LINE_AA)
count_x = count_x + count_x_offset
# Display M count
cv2.putText(image, 'M'
, (count_x, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, str(m_counter)
, (count_x - value_x_offset, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
cv2.LINE_AA)
count_x = count_x + count_x_offset
# Display C count
cv2.putText(image, 'C'
, (count_x, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, str(c_counter)
, (count_x - value_x_offset, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
cv2.LINE_AA)
count_x = count_x + count_x_offset
# Display A count
cv2.putText(image, 'A'
, (count_x, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
cv2.putText(image, str(a_counter)
, (count_x - value_x_offset, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
cv2.LINE_AA)
except Exception as exc:
print(f"{exc}")
cv2.imshow('Pose Prediction', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()