前言

OpenVINO C# API 是一个 OpenVINO 的 .Net wrapper,应用最新的 OpenVINO 库开发,通过 OpenVINO C API 实现 .Net 对 OpenVINO Runtime 调用,使用习惯与 OpenVINO C++ API 一致。OpenVINO C# API 由于是基于 OpenVINO 开发,所支持的平台与 OpenVINO 完全一致,具体信息可以参考 OpenVINO。通过使用 OpenVINO C# API,可以在 .NET、.NET Framework等框架下使用 C# 语言实现深度学习模型在指定平台推理加速。

OpenVINO C# API 项目链接为:

https://github.com/guojin-yan/OpenVINO-CSharp-API.git

项目源码链接为:

https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples.git

1. 简介

   Blazeface模型是Google推出的一款专为移动GPU推理量身定制的轻量级且性能卓越的人脸检测器,BlazeFace 在旗舰移动设备上以200-1000 + FPS的速度运行。 在本文中,我们将使用OpenVINO C# API 部署 Blazeface 实现人脸检测。

2. 项目环境与依赖

  该项目中所需依赖已经支持通过NuGet Package进行安装,在该项目中,需要安装以下NuGet Package:

  • OpenVINO C# API NuGet Package:
OpenVINO.CSharp.API
OpenVINO.runtime.win
OpenVINO.CSharp.API.Extensions
  • OpenCvSharp NuGet Package:
OpenCvSharp4
OpenCvSharp4.Extensions
OpenCvSharp4.runtime.win

3. 项目输出

  项目使用的是控制台输出,运行后输出如下所示:

<00:00:00> Sending http request to https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Model/blazeface_1000e.tar.
<00:00:01> Http Response Accquired.
<00:00:01> Total download length is 0.47 Mb.
<00:00:01> Download Started.
<00:00:01> File created.
<00:00:01> Downloading: [■■■■■■■■■■] 100% <00:00:01 0.24 Mb/s> 0.47 Mb/0.47 Mb downloaded.
<00:00:01> File Downloaded, saved in E:\GitSpace\OpenVINO-CSharp-API-Samples\model_samples\face_detection\blazeface_opencvsharp\bin\Release\net6.0\model\blazeface_1000e.tar.
<00:00:00> Sending http request to https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Image/face1.jpg.
<00:00:01> Http Response Accquired.
<00:00:01> Total download length is 0.14 Mb.
<00:00:01> Download Started.
<00:00:01> File created.
<00:00:01> Downloading: [■■■■■■■■■■] 100% <00:00:01 0.08 Mb/s> 0.14 Mb/0.14 Mb downloaded.
<00:00:01> File Downloaded, saved in E:\GitSpace\OpenVINO-CSharp-API-Samples\model_samples\face_detection\blazeface_opencvsharp\bin\Release\net6.0\model\face1.jpg.
[ INFO ] ---- OpenVINO INFO----
[ INFO ] Description : OpenVINO Runtime
[ INFO ] Build number: 2024.0.0-14509-34caeefd078-releases/2024/0
[ INFO ] Predict model files: ./model/blazeface_1000e.xml
[ INFO ] Predict image files: ./model/face1.jpg
[ INFO ] Inference device: CPU
[ INFO ] Start RT-DETR model inference.
[ INFO ] 1. Initialize OpenVINO Runtime Core success, time spend: 3.2045ms.
[ INFO ] 2. Read inference model success, time spend: 46.1753ms.
[ INFO ] Inference Model
[ INFO ] Model name: Model0
[ INFO ] Input:
[ INFO ] name: scale_factor
[ INFO ] type: float
[ INFO ] shape: Shape : {1,2}
[ INFO ] name: image
[ INFO ] type: float
[ INFO ] shape: Shape : {1,3,640,640}
[ INFO ] name: im_shape
[ INFO ] type: float
[ INFO ] shape: Shape : {1,2}
[ INFO ] Output:
[ INFO ] name: multiclass_nms3_0.tmp_0
[ INFO ] type: float
[ INFO ] shape: Shape : {750,6}
[ INFO ] name: multiclass_nms3_0.tmp_2
[ INFO ] type: int32_t
[ INFO ] shape: Shape : {1}
[ INFO ] 3. Loading a model to the device success, time spend:213.1551ms.
[ INFO ] 4. Create an infer request success, time spend:0.4506ms.
[ INFO ] 5. Process input images success, time spend:74.6076ms.
[ INFO ] 6. Set up input data success, time spend:1.7392ms.
[ INFO ] 7. Do inference synchronously success, time spend:21.3498ms.
[ INFO ] 8. Get infer result data success, time spend:1.1302ms.
[ INFO ] 9. Process reault success, time spend:0.4035ms.
[ INFO ] The result save to E:\GitSpace\OpenVINO-CSharp-API-Samples\model_samples\face_detection\blazeface_opencvsharp\bin\Release\net6.0\model\face1_result.jpg

  图像预测结果如下图所示:

