AtmosphericPressureALGO.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "AtmosphericPressureALGO.h"
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/highgui.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include "opencv2/imgproc/types_c.h"
  12. #include <onnxruntime_cxx_api.h>
  13. #include <vector>
  14. #include "YunDaISASImageRecognitionService.h"
  15. #define SEG_IMAGE_SIZE 512
  16. #define LINE_HEIGH 120
  17. #define LINE_WIDTH 1600
  18. #define CIRCLE_RADIUS 250
  19. #define METER_RANGE 0.25
  20. using namespace cv;
  21. using namespace std;
  22. using namespace Ort;
  23. const float pi = 3.1415926536f;
  24. const int circle_center[] = { 256, 256 };
  25. void AtmosphericPressureALGO::Init()
  26. {
  27. string model_path = "models/yalibiao_models.onnx";
  28. std::wstring widestr = std::wstring(model_path.begin(), model_path.end());
  29. sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
  30. ort_session = new Session(env, widestr.c_str(), sessionOptions);
  31. size_t numInputNodes = ort_session->GetInputCount();
  32. size_t numOutputNodes = ort_session->GetOutputCount();
  33. AllocatorWithDefaultOptions allocator;
  34. for (int i = 0; i < numInputNodes; i++)
  35. {
  36. input_names.push_back(ort_session->GetInputName(i, allocator));
  37. Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
  38. auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
  39. auto input_dims = input_tensor_info.GetShape();
  40. input_node_dims.push_back(input_dims);
  41. }
  42. for (int i = 0; i < numOutputNodes; i++)
  43. {
  44. output_names.push_back(ort_session->GetOutputName(i, allocator));
  45. Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
  46. auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
  47. auto output_dims = output_tensor_info.GetShape();
  48. output_node_dims.push_back(output_dims);
  49. }
  50. this->inpHeight = input_node_dims[0][2];
  51. this->inpWidth = input_node_dims[0][3];
  52. this->outHeight = output_node_dims[0][2];
  53. this->outWidth = output_node_dims[0][3];
  54. }
  55. bool AtmosphericPressureALGO::detect(Mat srcimg)
  56. {
  57. try
  58. {
  59. vector<float> input_image_ = { 1, 3, 512, 512 };
  60. Size resize_size(input_image_[2], input_image_[3]);
  61. //srcimg.convertTo(srcimg, CV_32FC3);
  62. resize(srcimg, dstimg, resize_size, 0, 0, cv::INTER_LINEAR);
  63. int channels = dstimg.channels();
  64. input_image_.resize((this->inpWidth * this->inpHeight * dstimg.channels()));
  65. for (int c = 0; c < channels; c++)
  66. {
  67. for (int i = 0; i < this->inpHeight; i++)
  68. {
  69. for (int j = 0; j < this->inpWidth; j++)
  70. {
  71. float pix = dstimg.ptr<uchar>(i)[j * 3 + 2 - c];
  72. input_image_[(c * this->inpHeight * this->inpWidth + i * this->inpWidth + j)] = (pix / 255.0 - mean[c]) / stds[c];
  73. }
  74. }
  75. }
  76. array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
  77. auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
  78. Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
  79. // 开始推理
  80. vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &input_names[0], &input_tensor_, 1, output_names.data(), output_names.size());
  81. float* pred = ort_outputs[0].GetTensorMutableData<float>();
  82. Mat result(outHeight, outWidth, CV_32FC1, pred);
  83. result = 6 - result;
  84. result *= 255;
  85. result.convertTo(result, CV_8UC1);
  86. dstimg = result;
  87. creat_line_image(256, 130);//圆半径 宽度
  88. return true;
  89. }
  90. catch (const std::exception& ex)
  91. {
  92. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  93. }
  94. return false;
  95. //return result;
  96. }
  97. void AtmosphericPressureALGO::creat_line_image(int Radius, int RingStride)
  98. {
  99. Mat rectangle;
  100. float theta;
  101. int rho;
  102. rectangle = Mat::zeros(Size(Radius * pi * 2, RingStride), CV_8UC1);
  103. int nl = rectangle.rows; // number of lines
  104. int nc = rectangle.cols * rectangle.channels(); // total number of elements per line
  105. for (int j = 0; j < nl; j++) {
  106. // get the address of row j
  107. uchar* data = rectangle.ptr<uchar>(j);
  108. for (int i = 0; i < nc; i++) {
  109. theta = pi * 2.0 / LINE_WIDTH * float(i + 1);
  110. rho = (Radius - j - 1);
  111. int position_y = (float)circle_center[0] + rho * (float)std::cos(theta) + 0.5;
  112. int position_x = (float)circle_center[1] - rho * (float)std::sin(theta) + 0.5;
  113. data[i] = dstimg.at<uchar>(position_y, position_x);
  114. }
  115. }
  116. dstimg = rectangle;
  117. get_meter_reader();
  118. }
  119. //像素值提取及累加
  120. void AtmosphericPressureALGO::get_meter_reader()
  121. {
  122. Mat histogram = Mat::zeros(Size(256, 1), CV_8UC1);
  123. int rows = LINE_HEIGH; //输入图像的行数
  124. int cols = LINE_WIDTH; //输入图像的列数
  125. int scale_num = 0;
  126. int pointer_num = 0;
  127. int sum_horsum = 0; //水平方向列相加和
  128. vector<int>num1;
  129. vector<int>num2;
  130. for (int c = 0; c < cols; c++)
  131. {
  132. int versum = 0;
  133. for (int r = 0; r < rows; r++)
  134. {
  135. int index = int(dstimg.at<uchar>(r, c));
  136. versum += index;
  137. }
  138. if (versum != 0)
  139. {
  140. //int max_sum_horsum = 0;
  141. sum_horsum += versum;
  142. //cout << "和:" << sum_horsum << endl;
  143. num1.push_back(sum_horsum);
  144. }
  145. if (versum == 0)
  146. {
  147. int maxValue = 0;
  148. for (auto v : num1)
  149. {
  150. if (maxValue < v) maxValue = v;
  151. }
  152. if (maxValue != 0)
  153. {
  154. //cout << "最大值:" << maxValue << endl;
  155. num2.push_back(maxValue);
  156. }
  157. sum_horsum = 0;
  158. vector<int>().swap(num1);
  159. }
  160. }
  161. //计算表盘读数
  162. int maxPosition = max_element(num2.begin(), num2.end()) - num2.begin();
  163. scale_num = num2.size();
  164. pointer_num = maxPosition;
  165. float result_ratio = (1.0 * pointer_num / scale_num);
  166. float result_value = (result_ratio * METER_RANGE) - 0.1;
  167. cout << "scale_num:" << scale_num << " pointer_num:" << pointer_num << " result_ratio:" << result_ratio << " result_value:" << result_value << endl;
  168. //return result_value;
  169. resultValue = result_value;
  170. }