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
// 获取视频的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
// 获取视频的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();
}
}
}
- 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;
-
- /// <summary>
- /// 窗体加载,初始化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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");
- }
-
- /// <summary>
- /// 选择视频
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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 = "";
-
- }
-
- /// <summary>
- /// 同步接口-视频推理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<DetectionResult> 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();
- }
-
-
- /// <summary>
- /// 异步接口-视频推理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<DetectionResult> 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();
- }
-
- }
-
- }
下载


评论记录:
回复评论: