当前位置: 首页 > news >正文

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

源码下载

http://www.lryc.cn/news/190744.html

相关文章:

  • ESP32-WROOM-32无法进入下载模式进行程序上传的问题
  • 尚硅谷Flink(一)
  • C++ 设计模式 —— 桥接模式
  • 微信怎么删除好友?非常简单,2个方法!
  • 小谈设计模式(25)—职责链模式
  • Python- JSON-RPC创建一个远程过程调用
  • Linux中scp命令复制文件
  • Interlay采用Moonbeam路由流动性,为波卡发展更多流动性
  • Jetson Orin NX 开发指南(9): Pixhawk 6X 飞控固件的烧写与 QGroundControl 参数配置
  • Redis(四)多级缓存
  • 网站安全防护
  • 腾讯云南京地域怎么样?南京服务器IP测速Ping值延迟
  • Harbor 简介
  • RuntimeError: “LayerNormKernelImpl“ not implemented for ‘Half‘解决方案
  • 《向量数据库指南》——向量数据库与 ANN 算法库的区别
  • JavaScript-es6-新版语法-export-import
  • [elasticsearch]使用postman来查询数据
  • 【小程序练习】文件操作案例
  • flask框架-[实现websocket]:将socketio处理函数部分集中管理,使用类的方式来管理,集中管理socketio处理函数
  • Vue的学习补充
  • vue移动端H5调起手机发送短信(兼容ios和android)
  • spring boot RabbitMq基础教程
  • springboot vue 部署至Rocky(Centos)并自启,本文部署是若依应用
  • Mysql之增删改查
  • 【考研数学】矩阵三大关系的梳理和讨论 | 等价、相似、合同
  • 在 Amazon SageMaker 上使用 ESMFold 语言模型加速蛋白质结构预测
  • node.js知识系列(4)-每天了解一点
  • can not remove .unionfs
  • 微服务 BFF 架构设计
  • 零基础学python之元组