C# OpenVINO 人脸识别
  yqdtHKhvd9Ja 2023年11月02日 67 0


效果

C# OpenVINO 人脸识别_Red

耗时

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

项目

C# OpenVINO 人脸识别_ide_02

代码

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下载 

源码下载

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
yqdtHKhvd9Ja