open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.54k stars 9.46k forks source link

Add API to adjust `font_families` when using Visualizer to render inference result #10687

Open FirokOtaku opened 1 year ago

FirokOtaku commented 1 year ago

(feature request)

Describe the feature

Add a property or setter method to allow user adjust font_families used by Visualizer.

model = init_detector(config_file, checkpoint_file, device='cuda:0')
visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.font_families = 'Microsoft Yahei'
# ...

Motivation

As this issue mentioned, an inference result has Chinese category names (or any other characters which are not included in default font sans-serif), could not be rendered properly.

Related resources

Modifying underlying code of Visualizer is a temporary solution, but not a good one.

Additional context

ypwhs commented 2 months ago

Try try this:

from mmdet.visualization.local_visualizer import DetLocalVisualizer as BaseVisualizer
from mmdet.visualization.local_visualizer import VISUALIZERS

@VISUALIZERS.register_module(name='DetLocalVisualizer', force=True)
class DetLocalVisualizer(BaseVisualizer):

    def __init__(
        self,
        name='visualizer',
        *args,
        font_families='SimHei',
        **kwargs,
    ):
        super().__init__(name=name, *args, **kwargs)
        self.font_families = font_families

    def draw_texts(self, *args, **kwargs):
        kwargs['font_families'] = self.font_families
        return super().draw_texts(*args, **kwargs)

Before:

vis_det

After:

vis_det(1)

maplelost commented 2 months ago

@ypwhs you truly made my day!