Skip to content

Commit c46d012

Browse files
authored
chore: update files (#513)
1 parent 1797850 commit c46d012

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

python/rapidocr/ch_ppocr_rec/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from ..utils import Logger
2525
from ..utils.download_file import DownloadFile, DownloadFileInput
26+
from ..utils.vis_res import VisRes
2627
from .typings import TextRecInput, TextRecOutput
2728
from .utils import CTCLabelDecode
2829

@@ -46,6 +47,8 @@ def __init__(self, cfg: Dict[str, Any]):
4647
self.rec_batch_num = cfg["rec_batch_num"]
4748
self.rec_image_shape = cfg["rec_img_shape"]
4849

50+
self.cfg = cfg
51+
4952
def get_character_dict(self, cfg):
5053
character = None
5154
dict_path = cfg.get("rec_keys_path", None)
@@ -132,7 +135,14 @@ def __call__(self, args: TextRecInput) -> TextRecOutput:
132135

133136
all_line_results, all_word_results = list(zip(*rec_res))
134137
txts, scores = list(zip(*all_line_results))
135-
return TextRecOutput(img_list, txts, scores, all_word_results, elapse)
138+
return TextRecOutput(
139+
img_list,
140+
txts,
141+
scores,
142+
all_word_results,
143+
elapse,
144+
viser=VisRes(lang_type=self.cfg.lang_type, font_path=self.cfg.font_path),
145+
)
136146

137147
def resize_norm_img(self, img: np.ndarray, max_wh_ratio: float) -> np.ndarray:
138148
img_channel, img_height, img_width = self.rec_image_shape

python/rapidocr/ch_ppocr_rec/typings.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TextRecOutput:
4343
("", 1.0, None),
4444
)
4545
elapse: Optional[float] = None
46-
lang_type: Optional[str] = None
46+
viser: Optional[VisRes] = None
4747

4848
def __len__(self):
4949
if self.txts is None:
@@ -55,10 +55,7 @@ def vis(self, save_path: Optional[Union[str, Path]] = None) -> Optional[np.ndarr
5555
logger.warning("No image or txts to visualize.")
5656
return None
5757

58-
vis = VisRes()
59-
vis_img = vis.draw_rec_res(
60-
self.imgs, self.txts, self.scores, lang_type=self.lang_type
61-
)
58+
vis_img = self.viser.draw_rec_res(self.imgs, self.txts, self.scores)
6259

6360
if save_path is not None:
6461
save_img(save_path, vis_img)

python/rapidocr/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def _initialize(self, cfg: DictConfig):
6767

6868
self.use_rec = cfg.Global.use_rec
6969
cfg.Rec.engine_cfg = cfg.EngineConfig[cfg.Rec.engine_type.value]
70+
cfg.Rec.font_path = cfg.Global.font_path
7071
self.text_rec = TextRecognizer(cfg.Rec)
7172

7273
self.load_img = LoadImage()

python/rapidocr/utils/vis_res.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import math
55
import random
66
from pathlib import Path
7-
from typing import List, Optional, Tuple, Union
7+
from typing import List, Optional, Sequence, Tuple, Union
88

99
import cv2
1010
import numpy as np
@@ -128,26 +128,19 @@ def get_font_path(
128128

129129
def draw_rec_res(
130130
self,
131-
imgs: List[InputType],
131+
imgs: Sequence[InputType],
132132
txts: Union[List[str], Tuple[str]],
133133
scores: Tuple[float],
134-
lang_type: Optional[str] = None,
135134
) -> np.ndarray:
136135
result_imgs = []
137136
for img, txt, score in zip(imgs, txts, scores):
138-
vis_img = self.draw_one_rec_res(img, txt, score, lang_type)
137+
vis_img = self.draw_one_rec_res(img, txt, score)
139138
result_imgs.append(vis_img)
140139
return self.concat_imgs(result_imgs, direction="vertical")
141140

142141
def draw_one_rec_res(
143-
self,
144-
img_content: InputType,
145-
txt: str,
146-
score: float,
147-
lang_type: Optional[str] = None,
142+
self, img_content: InputType, txt: str, score: float
148143
) -> np.ndarray:
149-
font_path = self.get_font_path(None, lang_type)
150-
151144
image = Image.fromarray(self.load_img(img_content))
152145
h, w = image.height, image.width
153146
if image.mode == "L":
@@ -162,15 +155,15 @@ def draw_one_rec_res(
162155
box_width = self.get_box_width(box)
163156
if box_height > 2 * box_width:
164157
font_size = max(int(box_width * 0.9), 10)
165-
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
158+
font = ImageFont.truetype(self.font_path, font_size, encoding="utf-8")
166159
cur_y = box[0][1]
167160

168161
for c in txt:
169162
draw_right.text((box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font)
170163
cur_y += self.get_char_size(font, c)
171164
else:
172165
font_size = max(int(box_height * 0.8), 10)
173-
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
166+
font = ImageFont.truetype(self.font_path, font_size, encoding="utf-8")
174167
draw_right.text([box[0][0], box[0][1]], txt, fill=(0, 0, 0), font=font)
175168

176169
img_left = Image.blend(image, img_left, 0.5)

0 commit comments

Comments
 (0)