4. 代码展示

  以下为嘛中所使用的命名空间代码:

using OpenCvSharp.Dnn;
using OpenCvSharp;
using OpenVinoSharp;
using OpenVinoSharp.Extensions;
using OpenVinoSharp.Extensions.utility;
using System.Runtime.InteropServices;
using OpenVinoSharp.preprocess;
using OpenVinoSharp.Extensions.result;
using OpenVinoSharp.Extensions.process;
using System;
using System.Reflection.Metadata; namespace blazeface_opencvsharp
{
internal class Program
{
....
}
}

  下面为定义的模型预测代码:

static void face_detection(string model_path, string image_path, string device)
{
// -------- Step 1. Initialize OpenVINO Runtime Core --------
Core core = new Core();
// -------- Step 2. Read inference model --------
Model model = core.read_model(model_path);
Dictionary<string, PartialShape> pairs = new Dictionary<string, PartialShape>();
pairs.Add("scale_factor", new PartialShape(new Shape(1, 2)));
pairs.Add("im_shape", new PartialShape(new Shape(1, 2)));
pairs.Add("image", new PartialShape(new Shape(1, 3, 640, 640)));
model.reshape(pairs);
OvExtensions.printf_model_info(model);
// -------- Step 3. Loading a model to the device --------
CompiledModel compiled_model = core.compile_model(model, device); // -------- Step 4. Create an infer request --------
InferRequest infer_request = compiled_model.create_infer_request(); // -------- Step 5. Process input images -------- Mat image = new Mat(image_path); // Read image by opencvsharp
Mat mat = new Mat();
Cv2.Resize(image, mat, new Size(640, 640));
mat = Normalize.run(mat, new float[] { 123f, 117f, 104f }, new float[] { 1 / 127.502231f, 1 / 127.502231f, 1 / 127.502231f },
false);
float[] input_data = Permute.run(mat); // -------- Step 6. Set up input data --------
Tensor input_tensor_data = infer_request.get_tensor("image");
//input_tensor_data.set_shape(new Shape(1, 3, image.Cols, image.Rows));
input_tensor_data.set_data<float>(input_data);
Tensor input_tensor_shape = infer_request.get_tensor("im_shape");
input_tensor_shape.set_shape(new Shape(1, 2));
input_tensor_shape.set_data<float>(new float[] { 640, 640 });
Tensor input_tensor_factor = infer_request.get_tensor("scale_factor");
input_tensor_factor.set_shape(new Shape(1, 2));
input_tensor_factor.set_data<float>(new float[] { ((float)640.0f / image.Rows), ((float)640.0 / image.Cols) }); // -------- Step 7. Do inference synchronously --------
infer_request.infer(); // -------- Step 8. Get infer result data --------
Tensor output_tensor = infer_request.get_output_tensor(0);
Shape output_shape = output_tensor.get_shape();
int output_length = (int)output_tensor.get_size();
float[] result_data = output_tensor.get_data<float>(output_length);
Tensor output_tensor1 = infer_request.get_output_tensor(1);
int output_length1 = (int)output_tensor1.get_size();
int[] result_len = output_tensor1.get_data<int>(output_length1); // -------- Step 9. Process reault --------
List<Rect> position_boxes = new List<Rect>();
List<float> confidences = new List<float>();
// Preprocessing output results
for (int i = 0; i < result_len[0]; i++)
{
double confidence = result_data[6 * i + 1];
if (confidence > 0.5)
{
float tlx = result_data[6 * i + 2];
float tly = result_data[6 * i + 3];
float brx = result_data[6 * i + 4];
float bry = result_data[6 * i + 5];
Rect box = new Rect((int)tlx, (int)tly, (int)(brx - tlx), (int)(bry - tly));
position_boxes.Add(box);
confidences.Add((float)confidence);
}
}
for (int i = 0; i < position_boxes.Count; i++)
{
int index = i;
Cv2.Rectangle(image, position_boxes[index], new Scalar(255, 0, 0), 1, LineTypes.Link8);
Cv2.PutText(image, confidences[index].ToString("0.00"),
new OpenCvSharp.Point(position_boxes[index].TopLeft.X, position_boxes[index].TopLeft.Y - 5),
HersheyFonts.HersheySimplex, 0.4, new Scalar(255, 0, 0), 1);
}
string output_path = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(image_path)),
Path.GetFileNameWithoutExtension(image_path) + "_result.jpg");
Cv2.ImWrite(output_path, image);
Slog.INFO("The result save to " + output_path);
Cv2.ImShow("Result", image);
Cv2.WaitKey(0);
}

  下面为程序运行的主函数代码,该代码会下载转换好的预测模型,并调用预测方法进行预测:

