Skip to content

📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Support transparent white font image rec in rapidocr_onnxruntime

SWHL55bb974b5d93
diff --git a/python/demo.py b/python/demo.py
index 5e5d6f7..cd5cf16 100644
--- a/python/demo.py
+++ b/python/demo.py
@@ -12,7 +12,7 @@ from rapidocr_onnxruntime import RapidOCR, VisRes
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()
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index 778066d..181dd91 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -208,7 +208,12 @@ class LoadImage:
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
diff --git a/python/tests/test_files/black_font_color_transparent.png b/python/tests/test_files/black_font_color_transparent.png
new file mode 100644
index 0000000..dfd20a1
Binary files /dev/null and b/python/tests/test_files/black_font_color_transparent.png differ
diff --git a/python/tests/test_files/white_font_color_transparent.png b/python/tests/test_files/white_font_color_transparent.png
new file mode 100644
index 0000000..9f9f095
Binary files /dev/null and b/python/tests/test_files/white_font_color_transparent.png differ
diff --git a/python/tests/test_ort.py b/python/tests/test_ort.py
index ca0e993..45e8971 100644
--- a/python/tests/test_ort.py
+++ b/python/tests/test_ort.py
@@ -20,6 +20,25 @@ img_path = tests_dir / "ch_en_num.jpg"
package_name = "rapidocr_onnxruntime"
+ "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",
[