diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py index ad2090d..7c03cff 100644 --- a/python/rapidocr_openvino/utils.py +++ b/python/rapidocr_openvino/utils.py @@ -135,7 +135,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/rapidocr_paddle/utils.py b/python/rapidocr_paddle/utils.py index cc4a339..78476fb 100644 --- a/python/rapidocr_paddle/utils.py +++ b/python/rapidocr_paddle/utils.py @@ -217,7 +217,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_paddle.py b/python/tests/test_paddle.py index 403d183..91edb99 100644 --- a/python/tests/test_paddle.py +++ b/python/tests/test_paddle.py @@ -20,6 +20,25 @@ img_path = tests_dir / "ch_en_num.jpg" package_name = "rapidocr_paddle" +@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", [ diff --git a/python/tests/test_vino.py b/python/tests/test_vino.py index 3ea998e..113f17d 100644 --- a/python/tests/test_vino.py +++ b/python/tests/test_vino.py @@ -20,6 +20,25 @@ img_path = tests_dir / "ch_en_num.jpg" package_name = "rapidocr_openvino" +@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", [