• C# OpenVINO 人脸识别


    目录

    介绍

    效果

    耗时

    项目

    代码

    下载


    介绍

    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。该工具包的初衷就是实现一处编码后,能在任何地方部署的机器学习推理的解决方案。

    效果

    耗时

    1. Preprocess: 1.41ms
    2. Infer: 4.38ms
    3. Postprocess: 0.03ms
    4. 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;

    1. using OpenCvSharp;
    2. using Sdcb.OpenVINO;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Diagnostics;
    6. using System.Drawing;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. namespace Sdcb.OpenVINO_人脸检测
    10. {
    11. public partial class Form1 : Form
    12. {
    13. public Form1()
    14. {
    15. InitializeComponent();
    16. }
    17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    18. string image_path = "";
    19. string startupPath;
    20. string model_path;
    21. Mat src;
    22. StringBuilder sb = new StringBuilder();
    23. private void button1_Click(object sender, EventArgs e)
    24. {
    25. OpenFileDialog ofd = new OpenFileDialog();
    26. ofd.Filter = fileFilter;
    27. if (ofd.ShowDialog() != DialogResult.OK) return;
    28. pictureBox1.Image = null;
    29. image_path = ofd.FileName;
    30. pictureBox1.Image = new Bitmap(image_path);
    31. textBox1.Text = "";
    32. src = new Mat(image_path);
    33. pictureBox2.Image = null;
    34. }
    35. private void button2_Click(object sender, EventArgs e)
    36. {
    37. pictureBox2.Image = null;
    38. textBox1.Text = "";
    39. Model m = SharedOVCore.Instance.ReadModel(model_path);
    40. CompiledModel cm = SharedOVCore.Instance.CompileModel(m, "CPU");
    41. InferRequest ir = cm.CreateInferRequest();
    42. NCHW modelInputSize = m.Inputs.Primary.Shape.ToNCHW();
    43. Console.WriteLine(modelInputSize);
    44. Stopwatch sw = Stopwatch.StartNew();
    45. Mat image = src.Clone();
    46. Mat resized = image.Resize(new OpenCvSharp.Size(modelInputSize.Width, modelInputSize.Height));
    47. Mat normalized = Common.Normalize(resized);
    48. float[] extracted = Common.ExtractMat(normalized);
    49. using (Tensor tensor = Tensor.FromArray(extracted, modelInputSize.ToShape()))
    50. {
    51. ir.Inputs.Primary = tensor;
    52. }
    53. double preprocessTime = sw.Elapsed.TotalMilliseconds;
    54. sw.Restart();
    55. ir.Run();
    56. double inferTime = sw.Elapsed.TotalMilliseconds;
    57. sw.Restart();
    58. Tensor output = ir.Outputs.Primary;
    59. Shape outputShape = output.Shape;
    60. Span<float> result = output.GetData<float>();
    61. List<DetectionResult> results = new List<DetectionResult>();
    62. for (int i = 0; i < outputShape[2]; ++i)
    63. {
    64. float confidence = result[i * 7 + 2];
    65. int clsId = (int)result[i * 7 + 1];
    66. if (confidence > 0.5)
    67. {
    68. int x1 = (int)(result[i * 7 + 3] * image.Width);
    69. int y1 = (int)(result[i * 7 + 4] * image.Height);
    70. int x2 = (int)(result[i * 7 + 5] * image.Width);
    71. int y2 = (int)(result[i * 7 + 6] * image.Height);
    72. results.Add(new DetectionResult(clsId, confidence, new Rect(x1, y1, x2 - x1, y2 - y1)));
    73. }
    74. }
    75. double postprocessTime = sw.Elapsed.TotalMilliseconds;
    76. double totalTime = preprocessTime + inferTime + postprocessTime;
    77. sb.Clear();
    78. foreach (DetectionResult r in results)
    79. {
    80. Cv2.PutText(image, $"{r.Confidence:P2}", r.Rect.TopLeft, HersheyFonts.HersheyPlain, 2, Scalar.Red, 2);
    81. sb.AppendLine($"{r.Confidence:P2}");
    82. Cv2.Rectangle(image, r.Rect, Scalar.Red, 3);
    83. }
    84. sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
    85. sb.AppendLine($"Infer: {inferTime:F2}ms");
    86. sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
    87. sb.AppendLine($"Total: {totalTime:F2}ms");
    88. //Cv2.PutText(image, $"Preprocess: {preprocessTime:F2}ms", new OpenCvSharp.Point(10, 20), HersheyFonts.HersheyPlain, 1, Scalar.Red);
    89. //Cv2.PutText(image, $"Infer: {inferTime:F2}ms", new OpenCvSharp.Point(10, 40), HersheyFonts.HersheyPlain, 1, Scalar.Red);
    90. //Cv2.PutText(image, $"Postprocess: {postprocessTime:F2}ms", new OpenCvSharp.Point(10, 60), HersheyFonts.HersheyPlain, 1, Scalar.Red);
    91. //Cv2.PutText(image, $"Total: {totalTime:F2}ms", new OpenCvSharp.Point(10, 80), HersheyFonts.HersheyPlain, 1, Scalar.Red);
    92. textBox1.Text = sb.ToString();
    93. pictureBox2.Image = new Bitmap(image.ToMemoryStream());
    94. }
    95. private void Form1_Load(object sender, EventArgs e)
    96. {
    97. startupPath = System.Windows.Forms.Application.StartupPath;
    98. model_path = startupPath + "\\face-detection-0200.xml";
    99. }
    100. }
    101. }

    下载

    可执行程序exe下载

    源码下载

  • 相关阅读:
    java计算机毕业设计基于ssm的基于个人需求和地域特色的外卖订餐推荐系统
    Linux常见面试题
    java-php-python-ssm基于线上订餐系统计算机毕业设计
    06 【生命周期 模板引用】
    2. 回流与重绘?
    Linux内核之completion机制
    Win11环境Mecab日语分词和词性分析以及动态库DLL not found问题(Python3.10)
    [机器学习、Spark]Spark MLlib实现数据基本统计
    开发者高评分IDE工具盘点
    leetcode 马拉松 6.27
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133803335