Skip to content

Commit

Permalink
Support transparent white font image rec in rapidocr_onnxruntime
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Mar 6, 2024
1 parent c594ed1 commit 55bb974
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
engine = RapidOCR()
vis = VisRes()

image_path = "tests/test_files/test_without_det.jpg"
image_path = "tests/test_files/black_font_color_transparent.png"
with open(image_path, "rb") as f:
img = f.read()

Expand Down
7 changes: 6 additions & 1 deletion python/rapidocr_onnxruntime/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ def cvt_four_to_three(img: np.ndarray) -> np.ndarray:
not_a = cv2.cvtColor(not_a, cv2.COLOR_GRAY2BGR)

new_img = cv2.bitwise_and(new_img, new_img, mask=a)
new_img = cv2.add(new_img, not_a)

mean_color = np.mean(new_img)
if mean_color <= 0.0:
new_img = cv2.add(new_img, not_a)
else:
new_img = cv2.bitwise_not(new_img)
return new_img

@staticmethod
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions python/tests/test_ort.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
package_name = "rapidocr_onnxruntime"


@pytest.mark.parametrize(
"img_name,gt",
[
(
"black_font_color_transparent.png",
"我是中国人",
),
(
"white_font_color_transparent.png",
"我是中国人",
),
],
)
def test_transparent_img(img_name: str, gt: str):
img_path = tests_dir / img_name
result, _ = engine(img_path)
assert result[0][1] == gt


@pytest.mark.parametrize(
"img_name,gt_len,gt_first_len",
[
Expand Down

0 comments on commit 55bb974

Please sign in to comment.