目录
OpenVINO.NET github地址:https://github.com/sdcb/OpenVINO.NET
High quality .NET wrapper for OpenVINO™ toolkit.
在AI的应用越来越广泛的今天,优化深度学习模型并进行推理部署已经成为了一门必要的技术。Intel开发的OpenVINO工具包(Open Visual Inference and Neural network Optimization)就是这样一款强大的工具。作为一个开源的工具包,OpenVINO为开发者提供了强大的深度学习模型优化和推理功能,支持跨不同的Intel硬件平台进行部署,包括CPU, 集成GPU, Intel Movidius VPU, 和FPGAs。该工具包的初衷就是实现一处编码后,能在任何地方部署的机器学习推理的解决方案。

- Preprocess: 1.41ms
- Infer: 4.38ms
- Postprocess: 0.03ms
- Total: 5.82ms
VS2022
.net framework 4.8
OpenCvSharp 4.8
Sdcb.OpenVINO

Model m = SharedOVCore.Instance.ReadModel(model_path);
CompiledModel cm = SharedOVCore.Instance.CompileModel(m, "CPU");
InferRequest ir = cm.CreateInferRequest();
NCHW modelInputSize = m.Inputs.Primary.Shape.ToNCHW();
Console.WriteLine(modelInputSize);
Stopwatch sw = Stopwatch.StartNew();
Mat image = src.Clone();
Mat resized = image.Resize(new OpenCvSharp.Size(modelInputSize.Width, modelInputSize.Height));
Mat normalized = Common.Normalize(resized);
float[] extracted = Common.ExtractMat(normalized);
using (Tensor tensor = Tensor.FromArray(extracted, modelInputSize.ToShape()))
{
ir.Inputs.Primary = tensor;
}
double preprocessTime = sw.Elapsed.TotalMilliseconds;
sw.Restart();
ir.Run();
double inferTime = sw.Elapsed.TotalMilliseconds;
- using OpenCvSharp;
- using Sdcb.OpenVINO;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
-
- namespace Sdcb.OpenVINO_人脸检测
- {
- 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;
-
- 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;
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- pictureBox2.Image = null;
- textBox1.Text = "";
-
- Model m = SharedOVCore.Instance.ReadModel(model_path);
- CompiledModel cm = SharedOVCore.Instance.CompileModel(m, "CPU");
- InferRequest ir = cm.CreateInferRequest();
-
- NCHW modelInputSize = m.Inputs.Primary.Shape.ToNCHW();
- Console.WriteLine(modelInputSize);
- Stopwatch sw = Stopwatch.StartNew();
- Mat image = src.Clone();
- Mat resized = image.Resize(new OpenCvSharp.Size(modelInputSize.Width, modelInputSize.Height));
- Mat normalized = Common.Normalize(resized);
- float[] extracted = Common.ExtractMat(normalized);
-
- using (Tensor tensor = Tensor.FromArray(extracted, modelInputSize.ToShape()))
- {
- ir.Inputs.Primary = tensor;
- }
- double preprocessTime = sw.Elapsed.TotalMilliseconds;
-
- sw.Restart();
- ir.Run();
- double inferTime = sw.Elapsed.TotalMilliseconds;
-
- sw.Restart();
- Tensor output = ir.Outputs.Primary;
- Shape outputShape = output.Shape;
- Span<float> result = output.GetData<float>();
-
- List<DetectionResult> results = new List<DetectionResult>();
- for (int i = 0; i < outputShape[2]; ++i)
- {
- float confidence = result[i * 7 + 2];
- int clsId = (int)result[i * 7 + 1];
- if (confidence > 0.5)
- {
- int x1 = (int)(result[i * 7 + 3] * image.Width);
- int y1 = (int)(result[i * 7 + 4] * image.Height);
- int x2 = (int)(result[i * 7 + 5] * image.Width);
- int y2 = (int)(result[i * 7 + 6] * image.Height);
-
- results.Add(new DetectionResult(clsId, confidence, new Rect(x1, y1, x2 - x1, y2 - y1)));
- }
- }
- double postprocessTime = sw.Elapsed.TotalMilliseconds;
-
- double totalTime = preprocessTime + inferTime + postprocessTime;
-
- sb.Clear();
-
- foreach (DetectionResult r in results)
- {
- Cv2.PutText(image, $"{r.Confidence:P2}", r.Rect.TopLeft, HersheyFonts.HersheyPlain, 2, Scalar.Red, 2);
- sb.AppendLine($"{r.Confidence:P2}");
- Cv2.Rectangle(image, r.Rect, Scalar.Red, 3);
- }
-
- sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
- sb.AppendLine($"Infer: {inferTime:F2}ms");
- sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
- sb.AppendLine($"Total: {totalTime:F2}ms");
-
- //Cv2.PutText(image, $"Preprocess: {preprocessTime:F2}ms", new OpenCvSharp.Point(10, 20), HersheyFonts.HersheyPlain, 1, Scalar.Red);
- //Cv2.PutText(image, $"Infer: {inferTime:F2}ms", new OpenCvSharp.Point(10, 40), HersheyFonts.HersheyPlain, 1, Scalar.Red);
- //Cv2.PutText(image, $"Postprocess: {postprocessTime:F2}ms", new OpenCvSharp.Point(10, 60), HersheyFonts.HersheyPlain, 1, Scalar.Red);
- //Cv2.PutText(image, $"Total: {totalTime:F2}ms", new OpenCvSharp.Point(10, 80), HersheyFonts.HersheyPlain, 1, Scalar.Red);
-
- textBox1.Text = sb.ToString();
- pictureBox2.Image = new Bitmap(image.ToMemoryStream());
- }
-
-
- private void Form1_Load(object sender, EventArgs e)
- {
- startupPath = System.Windows.Forms.Application.StartupPath;
- model_path = startupPath + "\\face-detection-0200.xml";
- }
- }
- }