Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Fix LoadImage class bug

SWHL8e2508e0a667
diff --git a/python/rapidocr_onnxruntime/__init__.py b/python/rapidocr_onnxruntime/__init__.py
index 6522633..fd53856 100644
--- a/python/rapidocr_onnxruntime/__init__.py
+++ b/python/rapidocr_onnxruntime/__init__.py
@@ -2,3 +2,4 @@
# @Author: SWHL
# @Contact: [email protected]
from .rapid_ocr_api import RapidOCR
+from .utils import LoadImageError
diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py
index 4e01443..d6abdac 100644
--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py
+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py
@@ -20,8 +20,9 @@ from typing import List
import cv2
import numpy as np
+from rapidocr_onnxruntime.utils import OrtInferSession, read_yaml
+
from .utils import ClsPostProcess
-from rapidocr_onnxruntime.utils import read_yaml, OrtInferSession
class TextClassifier():
diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py
index 6d8b6b6..99f7a00 100644
--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py
+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py
@@ -20,8 +20,9 @@ import time
import cv2
import numpy as np
+from rapidocr_onnxruntime.utils import OrtInferSession, read_yaml
+
from .utils import DBPostProcess, create_operators, transform
-from rapidocr_onnxruntime.utils import read_yaml, OrtInferSession
class TextDetector():
diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py
index 953d42a..1ce14a6 100644
--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py
+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py
@@ -19,8 +19,9 @@ from typing import List
import cv2
import numpy as np
+from rapidocr_onnxruntime.utils import OrtInferSession, read_yaml
+
from .utils import CTCLabelDecode
-from rapidocr_onnxruntime.utils import read_yaml, OrtInferSession
class TextRecognizer():
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index abf0ce9..2dad7e9 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -3,7 +3,6 @@
# @Contact: [email protected]
import argparse
import warnings
-from io import BytesIO
from pathlib import Path
from typing import Union
@@ -61,7 +60,7 @@ class OrtInferSession():
try:
return self.session.run(self.get_output_names(), input_dict)
except Exception as e:
- raise ONNXRuntimeError('ONNXRuntime inferece failed.') from e
+ raise ONNXRuntimeError('ONNXRuntime inference failed.') from e
def get_input_names(self, ):
return [v.name for v in self.session.get_inputs()]
@@ -115,13 +114,16 @@ class LoadImage():
self.verify_exist(img)
try:
img = np.array(Image.open(img))
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
except UnidentifiedImageError as e:
raise LoadImageError(
f'cannot identify image file {img}') from e
return img
if isinstance(img, bytes):
- return np.array(Image.open(BytesIO(img)))
+ img = np.frombuffer(img, dtype=np.uint8)
+ img = cv2.imdecode(img, cv2.IMREAD_COLOR)
+ return img
if isinstance(img, np.ndarray):
return img
diff --git a/python/rapidocr_openvino/__init__.py b/python/rapidocr_openvino/__init__.py
index 5a34512..fd53856 100644
--- a/python/rapidocr_openvino/__init__.py
+++ b/python/rapidocr_openvino/__init__.py
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
-from .rapid_ocr_api import RapidOCR
\ No newline at end of file
+from .rapid_ocr_api import RapidOCR
+from .utils import LoadImageError
diff --git a/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py b/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py
index cdbcbd8..a397d35 100644
--- a/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py
+++ b/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py
@@ -20,8 +20,9 @@ from typing import List
import cv2
import numpy as np
+from rapidocr_openvino.utils import OpenVINOInferSession, read_yaml
+
from .utils import ClsPostProcess
-from rapidocr_openvino.utils import read_yaml, OpenVINOInferSession
class TextClassifier():
diff --git a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
index 439f326..5c50cbb 100644
--- a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
+++ b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
@@ -18,8 +18,9 @@ import time
import cv2
import numpy as np
+from rapidocr_openvino.utils import OpenVINOInferSession, read_yaml
+
from .utils import DBPostProcess, create_operators, transform
-from rapidocr_openvino.utils import read_yaml, OpenVINOInferSession
class TextDetector():
diff --git a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py
index 0a6d497..1bf6bb0 100644
--- a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py
+++ b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py
@@ -17,14 +17,15 @@
import argparse
import math
import time
-from typing import List
from pathlib import Path
+from typing import List
import cv2
import numpy as np
+from rapidocr_openvino.utils import OpenVINOInferSession, read_yaml
+
from .utils import CTCLabelDecode
-from rapidocr_openvino.utils import read_yaml, OpenVINOInferSession
class TextRecognizer():
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index c9b69e2..cdef222 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -2,7 +2,6 @@
# @Author: SWHL
# @Contact: [email protected]
import argparse
-from io import BytesIO
from pathlib import Path
from typing import Union
@@ -63,13 +62,16 @@ class LoadImage():
self.verify_exist(img)
try:
img = np.array(Image.open(img))
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
except UnidentifiedImageError as e:
raise LoadImageError(
f'cannot identify image file {img}') from e
return img
if isinstance(img, bytes):
- return np.array(Image.open(BytesIO(img)))
+ img = np.frombuffer(img, dtype=np.uint8)
+ img = cv2.imdecode(img, cv2.IMREAD_COLOR)
+ return img
if isinstance(img, np.ndarray):
return img
diff --git a/python/tests/test_all_ort.py b/python/tests/test_all_ort.py
index 08ba888..6267c41 100644
--- a/python/tests/test_all_ort.py
+++ b/python/tests/test_all_ort.py
@@ -10,10 +10,10 @@ import pytest
root_dir = Path(__file__).resolve().parent.parent
+print(root_dir)
sys.path.append(str(root_dir))
-from rapidocr_onnxruntime import RapidOCR
-from rapidocr_onnxruntime.utils import LoadImageError
+from rapidocr_onnxruntime import RapidOCR, LoadImageError
rapid_ocr = RapidOCR()
tests_dir = root_dir / 'tests' / 'test_files'
diff --git a/python/tests/test_all_vino.py b/python/tests/test_all_vino.py
index c222c22..5551632 100644
--- a/python/tests/test_all_vino.py
+++ b/python/tests/test_all_vino.py
@@ -12,8 +12,7 @@ import pytest
root_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(root_dir))
-from rapidocr_openvino import RapidOCR
-from rapidocr_openvino.utils import LoadImageError
+from rapidocr_openvino import RapidOCR, LoadImageError
rapid_ocr = RapidOCR()