forked from kapoorparul/Towards-Automatic-Speech-to-SL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_videos.py
387 lines (297 loc) · 10.8 KB
/
plot_videos.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import sys
import math
import numpy as np
import cv2
import os
import _pickle as cPickle
import gzip
import subprocess
import torch
from dtw import dtw
from constants import PAD_TOKEN
import librosa
def writeAudio(vid_loc, audio_loc):
waveform, sr = librosa.load(audio_loc, sr = 16000)
waveform, index = librosa.effects.trim(waveform)
librosa.output.write_wav("tmp.wav", waveform, sr)
new_vid_loc = vid_loc.split(".mp4")[0] + "_audio.mp4"
cmd = "ffmpeg" + " -loglevel panic -i " + vid_loc + " -i " + "tmp.wav" #temporary .wav audio location
cmd += " -c:v copy -c:a aac -strict experimental " + new_vid_loc
os.system(cmd)
os.remove("tmp.wav") # delete temporary .wav file
os.remove(vid_loc)
return new_vid_loc
# Plot a video given a tensor of joints, a file path, video name and references
def plot_video(joints,
file_path,
video_name,
references=None,
skip_frames=1,
sequence_ID=None,
audio_path=None):
npy_folder = file_path.split('/')[1]+'/test_poses' # to store numpy files for output poses (optional)
if not os.path.exists(npy_folder):
os.mkdir(npy_folder)
pred_kpts, gt_kpts = [], []
# Create video template
FPS = (30 // skip_frames)
video_file = file_path + "{}.avi".format(video_name.split(".")[0])
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
if references is None:
video = cv2.VideoWriter(video_file, fourcc, float(FPS), (650, 650), True)
elif references is not None:
video = cv2.VideoWriter(video_file, fourcc, float(FPS), (1300, 650), True) # Long
num_frames = 0
for (j, frame_joints) in enumerate(joints):
# Reached padding
if PAD_TOKEN in frame_joints:
continue
# Initialise frame of white
frame = np.ones((650, 650, 3), np.uint8) * 255
# Cut off the percent_tok and restore joint size, change according to preprocessing applied
frame_joints = frame_joints[:-1] * 4
# Reduce the frame joints down to 2D for visualisation
frame_joints_2d = np.reshape(frame_joints, (50, 3))[:, :2]
# Draw the frame given 2D joints
draw_frame_2D(frame, frame_joints_2d)
pred_kpts.append(frame_joints_2d)
cv2.putText(frame, "Predicted Sign Pose", (180, 600), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 255), 2)
# If reference is provided, create and concatenate on the end
if references is not None:
# Extract the reference joints
ref_joints = references[j]
# Initialise frame of white
ref_frame = np.ones((650, 650, 3), np.uint8) * 255
# Cut off the percent_tok and restore joint size, change according to preprocessing applied
ref_joints = ref_joints[:-1] * 4
# Reduce the frame joints down to 2D
ref_joints_2d = np.reshape(ref_joints, (50, 3))[:, :2]
# Draw these joints on the frame
draw_frame_2D(ref_frame, ref_joints_2d)
gt_kpts.append(ref_joints_2d)
cv2.putText(ref_frame, "Ground Truth Pose", (190, 600), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 0), 2)
frame = np.concatenate((frame, ref_frame), axis=1)
sequence_ID_write = "Sequence ID: " + sequence_ID.split("/")[-1]
cv2.putText(frame, sequence_ID_write, (700, 635), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 0, 0), 2)
# Write the video frame
video.write(frame)
num_frames += 1
# Release the video
if audio_path is not None:
writeAudio(video_file, audio_path)
## to save numpy file for output poses (optional)
# np.save(os.path.join(npy_folder, video_name.split('.')[0]), np.stack((np.array(pred_kpts), np.array(gt_kpts)),0))
video.release()
# This is the format of the 3D data, outputted from the Inverse Kinematics model
def getSkeletalModelStructure():
# Definition of skeleton model structure:
# The structure is an n-tuple of:
#
# (index of a start point, index of an end point, index of a bone)
#
# E.g., this simple skeletal model
#
# (0)
# |
# |
# 0
# |
# |
# (2)--1--(1)--1--(3)
# | |
# | |
# 2 2
# | |
# | |
# (4) (5)
#
# has this structure:
#
# (
# (0, 1, 0),
# (1, 2, 1),
# (1, 3, 1),
# (2, 4, 2),
# (3, 5, 2),
# )
#
# Warning 1: The structure has to be a tree.
# Warning 2: The order isn't random. The order is from a root to lists.
#
return (
# head
(0, 1, 0),
# left shoulder
(1, 2, 1),
# left arm
(2, 3, 2),
# (3, 4, 3),
# Changed to avoid wrist, go straight to hands
(3, 29, 3),
# right shoulder
(1, 5, 1),
# right arm
(5, 6, 2),
# (6, 7, 3),
# Changed to avoid wrist, go straight to hands
(6, 8, 3),
# left hand - wrist
# (7, 8, 4),
# left hand - palm
(8, 9, 5),
(8, 13, 9),
(8, 17, 13),
(8, 21, 17),
(8, 25, 21),
# left hand - 1st finger
(9, 10, 6),
(10, 11, 7),
(11, 12, 8),
# left hand - 2nd finger
(13, 14, 10),
(14, 15, 11),
(15, 16, 12),
# left hand - 3rd finger
(17, 18, 14),
(18, 19, 15),
(19, 20, 16),
# left hand - 4th finger
(21, 22, 18),
(22, 23, 19),
(23, 24, 20),
# left hand - 5th finger
(25, 26, 22),
(26, 27, 23),
(27, 28, 24),
# right hand - wrist
# (4, 29, 4),
# right hand - palm
(29, 30, 5),
(29, 34, 9),
(29, 38, 13),
(29, 42, 17),
(29, 46, 21),
# right hand - 1st finger
(30, 31, 6),
(31, 32, 7),
(32, 33, 8),
# right hand - 2nd finger
(34, 35, 10),
(35, 36, 11),
(36, 37, 12),
# right hand - 3rd finger
(38, 39, 14),
(39, 40, 15),
(40, 41, 16),
# right hand - 4th finger
(42, 43, 18),
(43, 44, 19),
(44, 45, 20),
# right hand - 5th finger
(46, 47, 22),
(47, 48, 23),
(48, 49, 24),
)
# Draw a line between two points, if they are positive points
def draw_line(im, joint1, joint2, c=(0, 0, 255),t=1, width=3):
thresh = -100
if joint1[0] > thresh and joint1[1] > thresh and joint2[0] > thresh and joint2[1] > thresh:
center = (int((joint1[0] + joint2[0]) / 2), int((joint1[1] + joint2[1]) / 2))
length = int(math.sqrt(((joint1[0] - joint2[0]) ** 2) + ((joint1[1] - joint2[1]) ** 2))/2)
angle = math.degrees(math.atan2((joint1[0] - joint2[0]),(joint1[1] - joint2[1])))
cv2.ellipse(im, center, (width,length), -angle,0.0,360.0, c, -1)
# Draw the frame given 2D joints that are in the Inverse Kinematics format
def draw_frame_2D(frame, joints):
# Line to be between the stacked
draw_line(frame, [1, 650], [1, 1], c=(0,0,0), t=1, width=1)
# Give an offset to center the skeleton around
offset = [300,250]
# Get the skeleton structure details of each bone, and size
skeleton = getSkeletalModelStructure()
skeleton = np.array(skeleton)
number = skeleton.shape[0]
# Increase the size and position of the joints
joints = joints * 10 * 4 * 2
joints = joints + np.ones((50, 2)) * offset
# Loop through each of the bone structures, and plot the bone
for j in range(number):
c = get_bone_colour(skeleton,j)
draw_line(frame, [joints[skeleton[j, 0]][0], joints[skeleton[j, 0]][1]],
[joints[skeleton[j, 1]][0], joints[skeleton[j, 1]][1]], c=c, t=1, width=3)
# get bone colour given index
def get_bone_colour(skeleton,j):
bone = skeleton[j, 2]
if bone == 0: # head
c = (0, 153, 0)
elif bone == 1: # Shoulder
c = (0, 0, 255)
elif bone == 2 and skeleton[j, 1] == 3: # left arm
c = (0, 102, 204)
elif bone == 3 and skeleton[j, 0] == 3: # left lower arm
c = (0, 204, 204)
elif bone == 2 and skeleton[j, 1] == 6: # right arm
c = (0, 153, 0)
elif bone == 3 and skeleton[j, 0] == 6: # right lower arm
c = (0, 204, 0)
# Hands
elif bone in [5, 6, 7, 8]:
c = (0, 0, 255)
elif bone in [9, 10, 11, 12]:
c = (51, 255, 51)
elif bone in [13, 14, 15, 16]:
c = (255, 0, 0)
elif bone in [17, 18, 19, 20]:
c = (204, 153, 255)
elif bone in [21, 22, 23, 24]:
c = (51, 255, 255)
return c
# Apply DTW to the produced sequence, so it can be visually compared to the reference sequence
def alter_DTW_timing(pred_seq,ref_seq):
# Define a cost function
euclidean_norm = lambda x, y: np.sum(np.abs(x - y))
# Cut the reference down to the max count value
_ , ref_max_idx = torch.max(ref_seq[:, -1], 0)
if ref_max_idx == 0: ref_max_idx += 1
# Cut down frames by counter
ref_seq = ref_seq[:ref_max_idx,:].cpu().numpy()
# Cut the hypothesis down to the max count value
_, hyp_max_idx = torch.max(pred_seq[:, -1], 0)
if hyp_max_idx == 0: hyp_max_idx += 1
# Cut down frames by counter
pred_seq = pred_seq[:hyp_max_idx,:].cpu().numpy()
# Run DTW on the reference and predicted sequence
d, cost_matrix, acc_cost_matrix, path = dtw(ref_seq[:,:-1], pred_seq[:,:-1], dist=euclidean_norm)
# Normalise the dtw cost by sequence length
d = d / acc_cost_matrix.shape[0]
# Initialise new sequence
new_pred_seq = np.zeros_like(ref_seq)
# j tracks the position in the reference sequence
j = 0
skips = 0
squeeze_frames = []
for (i, pred_num) in enumerate(path[0]):
if i == len(path[0]) - 1:
break
if path[1][i] == path[1][i + 1]:
skips += 1
# If a double coming up
if path[0][i] == path[0][i + 1]:
squeeze_frames.append(pred_seq[i - skips])
j += 1
# Just finished a double
elif path[0][i] == path[0][i - 1]:
new_pred_seq[pred_num] = avg_frames(squeeze_frames)
squeeze_frames = []
else:
new_pred_seq[pred_num] = pred_seq[i - skips]
return new_pred_seq, ref_seq, d
# Find the average of the given frames
def avg_frames(frames):
frames_sum = np.zeros_like(frames[0])
for frame in frames:
frames_sum += frame
avg_frame = frames_sum / len(frames)
return avg_frame