【OpenVINO™】在 MacOS 上使用 OpenVINO™ C# API 部署 Yolov5 (下篇)
项目介绍YOLOv5 是革命性的 "单阶段"对象检测模型的第五次迭代,旨在实时提供高速、高精度的结果,是世界上最受欢迎的视觉人工智能模型,代表了Ultralytics对未来视觉人工智能方法的开源研究,融合了数千小时研发中积累的经验教训和最佳实践。同时官方发布的模型已经支持 OpenVINO™ 部署工具加速模型推理,因此在该项目中,我们将结合之前开发的 OpenVINO™ C# API 部署 YOLOv5 DET 模型实现物体对象检测。
项目链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API
项目源码链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov5/yolov5_det_opencvsharp https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov5/yolov5_det_emgucv
文章目录
- 3. Yolov5 DET 项目配置(OpenCvSharp版)
- 3.1 项目创建
- 3.2 添加项目依赖
- 3.3 定义预测方法
- (1) 使用常规方式部署模型
- (2) 使用模型结构处理处理数据
- (3) 使用 OpenVINO™ C# API 封装的接口
- 3.4 预测方法调用
- 4. Yolov5 DET 项目配置(Emgu.CV 版)
- 4.1 添加项目依赖
- 4.2 定义预测方法
- 5. 项目运行与演示
- 5.1 项目编译
- 6. 总结
3. Yolov5 DET 项目配置(OpenCvSharp版)
3.1 项目创建
如果开发者第一次在MacOS系统上使用C#编程语言,可以参考《在MacOS系统上配置OpenVINO™ C# API》文章进行配置。
首先使用dotnet创建一个测试项目,在终端中输入一下指令:
dotnet new console --framework net6.0 --use-program-main -o yolov5-det
3.2 添加项目依赖
MacOS系统目前主要分为两类,一类是使用intel处理器的X64位的系统,一类是使用M系列芯片的arm64位系统,目前OpenVINO官方针对这两种系统都提供了编译后的系统,所以目前OpenVINO.CSharp.API针对这两种系统都提供了支持。
此处以M系列处理器的MacOS平台为例安装项目依赖,首先是安装OpenVINO™ C# API项目依赖,在命令行中输入以下指令即可:
dotnet add package OpenVINO.CSharp.API
dotnet add package OpenVINO.runtime.macos-arm64
dotnet add package OpenVINO.CSharp.API.Extensions
dotnet add package OpenVINO.CSharp.API.Extensions.OpenCvSharp
关于在MacOS上搭建 OpenVINO™ C# API 开发环境请参考以下文章: 在MacOS上搭建OpenVINO™C#开发环境
接下来安装使用到的图像处理库 OpenCvSharp,在命令行中输入以下指令即可:
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.Extensions
dotnet add package OpenCvSharp4.runtime.osx_arm64 --prerelease
关于在MacOS上搭建 OpenCvSharp 开发环境请参考以下文章: 【OpenCV】在MacOS上使用OpenCvSharp
添加完成项目依赖后,项目的配置文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>yolov5_det</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.9.0.20240103" />
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.9.0.20240103" />
<PackageReference Include="OpenCvSharp4.runtime.osx_arm64" Version="4.8.1-rc" />
<PackageReference Include="OpenVINO.CSharp.API" Version="2023.2.0.4" />
<PackageReference Include="OpenVINO.CSharp.API.Extensions" Version="1.0.1" />
<PackageReference Include="OpenVINO.CSharp.API.Extensions.OpenCvSharp" Version="1.0.4" />
<PackageReference Include="OpenVINO.runtime.macos-arm64" Version="2023.3.0.1" />
</ItemGroup>
</Project>
3.3 定义预测方法
(1) 使用常规方式部署模型
Yolov5 属于比较经典单阶段目标检测模型,其模型输入为640*640的归一化处理后的图像数据,输出为未进行NMS的推理结果,因此在获取推理结果后,需要进行NMS,其实现代码如下所示:
static void yolov5_det(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);
OvExtensions.printf_model_info(model);
// -------- Step 3. Loading a model to the device --------
start = DateTime.Now;
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
int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
Rect roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));
float factor = (float)(max_image_length / 640.0);
// -------- Step 6. Set up input data --------
Tensor input_tensor = infer_request.get_input_tensor();
Shape input_shape = input_tensor.get_shape();
Mat input_mat = CvDnn.BlobFromImage(max_image, 1.0 / 255.0, new OpenCvSharp.Size(input_shape[2], input_shape[3]), 0, true, false);
float[] input_data = new float[input_shape[1] * input_shape[2] * input_shape[3]];
Marshal.Copy(input_mat.Ptr(0), input_data, 0, input_data.Length);
input_tensor.set_data<float>(input_data);
// -------- Step 7. Do inference synchronously --------
infer_request.infer();
// -------- Step 8. Get infer result data --------
Tensor output_tensor = infer_request.get_output_tensor();
int output_length = (int)output_tensor.get_size();
float[] output_data = output_tensor.get_data<float>(output_length);
// -------- Step 9. Process reault --------
Mat result_data = new Mat(25200, 85, MatType.CV_32F, output_data);
// Storage results list
List<Rect> position_boxes = new List<Rect>();
List<int> class_ids = new List<int>();
List<float> confidences = new List<float>();
// Preprocessing output results
for (int i = 0; i < result_data.Rows; i++)
{
float confidence = result_data.At<float>(i, 4);
if (confidence < 0.5)
{
continue;
}
Mat classes_scores = new Mat(result_data, new Rect(5, i, 80, 1));
OpenCvSharp.Point max_classId_point, min_classId_point;
double max_score, min_score;
// Obtain the maximum value and its position in a set of data
Cv2.MinMaxLoc(classes_scores, out min_score, out max_score,
out min_classId_point, out max_classId_point);
// Confidence level between 0 ~ 1
// Obtain identification box information
if (max_score > 0.25)
{
float cx = result_data.At<float>(i, 0);
float cy = result_data.At<float>(i, 1);
float ow = result_data.At<float>(i, 2);
float oh = result_data.At<float>(i, 3);
int x = (int)((cx - 0.5 * ow) * factor);
int y = (int)((cy - 0.5 * oh) * factor);
int width = (int)(ow * factor);
int height = (int)(oh * factor);
Rect box = new Rect();
box.X = x;
box.Y = y;
box.Width = width;
box.Height = height;
position_boxes.Add(box);
class_ids.Add(max_classId_point.X);
confidences.Add((float)confidence);
}
}
// NMS non maximum suppression
int[] indexes = new int[position_boxes.Count];
CvDnn.NMSBoxes(position_boxes, confidences, 0.5f, 0.5f, out indexes);
for (int i = 0; i < indexes.Length; i++)
{
int index = indexes[i];
Cv2.Rectangle(image, position_boxes[index], new Scalar(0, 0, 255), 2, LineTypes.Link8);
Cv2.Rectangle(image, new OpenCvSharp.Point(position_boxes[index].TopLeft.X, position_boxes[index].TopLeft.Y + 30),
new OpenCvSharp.Point(position_boxes[index].BottomRight.X, position_boxes[index].TopLeft.Y), new Scalar(0, 255, 255), -1);
Cv2.PutText(image, class_ids[index] + "-" + confidences[index].ToString("0.00"),
new OpenCvSharp.Point(position_boxes[index].X, position_boxes[index].Y + 25),
HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);
}
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);
}
(2) 使用模型结构处理处理数据
目前 OpenVINO™ 已经支持在模型结构中增加数据的前后处理流程,并且在 OpenVINO™ C# API 中也已经实现了该功能接口,所以在此处演示了如何将模型输入数据处理流程封装到模型中,通过 OpenVINO™ 进行数据处理的加速处理,如下面代码所示:
static void yolov5_det_with_process(string model_path, string image_path, string device)
{
······
// -------- Step 2. Read inference model --------
start = DateTime.Now;
Model model = core.read_model(model_path);
OvExtensions.printf_model_info(model);
PrePostProcessor processor = new PrePostProcessor(model);
Tensor input_tensor_pro = new Tensor(new OvType(ElementType.U8), new Shape(1, 640, 640, 3));
InputInfo input_info = processor.input(0);
InputTensorInfo input_tensor_info = input_info.tensor();
input_tensor_info.set_from(input_tensor_pro).set_layout(new Layout("NHWC")).set_color_format(ColorFormat.BGR);
PreProcessSteps process_steps = input_info.preprocess();
process_steps.convert_color(ColorFormat.RGB).resize(ResizeAlgorithm.RESIZE_LINEAR)
.convert_element_type(new OvType(ElementType.F32)).scale(255.0f).convert_layout(new Layout("NCHW"));
Model new_model = processor.build();
// -------- Step 3. Loading a model to the device --------
CompiledModel compiled_model = core.compile_model(new_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
int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
Rect roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));
Cv2.Resize(max_image, max_image, new OpenCvSharp.Size(640, 640));
float factor = (float)(max_image_length / 640.0);
// -------- Step 6. Set up input data --------
Tensor input_tensor = infer_request.get_input_tensor();
Shape input_shape = input_tensor.get_shape();
byte[] input_data = new byte[input_shape[1] * input_shape[2] * input_shape[3]];
Marshal.Copy(max_image.Ptr(0), input_data, 0, input_data.Length);
IntPtr destination = input_tensor.data();
Marshal.Copy(input_data, 0, destination, input_data.Length);
// -------- Step 7. Do inference synchronously --------
······
}
由于目前还没有完全实现所有的 OpenVINO™ 的预处理接口,因此只能实现部分预处理过程封装到模型中,此处主要是做了以下处理:
- 数据类型转换:byte->float
- 数据维度转换:NHWC->NCHW
- 图像色彩空间转换:BGR->RGB
- 数据归一化处理:[0,1]->[0,255]
因此将一些数据处理流程封装到模型中后,在进行模型推理时,只需要将读取到的图片数据Resize为640*640后,就可以直接将数据加载到模型即可。
(3) 使用 OpenVINO™ C# API 封装的接口
YOLOv5 是当前工业领域十分流行的目标检测模型,因此在封装 OpenVINO™ C# API 时,提供了快速部署 Yolov5 模型的接口,实现代码如下所示:
static void yolov5_det_using_extensions(string model_path, string image_path, string device)
{
Yolov5DetConfig config = new Yolov5DetConfig();
config.set_model(model_path);
Yolov5Det yolov8 = new Yolov5Det(config);
Mat image = Cv2.ImRead(image_path);
DetResult result = yolov8.predict(image);
Mat result_im = Visualize.draw_det_result(result, image);
Cv2.ImShow("Result", result_im);
Cv2.WaitKey(0);
}
3.4 预测方法调用
定义好上述方法后,便可以直接在主函数中调用该方法,只需要在主函数中增加以下代码即可:
yolov5_det("yolov5s.xml", "test_image.png", "AUTO");
yolov5_det_with_process("yolov5s.xml", "test_image.png", "AUTO");
yolov5_det_using_extensions("yolov5s.xml", "test_image.png", "AUTO");
如果开发者自己没有进行模型下载与转换,又同时想快速体验该项目,我此处提供了在线的转换后的模型以及带预测图片,开发者可以直接在主函数中增加以下代码,便可以直接自动下载模型以及推理数据,并调用推理方法,实现程序直接运行。
static void Main(string[] args)
{
string model_path = "";
string image_path = "";
string device = "AUTO";
if (args.Length == 0)
{
if (!Directory.Exists("./model"))
{
Directory.CreateDirectory("./model");
}
if (!File.Exists("./model/yolov5s.bin") && !File.Exists("./model/yolov5s.bin"))
{
if (!File.Exists("./model/yolov5s.tar"))
{
_ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Model/yolov5s.tar",
"./model/yolov5s.tar").Result;
}
Download.unzip("./model/yolov585s.tar", "./model/");
}
if (!File.Exists("./model/test_image.jpg"))
{
_ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Image/test_det_02.jpg",
"./model/test_image.jpg").Result;
}
model_path = "./model/yolov5s.xml";
image_path = "./model/test_image.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 yolov8 model inference.");
yolov5_det(model_path, image_path, device);
//yolov5_det_with_process(model_path, image_path, device);
//yolov5_det_using_extensions(model_path, image_path, device);
}
为了减少文章篇幅,所以此处只提供了有差异的代码,如果想获取完整代码,请访问GitHub代码仓库,获取项目源码,链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov5/yolov5_det_opencvsharp
4. Yolov5 DET 项目配置(Emgu.CV 版)
同样地,为了满足Emgu.CV开发者的需求,此处同样地提供了Emgu.CV版本的Yolov5的模型部署代码以及使用流程,此处为了简化文章内容,对于和上文重复的步骤不在进行展开讲述。
4.1 添加项目依赖
首先是安装OpenVINO™ C# API项目依赖,在命令行中输入以下指令即可:
dotnet add package OpenVINO.CSharp.API
dotnet add package OpenVINO.runtime.macos-arm64
dotnet add package OpenVINO.CSharp.API.Extensions
dotnet add package OpenVINO.CSharp.API.Extensions.EmguCV
接下来安装使用到的图像处理库 Emgu.CV,在命令行中输入以下指令即可:
dotnet add package Emgu.CV
dotnet add package Emgu.CV.runtime.mini.macos
关于在MacOS上搭建 OpenCvSharp 开发环境请参考以下文章: 【OpenCV】在MacOS上使用Emgu.CV
添加完成项目依赖后,项目的配置文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>yolov5_det</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Emgu.CV" Version="4.8.1.5350" />
<PackageReference Include="Emgu.CV.runtime.mini.macos" Version="4.8.1.5350" />
<PackageReference Include="OpenVINO.CSharp.API" Version="2023.2.0.4" />
<PackageReference Include="OpenVINO.CSharp.API.Extensions" Version="1.0.1" />
<PackageReference Include="OpenVINO.CSharp.API.Extensions.EmguCV" Version="1.0.4.1" />
<PackageReference Include="OpenVINO.runtime.macos-arm64" Version="2023.3.0.1" />
</ItemGroup>
</Project>
4.2 定义预测方法
模型部署流程与上一节中使用OpenCvSharp的基本一致,主要是替换了图像处理的工具,同时提供了如上一节中所展示的三种部署方式。此处为了减少文章篇幅,此处不在展示详细的部署代码,如果想获取相关代码,请访问项目GitHub,下载所有的测试代码,项目链接为:
https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov5/yolov5_det_emgucv
5. 项目运行与演示
5.1 项目编译
接下来输入项目编译指令进行项目编译,输入以下指令即可:
dotnet build
程序编译后输出为:
接下来运行编译后的程序文件,在CMD中输入以下指令,运行编译后的项目文件:
dotnet run --no-build
运行后项目输出为:
6. 总结
在该项目中,我们结合之前开发的 OpenVINO C# API 项目部署YOLOv5模型,成功实现了对象目标检测,并且根据不同开发者的使用习惯,同时提供了OpenCvSharp以及Emgu.CV两种版本,供各位开发者使用。最后如果各位开发者在使用中有任何问题,欢迎大家与我联系。