Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Support custom font to visualize some languages (e.g. Korean) #1567

Merged
merged 4 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mmocr/apis/inferencers/mmocr_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __init__(self,
self.visualizer = VISUALIZERS.build(
dict(
type='TextSpottingLocalVisualizer',
name=f'inferencer{ts}'))
name=f'inferencer{ts}',
font_families=self.textrec_inferencer.visualizer.
font_families))
else:
self.mode = 'rec'
if kie_config is not None:
Expand Down
12 changes: 10 additions & 2 deletions mmocr/visualization/base_visualizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
import math
from typing import Sequence, Union
from typing import List, Sequence, Union

import numpy as np
import torch
Expand Down Expand Up @@ -50,6 +50,13 @@ class BaseLocalVisualizer(Visualizer):
(95, 54, 80), (128, 76, 255), (201, 57, 1), (246, 0, 122),
(191, 162, 208)]

def __init__(self,
name: str = 'visualizer',
font_families: Union[str, List[str]] = 'sans-serif',
**kwargs) -> None:
super().__init__(name=name, **kwargs)
self.font_families = font_families

def get_labels_image(self,
image: np.ndarray,
labels: Union[np.ndarray, torch.Tensor],
Expand Down Expand Up @@ -88,7 +95,8 @@ def get_labels_image(self,
vertical_alignments='center',
horizontal_alignments='center',
colors='k',
font_sizes=font_size)
font_sizes=font_size,
font_families=self.font_families)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to pass font_families into the function

def get_labels_image(xxx, font_familyies='sans-serif'):
    self.draw_texts(
        labels, (bboxes[:, :2] + bboxes[:, 2:]) / 2,
        vertical_alignments='center',
        horizontal_alignments='center',
        colors='k',
        font_sizes=font_size,
        font_families=self.font_families)

Copy link
Contributor Author

@ProtossDragoon ProtossDragoon Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Harold-lkk

There are other functions that call self.draw_texts();

  • _draw_edge_label()
  • get_labels_image()

Do you think these function should contains the font_families argument?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Containing will be better

return self.get_image()

def get_polygons_image(self,
Expand Down
8 changes: 5 additions & 3 deletions mmocr/visualization/kie_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class KIELocalVisualizer(BaseLocalVisualizer):
"""

def __init__(self,
name='kie_visualizer',
name: str = 'kie_visualizer',
is_openset: bool = False,
**kwargs) -> None:
super().__init__(name=name, **kwargs)
Expand Down Expand Up @@ -89,13 +89,15 @@ def _draw_edge_label(self,
key_texts, (bboxes[key_index, :2] + bboxes[key_index, 2:]) / 2,
colors='k',
horizontal_alignments='center',
vertical_alignments='center')
vertical_alignments='center',
font_families=self.font_families)
if val_texts:
self.draw_texts(
val_texts, (bboxes[val_index, :2] + bboxes[val_index, 2:]) / 2,
colors='k',
horizontal_alignments='center',
vertical_alignments='center')
vertical_alignments='center',
font_families=self.font_families)
self.draw_arrows(
x_data,
y_data,
Expand Down
10 changes: 6 additions & 4 deletions mmocr/visualization/textrecog_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def __init__(self,
vis_backends: Optional[Dict] = None,
save_dir: Optional[str] = None,
gt_color: Optional[Union[str, Tuple[int, int, int]]] = 'g',
pred_color: Optional[Union[str, Tuple[int, int,
int]]] = 'r') -> None:
pred_color: Optional[Union[str, Tuple[int, int, int]]] = 'r',
**kwargs) -> None:
super().__init__(
name=name,
image=image,
vis_backends=vis_backends,
save_dir=save_dir)
save_dir=save_dir,
**kwargs)
self.gt_color = gt_color
self.pred_color = pred_color

Expand All @@ -66,7 +67,8 @@ def _draw_instances(self, image: np.ndarray, text: str) -> np.ndarray:
colors=self.gt_color,
font_sizes=font_size,
vertical_alignments='center',
horizontal_alignments='center')
horizontal_alignments='center',
font_families=self.font_families)
text_image = self.get_image()
return text_image

Expand Down
3 changes: 3 additions & 0 deletions mmocr/visualization/textspotting_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
@VISUALIZERS.register_module()
class TextSpottingLocalVisualizer(BaseLocalVisualizer):

def __init__(self, name: str = 'visualizer', **kwargs) -> None:
super().__init__(name=name, **kwargs)

gaotongxiao marked this conversation as resolved.
Show resolved Hide resolved
def _draw_instances(
self,
image: np.ndarray,
Expand Down