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

Conversation

ProtossDragoon
Copy link
Contributor

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

'san-serif' does not support to visualize Korean character.

Modification

python3 -m mmocr.ocr \
--det-ckpt ... \
--det-config ... \
--recog-ckpt ... \
--recog-config ... \
img_example \
--img-out-dir ...

before:

configs/textrecog/_base_/default_runtime.py

visualizer = dict(
    type='TextRecogLocalVisualizer',
    name='visualizer',
    vis_backends=vis_backends) 

before

before2

output

/usr/local/lib/python3.8/dist-packages/mmengine/visualization/utils.py:238: UserWarning: Glyph 48373 (\N{HANGUL SYLLABLE BOG}) missing from current font.
  s, (width, height) = canvas.print_to_buffer()
/usr/local/lib/python3.8/dist-packages/mmengine/visualization/utils.py:238: UserWarning: Glyph 54788 (\N{HANGUL SYLLABLE HYEON}) missing from current font.
  s, (width, height) = canvas.print_to_buffer()
/usr/local/lib/python3.8/dist-packages/mmengine/visualization/utils.py:238: UserWarning: Glyph 51456 (\N{HANGUL SYLLABLE JUN}) missing from current font.
  s, (width, height) = canvas.print_to_buffer()
/usr/local/lib/python3.8/dist-packages/mmengine/visualization/utils.py:238: UserWarning: Glyph 48276 (\N{HANGUL SYLLABLE BEOM}) missing from current font.
  s, (width, height) = canvas.print_to_buffer()
/usr/local/lib/python3.8/dist-packages/mmengine/visualization/utils.py:238: UserWarning: Glyph 54868 (\N{HANGUL SYLLABLE HWA}) missing from current font.
  s, (width, height) = canvas.print_to_buffer()
... # skipped

after:

configs/textrecog/_base_/default_runtime.py

visualizer = dict(
    type='TextRecogLocalVisualizer',
    name='visualizer',
    font_families='NanumGothic', # new feature
    vis_backends=vis_backends)

after

after2

I added self.font_families to BaseLocalVisualizer class.

  • KIELocalVisualizer._draw_instances() → KIELocalVisualizer._draw_edge_label() → Visualizer.draw_texts()
  • KIELocalVisualizer._draw_instances() → BaseLocalVisualizer.get_labels_image() → Visualizer.draw_texts()
  • TextRecogLocalVisualizer._draw_instances() → Visualizer.draw_texts()

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects.
  • CLA has been signed and all committers have signed the CLA in this PR.

@ProtossDragoon ProtossDragoon changed the title [Fix] Support custom font for some languages (e.g. Korean) [Fix] Support custom font to visualize some languages (e.g. Korean) Nov 23, 2022
mmocr/visualization/textspotting_visualizer.py Outdated Show resolved Hide resolved
@@ -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

@gaotongxiao
Copy link
Collaborator

Hi, would you polish this PR further?

@ProtossDragoon
Copy link
Contributor Author

@gaotongxiao Yes, maybe this week :)

@gaotongxiao gaotongxiao added this to the 1.0.0rc5 milestone Dec 7, 2022
@ProtossDragoon
Copy link
Contributor Author

Initially, I tried to add a font_family argument to the _draw_edge_label() function (@Harold-lkk suggested), but I did not explicitly add that argument to the function because text style related arguments such as font_size exist only in the get_labels_image() function. Let me know if you still think the function _draw_edge_label() needs a font_family argument. I'll add it.

@gaotongxiao gaotongxiao merged commit 0ec1524 into open-mmlab:dev-1.x Dec 27, 2022
@ProtossDragoon ProtossDragoon deleted the dev-1.x branch May 27, 2023 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants