paddleoc onnx转换推理

x33g5p2x  于2022-02-07 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(447)

paddle 文字识别验证代码:

onnx c++推理:

python onnx识别部分推理示例:

感谢博客:

PaddleOCR转ONNX模型(推理部分)_favorxin的博客-CSDN博客_paddleocr转onnx

paddle 文字识别验证代码:

  1. infer_rec.py

参见博客:

https://blog.csdn.net/jacke121/article/details/122647698

onnx c++推理:

参考项目:

A cross platform OCR Library based on PaddleOCR & OnnxRuntime

https://github.com/RapidAI/RapidOCR

库下载:

https://gitee.com/benjaminwan/ocr-lite-onnx/releases/v1.0

开源库:

https://github.com/RapidAI/RapidOCR/tree/main/cpp

c++推理示例代码:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <memory.h>
  5. #include <string>
  6. #include "../include/rapidocr_api.h"
  7. #define BPOCR_DET_MODEL "ch_ppocr_mobile_v2.0_det_infer.onnx"
  8. #define BPOCR_CLS_MODEL "ch_ppocr_mobile_v2.0_cls_infer.onnx"
  9. #define BPOCR_REC_MODEL "ch_ppocr_mobile_v2.0_rec_infer.onnx"
  10. #define BPOCR_KEY_PATH "ppocr_keys_v1.txt"
  11. #define THREAD_NUM 3
  12. #define MAX_PATH 260
  13. #ifdef WIN32
  14. const char* DEFAULT_MODEL_DIR = "E:\\bai-piao-ocr\\cpp\\BaiPiaoOcrOnnx\\models\\";
  15. const char* DEFAULT_TEST_IMG = "E:\\bai-piao-ocr\\cpp\\BaiPiaoOcrOnnx\\images\\long1.jpg";
  16. #else
  17. const char * DEFAULT_MODEL_DIR ="/data/workprj/RapidOCR/models/";
  18. const char * DEFAULT_TEST_IMG ="/data/workprj/RapidOCR/images/1.jpg";
  19. #endif
  20. int main(int argc, char * argv[])
  21. {
  22. const char *szModelDir = NULL;
  23. const char *szImagePath = NULL;
  24. if (argc == 1)
  25. {
  26. szModelDir = DEFAULT_MODEL_DIR;
  27. szImagePath = DEFAULT_TEST_IMG;
  28. }
  29. else
  30. {
  31. szModelDir = argv[1];
  32. szImagePath = argv[2];
  33. }
  34. char szDetModelPath[MAX_PATH] = { 0 };
  35. char szClsModelPath[MAX_PATH] = { 0 };
  36. char szRecModelPath[MAX_PATH] = { 0 };
  37. char szKeylPath[MAX_PATH] = { 0 };
  38. strcpy(szDetModelPath, szModelDir);
  39. strcpy(szClsModelPath, szModelDir);
  40. strcpy(szRecModelPath, szModelDir);
  41. strcpy(szKeylPath, szModelDir);
  42. strcat(szDetModelPath, BPOCR_DET_MODEL);
  43. strcat(szClsModelPath, BPOCR_CLS_MODEL);
  44. strcat(szRecModelPath, BPOCR_REC_MODEL);
  45. strcat(szKeylPath, BPOCR_KEY_PATH);
  46. BPHANDLE Handle = BPOcrInit(szDetModelPath, szClsModelPath, szRecModelPath, szKeylPath, THREAD_NUM);
  47. if (!Handle)
  48. {
  49. printf("cannot initialize the OCR Engine.\n");
  50. return -1;
  51. }
  52. RAPIDOCR_PARAM Param = { 0 };
  53. BOOL bRet = BPOcrDoOcr(Handle, szImagePath, true, false, &Param);
  54. if (bRet)
  55. {
  56. int nLen = BPOcrGetLen(Handle);
  57. if (nLen > 0)
  58. {
  59. char* szInfo = (char*)malloc(nLen);
  60. if (szInfo)
  61. {
  62. if (BPOcrGetResult(Handle, szInfo, nLen))
  63. {
  64. printf(szInfo);
  65. }
  66. free(szInfo);
  67. }
  68. }
  69. }
  70. if (Handle)
  71. {
  72. BPOcrDeinit(Handle);
  73. }
  74. return 0;
  75. }

