[Feature] visualizer.draw_texts() adds a font_properties argument (#887)
* [Feature] visualizer.draw_texts() add a font_properties argument in order to show Chinese characters. * [Fix] move font_properties argument to the end. * Update docstring of font_properties parameter. * [Fix] moving FontProperties import clause to draw_texts() and add TYPE_CHECKING * Update mmengine/visualization/visualizer.py --------- Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>pull/906/head
parent
1aa14b45a0
commit
4dad16fde2
|
@ -1,7 +1,10 @@
|
|||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import os.path as osp
|
||||
import warnings
|
||||
from typing import Dict, List, Optional, Sequence, Tuple, Union
|
||||
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from matplotlib.font_manager import FontProperties
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
@ -391,15 +394,18 @@ class Visualizer(ManagerMixin):
|
|||
|
||||
@master_only
|
||||
def draw_texts(
|
||||
self,
|
||||
texts: Union[str, List[str]],
|
||||
positions: Union[np.ndarray, torch.Tensor],
|
||||
font_sizes: Optional[Union[int, List[int]]] = None,
|
||||
colors: Union[str, tuple, List[str], List[tuple]] = 'g',
|
||||
vertical_alignments: Union[str, List[str]] = 'top',
|
||||
horizontal_alignments: Union[str, List[str]] = 'left',
|
||||
font_families: Union[str, List[str]] = 'sans-serif',
|
||||
bboxes: Optional[Union[dict, List[dict]]] = None) -> 'Visualizer':
|
||||
self,
|
||||
texts: Union[str, List[str]],
|
||||
positions: Union[np.ndarray, torch.Tensor],
|
||||
font_sizes: Optional[Union[int, List[int]]] = None,
|
||||
colors: Union[str, tuple, List[str], List[tuple]] = 'g',
|
||||
vertical_alignments: Union[str, List[str]] = 'top',
|
||||
horizontal_alignments: Union[str, List[str]] = 'left',
|
||||
font_families: Union[str, List[str]] = 'sans-serif',
|
||||
bboxes: Optional[Union[dict, List[dict]]] = None,
|
||||
font_properties: Optional[Union['FontProperties',
|
||||
List['FontProperties']]] = None
|
||||
) -> 'Visualizer':
|
||||
"""Draw single or multiple text boxes.
|
||||
|
||||
Args:
|
||||
|
@ -448,7 +454,21 @@ class Visualizer(ManagerMixin):
|
|||
the texts will have the same bbox. Reference to
|
||||
https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.FancyBboxPatch.html#matplotlib.patches.FancyBboxPatch
|
||||
for more details. Defaults to None.
|
||||
font_properties (Union[FontProperties, List[FontProperties]],
|
||||
optional): The font properties of texts. FontProperties is
|
||||
a `font_manager.FontProperties()` object.
|
||||
If you want to draw Chinese texts, you need to prepare
|
||||
a font file that can show Chinese characters properly.
|
||||
For example: `simhei.ttf`,`simsun.ttc`,`simkai.ttf` and so on.
|
||||
Then set font_properties=matplotlib.font_manager.FontProperties
|
||||
(fname='path/to/font_file')
|
||||
``font_properties`` can have the same length with texts or
|
||||
just single value. If ``font_properties`` is single value,
|
||||
all the texts will have the same font properties.
|
||||
Defaults to None.
|
||||
`New in version 0.6.0.`
|
||||
"""
|
||||
from matplotlib.font_manager import FontProperties
|
||||
check_type('texts', texts, (str, list))
|
||||
if isinstance(texts, str):
|
||||
texts = [texts]
|
||||
|
@ -489,6 +509,14 @@ class Visualizer(ManagerMixin):
|
|||
num_text)
|
||||
font_families = value2list(font_families, str, num_text)
|
||||
|
||||
if font_properties is None:
|
||||
font_properties = [None for _ in range(num_text)] # type: ignore
|
||||
else:
|
||||
check_type_and_length('font_properties', font_properties,
|
||||
(FontProperties, list), num_text)
|
||||
font_properties = value2list(font_properties, FontProperties,
|
||||
num_text)
|
||||
|
||||
if bboxes is None:
|
||||
bboxes = [None for _ in range(num_text)] # type: ignore
|
||||
else:
|
||||
|
@ -505,6 +533,7 @@ class Visualizer(ManagerMixin):
|
|||
verticalalignment=vertical_alignments[i],
|
||||
horizontalalignment=horizontal_alignments[i],
|
||||
family=font_families[i],
|
||||
fontproperties=font_properties[i],
|
||||
color=colors[i])
|
||||
return self
|
||||
|
||||
|
|
Loading…
Reference in New Issue