Skip to content

📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Update ocrweb config

SWHL422644160cb5
diff --git a/README.md b/README.md
index 7e15bc9..2456acc 100755
--- a/README.md
+++ b/README.md
@@ -191,7 +191,8 @@
## 致谢
- 非常感谢[DeliciaLaniD](https://github.com/DeliciaLaniD)修复ocrweb中扫描动画起始位置错位问题。
-- 非常感谢[zhsunlight](https://github.com/zhsunlight)关于参数化调用GPU推理的建议。
+- 非常感谢[zhsunlight](https://github.com/zhsunlight)关于参数化调用GPU推理的建议以及细致周到的测试。
+- 非常感谢[lzh111222334](https://github.com/lzh111222334)修复python版本下rec前处理部分bug。
## 版权声明
- 如果你的产品使用了本仓库中的全部或部分代码、文字或材料
diff --git a/ocrweb/config.yaml b/ocrweb/config.yaml
index 891d010..a55a535 100644
--- a/ocrweb/config.yaml
+++ b/ocrweb/config.yaml
@@ -57,7 +57,7 @@ Cls:
Rec:
module_name: ch_ppocr_v2_rec
class_name: TextRecognizer
- model_path: resources/models/ch_ppocr_mobile_v2.0_rec_infer.onnx
+ model_path: resources/models/ch_PP-OCRv3_rec_infer.onnx
use_cuda: false
CUDAExecutionProvider:
@@ -66,6 +66,6 @@ Rec:
cudnn_conv_algo_search: EXHAUSTIVE
do_copy_in_default_stream: true
- rec_img_shape: [3, 32, 320]
+ rec_img_shape: [3, 48, 320]
rec_batch_num: 6
keys_path: resources/rec_dict/ppocr_keys_v1.txt
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/README.md b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/README.md
deleted file mode 100644
index cfef298..0000000
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/README.md
+++ /dev/null
@@ -1,9 +0,0 @@
-
-#### 简单对比
-
-|输入尺寸|前处理函数|模型|测试集|Score|Exact_Match|Char_Match|Rec_Speed(s/条)|
-|---|---|---|---|---|---|---|---|
-|48 |resize_norm_img_svtr |ch_PP-OCRv3_rec_infer.onnx|中英印刷体|0.5011|0.3571|0.6450|0.0203|
-|48 |resize_norm_img |ch_PP-OCRv3_rec_infer.onnx|中英印刷体|0.7248|0.5417|0.9079|0.0209|
-|32 | resize_norm_img_svtr |ch_PP-OCRv3_rec_infer.onnx|中英印刷体|0.3019|0.1607|0.4431|0.0161|
-|32| resize_norm_img |ch_PP-OCRv3_rec_infer.onnx|中英印刷体|0.5537|0.3274|0.7800|0.0152|
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/__init__.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/__init__.py
deleted file mode 100644
index ea54cd3..0000000
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/__init__.py
+++ /dev/null
@@ -1,4 +0,0 @@
-# -*- encoding: utf-8 -*-
-# @Author: SWHL
-# @Contact: [email protected]
-from .text_recognize import TextRecognizer
\ No newline at end of file
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml
deleted file mode 100644
index 2428429..0000000
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml
+++ /dev/null
@@ -1,14 +0,0 @@
-model_path: resources/models/PPOCRv3/ch_PP-OCRv3_rec_infer.onnx
-
-use_cuda: false
-# Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html
-CUDAExecutionProvider:
- device_id: 0
- arena_extend_strategy: kNextPowerOfTwo
- gpu_mem_limit: 2 * 1024 * 1024 * 1024
- cudnn_conv_algo_search: EXHAUSTIVE
- do_copy_in_default_stream: true
-
-rec_img_shape: [3, 48, 320]
-rec_batch_num: 6
-keys_path: resources/rec_dict/ppocr_keys_v1.txt
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py
deleted file mode 100644
index b00bf95..0000000
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py
+++ /dev/null
@@ -1,130 +0,0 @@
-# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-import argparse
-import math
-import time
-from pathlib import Path
-from typing import List
-
-import cv2
-import numpy as np
-import onnxruntime
-
-try:
- from .utils import CTCLabelDecode, read_yaml, OrtInferSession
-except:
- from utils import CTCLabelDecode, read_yaml, OrtInferSession
-
-root_path = Path(__file__).parent
-
-
-class TextRecognizer(object):
- def __init__(self, config):
- self.rec_image_shape = config['rec_img_shape']
- self.rec_batch_num = config['rec_batch_num']
- self.character_dict_path = config['keys_path']
- self.postprocess_op = CTCLabelDecode(self.character_dict_path)
-
- session_instance = OrtInferSession(config)
- self.session = session_instance.session
- self.input_name = session_instance.get_input_name()
-
- def resize_norm_img(self, img, max_wh_ratio):
- img_channel, img_height, img_width = self.rec_image_shape
- assert img_channel == img.shape[2]
-
- img_width = int((32 * max_wh_ratio))
- max_wh_ratio = 1
-
- h, w = img.shape[:2]
- ratio = w / float(h)
- if math.ceil(img_height * ratio) > img_width:
- resized_w = img_width
- else:
- resized_w = int(math.ceil(img_height * ratio))
-
- resized_image = cv2.resize(img, (resized_w, img_height))
- resized_image = resized_image.astype('float32')
- resized_image = resized_image.transpose((2, 0, 1)) / 255
- resized_image -= 0.5
- resized_image /= 0.5
-
- padding_im = np.zeros((img_channel, img_height, img_width),
- dtype=np.float32)
- padding_im[:, :, 0:resized_w] = resized_image
- return padding_im
-
- def resize_norm_img_svtr(self, img, max_wh_ratio=None):
- img_c, img_h, img_w = self.rec_image_shape
- resized_image = cv2.resize(img, (img_w, img_h),
- interpolation=cv2.INTER_LINEAR)
-
- resized_image = resized_image.astype('float32')
- resized_image = resized_image.transpose((2, 0, 1)) / 255
- resized_image -= 0.5
- resized_image /= 0.5
- return resized_image
-
- def __call__(self, img_list: List[np.ndarray]):
- if isinstance(img_list, np.ndarray):
- img_list = [img_list]
-
- # Calculate the aspect ratio of all text bars
- width_list = [img.shape[1] / float(img.shape[0]) for img in img_list]
-
- # Sorting can speed up the recognition process
- indices = np.argsort(np.array(width_list))
-
- img_num = len(img_list)
- rec_res = [['', 0.0]] * img_num
-
- batch_num = self.rec_batch_num
- elapse = 0
- for beg_img_no in range(0, img_num, batch_num):
- end_img_no = min(img_num, beg_img_no + batch_num)
- max_wh_ratio = 0
- for ino in range(beg_img_no, end_img_no):
- h, w = img_list[indices[ino]].shape[0:2]
- wh_ratio = w * 1.0 / h
- max_wh_ratio = max(max_wh_ratio, wh_ratio)
-
- norm_img_batch = []
- for ino in range(beg_img_no, end_img_no):
- norm_img = self.resize_norm_img_svtr(img_list[indices[ino]])
- norm_img_batch.append(norm_img[np.newaxis, :])
- norm_img_batch = np.concatenate(norm_img_batch).astype(np.float32)
-
- starttime = time.time()
- onnx_inputs = {self.input_name: norm_img_batch}
- preds = self.session.run(None, onnx_inputs)[0]
- rec_result = self.postprocess_op(preds)
-
- for rno in range(len(rec_result)):
- rec_res[indices[beg_img_no + rno]] = rec_result[rno]
- elapse += time.time() - starttime
- return rec_res, elapse
-
-
-if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.add_argument('--image_path', type=str, help='image_dir|image_path')
- parser.add_argument('--config_path', type=str, default='config.yaml')
- args = parser.parse_args()
-
- config = read_yaml(args.config_path)
- text_recognizer = TextRecognizer(config)
-
- img = cv2.imread(args.image_path)
- rec_res, predict_time = text_recognizer(img)
- print(f'识别结果: {rec_res}\t cost: {predict_time}s')
diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py
deleted file mode 100644
index 9e0d436..0000000
--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py
+++ /dev/null
@@ -1,115 +0,0 @@
-# -*- encoding: utf-8 -*-
-# @Author: SWHL
-# @Contact: [email protected]
-import warnings
-
-import numpy as np
-import yaml
-from onnxruntime import (InferenceSession, SessionOptions,
- get_available_providers, get_device)
-
-
-class OrtInferSession(object):
- def __init__(self, config):
- sess_opt = SessionOptions()
- sess_opt.log_severity_level = 4
- sess_opt.enable_cpu_mem_arena = False
-
- cuda_ep = 'CUDAExecutionProvider'
- cpu_ep = 'CPUExecutionProvider'
-
- EP_list = []
- if config['use_cuda'] and get_device() == 'GPU' \
- and cuda_ep in get_available_providers():
- EP_list = [(cuda_ep, config[cuda_ep])]
- EP_list.append(cpu_ep)
-
- self.session = InferenceSession(config['model_path'],
- sess_options=sess_opt,
- providers=EP_list)
-
- if config['use_cuda'] and cuda_ep not in self.session.get_providers():
- warnings.warn(f'{cuda_ep} is not avaiable for current env, the inference part is automatically shifted to be executed under {cpu_ep}.\n'
- 'Please ensure the installed onnxruntime-gpu version matches your cuda and cudnn version, '
- 'you can check their relations from the offical web site: '
- 'https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html',
- RuntimeWarning)
-
- def get_input_name(self, input_idx=0):
- return self.session.get_inputs()[input_idx].name
-
- def get_output_name(self, output_idx=0):
- return self.session.get_outputs()[output_idx].name
-
-
-def read_yaml(yaml_path):
- with open(yaml_path, 'rb') as f:
- data = yaml.load(f, Loader=yaml.Loader)
- return data
-
-
-class CTCLabelDecode(object):
- """ Convert between text-label and text-index """
-
- def __init__(self, character_dict_path):
- super(CTCLabelDecode, self).__init__()
-
- self.character_str = []
- assert character_dict_path is not None, "character_dict_path should not be None"
- with open(character_dict_path, "rb") as fin:
- lines = fin.readlines()
- for line in lines:
- line = line.decode('utf-8').strip("\n").strip("\r\n")
- self.character_str.append(line)
- self.character_str.append(' ')
-
- dict_character = self.add_special_char(self.character_str)
- self.character = dict_character
-
- self.dict = {}
- for i, char in enumerate(dict_character):
- self.dict[char] = i
-
- def __call__(self, preds, label=None):
- preds_idx = preds.argmax(axis=2)
- preds_prob = preds.max(axis=2)
- text = self.decode(preds_idx, preds_prob,
- is_remove_duplicate=True)
- if label is None:
- return text
- label = self.decode(label)
- return text, label
-
- def add_special_char(self, dict_character):
- dict_character = ['blank'] + dict_character
- return dict_character
-
- def get_ignored_tokens(self):
- return [0] # for ctc blank
-
- def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
- """ convert text-index into text-label. """
-
- result_list = []
- ignored_tokens = self.get_ignored_tokens()
- batch_size = len(text_index)
- for batch_idx in range(batch_size):
- char_list = []
- conf_list = []
- for idx in range(len(text_index[batch_idx])):
- if text_index[batch_idx][idx] in ignored_tokens:
- continue
- if is_remove_duplicate:
- # only for predict
- if idx > 0 and text_index[batch_idx][idx - 1] == text_index[
- batch_idx][idx]:
- continue
- char_list.append(self.character[int(text_index[batch_idx][
- idx])])
- if text_prob is not None:
- conf_list.append(text_prob[batch_idx][idx])
- else:
- conf_list.append(1)
- text = ''.join(char_list)
- result_list.append((text, np.mean(conf_list)))
- return result_list