123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include"InferNetOnnxPaddleOcrAngClsC.h"
- InferNetOnnxPaddleOcrAngCls::InferNetOnnxPaddleOcrAngCls()
- {
- }
- void InferNetOnnxPaddleOcrAngCls::LoadNetwork(const void* modelData, size_t modelDataLength)
- {
- if (_modelLoaded)
- {
- // 如果模型已加载,则释放之前的模型
- delete ort_session;
- ort_session = nullptr;
- }
- sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
- ort_session = new Session(env, modelData, modelDataLength, sessionOptions);
- size_t numInputNodes = ort_session->GetInputCount();
- size_t numOutputNodes = ort_session->GetOutputCount();
- AllocatorWithDefaultOptions allocator;
- for (int i = 0; i < numInputNodes; i++)
- {
- inputNames.push_back(ort_session->GetInputName(i, allocator));
- Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
- auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
- auto input_dims = input_tensor_info.GetShape();
- inputNodeDims.push_back(input_dims);
- }
- for (int i = 0; i < numOutputNodes; i++)
- {
- outputNames.push_back(ort_session->GetOutputName(i, allocator));
- Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
- auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
- auto output_dims = output_tensor_info.GetShape();
- outputNodeDims.push_back(output_dims);
- }
- numOut = outputNodeDims[0][1];
- _modelLoaded = true;
- }
- int InferNetOnnxPaddleOcrAngCls::Process(cv::Mat imageCv)
- {
- // 对输入图像进行预处理
- cv::Mat dstimg = this->preprocess(imageCv);
- this->normalize_(dstimg); // 对预处理后的图像进行归一化处理
- array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth }; // 定义输入张量的形状
- // 创建用于输入的张量
- auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
- Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
- // 开始推理
- std::vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &inputNames[0], &input_tensor_, 1, outputNames.data(), outputNames.size()); // 开始推理
- float* pdata = ort_outputs[0].GetTensorMutableData<float>();
- int clsReuslt;
- clsReuslt = PostProcess(pdata);
- return clsReuslt;
- }
- int InferNetOnnxPaddleOcrAngCls::PostProcess(float* pdataIn)
- {
- int maxId = 0;
- float maxProb = -1;
- for (int i = 0; i < numOut; i++)
- {
- if (pdataIn[i] > maxProb)
- {
- maxProb = pdataIn[i];
- maxId = i;
- }
- }
- return maxId;
- }
- void InferNetOnnxPaddleOcrAngCls::Dispose()
- {
- // 释放 ort_session 对象
- if (ort_session != nullptr)
- {
- delete ort_session;
- ort_session = nullptr;
- }
- }
- cv::Mat InferNetOnnxPaddleOcrAngCls::preprocess(cv::Mat srcimg)
- {
- cv::Mat dstimg;
- int h = srcimg.rows;
- int w = srcimg.cols;
- const float ratio = w / float(h);
- int resized_w = int(ceil((float)this->inpHeight * ratio));
- if (ceil(this->inpHeight * ratio) > this->inpWidth)
- {
- resized_w = this->inpWidth;
- }
- resize(srcimg, dstimg, Size(resized_w, this->inpHeight), INTER_LINEAR);
- return dstimg;
- }
- void InferNetOnnxPaddleOcrAngCls::normalize_(cv::Mat img)
- {
- // img.convertTo(img, CV_32F);
- int row = img.rows;
- int col = img.cols;
- this->input_image_.resize(this->inpHeight * this->inpWidth * img.channels());
- for (int c = 0; c < 3; c++)
- {
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < inpWidth; j++)
- {
- if (j < col)
- {
- float pix = img.ptr<uchar>(i)[j * 3 + c];
- this->input_image_[c * row * inpWidth + i * inpWidth + j] = (pix / 255.0 - 0.5) / 0.5;
- }
- else
- {
- this->input_image_[c * row * inpWidth + i * inpWidth + j] = 0;
- }
- }
- }
- }
- }
|