效果

耗时
elephant:89%
Preprocess: 0.00ms
Infer: 47.21ms
Postprocess: 11.63ms
Total: 58.84ms
项目

代码
using OpenCvSharp
using Sdcb.OpenVINO
using Sdcb.OpenVINO.Natives
using System
using System.Diagnostics
using System.Drawing
using System.Text
using System.Windows.Forms
using System.Xml.Linq
using System.Xml.XPath
namespace OpenVINO_Det_物体检测
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"
string image_path = ""
string startupPath
string model_path
Mat src
string[] dicts
StringBuilder sb = new StringBuilder()
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
ofd.Filter = fileFilter
if (ofd.ShowDialog() != DialogResult.OK) return
pictureBox1.Image = null
image_path = ofd.FileName
pictureBox1.Image = new Bitmap(image_path)
textBox1.Text = ""
src = new Mat(image_path)
pictureBox2.Image = null
}
unsafe private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return
}
pictureBox2.Image = null
textBox1.Text = ""
sb.Clear()
Model rawModel = OVCore.Shared.ReadModel(model_path)
PrePostProcessor pp = rawModel.CreatePrePostProcessor()
PreProcessInputInfo inputInfo = pp.Inputs.Primary
inputInfo.TensorInfo.Layout = Sdcb.OpenVINO.Layout.NHWC
inputInfo.ModelInfo.Layout = Sdcb.OpenVINO.Layout.NCHW
Model m = pp.BuildModel()
CompiledModel cm = OVCore.Shared.CompileModel(m, "CPU")
InferRequest ir = cm.CreateInferRequest()
Shape inputShape = m.Inputs.Primary.Shape
Size2f sizeRatio = new Size2f(1f * src.Width / inputShape[2], 1f * src.Height / inputShape[1])
Stopwatch stopwatch = new Stopwatch()
Mat resized = src.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]))
Mat f32 = new Mat()
resized.ConvertTo(f32, MatType.CV_32FC3, 1.0 / 255)
using (Tensor input = Tensor.FromRaw(
new ReadOnlySpan<byte>((void*)f32.Data, (int)((long)f32.DataEnd - (long)f32.DataStart)),
new Shape(1, f32.Rows, f32.Cols, 3),
ov_element_type_e.F32))
{
ir.Inputs.Primary = input
}
double preprocessTime = stopwatch.Elapsed.TotalMilliseconds
stopwatch.Restart()
ir.Run()
double inferTime = stopwatch.Elapsed.TotalMilliseconds
stopwatch.Restart()
using (Tensor output = ir.Outputs.Primary)
{
ReadOnlySpan<float> data = output.GetData<float>()
DetectionResult[] results = DetectionResult.FromYolov8DetectionResult(data, output.Shape, sizeRatio, dicts)
double postprocessTime = stopwatch.Elapsed.TotalMilliseconds
stopwatch.Stop()
double totalTime = preprocessTime + inferTime + postprocessTime
Mat result_image = src.Clone()
//Cv2.PutText(result_image, $"Preprocess: {preprocessTime:F2}ms", new Point(10, 20), HersheyFonts.HersheyPlain, 1, Scalar.Red)
//Cv2.PutText(result_image, $"Infer: {inferTime:F2}ms", new Point(10, 40), HersheyFonts.HersheyPlain, 1, Scalar.Red)
//Cv2.PutText(result_image, $"Postprocess: {postprocessTime:F2}ms", new Point(10, 60), HersheyFonts.HersheyPlain, 1, Scalar.Red)
//Cv2.PutText(result_image, $"Total: {totalTime:F2}ms", new Point(10, 80), HersheyFonts.HersheyPlain, 1, Scalar.Red)
foreach (DetectionResult r in results)
{
sb.AppendLine($"{r.Class}:{r.Confidence:P0}")
Cv2.PutText(result_image, $"{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(result_image, r.Rect, Scalar.Red, thickness: 2)
}
sb.AppendLine($"Preprocess: {preprocessTime:F2}ms")
sb.AppendLine($"Infer: {inferTime:F2}ms")
sb.AppendLine($"Postprocess: {postprocessTime:F2}ms")
sb.AppendLine($"Total: {totalTime:F2}ms")
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream())
textBox1.Text = sb.ToString()
}
}
private void Form1_Load(object sender, EventArgs e)
{
startupPath = Application.StartupPath
model_path = startupPath + "\\yolov8n.xml"
dicts = XDocument.Load(model_path)
.XPathSelectElement(@"/net/rt_info/model_info/labels").Attribute("value").Value
.Split(' ')
}
}
}
下载
可执行程序exe下载
源码下载