InferNetOnnxPaddleOcrAngClsC.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include"InferNetOnnxPaddleOcrAngClsC.h"
  2. InferNetOnnxPaddleOcrAngCls::InferNetOnnxPaddleOcrAngCls()
  3. {
  4. }
  5. void InferNetOnnxPaddleOcrAngCls::LoadNetwork(const void* modelData, size_t modelDataLength)
  6. {
  7. if (_modelLoaded)
  8. {
  9. // 如果模型已加载,则释放之前的模型
  10. delete ort_session;
  11. ort_session = nullptr;
  12. }
  13. sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
  14. ort_session = new Session(env, modelData, modelDataLength, sessionOptions);
  15. size_t numInputNodes = ort_session->GetInputCount();
  16. size_t numOutputNodes = ort_session->GetOutputCount();
  17. AllocatorWithDefaultOptions allocator;
  18. for (int i = 0; i < numInputNodes; i++)
  19. {
  20. inputNames.push_back(ort_session->GetInputName(i, allocator));
  21. Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
  22. auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
  23. auto input_dims = input_tensor_info.GetShape();
  24. inputNodeDims.push_back(input_dims);
  25. }
  26. for (int i = 0; i < numOutputNodes; i++)
  27. {
  28. outputNames.push_back(ort_session->GetOutputName(i, allocator));
  29. Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
  30. auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
  31. auto output_dims = output_tensor_info.GetShape();
  32. outputNodeDims.push_back(output_dims);
  33. }
  34. numOut = outputNodeDims[0][1];
  35. _modelLoaded = true;
  36. }
  37. int InferNetOnnxPaddleOcrAngCls::Process(cv::Mat imageCv)
  38. {
  39. // 对输入图像进行预处理
  40. cv::Mat dstimg = this->preprocess(imageCv);
  41. this->normalize_(dstimg); // 对预处理后的图像进行归一化处理
  42. array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth }; // 定义输入张量的形状
  43. // 创建用于输入的张量
  44. auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
  45. Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
  46. // 开始推理
  47. std::vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &inputNames[0], &input_tensor_, 1, outputNames.data(), outputNames.size()); // 开始推理
  48. float* pdata = ort_outputs[0].GetTensorMutableData<float>();
  49. int clsReuslt;
  50. clsReuslt = PostProcess(pdata);
  51. return clsReuslt;
  52. }
  53. int InferNetOnnxPaddleOcrAngCls::PostProcess(float* pdataIn)
  54. {
  55. int maxId = 0;
  56. float maxProb = -1;
  57. for (int i = 0; i < numOut; i++)
  58. {
  59. if (pdataIn[i] > maxProb)
  60. {
  61. maxProb = pdataIn[i];
  62. maxId = i;
  63. }
  64. }
  65. return maxId;
  66. }
  67. void InferNetOnnxPaddleOcrAngCls::Dispose()
  68. {
  69. // 释放 ort_session 对象
  70. if (ort_session != nullptr)
  71. {
  72. delete ort_session;
  73. ort_session = nullptr;
  74. }
  75. }
  76. cv::Mat InferNetOnnxPaddleOcrAngCls::preprocess(cv::Mat srcimg)
  77. {
  78. cv::Mat dstimg;
  79. int h = srcimg.rows;
  80. int w = srcimg.cols;
  81. const float ratio = w / float(h);
  82. int resized_w = int(ceil((float)this->inpHeight * ratio));
  83. if (ceil(this->inpHeight * ratio) > this->inpWidth)
  84. {
  85. resized_w = this->inpWidth;
  86. }
  87. resize(srcimg, dstimg, Size(resized_w, this->inpHeight), INTER_LINEAR);
  88. return dstimg;
  89. }
  90. void InferNetOnnxPaddleOcrAngCls::normalize_(cv::Mat img)
  91. {
  92. // img.convertTo(img, CV_32F);
  93. int row = img.rows;
  94. int col = img.cols;
  95. this->input_image_.resize(this->inpHeight * this->inpWidth * img.channels());
  96. for (int c = 0; c < 3; c++)
  97. {
  98. for (int i = 0; i < row; i++)
  99. {
  100. for (int j = 0; j < inpWidth; j++)
  101. {
  102. if (j < col)
  103. {
  104. float pix = img.ptr<uchar>(i)[j * 3 + c];
  105. this->input_image_[c * row * inpWidth + i * inpWidth + j] = (pix / 255.0 - 0.5) / 0.5;
  106. }
  107. else
  108. {
  109. this->input_image_[c * row * inpWidth + i * inpWidth + j] = 0;
  110. }
  111. }
  112. }
  113. }
  114. }