12
12
from skvideo import io
13
13
from moviepy .editor import VideoFileClip , concatenate_videoclips
14
14
import enum
15
- import subprocess
16
15
import time
16
+ import subprocess
17
+
17
18
18
19
class OpenposeOutputFormat (enum .Enum ):
19
20
COCO = "coco" ,
@@ -71,8 +72,63 @@ class OpenposeOutputFormat(enum.Enum):
71
72
[POSE_COLORS_B25 , FACE_COLORS , HAND_COLORS , HAND_COLORS ]]
72
73
73
74
75
+ ##################################################################################################################
76
+
77
+ def main ():
78
+ parser = argparse .ArgumentParser (description = 'Draw the lines given with the json data on to the given video.' )
79
+ parser .add_argument ("videofile" , type = argparse .FileType (mode = "r" ), help = "the video file to write on" )
80
+ parser .add_argument ("json" , help = "the folder of json files with the data for each frame (might be zipped)" )
81
+ parser .add_argument ("--outfile" , help = 'the output video file (.mp4-format). If not specified, it will only display the video' )
82
+ parser .add_argument ("-oi --out_images" , dest = "out_images" , help = 'the output folder for the images' )
83
+ parser .add_argument ("-coco" , dest = "coco_format" ,
84
+ help = 'add if the COCO openpose format is used instead of body_25' , action = 'store_true' )
85
+ parser .add_argument ("-t --temp" , metavar = 'tempfolder' , dest = "temp" , help = "folder for saving the temp files" )
86
+ parser .add_argument ("--maxframes" , metavar = "maxframes" , type = int , default = 100 ,
87
+ help = "maximal number of frames before splitting the video sequence - default to 100" )
88
+ parser .add_argument ('--noBG' , help = 'Include to show skeleton only.' , action = 'store_true' )
89
+ parser .add_argument ('--noFPS' , help = 'Include to not show the FPS.' , action = 'store_true' )
90
+ args = parser .parse_args ()
91
+ video , json_folder , out_video_path , out_images_path , use_coco_format , temp \
92
+ = args .videofile .name , args .json , args .outfile , args .out_images , args .coco_format , args .temp
93
+ no_bg , no_fps = args .noBG , args .noFPS
94
+
95
+ if not os .path .exists (json_folder ):
96
+ print ("Json folder not found!" )
97
+ exit (- 1 )
98
+
99
+ if out_video_path :
100
+ _ , out_extension = os .path .splitext (out_video_path )
101
+ if out_extension != ".mp4" :
102
+ print ("So far only .mp4 extension allowed for outfile!" )
103
+ exit (- 1 )
104
+ out_folder , _ = os .path .split (out_video_path )
105
+ os .makedirs (out_folder , exist_ok = True )
106
+ if out_images_path is not None :
107
+ os .makedirs (out_images_path , exist_ok = True )
108
+
109
+ if not temp :
110
+ temp = tempfile .mkdtemp ()
111
+
112
+ used_format = OpenposeOutputFormat .COCO if use_coco_format else OpenposeOutputFormat .BODY_25
113
+ color_video (json_folder , video , out_video_path , temp_folder = temp , out_images = out_images_path ,
114
+ max_frames = args .maxframes , openpose_format = used_format , no_bg = no_bg , no_fps = no_fps )
115
+
116
+
117
+
118
+
119
+
120
+ import matplotlib .pyplot as plt
121
+ import matplotlib .colors as mplcolors
122
+ def imshow (img , title = '' , hsv = False ):
123
+ if hsv : img = mplcolors .hsv_to_rgb (img )
124
+ plt .imshow (img , cmap = 'Greys_r' )
125
+ if title or title == 0 : plt .title (title )
126
+ plt .show ()
127
+
128
+
129
+
74
130
def color_video (frames_json_folder , vid_file , out_file , temp_folder , out_images = None , max_frames = None ,
75
- frame_range = None , openpose_format = OpenposeOutputFormat .BODY_25 , no_bg = False ):
131
+ frame_range = None , openpose_format = OpenposeOutputFormat .BODY_25 , no_bg = False , no_fps = False ):
76
132
"""
77
133
Create a video from the vid_file with the poses given in the frames_json_folder colored on it
78
134
Args:
@@ -86,7 +142,10 @@ def color_video(frames_json_folder, vid_file, out_file, temp_folder, out_images=
86
142
openpose_format(OpenposeOutputFormat): the used output format of openpose
87
143
"""
88
144
video_capture = cv2 .VideoCapture (vid_file )
89
- output_without_ext , output_type = os .path .splitext (out_file )
145
+ if out_file :
146
+ output_without_ext , output_type = os .path .splitext (out_file )
147
+ else :
148
+ output_without_ext = os .path .splitext (vid_file )[0 ]
90
149
frame_count = int (video_capture .get (cv2 .CAP_PROP_FRAME_COUNT ))
91
150
fps_string = subprocess .Popen ('ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate' .split (' ' )+ [vid_file ], stdout = subprocess .PIPE , stderr = subprocess .STDOUT ).communicate ()[0 ].decode ('UTF-8' )[:- 1 ]
92
151
if fps_string != '0/0' :
@@ -108,6 +167,7 @@ def color_video(frames_json_folder, vid_file, out_file, temp_folder, out_images=
108
167
if frame_range .start > 0 :
109
168
video_capture .set (cv2 .CAP_PROP_POS_FRAMES , frame_range .start )
110
169
video_number = None
170
+ fps_time = 0
111
171
for i , file_name in enumerating :
112
172
if i % 10 + 1 == 0 :
113
173
print ("{}/{} frames ready " .format (i , frame_count ), end = '\r ' )
@@ -124,21 +184,35 @@ def color_video(frames_json_folder, vid_file, out_file, temp_folder, out_images=
124
184
break
125
185
written_canvas = canvas_for_frame (canvas , file_name , frames_json_folder , openpose_format = openpose_format )
126
186
canvas = cv2 .addWeighted (canvas , 0.1 , written_canvas , 0.9 , 0 )
127
- all_colored_frames .append ((file_name , canvas ))
128
- colored_frames .append (canvas [:, :, [2 , 1 , 0 ]])
129
- if max_frames and (i + 1 ) % max_frames == 0 and max_frames != frame_count :
187
+ if (not out_file ) and (out_images is None ):
188
+ time .sleep (max (0 , 1 / (fps * 1.02 ) - (time .time ()- fps_time ))) #without the *1.02 it's too slow
189
+ if not no_fps :
190
+ cv2 .putText (canvas , "FPS: %f" % (1.0 / (time .time () - fps_time )), (10 , 10 ), cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (0 , 255 , 0 ), 2 )
191
+ # print(f'Pause: {(time.time() - fps_time)*1000}, FPS: {(1.0 / (time.time() - fps_time))}, wished FPS: {fps}')
192
+ cv2 .imshow ('tf-pose-estimation result' , canvas )
193
+ fps_time = time .time ()
194
+ if cv2 .waitKey (1 ) == 27 :
195
+ break
196
+ else :
197
+ all_colored_frames .append ((file_name , canvas ))
198
+ #imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB))
199
+ colored_frames .append (canvas [:, :, [2 , 1 , 0 ]])
200
+ if max_frames and (i + 1 ) % max_frames == 0 and max_frames != frame_count :
201
+ colored_frames = np .array (colored_frames )
202
+ write_file (colored_frames , fps , output_type , temp_name , video_number )
203
+ colored_frames = []
204
+ if out_file :
205
+ if len (colored_frames ) > 0 :
130
206
colored_frames = np .array (colored_frames )
131
- write_file (colored_frames , fps , output_type , temp_name , video_number )
132
- colored_frames = []
133
- if len (colored_frames ) > 0 :
134
- colored_frames = np .array (colored_frames )
135
- video_number = video_number + 1 if splitted else None
136
- write_file (colored_frames , fps , output_type , temp_name if splitted else output_without_ext , video_number )
207
+ video_number = video_number + 1 if splitted else None
208
+ write_file (colored_frames , fps , output_type , temp_name if splitted else output_without_ext , video_number )
209
+ if splitted :
210
+ combine_videos (out_file , temp_folder , True )
137
211
if out_images is not None :
138
212
write_out_images (out_images , all_colored_frames )
139
- if splitted :
140
- combine_videos (out_file , temp_folder , True )
141
213
214
+ else :
215
+ cv2 .destroyAllWindows ()
142
216
143
217
def write_out_images (out_images_path , all_colored_frames ):
144
218
for json_name , frame in all_colored_frames :
@@ -253,37 +327,4 @@ def combine_videos(outfile, temp_path, delete=False):
253
327
254
328
255
329
if __name__ == '__main__' :
256
- parser = argparse .ArgumentParser (description = 'Draw the lines given with the json data on to the given video.' )
257
- parser .add_argument ("videofile" , type = argparse .FileType (mode = "r" ), help = "the video file to write on" )
258
- parser .add_argument ("json" , help = "the folder of json files with the data for each frame (might be zipped)" )
259
- parser .add_argument ("outfile" , help = 'the output video file' )
260
- parser .add_argument ("-oi --out_images" , dest = "out_images" , help = 'the output folder for the images' )
261
- parser .add_argument ("-coco" , dest = "coco_format" ,
262
- help = 'add if the COCO openpose format is used instead of body_25' , action = 'store_true' )
263
- parser .add_argument ("-t --temp" , metavar = 'tempfolder' , dest = "temp" , help = "folder for saving the temp files" )
264
- parser .add_argument ("--maxframes" , metavar = "maxframes" , type = int , default = 100 ,
265
- help = "maximal number of frames before splitting the video sequence - default to 100" )
266
- parser .add_argument ('--noBG' , help = 'Include to show skeleton only.' , action = 'store_true' )
267
- args = parser .parse_args ()
268
- video , json_folder , out_video_path , out_images_path , use_coco_format , temp , no_bg \
269
- = args .videofile .name , args .json , args .outfile , args .out_images , args .coco_format , args .temp , args .noBG
270
-
271
- if not os .path .exists (json_folder ):
272
- print ("Json folder not found!" )
273
- exit (- 1 )
274
-
275
- _ , out_extension = os .path .splitext (out_video_path )
276
- if out_extension != ".mp4" :
277
- print ("So far only .mp4 extension allowed for outfile!" )
278
- exit (- 1 )
279
- out_folder , _ = os .path .split (out_video_path )
280
- os .makedirs (out_folder , exist_ok = True )
281
- if out_images_path is not None :
282
- os .makedirs (out_images_path , exist_ok = True )
283
-
284
- if not temp :
285
- temp = tempfile .mkdtemp ()
286
-
287
- used_format = OpenposeOutputFormat .COCO if use_coco_format else OpenposeOutputFormat .BODY_25
288
- color_video (json_folder , video , out_video_path , temp_folder = temp , out_images = out_images_path ,
289
- max_frames = args .maxframes , openpose_format = used_format , no_bg = no_bg )
330
+ main ()
0 commit comments