python onnx识别部分推理示例:

  1. import math
  2. import cv2
  3. import onnxruntime
  4. import numpy as np
  5. small_rec_file="vc_rec_dynamic.onnx"
  6. onet_rec_session = onnxruntime.InferenceSession(small_rec_file)
  7. ## 根据推理结果解码识别结果
  8. class process_pred(object):
  9. def __init__(self, character_dict_path=None, character_type='ch', use_space_char=False):
  10. self.character_str = ''
  11. with open(character_dict_path, 'rb') as fin:
  12. lines = fin.readlines()
  13. for line in lines:
  14. line = line.decode('utf-8').strip('\n').strip('\r\n')
  15. self.character_str += line
  16. if use_space_char:
  17. self.character_str += ' '
  18. dict_character = list(self.character_str)
  19. dict_character = self.add_special_char(dict_character)
  20. self.dict = {}
  21. for i, char in enumerate(dict_character):
  22. self.dict[char] = i
  23. self.character = dict_character
  24. def add_special_char(self, dict_character):
  25. dict_character = ['blank'] + dict_character
  26. return dict_character
  27. def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
  28. result_list = []
  29. ignored_tokens = [0]
  30. batch_size = len(text_index)
  31. for batch_idx in range(batch_size):
  32. char_list = []
  33. conf_list = []
  34. for idx in range(len(text_index[batch_idx])):
  35. if text_index[batch_idx][idx] in ignored_tokens:
  36. continue
  37. if is_remove_duplicate:
  38. if idx > 0 and text_index[batch_idx][idx - 1] == text_index[batch_idx][idx]:
  39. continue
  40. char_list.append(self.character[int(text_index[batch_idx][idx])])
  41. if text_prob is not None:
  42. conf_list.append(text_prob[batch_idx][idx])
  43. else:
  44. conf_list.append(1)
  45. text = ''.join(char_list)
  46. result_list.append((text, np.mean(conf_list)))
  47. return result_list
  48. def __call__(self, preds, label=None):
  49. if not isinstance(preds, np.ndarray):
  50. preds = np.array(preds)
  51. preds_idx = preds.argmax(axis=2)
  52. preds_prob = preds.max(axis=2)
  53. text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
  54. if label is None:
  55. return text
  56. label = self.decode(label)
  57. return text, label
  58. postprocess_op = process_pred('../ppocr/utils/ppocr_keys_v1.txt', 'ch', True)
  59. def resize_norm_img(img, max_wh_ratio):
  60. imgC, imgH, imgW = [int(v) for v in "3, 32, 100".split(",")]
  61. assert imgC == img.shape[2]
  62. imgW = int((32 * max_wh_ratio))
  63. h, w = img.shape[:2]
  64. ratio = w / float(h)
  65. if math.ceil(imgH * ratio) > imgW:
  66. resized_w = imgW
  67. else:
  68. resized_w = int(math.ceil(imgH * ratio))
  69. resized_image = cv2.resize(img, (resized_w, imgH))
  70. resized_image = resized_image.astype('float32')
  71. resized_image = resized_image.transpose((2, 0, 1)) / 255
  72. resized_image -= 0.5
  73. resized_image /= 0.5
  74. padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
  75. padding_im[:, :, 0:resized_w] = resized_image
  76. return padding_im
  77. def get_img_res(onnx_model, img, process_op):
  78. h, w = img.shape[:2]
  79. img = resize_norm_img(img, w * 1.0 / h)
  80. img = img[np.newaxis, :]
  81. inputs = {onnx_model.get_inputs()[0].name: img}
  82. outs = onnx_model.run(None, inputs)
  83. result = process_op(outs[0])
  84. return result
  85. pic=cv2.imread(r"F:\project\jushi\data\shuini\ocr_crop\image\093807_148562_1.jpg")
  86. # pic=cv2.cvtColor(pic,cv2.COLOR_BGR2RGB)
  87. res = get_img_res(onet_rec_session, pic, postprocess_op)
  88. print(res)

相关文章