-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_pose_estimation_adaptive.py
461 lines (420 loc) · 13.5 KB
/
run_pose_estimation_adaptive.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import os
import click
import cv2
from raga_pose_estimation.csv_writer import write_csv
# load OpenPoseJsonParser from a new file
from raga_pose_estimation.openpose_json_parser_adaptive import (
OpenPoseJsonParser,
)
from raga_pose_estimation.openpose_parts import (
OpenPosePartGroups,
OpenPoseParts,
)
from raga_pose_estimation.reshaper import reshape_dataframes
from raga_pose_estimation.smoother import Smoother
from raga_pose_estimation.video_utils import crop_video
# load visualizer from a new file
from raga_pose_estimation.visualizer_with_label import Visualizer
@click.command()
@click.option(
"-v",
"--input-video",
default=None,
help="Path to the video file on which to run openpose",
)
@click.option(
"-j",
"--input-json",
default=None,
help="Path to a directory of previously generated openpose json files",
)
@click.option(
"-o",
"--output-dir",
prompt="Output directory",
help="Path to the directory in which to output CSV files (and "
"videos if required).",
)
@click.option(
"-r",
"--crop-rectangle",
nargs=4,
type=int,
default=None,
help="Coordinates of rectangle to crop the video before processing, in "
"the form w h x y where w and h are the width and height of the cropped "
"rectangle and (x,y) is the top-left of the rectangle, as measured in "
"pixels from the top-left corner.",
)
@click.option(
"-n",
"--number-of-people",
default=1,
help="Number of people to include in output.",
)
@click.option(
"-O",
"--openpose-dir",
help="Path to the directory in which openpose is installed.",
)
@click.option(
"-a",
"--openpose-args",
help="Additional arguments to pass to OpenPose. See "
"https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/include/openpose/flags.hpp"
" for a full list of options.",
)
@click.option(
"-m",
"--create-model-video",
is_flag=True,
default=False,
help="Whether to create a video showing the poses on a blank "
"background",
)
@click.option(
"-V",
"--create-overlay-video",
is_flag=True,
default=False,
help="Whether to create a video showing the poses as an oVerlay",
)
@click.option(
"-w",
"--width",
default=0,
help="Width of original video (mandatory for creating video if "
" not providing input-video)",
)
@click.option(
"-h",
"--height",
default=0,
help="Height of original video (mandatory for creating video if "
" not providing input-video)",
)
@click.option(
"-c",
"--confidence-threshold",
default=0.0,
help="Confidence threshold. Items with a confidence lower than "
"the threshold will be replaced by values from a previous frame.",
)
@click.option(
"-s",
"--smoothing-parameters",
default=(None, None),
type=(int, int),
help="Window and polynomial order for smoother. See README for "
"details.",
)
@click.option(
"-b",
"--body-parts",
default=None,
help="Body parts to include in output. Should be a "
"comma-separated list of strings as in the list at "
"https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/output.md#keypoint-ordering-in-cpython"
', e.g. "LEye,RElbow". Overrides --upper-body-parts and '
"--lower-body-parts.",
)
@click.option(
"-u",
"--upper-body-parts",
"bodypartsgroup",
flag_value="upper",
help="Output upper body parts only",
)
@click.option(
"-l",
"--lower-body-parts",
"bodypartsgroup",
flag_value="lower",
help="Output lower body parts only",
)
@click.option(
"-f",
"--flatten",
"flatten",
default=False,
help="Export CSV in flattened format, i.e. with a single header row (see README)",
)
def openpose_cli(
output_dir,
openpose_dir,
openpose_args,
input_video,
input_json,
crop_rectangle,
number_of_people,
create_model_video,
create_overlay_video,
width,
height,
confidence_threshold,
smoothing_parameters,
body_parts,
bodypartsgroup,
flatten,
):
"""Runs openpose on the video, does post-processing, and outputs CSV
files. See cli docs for parameter details."""
body_parts_list = None
if body_parts:
try:
body_parts_list = [
OpenPoseParts(part) for part in body_parts.split(",")
]
except ValueError as e:
click.echo(f"Invalid body-parts value {body_parts}: {e}")
exit(1)
elif bodypartsgroup == "upper":
body_parts_list = OpenPosePartGroups.UPPER_BODY_PARTS
elif bodypartsgroup == "lower":
body_parts_list = OpenPosePartGroups.LOWER_BODY_PARTS
if smoothing_parameters == (None, None):
smoothing_parameters = None
run_pose_estimation(
output_dir,
openpose_dir,
openpose_args,
input_video,
input_json,
crop_rectangle,
number_of_people,
create_model_video,
create_overlay_video,
width,
height,
confidence_threshold,
smoothing_parameters,
body_parts_list,
flatten,
)
def run_pose_estimation(
output_dir,
openpose_dir=None,
openpose_args=None,
input_video=None,
input_json=None,
crop_rectangle=None,
number_of_people=1,
create_model_video=False,
create_overlay_video=False,
width=0,
height=0,
confidence_threshold=0,
smoothing_parameters=None,
body_parts=None,
flatten=False,
):
"""Runs openpose on the video, does post-processing, and outputs CSV files.
Non-click version to work from jupyter notebooks.
Parameters
----------
output_dir : str
Path to the directory in which to output CSV
files (and videos if required).
openpose_dir : str
Path to the directory in which openpose is
installed.
openpose_args : str
Additional arguments to pass to openpose.
input_video : str
Path to the video file on which to run
openpose.
crop_rectangle : tuple(int)
Coordinates for cropping the video before
processing. Should be a tuple (width, height, x, y)
giving the width and height of the cropped
rectangle and the coordinates (x,y) of the
top-left of the rectangle, as measured in
pixels from the top-left corner.
input_json : str
Path to a directory of previously generated
openpose json files.
number_of_people : int
Number of people for which to output results.
create_model_video : bool
Whether to create a video showing the poses on
a blank background.
create_overlay_video : type
Whether to create a video showing the poses as
an overlay.
width : int
Width of original video (mandatory for
creating video if not providing input_video).
height : int
Height of original video (mandatory for
creating video if not providing input_video).
confidence_threshold : float
Confidence threshold. Items with a confidence
lower than the threshold will be replaced by
values from a previous frame.
smoothing_parameters : (int, int)
Pair of parameters (smoothing_window, polyorder)
defining the smoothing function applied. If None,
no smoothing is attempted. See README.
body_parts : list of OpenPoseParts
Body parts to include in output.
flatten : type
Export CSV in flattened format, i.e. with a
single header row (see README).
"""
# Check output directory
output_dir = os.path.abspath(output_dir)
if os.path.isdir(output_dir) and os.listdir(output_dir):
print(
f"Directory {output_dir} exists and is not empty, so files would be overridden."
)
exit(1)
# Check input values
if input_json is None and input_video is None:
print("You must provide either an input video or input json files.")
exit(1)
if input_json is None and openpose_dir is None:
print(
"You must provide a path to an openpose executable unless you "
"provide input json."
)
exit(1)
if input_video is None and crop_rectangle:
print("You must provide an input video in order to crop the video.")
exit(1)
if input_video is None and create_overlay_video:
print(
"You must provide an input video in order to create an overlay "
"video."
)
exit(1)
if (
input_video is None
and create_model_video
and (width == 0 or height == 0)
):
print(
"You must provide width and height of the original video in "
"order to produce a model video."
)
exit(1)
if crop_rectangle and (
type(crop_rectangle) != tuple
or len([c for c in crop_rectangle if type(c) == int]) != 4
):
print("You must provide 4 integer coordinates to crop_rectangle.")
exit(1)
# Create output directory
os.makedirs(output_dir, exist_ok=True)
# Run openpose if necessary
if input_json:
path_to_json = os.path.abspath(input_json)
if not os.path.exists(path_to_json):
print(f"Invalid input_json path {path_to_json}.")
exit(1)
else:
print(f"Processing JSON from {path_to_json}...")
else:
input_video = os.path.realpath(input_video)
# Crop the video before processing
if crop_rectangle:
print(f"Cropping video {input_video}...")
try:
input_video = crop_video(
input_video, output_dir, *crop_rectangle
)
print(f"Cropped video at {input_video}...")
except Exception as e:
print(
f"Unable to crop video {input_video} with coords {crop_rectangle}. Error was: {e}"
)
exit(1)
print(f"Detecting poses on {input_video}...")
# Run openpose over the video
openpose_dir = os.path.abspath(openpose_dir)
if not os.path.exists(openpose_dir):
print(f"Invalid openpose path {openpose_dir}.")
exit(1)
# Calling out to the binary seems to be quicker than the python wrapper
path_to_json = os.path.join(output_dir, "json")
cmd = (
f"cd {openpose_dir} && "
"./build/examples/openpose/openpose.bin "
f"--video {input_video} "
f"--write_json {path_to_json} --display 0 --render-pose 0"
)
if openpose_args:
cmd = f"{cmd} {openpose_args}"
try:
# If running in Colab, need to use ipython's system call
ip = get_ipython()
result = ip.system_piped(cmd)
except NameError:
# Otherwise (if get_ipython doesn't exist) we can just use os.system
result = os.system(cmd)
if result:
print(f"Unable to run openpose from {openpose_dir}.")
exit(1)
# Get list of json files
json_files = [
pos_json
for pos_json in os.listdir(path_to_json)
if pos_json.endswith(".json")
]
if len(json_files) == 0:
print(f"No json files found in {path_to_json}.")
exit(1)
# Get array for dataframes
body_keypoints_dfs = []
# Loop through all json files in output directory
# Each file is a frame in the video
json_files.sort()
previous_body_keypoints_df = None
for num, file in enumerate(json_files):
# if num < 4300:
# continue
parser = OpenPoseJsonParser(os.path.join(path_to_json, file))
body_keypoints_df = parser.get_multiple_keypoints(
list(range(number_of_people)),
body_parts,
confidence_threshold,
previous_body_keypoints_df,
)
body_keypoints_df = parser.sort_persons_by_x_position(
body_keypoints_df
)
body_keypoints_df.reset_index()
body_keypoints_dfs.append(body_keypoints_df)
previous_body_keypoints_df = body_keypoints_df
person_dfs = reshape_dataframes(body_keypoints_dfs)
if smoothing_parameters:
print("Smoothing output...")
smoother = Smoother(*smoothing_parameters)
person_dfs = smoother.smooth(person_dfs)
print(f"Saving CSVs to {output_dir}...")
write_csv(person_dfs, output_dir, flatten=flatten)
print("Done.")
if create_model_video or create_overlay_video:
if not width or not height:
cap = cv2.VideoCapture(input_video)
width = int(cap.get(3))
height = int(cap.get(4))
cap.release()
visualizer = Visualizer(output_directory=output_dir)
if create_model_video:
print("Creating model video...")
visualizer.create_video_from_dataframes(
"video", person_dfs, width, height
)
if create_overlay_video:
print("Creating overlay video...")
visualizer.create_video_from_dataframes(
"video",
person_dfs,
width,
height,
create_overlay=create_overlay_video,
video_to_overlay=input_video,
)
# debugfile('/Users/jinli/code/openpose-music/run_pose_estimation_Jin.py', args='-j data/JSON/NIR_SCh_Malhar_SideL_StereoMix/json -v data/video_group/NIR_SCh_Malhar_SideL_StereoMix.mp4 -o output/video_group/NIR_SCh_Malhar_SideL_StereoMix -u -V -n 10 -c 0.7 -s 13 2', wdir='/Users/jinli/code/openpose-music')
if __name__ == "__main__":
openpose_cli()