RapidOCR icon indicating copy to clipboard operation
RapidOCR copied to clipboard

Bug: Processing bitmap PNG image

Open HighDoping opened this issue 1 year ago • 3 comments

Error when passing bitmap PNG image with path. Can be solved by first reading the image using OpenCV and then passing the image to the OCR engine.

请提供下述完整信息以便快速定位问题 (Please provide the following information to quickly locate the problem)

  • 系统环境/System Environment: Microsoft Windows Version 22H2 (OS Build 19045.4291)
  • 使用的是哪门语言的程序/Which programing language: Python 3.11.6
  • OnnxRuntime版本/OnnxRuntime Version: 1.17.3
  • 使用当前库的版本/Use version: 1.3.16
  • 可复现问题的demo和文件/Demo of reproducible problems: 1
from pathlib import Path
from rapidocr_onnxruntime import RapidOCR
import cv2

img_path=Path(r"c:\Test\1.png")
print(img_path)

try:
    engine = RapidOCR()
    result, elapse = engine(img_path, use_det=True, use_cls=True, use_rec=True)
    print(result)
except Exception as e:
    print(e)

try:
    img= cv2.imread(img_path.as_posix())
    engine = RapidOCR()
    result, elapse = engine(img, use_det=True, use_cls=True, use_rec=True)
    print(result)
except Exception as e:
    print(e)
  • 完整报错/Complete Error Message:
Traceback (most recent call last):
  File "c:\Users\Luke\Documents\PicFinder\test.py", line 12, in <module>
    result, elapse = engine(img_path, use_det=True, use_cls=True, use_rec=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Luke\Documents\PicFinder\.venv\Lib\site-packages\rapidocr_onnxruntime\main.py", line 77, in __call__
    img = self.load_img(img_content)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Luke\Documents\PicFinder\.venv\Lib\site-packages\rapidocr_onnxruntime\utils.py", line 137, in __call__
    img = self.convert_img(img, origin_img_type)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Luke\Documents\PicFinder\.venv\Lib\site-packages\rapidocr_onnxruntime\utils.py", line 163, in convert_img
    return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src data type = bool is not supported
>  - Expected Ptr<cv::UMat> for argument 'src'
  • 可能的解决方案/Possible solutions:

The problem may be that Pillow processed bitmap png file is incompatible with OpenCV. Fix of load_img and convert_img functions in the utils.py needed.

HighDoping avatar Apr 18 '24 12:04 HighDoping

Looks good to me. Thanks.

SWHL avatar Apr 19 '24 01:04 SWHL

依然有这个问题,下面两个图片都错误类似

Image Image

cryptography           43.0.1
rapidocr               2.1.0
rapidocr-onnxruntime   1.3.25
wrapt                  1.17.0
onnx                   1.18.0
onnxruntime            1.22.0
opencv-python          4.6.0.66
opencv-python-headless 4.6.0.66

第一图

Traceback (most recent call last):
  File "/Users/DDDD/ocr.py", line 13, in <module>
    result, elapse = engine(Image.open(img), use_det=True, use_cls=True, use_rec=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/main.py", line 107, in __call__
    ori_img = self.load_img(img_content)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/utils/load_image.py", line 31, in __call__
    img = self.convert_img(img, origin_img_type)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/utils/load_image.py", line 80, in convert_img
    return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src'

第二图

Traceback (most recent call last):
  File "/Users/DDDD/ocr.py", line 13, in <module>
    result, elapse = engine(Image.open(img), use_det=True, use_cls=True, use_rec=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/main.py", line 107, in __call__
    ori_img = self.load_img(img_content)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/utils/load_image.py", line 31, in __call__
    img = self.convert_img(img, origin_img_type)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/utils/load_image.py", line 84, in convert_img
    return self.cvt_four_to_three(img)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/DDDD/miniconda3/envs/testttt/lib/python3.12/site-packages/rapidocr/utils/load_image.py", line 109, in cvt_four_to_three
    r, g, b, a = cv2.split(img)
                 ^^^^^^^^^^^^^^
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'split'
> Overload resolution failed:
>  - m is not a numpy array, neither a scalar

SeeFlowerX avatar May 27 '25 12:05 SeeFlowerX

readme demo的也是这样,不知道是不是和系统有关系,我是mac m1 arm64

SeeFlowerX avatar May 27 '25 12:05 SeeFlowerX

rapidocr==3.4.1版本中,不存在该问题了。

from rapidocr import RapidOCR

engine = RapidOCR()

img_url = "https://private-user-images.githubusercontent.com/70688581/447924683-1888acdb-0554-438f-936e-f3471aa7dca4.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjAxOTAwNzIsIm5iZiI6MTc2MDE4OTc3MiwicGF0aCI6Ii83MDY4ODU4MS80NDc5MjQ2ODMtMTg4OGFjZGItMDU1NC00MzhmLTkzNmUtZjM0NzFhYTdkY2E0LmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTEwMTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUxMDExVDEzMzYxMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY2OWVjNDRlMzgzYTFlNWE2ZjcyYjk0ZTk0MWFkYjRiMWZlYjJhMTkxODljOWY4YjJmMDZhNGZlMTIwYzQ1NTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.A9P4uAkgv9cslkfYC7p9eIwriRAJTxWQ71KboM1RaL0"
result = engine(img_url, return_word_box=True)
print(result)

result.vis("vis_result.jpg")
print(result)

我这里先关闭该问题了,后续有问题可以再开。

SWHL avatar Oct 11 '25 13:10 SWHL