static void Main(string[] args)
{
string model_path = "";
string image_path = "";
string device = "CPU";
if (args.Length == 0)
{
if (!Directory.Exists("./model"))
{
Directory.CreateDirectory("./model");
}
if (!File.Exists("./model/blazeface_1000e.xml")
&& !File.Exists("./model/blazeface_1000e.bin"))
{
if (!File.Exists("./model/blazeface_1000e.tar"))
{
_ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Model/blazeface_1000e.tar",
"./model/blazeface_1000e.tar").Result;
}
Download.unzip("./model/blazeface_1000e.tar", "./model/");
} if (!File.Exists("./model/face1.jpg"))
{
_ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Image/face1.jpg",
"./model/face1.jpg").Result;
}
model_path = "./model/blazeface_1000e.xml";
image_path = "./model/face1.jpg";
}
else if (args.Length >= 2)
{
model_path = args[0];
image_path = args[1];
device = args[2];
}
else
{
Console.WriteLine("Please enter the correct command parameters, for example:");
Console.WriteLine("> 1. dotnet run");
Console.WriteLine("> 2. dotnet run <model path> <image path> <device name>");
}
// -------- Get OpenVINO runtime version -------- OpenVinoSharp.Version version = Ov.get_openvino_version(); Slog.INFO("---- OpenVINO INFO----");
Slog.INFO("Description : " + version.description);
Slog.INFO("Build number: " + version.buildNumber); Slog.INFO("Predict model files: " + model_path);
Slog.INFO("Predict image files: " + image_path);
Slog.INFO("Inference device: " + device);
Slog.INFO("Start RT-DETR model inference."); face_detection(model_path, image_path, device); }

5. 总结

  在该项目中,我们结合之前开发的 OpenVINO C# API 项目部署 Blazeface 模型,成功实现了人脸检测。

  • 项目完整代码链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/blob/master/model_samples/face_detection/blazeface_opencvsharp/Program.cs
  • 为了方便EmguCV用户使用需求,同时开发了EmguCV版本,项目链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/blob/master/model_samples/face_detection/blazeface_emgucv/Program.cs

最后如果各位开发者在使用中有任何问题,欢迎大家与我联系。

