Skip to content

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

Fixed issue #116 and update the issue template

SWHL436a9f6b6899
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
new file mode 100644
index 0000000..2e74434
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -0,0 +1,11 @@
+blank_issues_enabled: false
+contact_links:
+ - name: ❓ Questions
+ url: https://github.com/RapidAI/RapidOCR/discussions/categories/q-a
+ about: Please use the community forum for help and questions regarding RapidOCR.
+ - name: πŸ’‘ Feature requests and ideas
+ url: https://github.com/RapidAI/RapidOCR/discussions/categories/ideas
+ about: Please vote for and post new feature ideas in the community forum.
+ - name: πŸ“– Documentation
+ url: https://github.com/RapidAI/RapidOCR/wiki
+ about: A great place to find instructions and answers about RapidOCR.
diff --git a/python/docs/page2.pdf b/python/docs/page2.pdf
deleted file mode 100644
index bdc689e..0000000
Binary files a/python/docs/page2.pdf and /dev/null differ
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index edbeaae..6f75b0e 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -120,17 +120,7 @@ class LoadImage:
)
img = self.load_img(img)
-
- if img.ndim == 2:
- return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
-
- if img.ndim == 3:
- if img.shape[2] == 4:
- return self.cvt_four_to_three(img)
-
- if img.shape[2] == 2:
- return self.cvt_two_to_three(img)
-
+ img = self.convert_img(img)
return img
def load_img(self, img: InputType) -> np.ndarray:
@@ -138,22 +128,43 @@ 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):
img = np.array(Image.open(BytesIO(img)))
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
if isinstance(img, np.ndarray):
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
raise LoadImageError(f"{type(img)} is not supported!")
+ def convert_img(self, img: np.ndarray):
+ if img.ndim == 2:
+ return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
+
+ if img.ndim == 3:
+ channel = img.shape[2]
+ if channel == 1:
+ return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
+
+ if channel == 2:
+ return self.cvt_two_to_three(img)
+
+ if channel == 4:
+ return self.cvt_four_to_three(img)
+
+ if channel == 3:
+ return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
+
+ raise LoadImageError(
+ f"The channel({channel}) of the img is not in [1, 2, 3, 4]"
+ )
+
+ raise LoadImageError(f"The ndim({img.ndim}) of the img is not in [2, 3]")
+
@staticmethod
def cvt_four_to_three(img: np.ndarray) -> np.ndarray:
"""RGBA β†’ BGR"""
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index c8bab09..0fca484 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -52,17 +52,7 @@ class LoadImage:
)
img = self.load_img(img)
-
- if img.ndim == 2:
- return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
-
- if img.ndim == 3:
- if img.shape[2] == 4:
- return self.cvt_four_to_three(img)
-
- if img.shape[2] == 2:
- return self.cvt_two_to_three(img)
-
+ img = self.convert_img(img)
return img
def load_img(self, img: InputType) -> np.ndarray:
@@ -70,14 +60,12 @@ 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):
img = np.array(Image.open(BytesIO(img)))
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
if isinstance(img, np.ndarray):
@@ -85,6 +73,30 @@ class LoadImage:
raise LoadImageError(f"{type(img)} is not supported!")
+ def convert_img(self, img: np.ndarray):
+ if img.ndim == 2:
+ return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
+
+ if img.ndim == 3:
+ channel = img.shape[2]
+ if channel == 1:
+ return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
+
+ if channel == 2:
+ return self.cvt_two_to_three(img)
+
+ if channel == 4:
+ return self.cvt_four_to_three(img)
+
+ if channel == 3:
+ return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
+
+ raise LoadImageError(
+ f"The channel({channel}) of the img is not in [1, 2, 3, 4]"
+ )
+
+ raise LoadImageError(f"The ndim({img.ndim}) of the img is not in [2, 3]")
+
@staticmethod
def cvt_four_to_three(img: np.ndarray) -> np.ndarray:
"""RGBA β†’ BGR"""
diff --git a/python/res.png b/python/res.png
deleted file mode 100644
index dd2706c..0000000
Binary files a/python/res.png and /dev/null differ
diff --git a/python/test.py b/python/test.py
deleted file mode 100644
index 934d7cb..0000000
--- a/python/test.py
+++ /dev/null
@@ -1,20 +0,0 @@
-import fitz
-import numpy as np
-
-from rapidocr_onnxruntime import RapidOCR
-
-if __name__ == "__main__":
- ocr = RapidOCR()
- for pdf_path in ["docs/page2.pdf"]:
- doc = fitz.open(pdf_path)
- for page in doc:
- img_list = page.get_images()
- for img in img_list:
- pix = fitz.Pixmap(doc, img[0])
- img_array = np.frombuffer(pix.samples, dtype=np.uint8).reshape(
- pix.height, pix.width, -1
- )
- result, _ = ocr(img_array)
- if result:
- ocr_result = [line[1] for line in result]
- print(ocr_result)
diff --git a/python/tests/test_all_ort.py b/python/tests/test_all_ort.py
index 80722af..718610e 100644
--- a/python/tests/test_all_ort.py
+++ b/python/tests/test_all_ort.py
@@ -19,8 +19,10 @@ tests_dir = root_dir / "tests" / "test_files"
def test_normal():
- image_path = tests_dir / "ch_en_num.jpg"
- img = cv2.imread(str(image_path))
+ img_path = tests_dir / "ch_en_num.jpg"
+ img = cv2.imread(str(img_path))
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+
result, _ = rapid_ocr(img)
assert result[0][1] == "正品促销"
assert len(result) == 16
@@ -35,71 +37,84 @@ def test_empty():
def test_zeros():
- img = np.zeros([640, 640, 3])
+ img = np.zeros([640, 640, 3], np.uint8)
result, _ = rapid_ocr(img)
assert result is None
def test_input_str():
- image_path = tests_dir / "ch_en_num.jpg"
- result, _ = rapid_ocr(str(image_path))
+ img_path = tests_dir / "ch_en_num.jpg"
+ result, _ = rapid_ocr(str(img_path))
assert result[0][1] == "正品促销"
assert len(result) == 16
def test_input_bytes():
- image_path = tests_dir / "ch_en_num.jpg"
- with open(image_path, "rb") as f:
+ img_path = tests_dir / "ch_en_num.jpg"
+ with open(img_path, "rb") as f:
result, _ = rapid_ocr(f.read())
assert result[0][1] == "正品促销"
assert len(result) == 16
def test_input_path():
- image_path = tests_dir / "ch_en_num.jpg"
- result, _ = rapid_ocr(image_path)
+ img_path = tests_dir / "ch_en_num.jpg"
+ result, _ = rapid_ocr(img_path)
assert result[0][1] == "正品促销"
assert len(result) == 16
def test_input_parameters():
- image_path = tests_dir / "ch_en_num.jpg"
+ img_path = tests_dir / "ch_en_num.jpg"
rapid_ocr = RapidOCR(text_score=1)
- result, _ = rapid_ocr(image_path)
+ result, _ = rapid_ocr(img_path)
assert result is None
def test_input_det_parameters():
- image_path = tests_dir / "ch_en_num.jpg"
+ img_path = tests_dir / "ch_en_num.jpg"
with pytest.raises(FileNotFoundError) as exc_info:
rapid_ocr = RapidOCR(det_model_path="1.onnx")
- result, _ = rapid_ocr(image_path)
+ result, _ = rapid_ocr(img_path)
raise FileNotFoundError()
assert exc_info.type is FileNotFoundError
def test_input_cls_parameters():
- image_path = tests_dir / "ch_en_num.jpg"
+ img_path = tests_dir / "ch_en_num.jpg"
with pytest.raises(FileNotFoundError) as exc_info:
rapid_ocr = RapidOCR(cls_model_path="1.onnx")
- result, _ = rapid_ocr(image_path)
+ result, _ = rapid_ocr(img_path)
raise FileNotFoundError()
assert exc_info.type is FileNotFoundError
def test_input_rec_parameters():
- image_path = tests_dir / "ch_en_num.jpg"
+ img_path = tests_dir / "ch_en_num.jpg"
with pytest.raises(FileNotFoundError) as exc_info:
rapid_ocr = RapidOCR(rec_model_path="1.onnx")
- result, _ = rapid_ocr(image_path)
+ result, _ = rapid_ocr(img_path)
raise FileNotFoundError()
assert exc_info.type is FileNotFoundError
-def test_input_two_ndim():
+def test_input_three_ndim_two_channel():
img_npy = tests_dir / "two_dim_image.npy"
image_array = np.load(str(img_npy))
result, _ = rapid_ocr(image_array)
assert len(result) == 87
+
+
+def test_input_three_ndim_one_channel():
+ img_path = tests_dir / "ch_en_num.jpg"
+ img = cv2.imread(str(img_path))
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+ img = img[:, :, 0]
+ img = img[..., None] # (H, W, 1)
+
+ result, _ = rapid_ocr(img)
+
+ assert result[0][1] == "正品促销"
+ assert len(result) == 16
diff --git a/python/tests/test_all_vino.py b/python/tests/test_all_vino.py
index 5175bfb..cf74e6a 100644
--- a/python/tests/test_all_vino.py
+++ b/python/tests/test_all_vino.py
@@ -21,6 +21,8 @@ tests_dir = root_dir / "tests" / "test_files"
def test_normal():
image_path = tests_dir / "ch_en_num.jpg"
img = cv2.imread(str(image_path))
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+
result, _ = rapid_ocr(img)
assert result[0][1] == "正品促销"
assert len(result) == 16
@@ -35,7 +37,7 @@ def test_empty():
def test_zeros():
- img = np.zeros([640, 640, 3])
+ img = np.zeros([640, 640, 3], np.uint8)
result, _ = rapid_ocr(img)
assert result is None
@@ -95,11 +97,3 @@ def test_input_rec_parameters():
result, _ = rapid_ocr(image_path)
raise FileNotFoundError()
assert exc_info.type is FileNotFoundError
-
-
-def test_input_two_ndim():
- img_npy = tests_dir / "two_dim_image.npy"
- image_array = np.load(str(img_npy))
- result, _ = rapid_ocr(image_array)
-
- assert len(result) == 87