-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_classify_pose.py
246 lines (200 loc) · 9.68 KB
/
demo_classify_pose.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
# Pose Detections with Model
import cv2
import numpy as np
import pandas as pd
import mediapipe as mp
import pickle
def display_classify_pose(cap, model):
if (cap.isOpened() == False):
print("Error opening the video file.")
else:
input_fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f'Frames per second: {input_fps}')
print(f'Frame count: {frame_count}')
mp_drawing = mp.solutions.drawing_utils # Drawing helpers.
mp_holistic = mp.solutions.holistic # Mediapipe Solutions.
# Initiate holistic model
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
if ret == True:
# Recolor Feed
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make Detections
results = holistic.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Pose Detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.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
pose = results.pose_landmarks.landmark
pose_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in pose]).flatten())
# Concate rows
row = pose_row
# Make Detections
X = pd.DataFrame([row])
body_language_class = model.predict(X)[0]
body_language_prob = model.predict_proba(X)[0]
print(f'class: {body_language_class}, prob: {body_language_prob}')
# Grab ear coords
coords = tuple(np.multiply(
np.array(
(results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].x,
results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].y)),
[640,480]
).astype(int))
cv2.rectangle(image,
(coords[0], coords[1]+5),
(coords[0]+len(body_language_class)*20, coords[1]-30),
(245, 117, 16), -1)
cv2.putText(image, body_language_class, coords,
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Get status box
cv2.rectangle(image, (0,0), (250, 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
)
except:
pass
cv2.imshow('Raw Webcam Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
break
print('Done!')
cap.release()
cv2.destroyAllWindows()
def save_display_classify_pose(cap, model, out_video):
if (cap.isOpened() == False):
print("Error opening the video file.")
else:
input_fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
output_fps = input_fps - 1
print(f'Frames per second: {input_fps}')
print(f'Frame count: {frame_count}')
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f'video_w: {w}, video_h: {h}')
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 輸出附檔名為 mp4.
out = cv2.VideoWriter(out_video, fourcc, output_fps, (w, h))
mp_drawing = mp.solutions.drawing_utils # Drawing helpers.
mp_holistic = mp.solutions.holistic # Mediapipe Solutions.
# Initiate holistic model
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
if ret == True:
# Recolor Feed
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make Detections
results = holistic.process(image)
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Pose Detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.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
pose = results.pose_landmarks.landmark
pose_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in pose]).flatten())
# Concate rows
row = pose_row
# Make Detections
X = pd.DataFrame([row])
body_language_class = model.predict(X)[0]
body_language_prob = model.predict_proba(X)[0]
print(f'class: {body_language_class}, prob: {body_language_prob}')
# Grab ear coords
coords = tuple(np.multiply(
np.array(
(results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].x,
results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].y)),
[640,480]
).astype(int))
cv2.rectangle(image,
(coords[0], coords[1]+5),
(coords[0]+len(body_language_class)*20, coords[1]-30),
(245, 117, 16), -1)
cv2.putText(image, body_language_class, coords, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Get status box
cv2.rectangle(image, (0,0), (250, 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
)
body_language_prob = body_language_prob*100
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
)
except:
pass
out.write(image)
cv2.imshow('Raw Webcam Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
break
print('Done!')
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
# Test video file name: cat_camel2, bridge2, heel_raise2.
video_file_name = "cat_camel2"
model_weights = './model_weights/weights_body_language.pkl'
video_path = "./resource/video/" + video_file_name +".mp4"
output_video = video_file_name + "_out.mp4"
cap = cv2.VideoCapture(video_path)
# Load Model.
with open(model_weights, 'rb') as f:
model = pickle.load(f)
# display_classify_pose(cap=cap, model=model)
save_display_classify_pose(cap=cap, model=model, out_video=output_video)