Skip to content

Commit b42c4e5

Browse files
authored
Fix docs and impr visualize (#422)
1 parent 72fd3d9 commit b42c4e5

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
English | [中文](README_CN.md)
1313

14-
[Introduction](#introduction) |
15-
[Installation](#installation) |
16-
[Quick Start](#quick-start) |
17-
[Tutorials](#tutorials) |
18-
[Model List](#model-list) |
19-
[Dataset List](#dataset-list) |
20-
[Notes](#notes)
14+
[📝Introduction](#introduction) |
15+
[🔨Installation](#installation) |
16+
[🚀Quick Start](#quick-start) |
17+
[📚Tutorials](#tutorials) |
18+
[🎁Model List](#model-list) |
19+
[📰Dataset List](#dataset-list) |
20+
[🎉Notes](#notes)
2121

2222
</div>
2323

README_CN.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
[English](README.md) | 中文
1313

14-
[简介](#简介) |
15-
[安装教程](#安装教程) |
16-
[快速开始](#快速开始) |
17-
[使用教程](#使用教程) |
18-
[模型列表](#模型列表) |
19-
[数据集列表](#数据集列表) |
20-
[更新日志](#更新日志)
14+
[📝简介](#简介) |
15+
[🔨安装教程](#安装教程) |
16+
[🚀快速开始](#快速开始) |
17+
[📚使用教程](#使用教程) |
18+
[🎁模型列表](#模型列表) |
19+
[📰数据集列表](#数据集列表) |
20+
[🎉更新日志](#更新日志)
2121

2222
</div>
2323

mindocr/utils/visualize.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from typing import List, Union
23

34
import cv2
@@ -66,8 +67,9 @@ def draw_boxes(
6667
image: Union[str, np.array],
6768
bboxes: Union[list, np.array],
6869
color: Union[tuple, str] = (255, 0, 0),
69-
thickness=2,
70+
thickness=1,
7071
is_bgr_img=False,
72+
color_random=False,
7173
): # , to_rgb=False):
7274
"""image can be str or np.array for image in 'BGR' colorm mode.
7375
color: list for color of each box, or tuple for color of all boxes with the same color. in RGB order
@@ -132,15 +134,18 @@ def _get_draw_point_and_font_size(box, font_size="auto", text_inside_box=True, i
132134
pt_sums = np.array(box).sum(axis=1)
133135
corner = box[np.argmin(pt_sums)]
134136

135-
box_h = box[:, 1].max() - box[:, 1].min()
136-
box_w = box[:, 0].max() - box[:, 0].min()
137+
# box_h = box[:, 1].max() - box[:, 1].min()
138+
# box_w = box[:, 0].max() - box[:, 0].min()
139+
140+
box_h = int(math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2))
141+
box_w = int(math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2))
137142

138143
# print(font_size)
139144
# TODO: consider the height and witdh of the text
140145
if text_inside_box:
141146
draw_point_w = corner[0] + box_w * 0.1
142147
draw_point_h = corner[1] - box_h * 0.05
143-
font_size = round(box_h * 0.8) if not isinstance(font_size, int) else font_size
148+
font_size = round(box_h * 0.9) if not isinstance(font_size, int) else font_size
144149
else:
145150
if isinstance(font_size, int) or isinstance(font_size, float):
146151
font_size = font_size
@@ -154,14 +159,20 @@ def _get_draw_point_and_font_size(box, font_size="auto", text_inside_box=True, i
154159

155160
for i, text in enumerate(texts):
156161
# draw text on the most left-top point
157-
draw_point, fs = _get_draw_point_and_font_size(
158-
bboxes[i], font_size, text_inside_box=text_inside_box, img_h=img_h
159-
)
162+
box = bboxes[i]
163+
draw_point, fs = _get_draw_point_and_font_size(box, font_size, text_inside_box=text_inside_box, img_h=img_h)
160164

161165
# TODO: use other lib which can set font size dynamically after font loading
162-
ttf = ImageFont.load_default() if not font_path else ImageFont.truetype(font_path, fs)
166+
font = ImageFont.load_default() if not font_path else ImageFont.truetype(font_path, fs, encoding="utf-8")
167+
168+
font_width = font.getsize(text)[0]
169+
box_width = int(math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2))
170+
if font_width > box_width:
171+
font_size = int(fs * box_width / font_width)
172+
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
173+
163174
# refine the draw starting
164-
img_draw.text(draw_point, text, font=ttf, fill=text_color)
175+
img_draw.text(draw_point, text, font=font, fill=text_color)
165176

166177
return np.array(pimg)
167178

tools/infer/text/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Thus, online inference is more suitable for demonstration and to visually evalua
1414
| Python | >=3.7 |
1515

1616
Supported platforms: Linux, MacOS, Windows (Not tested)
17+
1718
Supported devices: CPU, GPU, and Ascend.
1819

1920
Please clone MindOCR at first
@@ -173,6 +174,7 @@ python tools/infer/text/predict_system.py --image_dir {path_to_img or dir_to_img
173174
--det_algorithm DB++ \
174175
--rec_algorithm CRNN
175176
```
177+
> Note: set `--visualize_output True` if you want to visualize the detection and recognition results on the input image.
176178
177179
After running, the inference results will be saved in `{args.draw_img_save_dir}/system_results.txt`, where `--draw_img_save_dir` is the directory for saving results and is set to `./inference_results` by default. Here are some results for examples.
178180

0 commit comments

Comments
 (0)