【OpenVINO™】基于 C# 和 OpenVINO™ 部署 Blazeface 模型实现人脸检测的更多相关文章

  1. 手把手教你 在Pytorch框架上部署和测试 关键点人脸检测项目DBFace,成功实现人脸检测效果

    这期教向大家介绍仅仅 1.3M 的轻量级高精度的关键点人脸检测模型DBFace,并手把手教你如何在自己的电脑端进行部署和测试运行,运行时bug解决. 01. 前言 前段时间DBFace人脸检测库横空出 ...

  2. 基于OpenCv的人脸检测、识别系统学习制作笔记之一

    基于OpenCv从视频文件到摄像头的人脸检测 在OpenCv中读取视频文件和读取摄像头的的视频流然后在放在一个窗口中显示结果其实是类似的一个实现过程. 先创建一个指向CvCapture结构的指针 Cv ...

  3. 基于FastAPI和Docker的机器学习模型部署快速上手

    针对前文所述 机器学习模型部署摘要 中docker+fastapi部署机器学习的一个完整示例 outline fastapi简单示例 基于文件内容检测的机器学习&fastapi 在docker ...

  4. [转]基于AWS的自动化部署实践

    作者 徐桂林 发布于 2014年1月22日 -------------------------------------------------------------------- 1. 背景 在过去 ...

  5. 谈谈分布式事务之二:基于DTC的分布式事务管理模型[下篇]

    [续上篇] 当基于LTM或者KTM的事务提升到基于DTC的分布式事务后,DTC成为了本机所有事务型资源管理器的管理者:此外,当一个事务型操作超出了本机的范 围,出现了跨机器的调用后,本机的DTC需要于 ...

  6. 使用pmml实现跨平台部署机器学习模型

    一.概述   对于由Python训练的机器学习模型,通常有pickle和pmml两种部署方式,pickle方式用于在python环境中的部署,pmml方式用于跨平台(如Java环境)的部署,本文叙述的 ...

  7. 使用pmml跨平台部署机器学习模型Demo——房价预测

      基于房价数据,在python中训练得到一个线性回归的模型,在JavaWeb中加载模型完成房价预测的功能. 一. 训练.保存模型 工具:PyCharm-2017.Python-39.sklearn2 ...

  8. 基于隐马尔科夫模型(HMM)的地图匹配(Map-Matching)算法

    文章目录 1. 1. 摘要 2. 2. Map-Matching(MM)问题 3. 3. 隐马尔科夫模型(HMM) 3.1. 3.1. HMM简述 3.2. 3.2. 基于HMM的Map-Matchi ...

  9. 基于SourceTree 下的 Git Flow 模型

    基于SourceTree 下的 Git Flow 模型 1. sourceTree  是一个开源的git 图形管理工具,可下载mac版本,windows版本 2. Git Flow 是一套使用Git进 ...

  10. 基于AWS的自动化部署实践

    过年前,我给InfoQ写了篇文章详细介绍我们团队在过去4年基于AWS的自动化部署实践.文章包括了:为什么选择AWS.AWS上自动化部署的优势和挑战.我们的解决方案,以及和AWS DevOps方案(Op ...

随机推荐

  1. linux xfce 在文件管理器里点击运行shell脚本文件

    1.打开 Settings Editor 2.点击左边的 thunar 3.点击右边的 添加 ,在属性中输入 /misc-exec-shell-scripts-by-default 在类型中选择布尔类 ...

  2. Scala 简单分词求和

    1 package chapter07 2 3 object Test17_CommonWordCount { 4 def main(args: Array[String]): Unit = { 5 ...

  3. 05 Ajax请求(扩展,延伸)

    05 Ajax请求(扩展,延伸) 首先, 我们用Flask创建一个后台服务器(自己做网站了哈) 目录结构: 服务端: from flask import Flask, render_template, ...

  4. 详解SSL证书系列(10)SSL的加密算法

    HTTPS协议的主要功能依赖于SSL,SSL全称为安全套接层(Secure Socket Layer). SSL的功能主要依赖于三类加密算法,散列函数,对称加密和非对称加密.   HASH算法 HAS ...

  5. hive窗口分析函数使用详解系列一

    1.综述 Hive的聚合函数衍生的窗口函数在我们进行数据处理和数据分析过程中起到了很大的作用 在Hive中,窗口函数允许你在结果集的行上进行计算,这些计算不会影响你查询的结果集的行数. Hive提供的 ...

  6. openGauss Sqlines 使用指导

    openGauss Sqlines 使用指导 Sqlines 简介 Sqlines 是一款开源软件,支持多种数据库之间的 SQL 语句语法的的转换,openGauss 将此工具修改适配,新增了 ope ...

  7. openGauss资源池化开发者入门指南(二)

    openGauss 资源池化开发者入门指南(二) 一.内容简介 openGauss 资源池化是 openGauss 推出的一种新型的集群架构.通过 DMS 和 DSS 组件,实现集群中多个节点的底层存 ...

  8. Windows系统编译libhv带SSL,开启WITH_OPENSSL

    需要开发一个https的服务,使用libhv来做,需要重新编译libhv,需要开启 WITH_OPENSSL,前面编译一直很顺利,但是打开VS生成动态库的时候,报错,找不到ssl相关的文件,看了官方的 ...

  9. CentOS 安装openssh-6.XX

    安装openssh-6.0p1 1.安装依赖包 有遇到 报ZLIB有问题的,要安装以下包 rpm -ivh zlib-devel-1.2.3-3.* rpm -ivh libsepol-devel-1 ...

  10. 英语 one day

    前言 I do not know if it work,but just go. 内容 1.quote vt:摘要,引用 n:引语 He quote a passage from the presid ...