C# OpenVINO 人脸识别

效果

耗时

Preprocess: 1.41ms
Infer: 4.38ms
Postprocess: 0.03ms
Total: 5.82ms

项目

代码

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";
        }
    }
}

下载

可执行程序exe下载

源码下载