效果
耗时
class id=brown_bear, score=0.86
preprocess time: 0.00ms
infer time: 2.72ms
postprocess time: 0.02ms
Total time: 2.74ms
项目
代码
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_Cls_图像分类
{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;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>();int maxIndex = Common.MaxIndexOfSpan(data);double postProcessTime = stopwatch.Elapsed.TotalMilliseconds;stopwatch.Stop();sb.AppendLine($"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}");double totalTime = preprocessTime + inferTime + postProcessTime;sb.AppendLine($"preprocess time: {preprocessTime:F2}ms");sb.AppendLine($"infer time: {inferTime:F2}ms");sb.AppendLine($"postprocess time: {postProcessTime:F2}ms");sb.AppendLine($"Total time: {totalTime:F2}ms");Mat result_image = src.Clone();Cv2.PutText(result_image, $"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);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-cls.xml";dicts = XDocument.Load(model_path).XPathSelectElement(@"/net/rt_info/model_info/labels").Attribute("value").Value.Split(' ');}}
}
下载
可执行程序exe下载
源码下载