Skip to content

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

Fix LoadImage error when bytes comes from gif

SWHLf68f98b0952d
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index 2dad7e9..4952d7d 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -3,6 +3,7 @@
# @Contact: [email protected]
import argparse
import warnings
+from io import BytesIO
from pathlib import Path
from typing import Union
@@ -121,8 +122,8 @@ class LoadImage():
return img
if isinstance(img, bytes):
- img = np.frombuffer(img, dtype=np.uint8)
- img = cv2.imdecode(img, cv2.IMREAD_COLOR)
+ img = np.array(Image.open(BytesIO(img)))
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
if isinstance(img, np.ndarray):
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index cdef222..4f01f38 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -2,6 +2,7 @@
# @Author: SWHL
# @Contact: [email protected]
import argparse
+from io import BytesIO
from pathlib import Path
from typing import Union
@@ -69,8 +70,8 @@ class LoadImage():
return img
if isinstance(img, bytes):
- img = np.frombuffer(img, dtype=np.uint8)
- img = cv2.imdecode(img, cv2.IMREAD_COLOR)
+ img = np.array(Image.open(BytesIO(img)))
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
if isinstance(img, np.ndarray):