• C# Onnx LSTR 基于Transformer的端到端实时车道线检测


    目录

    效果

    模型信息

    项目

    代码

    下载


    效果

    端到端实时车道线检测

    模型信息

    lstr_360x640.onnx

    Inputs
    -------------------------
    name:input_rgb
    tensor:Float[1, 3, 360, 640]
    name:input_mask
    tensor:Float[1, 1, 360, 640]
    ---------------------------------------------------------------

    Outputs
    -------------------------
    name:pred_logits
    tensor:Float[1, 7, 2]
    name:pred_curves
    tensor:Float[1, 7, 8]
    name:foo_out_1
    tensor:Float[1, 7, 2]
    name:foo_out_2
    tensor:Float[1, 7, 8]
    name:weights
    tensor:Float[1, 240, 240]
    ---------------------------------------------------------------

    项目

    VS2022+.net framework 4.8

    OpenCvSharp 4.8

    Microsoft.ML.OnnxRuntime 1.16.2

    代码

    1. using Microsoft.ML.OnnxRuntime.Tensors;
    2. using Microsoft.ML.OnnxRuntime;
    3. using OpenCvSharp;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.Windows.Forms;
    7. using System.Linq;
    8. using System.IO;
    9. using System.Text;
    10. using System.Drawing;
    11. namespace Onnx_Demo
    12. {
    13. public partial class frmMain : Form
    14. {
    15. public frmMain()
    16. {
    17. InitializeComponent();
    18. }
    19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. string image_path = "";
    21. DateTime dt1 = DateTime.Now;
    22. DateTime dt2 = DateTime.Now;
    23. int inpWidth;
    24. int inpHeight;
    25. Mat image;
    26. string model_path = "";
    27. float[] factors = new float[2];
    28. SessionOptions options;
    29. InferenceSession onnx_session;
    30. Tensor<float> input_tensor;
    31. Tensor<float> mask_tensor;
    32. List input_ontainer;
    33. IDisposableReadOnlyCollection result_infer;
    34. DisposableNamedOnnxValue[] results_onnxvalue;
    35. Tensor<float> result_tensors;
    36. int len_log_space = 50;
    37. float[] log_space;
    38. float[] mean = new float[] { 0.485f, 0.456f, 0.406f };
    39. float[] std = new float[] { 0.229f, 0.224f, 0.225f };
    40. Scalar[] lane_colors = new Scalar[] { new Scalar(68, 65, 249), new Scalar(44, 114, 243), new Scalar(30, 150, 248), new Scalar(74, 132, 249), new Scalar(79, 199, 249), new Scalar(109, 190, 144), new Scalar(142, 144, 77), new Scalar(161, 125, 39) };
    41. private void button1_Click(object sender, EventArgs e)
    42. {
    43. OpenFileDialog ofd = new OpenFileDialog();
    44. ofd.Filter = fileFilter;
    45. if (ofd.ShowDialog() != DialogResult.OK) return;
    46. pictureBox1.Image = null;
    47. pictureBox2.Image = null;
    48. textBox1.Text = "";
    49. image_path = ofd.FileName;
    50. pictureBox1.Image = new System.Drawing.Bitmap(image_path);
    51. image = new Mat(image_path);
    52. }
    53. private void Form1_Load(object sender, EventArgs e)
    54. {
    55. // 创建输入容器
    56. input_ontainer = new List();
    57. // 创建输出会话
    58. options = new SessionOptions();
    59. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    60. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
    61. // 创建推理模型类,读取本地模型文件
    62. model_path = "model/lstr_360x640.onnx";
    63. inpWidth = 640;
    64. inpHeight = 360;
    65. onnx_session = new InferenceSession(model_path, options);
    66. // 创建输入容器
    67. input_ontainer = new List();
    68. FileStream fileStream = new FileStream("model/log_space.bin", FileMode.Open);
    69. BinaryReader br = new BinaryReader(fileStream, Encoding.UTF8);
    70. log_space = new float[len_log_space];
    71. byte[] byteTemp;
    72. float fTemp;
    73. for (int i = 0; i < len_log_space; i++)
    74. {
    75. byteTemp = br.ReadBytes(4);
    76. fTemp = BitConverter.ToSingle(byteTemp, 0);
    77. log_space[i] = fTemp;
    78. }
    79. br.Close();
    80. image_path = "test_img/0.jpg";
    81. pictureBox1.Image = new Bitmap(image_path);
    82. }
    83. private unsafe void button2_Click(object sender, EventArgs e)
    84. {
    85. if (image_path == "")
    86. {
    87. return;
    88. }
    89. textBox1.Text = "检测中,请稍等……";
    90. pictureBox2.Image = null;
    91. System.Windows.Forms.Application.DoEvents();
    92. //图片缩放
    93. image = new Mat(image_path);
    94. int img_height = image.Rows;
    95. int img_width = image.Cols;
    96. Mat resize_image = new Mat();
    97. Cv2.Resize(image, resize_image, new OpenCvSharp.Size(inpWidth, inpHeight));
    98. int row = resize_image.Rows;
    99. int col = resize_image.Cols;
    100. float[] input_tensor_data = new float[1 * 3 * inpHeight * inpWidth];
    101. for (int c = 0; c < 3; c++)
    102. {
    103. for (int i = 0; i < row; i++)
    104. {
    105. for (int j = 0; j < col; j++)
    106. {
    107. float pix = ((byte*)(resize_image.Ptr(i).ToPointer()))[j * 3 + c];
    108. input_tensor_data[c * row * col + i * col + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
    109. }
    110. }
    111. }
    112. input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth });
    113. float[] input_mask_data = new float[1 * 1 * inpHeight * inpWidth];
    114. for (int i = 0; i < input_mask_data.Length; i++)
    115. {
    116. input_mask_data[i] = 0.0f;
    117. }
    118. mask_tensor = new DenseTensor<float>(input_mask_data, new[] { 1, 1, inpHeight, inpWidth });
    119. //将 input_tensor 放入一个输入参数的容器,并指定名称
    120. input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input_rgb", input_tensor));
    121. input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input_mask", mask_tensor));
    122. dt1 = DateTime.Now;
    123. //运行 Inference 并获取结果
    124. result_infer = onnx_session.Run(input_ontainer);
    125. dt2 = DateTime.Now;
    126. //将输出结果转为DisposableNamedOnnxValue数组
    127. results_onnxvalue = result_infer.ToArray();
    128. float[] pred_logits = results_onnxvalue[0].AsTensor<float>().ToArray();
    129. float[] pred_curves = results_onnxvalue[1].AsTensor<float>().ToArray();
    130. int logits_h = results_onnxvalue[0].AsTensor<float>().Dimensions[1];
    131. int logits_w = results_onnxvalue[0].AsTensor<float>().Dimensions[2];
    132. int curves_w = results_onnxvalue[1].AsTensor<float>().Dimensions[2];
    133. List<int> good_detections = new List<int>();
    134. List> lanes = new List>();
    135. for (int i = 0; i < logits_h; i++)
    136. {
    137. float max_logits = -10000;
    138. int max_id = -1;
    139. for (int j = 0; j < logits_w; j++)
    140. {
    141. float data = pred_logits[i * logits_w + j];
    142. if (data > max_logits)
    143. {
    144. max_logits = data;
    145. max_id = j;
    146. }
    147. }
    148. if (max_id == 1)
    149. {
    150. good_detections.Add(i);
    151. int index = i * curves_w;
    152. List lane_points = new List();
    153. for (int k = 0; k < len_log_space; k++)
    154. {
    155. float y = pred_curves[0 + index] + log_space[k] * (pred_curves[1 + index] - pred_curves[0 + index]);
    156. float x = (float)(pred_curves[2 + index] / Math.Pow(y - pred_curves[3 + index], 2.0) + pred_curves[4 + index] / (y - pred_curves[3 + index]) + pred_curves[5 + index] + pred_curves[6 + index] * y - pred_curves[7 + index]);
    157. lane_points.Add(new OpenCvSharp.Point(x * img_width, y * img_height));
    158. }
    159. lanes.Add(lane_points);
    160. }
    161. }
    162. Mat result_image = image.Clone();
    163. //draw lines
    164. List<int> right_lane = new List<int>();
    165. List<int> left_lane = new List<int>();
    166. for (int i = 0; i < good_detections.Count; i++)
    167. {
    168. if (good_detections[i] == 0)
    169. {
    170. right_lane.Add(i);
    171. }
    172. if (good_detections[i] == 5)
    173. {
    174. left_lane.Add(i);
    175. }
    176. }
    177. if (right_lane.Count() == left_lane.Count())
    178. {
    179. Mat lane_segment_img = result_image.Clone();
    180. List points = new List();
    181. points.AddRange(lanes.First());
    182. points.Reverse();
    183. points.AddRange(lanes[left_lane[0]]);
    184. Cv2.FillConvexPoly(lane_segment_img, points, new Scalar(0, 191, 255));
    185. Cv2.AddWeighted(result_image, 0.7, lane_segment_img, 0.3, 0, result_image);
    186. }
    187. for (int i = 0; i < lanes.Count(); i++)
    188. {
    189. for (int j = 0; j < lanes[i].Count(); j++)
    190. {
    191. Cv2.Circle(result_image, lanes[i][j], 3, lane_colors[good_detections[i]], -1);
    192. }
    193. }
    194. pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());
    195. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    196. }
    197. private void pictureBox2_DoubleClick(object sender, EventArgs e)
    198. {
    199. Common.ShowNormalImg(pictureBox2.Image);
    200. }
    201. private void pictureBox1_DoubleClick(object sender, EventArgs e)
    202. {
    203. Common.ShowNormalImg(pictureBox1.Image);
    204. }
    205. }
    206. }

    下载

    源码下载

  • 相关阅读:
    理论与实战:一篇看懂Python词云
    【JAVA】SpringBoot
    TIA博途V17中ProDiag功能的使用方法示例(一)PLC数据类型的监控
    数字孪生有哪些应用价值?
    使用Docker中部署GitLab 避坑指南
    内插散点数据
    c++ day 4
    VUE3基础知识梳理
    基于C语言编程的职工工资管理系统项目的设计与开发
    C++ vector模拟实现
  • 原文地址:https://blog.csdn.net/lw112190/article/details/134369126