Skip to content

Commit 32c705b

Browse files
authored
fix: fixed error of showing cls result (#621)
* fix: fixed error of showing cls result Related #616 * refactor(cls/utils): use cv2.ROTATE_180 and add type hint
1 parent 8487133 commit 32c705b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

python/rapidocr/ch_ppocr_cls/utils.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pathlib import Path
1616
from typing import List, Optional, Tuple, Union
1717

18+
import cv2
1819
import numpy as np
1920

2021
from ..utils.log import logger
@@ -38,17 +39,32 @@ def vis(self, save_path: Optional[Union[str, Path]] = None) -> Optional[np.ndarr
3839
logger.warning("No image or txts to visualize.")
3940
return None
4041

42+
vis = VisRes()
43+
4144
txts = [f"{txt} {score:.2f}" for txt, score in self.cls_res]
42-
scores = [score for _, score in self.cls_res]
45+
img_degrees, scores = list(zip(*self.cls_res))
4346

44-
vis = VisRes()
45-
vis_img = vis.draw_rec_res(self.img_list, txts, scores)
47+
raw_img_list = self.restore_image_orientation(self.img_list, img_degrees)
48+
vis_img = vis.draw_rec_res(raw_img_list, txts, list(scores))
4649

4750
if save_path is not None:
4851
save_img(save_path, vis_img)
4952
logger.info("Visualization saved as %s", save_path)
5053
return vis_img
5154

55+
def restore_image_orientation(
56+
self, img_list: List[np.ndarray], img_degrees: Tuple[str]
57+
) -> List[np.ndarray]:
58+
results = []
59+
for img, rotate_degree in zip(img_list, img_degrees):
60+
if rotate_degree != "180":
61+
results.append(img)
62+
continue
63+
64+
rotate_img = cv2.rotate(img, cv2.ROTATE_180)
65+
results.append(rotate_img)
66+
return results
67+
5268

5369
class ClsPostProcess:
5470
def __init__(self, label_list: List[str]):

0 commit comments

Comments
 (0)