Skip to content

Commit 12ea82a

Browse files
committed
chore: update files
1 parent 0bca33c commit 12ea82a

File tree

9 files changed

+114
-75
lines changed

9 files changed

+114
-75
lines changed

python/demo.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,19 @@
1919
img = f.read()
2020

2121
# result, elapse_list = engine(img, use_det=True, use_cls=False, use_rec=False)
22-
result, elapse_list = engine(img, return_word_box=True)
22+
result = engine(img, return_word_box=True)
2323
print(result)
24-
print(elapse_list)
24+
print(result.elapse)
2525

26-
# result, elapse = engine(image_path, use_det=False, use_cls=False, use_rec=True)
26+
boxes = result.boxes
27+
txts = result.txts
28+
scores = result.scores
2729

28-
(boxes, txts, scores, words_boxes, words, words_scores) = list(zip(*result))
2930
font_path = "resources/fonts/FZYTK.TTF"
31+
vis_img = vis(img, result.boxes, result.txts, result.scores, font_path)
32+
cv2.imwrite("vis.png", vis_img)
3033

31-
words_boxes = sum(words_boxes, [])
32-
words_all = sum(words, [])
33-
words_scores = sum(words_scores, [])
34-
vis_img = vis(img, words_boxes, words_all, words_scores, font_path)
34+
words_results = result.word_results
35+
words, words_scores, words_boxes = list(zip(*words_results))
36+
vis_img = vis(img, words_boxes, words, words_scores, font_path)
3537
cv2.imwrite("vis_single.png", vis_img)
36-
37-
# (boxes, txts, scores, words_boxes, words) = list(zip(*result))
38-
39-
# vis_img = vis(img, boxes, txts, scores, font_path)
40-
# cv2.imwrite("vis.png", vis_img)
41-
42-
# words_boxes = sum(words_boxes, [])
43-
# words_all = sum(words, [])
44-
# words_scores = [1.0] * len(words_boxes)
45-
# vis_img = vis(img, words_boxes, words_all, words_scores, font_path)
46-
# cv2.imwrite("vis_single.png", vis_img)

python/rapidocr/cal_rec_boxes/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __call__(
2828
for idx, (img, box) in enumerate(zip(imgs, dt_boxes)):
2929
direction = self.get_box_direction(box)
3030

31-
rec_txt = rec_res.line_txts[idx]
31+
rec_txt = rec_res.txts[idx]
3232
rec_word_info = rec_res.word_results[idx]
3333

3434
h, w = img.shape[:2]

python/rapidocr/ch_ppocr_cls/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class TextClsOutput:
2323
cls_res: Optional[List[Tuple[str, float]]] = None
2424
elapse: Optional[float] = None
2525

26+
def __len__(self):
27+
if self.img_list is None:
28+
return 0
29+
return len(self.img_list)
30+
2631

2732
class ClsPostProcess:
2833
def __init__(self, label_list: List[str]):

python/rapidocr/ch_ppocr_det/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class TextDetOutput:
1616
scores: Optional[Tuple[float]] = None
1717
elapse: float = 0.0
1818

19+
def __len__(self):
20+
if self.boxes is None:
21+
return 0
22+
return len(self.boxes)
23+
1924

2025
class DetPreProcess:
2126
def __init__(

python/rapidocr/ch_ppocr_rec/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def __call__(self, args: TextRecInput) -> TextRecOutput:
9090
elapse += time.perf_counter() - start_time
9191

9292
all_line_results, all_word_results = list(zip(*rec_res))
93-
line_txts, line_scores = list(zip(*all_line_results))
94-
return TextRecOutput(line_txts, line_scores, all_word_results, elapse)
93+
txts, scores = list(zip(*all_line_results))
94+
return TextRecOutput(txts, scores, all_word_results, elapse)
9595

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

python/rapidocr/ch_ppocr_rec/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ class TextRecInput:
2929

3030
@dataclass
3131
class TextRecOutput:
32-
line_txts: Optional[Tuple[str]] = None
33-
line_scores: Tuple[float] = (1.0,)
32+
txts: Optional[Tuple[str]] = None
33+
scores: Tuple[float] = (1.0,)
3434
word_results: Tuple[Tuple[str, float, Optional[List[List[int]]]]] = (
3535
("", 1.0, None),
3636
)
3737
elapse: Optional[float] = None
3838

39+
def __len__(self):
40+
if self.txts is None:
41+
return 0
42+
return len(self.txts)
43+
3944

4045
class CTCLabelDecode:
4146
def __init__(

python/rapidocr/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self, config_path: Optional[str] = None, **kwargs):
6464

6565
self.cal_rec_boxes = CalRecBoxes()
6666

67-
self.return_paddleocr_format = True
67+
self.return_paddleocr_format = False
6868

6969
def __call__(
7070
self,
@@ -255,7 +255,7 @@ def get_final_res(
255255
self, det_res: TextDetOutput, cls_res: TextClsOutput, rec_res: TextRecOutput
256256
) -> Union[TextDetOutput, TextClsOutput, TextRecOutput, RapidOCROutput]:
257257
dt_boxes = det_res.boxes
258-
txt_res = rec_res.line_txts
258+
txt_res = rec_res.txts
259259

260260
if dt_boxes is None and txt_res is None and cls_res.cls_res is not None:
261261
return cls_res
@@ -266,13 +266,13 @@ def get_final_res(
266266
if dt_boxes is None and txt_res is not None:
267267
return rec_res
268268

269-
if dt_boxes is not None and rec_res is None:
269+
if dt_boxes is not None and txt_res is None:
270270
return det_res
271271

272272
ocr_res = RapidOCROutput(
273273
boxes=det_res.boxes,
274-
txts=rec_res.line_txts,
275-
scores=rec_res.line_scores,
274+
txts=rec_res.txts,
275+
scores=rec_res.scores,
276276
word_results=rec_res.word_results,
277277
elapse_list=[det_res.elapse, cls_res.elapse, rec_res.elapse],
278278
)

python/rapidocr/utils/typings.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ class RapidOCROutput:
1717
elapse: float = field(init=False)
1818

1919
def __post_init__(self):
20-
self.elapse = sum(self.elapse_list)
20+
self.elapse = sum(v for v in self.elapse_list if isinstance(v, float))
21+
22+
def __len__(self):
23+
if self.txts is None:
24+
return 0
25+
return len(self.txts)
2126

2227
def to_json(self):
2328
pass
2429

2530
def to_paddleocr_format(self):
26-
"""Return format like:
31+
"""Refer: https://pypi.org/project/paddleocr/2.7.3/
32+
Return format like:
2733
[
2834
[[[6.0, 2.0], [322.0, 9.0], [320.0, 104.0], [4.0, 97.0]], ['正品促销', 0.99893]],
2935
[[[70.0, 98.0], [252.0, 98.0], [252.0, 125.0], [70.0, 125.0]], ['大桶装更划算', 0.9843]]

0 commit comments

Comments
 (0)