Skip to content

Commit 13fb8cc

Browse files
authored
fix: fixed issue #542 (#564)
* fix: fixed issue #542 * test: update the image of the test cls * test: update the ground truth of the test cls image
1 parent 32dad48 commit 13fb8cc

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

python/rapidocr/utils/load_image.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import cv2
99
import numpy as np
1010
import requests
11-
from PIL import Image, UnidentifiedImageError
11+
from PIL import Image, ImageOps, UnidentifiedImageError
1212

1313
from .utils import is_url
1414

@@ -33,12 +33,14 @@ def __call__(self, img: InputType) -> np.ndarray:
3333

3434
def load_img(self, img: InputType) -> np.ndarray:
3535
if isinstance(img, (str, Path)):
36-
if is_url(img):
36+
if is_url(str(img)):
3737
img = Image.open(requests.get(img, stream=True, timeout=60).raw)
3838
else:
3939
self.verify_exist(img)
4040
img = Image.open(img)
4141

42+
img = self.exif_transpose(img)
43+
4244
try:
4345
img = self.img_to_ndarray(img)
4446
except UnidentifiedImageError as e:
@@ -57,6 +59,21 @@ def load_img(self, img: InputType) -> np.ndarray:
5759

5860
raise LoadImageError(f"{type(img)} is not supported!")
5961

62+
@staticmethod
63+
def verify_exist(file_path: Union[str, Path]):
64+
if not Path(file_path).exists():
65+
raise LoadImageError(f"{file_path} does not exist.")
66+
67+
@staticmethod
68+
def exif_transpose(img: Image.Image) -> Image.Image:
69+
try:
70+
img_corrected = ImageOps.exif_transpose(img)
71+
if img_corrected is None:
72+
return img
73+
return img_corrected
74+
except Exception as e:
75+
return img
76+
6077
def img_to_ndarray(self, img: Image.Image) -> np.ndarray:
6178
if img.mode == "1":
6279
img = img.convert("L")
@@ -121,11 +138,6 @@ def cvt_four_to_three(img: np.ndarray) -> np.ndarray:
121138
new_img = cv2.bitwise_not(new_img)
122139
return new_img
123140

124-
@staticmethod
125-
def verify_exist(file_path: Union[str, Path]):
126-
if not Path(file_path).exists():
127-
raise LoadImageError(f"{file_path} does not exist.")
128-
129141

130142
class LoadImageError(Exception):
131143
pass

python/tests/test_det_cls_rec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_only_cls(engine):
3131
img_path = tests_dir / "text_cls.jpg"
3232
result = engine(img_path, use_det=False, use_cls=True, use_rec=False)
3333
assert len(result) == 1
34-
assert result.cls_res[0][0] == "0"
34+
assert result.cls_res[0][0] == "180"
3535

3636

3737
def test_only_rec(engine):
@@ -53,7 +53,7 @@ def test_cls_rec(engine):
5353

5454
assert result is not None
5555
assert len(result) == 1
56-
assert result.txts[0] == "韩国小馆"
56+
assert result.txts[0] == "怪我咯"
5757

5858

5959
def test_det_cls_rec(engine):
9.51 KB
Loading
-14.6 KB
Loading

python/tests/test_input.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
from rapidocr import LoadImageError, RapidOCR
1616

17-
tests_dir = root_dir / "tests" / "test_files"
18-
img_path = tests_dir / "ch_en_num.jpg"
17+
test_dir = root_dir / "tests" / "test_files"
18+
img_path = test_dir / "ch_en_num.jpg"
1919

2020

2121
@pytest.fixture()
@@ -24,6 +24,12 @@ def engine():
2424
return engine
2525

2626

27+
def test_exif_transpose(engine):
28+
img_path = test_dir / "img_exif_orientation.jpg"
29+
result = engine(img_path, use_cls=False)
30+
assert result.txts[0] == "我是中国人"
31+
32+
2733
@mark.parametrize(
2834
"img_name,gt",
2935
[
@@ -38,7 +44,7 @@ def engine():
3844
],
3945
)
4046
def test_transparent_img(engine, img_name: str, gt: str):
41-
img_path = tests_dir / img_name
47+
img_path = test_dir / img_name
4248
result = engine(img_path)
4349
assert result.txts[0] == gt
4450

@@ -52,7 +58,7 @@ def test_long_img(engine):
5258

5359

5460
def test_full_black_img(engine):
55-
img_path = tests_dir / "empty_black.jpg"
61+
img_path = test_dir / "empty_black.jpg"
5662
result = engine(img_path)
5763
assert result.img is None
5864
assert result.boxes is None
@@ -104,7 +110,7 @@ def test_input_parameters(engine):
104110

105111

106112
def test_input_three_ndim_two_channel(engine):
107-
img_npy = tests_dir / "two_dim_image.npy"
113+
img_npy = test_dir / "two_dim_image.npy"
108114
image_array = np.load(str(img_npy))
109115
result = engine(image_array)
110116

@@ -124,7 +130,7 @@ def test_input_three_ndim_one_channel(engine):
124130

125131

126132
def test_mode_one_img(engine):
127-
img_path = tests_dir / "issue_170.png"
133+
img_path = test_dir / "issue_170.png"
128134
result = engine(img_path)
129135
assert result.txts[0] == "TEST"
130136

@@ -141,7 +147,7 @@ def test_mode_one_img(engine):
141147
],
142148
)
143149
def test_letterbox_like(engine, img_name, gt_len, gt_first_len):
144-
img_path = tests_dir / img_name
150+
img_path = test_dir / img_name
145151
result = engine(img_path)
146152

147153
assert len(result) == gt_len

0 commit comments

Comments
 (0)