Skip to content

Commit 387b275

Browse files
irexyclvhan028
authored andcommitted
[Fix] fix mmaction2 visualizer (#1703)
* fix mmaction2 visualizer * fix lint
1 parent 4f224bf commit 387b275

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

mmdeploy/codebase/mmaction/deploy/video_recognition.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -186,42 +186,45 @@ def visualize(self,
186186
try:
187187
import decord
188188
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)
189224
except Exception:
190225
logger.warn('Please install moviepy and decord to '
191226
'enable visualize for mmaction')
192227

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-
225228
@staticmethod
226229
def get_partition_cfg(partition_type: str) -> Dict:
227230
"""Get a certain partition config.

0 commit comments

Comments
 (0)