@@ -186,42 +186,45 @@ def visualize(self,
186
186
try :
187
187
import decord
188
188
from moviepy .editor import ImageSequenceClip
189
+
190
+ save_dir , save_name = osp .split (output_file )
191
+ video = decord .VideoReader (image )
192
+ frames = [x .asnumpy ()[..., ::- 1 ] for x in video ]
193
+ pred_scores = result .pred_scores .item .tolist ()
194
+ score_tuples = tuple (zip (range (len (pred_scores )), pred_scores ))
195
+ score_sorted = sorted (
196
+ score_tuples , key = itemgetter (1 ), reverse = True )
197
+ top1_item = score_sorted [0 ]
198
+ short_edge_length = min (frames [0 ].shape [:2 ])
199
+ scale = short_edge_length // 224.
200
+ img_scale = min (max (scale , 0.3 ), 3.0 )
201
+ text_cfg = {
202
+ 'positions' :
203
+ np .array ([(img_scale * 5 , ) * 2 ]).astype (np .int32 ),
204
+ 'font_sizes' : int (img_scale * 7 ),
205
+ 'font_families' : 'monospace' ,
206
+ 'colors' : 'white' ,
207
+ 'bboxes' : dict (facecolor = 'black' , alpha = 0.5 , boxstyle = 'Round' )
208
+ }
209
+
210
+ visualizer = self .get_visualizer (window_name , save_dir )
211
+ out_frames = []
212
+ for i , frame in enumerate (frames ):
213
+ visualizer .set_image (frame )
214
+ texts = [f'Frame { i } of total { len (frames )} frames' ]
215
+ texts .append (
216
+ f'top-1 label: { top1_item [0 ]} , score: { top1_item [1 ]} ' )
217
+ visualizer .draw_texts ('\n ' .join (texts ), ** text_cfg )
218
+ drawn_img = visualizer .get_image ()
219
+ out_frames .append (drawn_img )
220
+ out_frames = [x [..., ::- 1 ] for x in out_frames ]
221
+ video_clips = ImageSequenceClip (out_frames , fps = 30 )
222
+ output_file = output_file [:output_file .rfind ('.' )] + '.mp4'
223
+ video_clips .write_videofile (output_file )
189
224
except Exception :
190
225
logger .warn ('Please install moviepy and decord to '
191
226
'enable visualize for mmaction' )
192
227
193
- save_dir , save_name = osp .split (output_file )
194
- video = decord .VideoReader (image )
195
- frames = [x .asnumpy ()[..., ::- 1 ] for x in video ]
196
- pred_scores = result .pred_scores .item .tolist ()
197
- score_tuples = tuple (zip (range (len (pred_scores )), pred_scores ))
198
- score_sorted = sorted (score_tuples , key = itemgetter (1 ), reverse = True )
199
- top1_item = score_sorted [0 ]
200
- short_edge_length = min (frames [0 ].shape [:2 ])
201
- scale = short_edge_length // 224.
202
- img_scale = min (max (scale , 0.3 ), 3.0 )
203
- text_cfg = {
204
- 'positions' : np .array ([(img_scale * 5 , ) * 2 ]).astype (np .int32 ),
205
- 'font_sizes' : int (img_scale * 7 ),
206
- 'font_families' : 'monospace' ,
207
- 'colors' : 'white' ,
208
- 'bboxes' : dict (facecolor = 'black' , alpha = 0.5 , boxstyle = 'Round' )
209
- }
210
-
211
- visualizer = self .get_visualizer (window_name , save_dir )
212
- out_frames = []
213
- for i , frame in enumerate (frames ):
214
- visualizer .set_image (frame )
215
- texts = [f'Frame { i } of total { len (frames )} frames' ]
216
- texts .append (f'top-1 label: { top1_item [0 ]} , score: { top1_item [0 ]} ' )
217
- visualizer .draw_texts ('\n ' .join (texts ), ** text_cfg )
218
- drawn_img = visualizer .get_image ()
219
- out_frames .append (drawn_img )
220
- out_frames = [x [..., ::- 1 ] for x in out_frames ]
221
- video_clips = ImageSequenceClip (out_frames , fps = 30 )
222
- output_file = output_file [:output_file .rfind ('.' )] + '.mp4'
223
- video_clips .write_videofile (output_file )
224
-
225
228
@staticmethod
226
229
def get_partition_cfg (partition_type : str ) -> Dict :
227
230
"""Get a certain partition config.
0 commit comments