#include "DoubleTemperatureGuageALGO.h" #define _CRT_SECURE_NO_WARNINGS using namespace cv; using namespace std; using namespace Ort; void DoubleTemperatureGuageALGO::Init(bool IsCuda) { string model_path = "models/double_temperature_guage.onnx"; std::wstring widestr = std::wstring(model_path.begin(), model_path.end()); sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC); ort_session = new Session(env, widestr.c_str(), sessionOptions); size_t numInputNodes = ort_session->GetInputCount(); size_t numOutputNodes = ort_session->GetOutputCount(); AllocatorWithDefaultOptions allocator; for (int i = 0; i < numInputNodes; i++) { input_names.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(); input_node_dims.push_back(input_dims); } for (int i = 0; i < numOutputNodes; i++) { output_names.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(); output_node_dims.push_back(output_dims); } this->inpHeight = input_node_dims[0][2]; this->inpWidth = input_node_dims[0][3]; this->outHeight = output_node_dims[0][2]; this->outWidth = output_node_dims[0][3]; } pair DoubleTemperatureGuageALGO::detect(Mat& srcimg,int MaxRange) { METER_RANGE = MaxRange; vector input_image_ = { 1, 3, 512, 512 }; Mat dstimg; Size resize_size(input_image_[2], input_image_[3]); resize(srcimg, dstimg, resize_size, 0, 0, cv::INTER_LINEAR); int channels = dstimg.channels(); input_image_.resize((this->inpWidth * this->inpHeight * dstimg.channels())); for (int c = 0; c < channels; c++) { for (int i = 0; i < this->inpHeight; i++) { for (int j = 0; j < this->inpWidth; j++) { float pix = dstimg.ptr(i)[j * 3 + 2 - c]; input_image_[(c * this->inpHeight * this->inpWidth + i * this->inpWidth + j)] = (pix / 255.0 - mean[c]) / stds[c]; } } } array input_shape_{ 1, 3, this->inpHeight, this->inpWidth }; auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); Value input_tensor_ = Value::CreateTensor(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size()); // 开始推理 vector ort_outputs = ort_session->Run(RunOptions{ nullptr }, &input_names[0], &input_tensor_, 1, output_names.data(), output_names.size()); float* pred = ort_outputs[0].GetTensorMutableData(); Mat result(outHeight, outWidth, CV_32FC1, pred); result = 5 - result; result *= 255; result.convertTo(result, CV_8UC1); return get_meter_reader(creat_line_image(result, 230, 160)); } Mat DoubleTemperatureGuageALGO::creat_line_image(const Mat& circle, int Radius, int RingStride) { Mat rectangle; float theta; int rho; rectangle = Mat::zeros(Size(Radius * pi * 2, RingStride), CV_8UC1); int nl = rectangle.rows; // number of lines int nc = rectangle.cols * rectangle.channels(); // total number of elements per line for (int j = 0; j < nl; j++) { // get the address of row j uchar* data = rectangle.ptr(j); for (int i = 0; i < nc; i++) { theta = pi * 2.0 / LINE_WIDTH * float(i + 1); rho = (Radius - j - 1); int position_y = (float)circle_center[0] + rho * (float)std::cos(theta) + 0.5; int position_x = (float)circle_center[1] - rho * (float)std::sin(theta) + 0.5; data[i] = circle.at(position_y, position_x); } } return rectangle; } //像素值提取及累加 pair DoubleTemperatureGuageALGO::get_meter_reader(const Mat& image) { Mat histogram = Mat::zeros(Size(256, 1), CV_8UC1); int rows = LINE_HEIGH; //输入图像的行数 int cols = LINE_WIDTH; //输入图像的列数 int first_scale_num = 0; int second_scale_num = 0; int first_pointer_num = 0; int second_pointer_num = 0; int sum_horsum = 0; vectornum1; vectornum2; vectornum3; for (int c = 0; c < 1445; c++) { int versum = 0; for (int r = 0; r < rows; r++) { int index = int(image.at(r, c)); //获取每个点的像素值 versum += index; } if (versum != 0) { sum_horsum += versum; num1.push_back(sum_horsum); } if (versum == 0) { int num2_maxValue = 0; for (auto v : num1) { if (num2_maxValue < v) num2_maxValue = v; } if (num2_maxValue != 0) { num2.push_back(num2_maxValue); } sum_horsum = 0; vector().swap(num1); } } //计算表盘读数 //first pointer int first_maxPosition = max_element(num2.begin(), num2.end()) - num2.begin(); auto iter = num2.erase(num2.begin() + first_maxPosition); //second pointer num3 = num2; int num3_maxValue = 0; for (auto v : num3) { if (num3_maxValue < v) { num3_maxValue = v; } } //计算指针刻度值 int second_maxPosition = max_element(num2.begin(), num2.end()) - num2.begin(); float default_value = 0; //red pointer float read_value = 0; first_scale_num = num2.size(); second_scale_num = num2.size(); first_pointer_num = first_maxPosition; second_pointer_num = second_maxPosition; float first_result_ratio = (1.0 * first_pointer_num / first_scale_num); float second_result_ratio = (1.0 * second_pointer_num / second_scale_num); float first_result_value = (first_result_ratio * METER_RANGE); float second_result_value = (second_result_ratio * METER_RANGE); //compare two pointers default_value = first_result_value > second_result_value ? first_result_value : second_result_value; read_value = second_result_value < first_result_value ? second_result_value : first_result_value; cout << "scale_num:" << first_scale_num << " pointer_num:" << first_pointer_num << " result_ratio:" << first_result_ratio << " result_value:" << first_result_value << endl; cout << "scale_num:" << second_scale_num << " pointer_num:" << second_pointer_num << " result_ratio:" << second_result_ratio << " result_value:" << second_result_value << endl; if (read_value>4) { read_value -= 4; } if (default_value != 0) { //default_value += 4; } cout << "读数:" << read_value << " 预设值:" << default_value << endl; return pair( read_value, default_value); }