• C# yolov8 OpenVINO 同步、异步接口视频推理


    C# yolov8 OpenVINO 同步、异步接口视频推理

    目录

    效果

    项目

    代码

    下载


    效果

    同步推理效果

    异步推理效果

    项目

    代码

    using OpenCvSharp;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Forms;


    namespace yolov8_OpenVINO_Demo
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            string imgFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

            YoloV8 yoloV8;
            YoloV8Async yoloV8Async;

            string model_path;

            string video_path = "";
            string videoFilter = "*.mp4|*.mp4;";
            VideoCapture vcapture;

            ///


            /// 窗体加载,初始化
            ///

            ///
            ///
            private void Form1_Load(object sender, EventArgs e)
            {
                model_path = "model/yolov8n.onnx";
                yoloV8 = new YoloV8(model_path, "model/lable.txt");

                yoloV8Async = new YoloV8Async(model_path, "model/lable.txt");
            }

            ///


            /// 选择视频
            ///

            ///
            ///
            private void button4_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = videoFilter;
                ofd.InitialDirectory = Application.StartupPath + "\\test";
                if (ofd.ShowDialog() != DialogResult.OK) return;

                video_path = ofd.FileName;
                textBox1.Text = "";

            }

            ///


            /// 同步接口-视频推理
            ///

            ///
            ///
            private void button3_Click(object sender, EventArgs e)
            {
                if (video_path == "")
                {
                    MessageBox.Show("请先选择视频!");
                    return;
                }

                textBox1.Text = "开始检测";

                Application.DoEvents();

                Thread thread = new Thread(new ThreadStart(VideoDetection));

                thread.Start();
                thread.Join();

                textBox1.Text = "检测完成!";
            }

            void VideoDetection()
            {
                vcapture = new VideoCapture(video_path);
                if (!vcapture.IsOpened())
                {
                    MessageBox.Show("打开视频文件失败");
                    return;
                }

                Mat frame = new Mat();
                List detResults;

                // 获取视频的fps
                double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
                // 计算等待时间(毫秒)
                int delay = (int)(1000 / videoFps);
                Stopwatch _stopwatch = new Stopwatch();

                Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
                Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth / 2, vcapture.FrameHeight / 2);

                while (vcapture.Read(frame))
                {
                    if (frame.Empty())
                    {
                        MessageBox.Show("读取失败");
                        return;
                    }

                    _stopwatch.Restart();

                    delay = (int)(1000 / videoFps);

                    detResults = yoloV8.Detect(frame);

                    //绘制结果
                    foreach (DetectionResult r in detResults)
                    {
                        Cv2.PutText(frame, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                        Cv2.Rectangle(frame, r.Rect, Scalar.Red, thickness: 2);
                    }

                    Cv2.PutText(frame, "preprocessTime:" + yoloV8.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "inferTime:" + yoloV8.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "postprocessTime:" + yoloV8.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "totalTime:" + yoloV8.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "det fps:" + yoloV8.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);

                    Cv2.ImShow("DetectionResult 按下ESC,退出", frame);

                    //for test
                    delay = 1;
                    //delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
                    //if (delay <= 0)
                    //{
                    //    delay = 1;
                    //}
                    //Console.WriteLine("delay:" + delay.ToString()) ;
                    if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
                    {
                        Cv2.DestroyAllWindows();
                        vcapture.Release();
                        break; // 如果按下ESC,退出循环
                    }
                }

                Cv2.DestroyAllWindows();
                vcapture.Release();
            }


            ///


            /// 异步接口-视频推理
            ///

            ///
            ///
            private void button1_Click(object sender, EventArgs e)
            {
                if (video_path == "")
                {
                    MessageBox.Show("请先选择视频!");
                    return;
                }

                textBox1.Text = "开始异步推理检测";

                Application.DoEvents();

                Thread thread = new Thread(new ThreadStart(VideoDetectionAsync));

                thread.Start();
                thread.Join();

                textBox1.Text = "异步推理检测完成!";
            }

            void VideoDetectionAsync()
            {
                vcapture = new VideoCapture(video_path);
                if (!vcapture.IsOpened())
                {
                    MessageBox.Show("打开视频文件失败");
                    return;
                }

                Mat frame = new Mat();
                List detResults;

                // 获取视频的fps
                double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
                // 计算等待时间(毫秒)
                int delay = (int)(1000 / videoFps);
                Stopwatch _stopwatch = new Stopwatch();

                Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
                Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth / 2, vcapture.FrameHeight / 2);

                vcapture.Read(frame);

                while (true)
                {
                    if (!vcapture.Read(frame))
                    {
                        break;
                    }

                    detResults = yoloV8Async.Detect(frame);

                    //绘制结果
                    foreach (DetectionResult r in detResults)
                    {
                        Cv2.PutText(frame, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                        Cv2.Rectangle(frame, r.Rect, Scalar.Red, thickness: 2);
                    }
                    Cv2.PutText(frame, "preprocessTime:" + yoloV8Async.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "inferTime:" + yoloV8Async.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "postprocessTime:" + yoloV8Async.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "totalTime:" + yoloV8Async.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.PutText(frame, "det fps:" + yoloV8Async.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);

                    Cv2.ImShow("DetectionResult 按下ESC,退出", frame);

                    // for test
                    delay = 1;
                    //delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
                    //if (delay <= 0)
                    //{
                    //    delay = 1;
                    //}
                    //Console.WriteLine("delay:" + delay.ToString()) ;
                    if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
                    {
                        Cv2.DestroyAllWindows();
                        vcapture.Release();
                        break; // 如果按下ESC,退出循环
                    }
                }

                Cv2.DestroyAllWindows();
                vcapture.Release();
            }

        }

    }

    1. using OpenCvSharp;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Diagnostics;
    5. using System.Threading;
    6. using System.Windows.Forms;
    7. namespace yolov8_OpenVINO_Demo
    8. {
    9. public partial class Form2 : Form
    10. {
    11. public Form2()
    12. {
    13. InitializeComponent();
    14. }
    15. string imgFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    16. YoloV8 yoloV8;
    17. YoloV8Async yoloV8Async;
    18. string model_path;
    19. string video_path = "";
    20. string videoFilter = "*.mp4|*.mp4;";
    21. VideoCapture vcapture;
    22. /// <summary>
    23. /// 窗体加载,初始化
    24. /// </summary>
    25. /// <param name="sender"></param>
    26. /// <param name="e"></param>
    27. private void Form1_Load(object sender, EventArgs e)
    28. {
    29. model_path = "model/yolov8n.onnx";
    30. yoloV8 = new YoloV8(model_path, "model/lable.txt");
    31. yoloV8Async = new YoloV8Async(model_path, "model/lable.txt");
    32. }
    33. /// <summary>
    34. /// 选择视频
    35. /// </summary>
    36. /// <param name="sender"></param>
    37. /// <param name="e"></param>
    38. private void button4_Click(object sender, EventArgs e)
    39. {
    40. OpenFileDialog ofd = new OpenFileDialog();
    41. ofd.Filter = videoFilter;
    42. ofd.InitialDirectory = Application.StartupPath + "\\test";
    43. if (ofd.ShowDialog() != DialogResult.OK) return;
    44. video_path = ofd.FileName;
    45. textBox1.Text = "";
    46. }
    47. /// <summary>
    48. /// 同步接口-视频推理
    49. /// </summary>
    50. /// <param name="sender"></param>
    51. /// <param name="e"></param>
    52. private void button3_Click(object sender, EventArgs e)
    53. {
    54. if (video_path == "")
    55. {
    56. MessageBox.Show("请先选择视频!");
    57. return;
    58. }
    59. textBox1.Text = "开始检测";
    60. Application.DoEvents();
    61. Thread thread = new Thread(new ThreadStart(VideoDetection));
    62. thread.Start();
    63. thread.Join();
    64. textBox1.Text = "检测完成!";
    65. }
    66. void VideoDetection()
    67. {
    68. vcapture = new VideoCapture(video_path);
    69. if (!vcapture.IsOpened())
    70. {
    71. MessageBox.Show("打开视频文件失败");
    72. return;
    73. }
    74. Mat frame = new Mat();
    75. List<DetectionResult> detResults;
    76. // 获取视频的fps
    77. double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
    78. // 计算等待时间(毫秒)
    79. int delay = (int)(1000 / videoFps);
    80. Stopwatch _stopwatch = new Stopwatch();
    81. Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
    82. Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth / 2, vcapture.FrameHeight / 2);
    83. while (vcapture.Read(frame))
    84. {
    85. if (frame.Empty())
    86. {
    87. MessageBox.Show("读取失败");
    88. return;
    89. }
    90. _stopwatch.Restart();
    91. delay = (int)(1000 / videoFps);
    92. detResults = yoloV8.Detect(frame);
    93. //绘制结果
    94. foreach (DetectionResult r in detResults)
    95. {
    96. Cv2.PutText(frame, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    97. Cv2.Rectangle(frame, r.Rect, Scalar.Red, thickness: 2);
    98. }
    99. Cv2.PutText(frame, "preprocessTime:" + yoloV8.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    100. Cv2.PutText(frame, "inferTime:" + yoloV8.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    101. Cv2.PutText(frame, "postprocessTime:" + yoloV8.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    102. Cv2.PutText(frame, "totalTime:" + yoloV8.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    103. Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    104. Cv2.PutText(frame, "det fps:" + yoloV8.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    105. Cv2.ImShow("DetectionResult 按下ESC,退出", frame);
    106. //for test
    107. delay = 1;
    108. //delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
    109. //if (delay <= 0)
    110. //{
    111. // delay = 1;
    112. //}
    113. //Console.WriteLine("delay:" + delay.ToString()) ;
    114. if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
    115. {
    116. Cv2.DestroyAllWindows();
    117. vcapture.Release();
    118. break; // 如果按下ESC,退出循环
    119. }
    120. }
    121. Cv2.DestroyAllWindows();
    122. vcapture.Release();
    123. }
    124. /// <summary>
    125. /// 异步接口-视频推理
    126. /// </summary>
    127. /// <param name="sender"></param>
    128. /// <param name="e"></param>
    129. private void button1_Click(object sender, EventArgs e)
    130. {
    131. if (video_path == "")
    132. {
    133. MessageBox.Show("请先选择视频!");
    134. return;
    135. }
    136. textBox1.Text = "开始异步推理检测";
    137. Application.DoEvents();
    138. Thread thread = new Thread(new ThreadStart(VideoDetectionAsync));
    139. thread.Start();
    140. thread.Join();
    141. textBox1.Text = "异步推理检测完成!";
    142. }
    143. void VideoDetectionAsync()
    144. {
    145. vcapture = new VideoCapture(video_path);
    146. if (!vcapture.IsOpened())
    147. {
    148. MessageBox.Show("打开视频文件失败");
    149. return;
    150. }
    151. Mat frame = new Mat();
    152. List<DetectionResult> detResults;
    153. // 获取视频的fps
    154. double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
    155. // 计算等待时间(毫秒)
    156. int delay = (int)(1000 / videoFps);
    157. Stopwatch _stopwatch = new Stopwatch();
    158. Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
    159. Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth / 2, vcapture.FrameHeight / 2);
    160. vcapture.Read(frame);
    161. while (true)
    162. {
    163. if (!vcapture.Read(frame))
    164. {
    165. break;
    166. }
    167. detResults = yoloV8Async.Detect(frame);
    168. //绘制结果
    169. foreach (DetectionResult r in detResults)
    170. {
    171. Cv2.PutText(frame, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    172. Cv2.Rectangle(frame, r.Rect, Scalar.Red, thickness: 2);
    173. }
    174. Cv2.PutText(frame, "preprocessTime:" + yoloV8Async.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    175. Cv2.PutText(frame, "inferTime:" + yoloV8Async.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    176. Cv2.PutText(frame, "postprocessTime:" + yoloV8Async.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    177. Cv2.PutText(frame, "totalTime:" + yoloV8Async.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    178. Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    179. Cv2.PutText(frame, "det fps:" + yoloV8Async.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
    180. Cv2.ImShow("DetectionResult 按下ESC,退出", frame);
    181. // for test
    182. delay = 1;
    183. //delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
    184. //if (delay <= 0)
    185. //{
    186. // delay = 1;
    187. //}
    188. //Console.WriteLine("delay:" + delay.ToString()) ;
    189. if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
    190. {
    191. Cv2.DestroyAllWindows();
    192. vcapture.Release();
    193. break; // 如果按下ESC,退出循环
    194. }
    195. }
    196. Cv2.DestroyAllWindows();
    197. vcapture.Release();
    198. }
    199. }
    200. }

    下载

    源码下载

  • 相关阅读:
    matlab读写json文件
    【无标题】
    【软考软件评测师】第二十章 计算机组成与体系结构(CPU指令系统)
    go语言中的锁底层分析(二)
    《分治中的典型题型-求字符串的最大子段和》
    pagehelper分页查询
    资源画像,看得见的容器资源优化助手
    RML2016调制识别
    CMake语法结构说明
    LeetCode 1159.市场分析2
  • 原文地址:https://blog.csdn.net/lw112190/article/details/139780455