经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
【OpenVINO?】在 Windows 上使用 OpenVINO? C# API 部署 Yolov8-obb 实现任意方向的目标检测
来源:cnblogs  作者:椒颜皮皮虾  时间:2024/2/2 9:13:32  对本文有异议
?前言

Ultralytics YOLOv8 基于深度学习和计算机视觉领域的尖端技术,在速度和准确性方面具有无与伦比的性能。其流线型设计使其适用于各种应用,并可轻松适应从边缘设备到云 API 等不同硬件平台。YOLOv8 OBB 模型是YOLOv8系列模型最新推出的任意方向的目标检测模型,可以检测任意方向的对象,大大提高了物体检测的精度。同时官方发布的模型已经支持 OpenVINO? 部署工具加速模型推理,因此在该项目中,我们将结合之前开发的 OpenVINO? C# API 部署YOLOv8 OBB 模型实现旋转物体对象检测。

项目链接为:

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

项目源码链接为:

  1. https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov8/yolov8_obb_opencvsharp
  2. https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov8/yolov8_obb_emgucv

1. 前言

1.1 OpenVINO? C# API

英特尔发行版 OpenVINO? 工具套件基于 oneAPI 而开发,可以加快高性能计算机视觉和深度学习视觉应用开发速度工具套件,适用于从边缘到云的各种英特尔平台上,帮助用户更快地将更准确的真实世界结果部署到生产系统中。通过简化的开发工作流程,OpenVINO? 可赋能开发者在现实世界中部署高性能应用程序和算法。

OpenVINO? 2023.2 于 2023 年 11 月 16 日发布,该工具包带来了挖掘生成人工智能全部潜力的新功能。更多的生成式 AI 覆盖和框架集成,以最大限度地减少代码更改,并且扩展了对直接 PyTorch 模型转换的模型支持。支持更多新的模型,包括 LLaVA、chatGLM、Bark 和 LCM 等著名模型。支持更广泛的大型语言模型(LLM)和更多模型压缩技术,支持运行时推理支持以下 Int4 模型压缩格式,通过神经网络压缩框架(NNCF) 进行本机 Int4 压缩等一系列新的功能。

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# 语言实现深度学习模型在指定平台推理加速。

1.2 YOLOv8 OBB 模型

目标检测是计算机视觉中的一项基本任务,目前许多研究都是采用水平边界框来定位图像中的物体。然而,图像中的物体通常是任意方向的。因此,使用水平边界框来检测目标会引起物体检测框通常包含许多背景区域,检测框内存在过多的背景区域,不仅增加了分类任务的难度,而且会导致目标范围表示不准确的问题。其次,水平边界框会导致检测框之间出现重叠,降低检测精度。

通过使用带有角度信息的旋转检测框可以有效解决上述问题,它引入了一个额外的角度来更准确地定位图像中的物体。Ultralytics YOLOv8 基于深度学习和计算机视觉领域的尖端技术,在速度和准确性方面具有无与伦比的性能。其流线型设计使其适用于各种应用,并可轻松适应从边缘设备到云 API 等不同硬件平台。YOLOv8 OBB 模型是YOLOv8系列模型最新推出的任意方向的目标检测模型,其模型输出结果是一组旋转的边界框,这些边界框精确地包围了图像中的物体,同时还包括每个边界框的类标签和置信度分数。当你需要识别场景中感兴趣的物体,但又不需要知道物体的具体位置或确切形状时,物体检测是一个不错的选择。

2. YOLOv8 OBB模型下载与转换

2.1 安装模型下载与转换环境

此处主要安装 YOLOv8 OBB模型导出环境以及OpenVINO?模型转换环境,使用Anaconda进行环境创建,依次输入以下指令:

  1. conda create -n yolo python=3.10
  2. conda activate yolo
  3. pip install ultralytics
  4. pip install --upgrade openvino-nightly

此处只需要安装以上两个程序包即可。

2.2 导出 YOLOv8 OBB模型

接下来以Yolov8s-obb模型到处为例,演示如何快速导出官方提供的预训练模型,首先在创建的虚拟环境中输入以下命令:

  1. yolo export model=yolov8s-obb.pt format=onnx

下图展示了模型导出命令输出情况:

接下来查看导出的Yolov8s-obb模型的结构情况,通过Netron可以进行查看,如下所示:

模型识别类别:由于官方模型是在DOTAv1数据集上预训练的,因此导出的模型可以识别出15种类别,分别是:plane 、ship storage tank、baseball diamond、 tennis court、basketball court、ground track field、harbor、 bridge、large vehicle、 small vehicle、helicopter、roundabout、soccer ball field、 swimming pool。

模型输入:模型输入名称为“image”,输入大小为1×3×1024×1024的归一化后的图像数据,数据类型为float。

模型输出:模型的输出名称为“output0”,输出大小为1×20×21504,输出数据为float。其中“20”表示为[x, y, w, h, sorce0, ···, sorce14, angle]的数组,数组中03这四个参数为预测框的矩形位置,数组中418这15个参数表示15各类别的分类置信度,数组中19这个参数表示预测框的旋转角度;“21504”表示输入为1024的模型三个检测头的输出大小,总共有21504(1024÷8=128,1024÷16=64,1024÷32=32,128×128+64×64+32×32=21504)种结果。

2.3 转换 IR 模型

接下来直接使用OpenVINO?工具直接进行模型转换,在CMD中输入以下指令即可:

  1. ovc yolov8s-obb.onnx

模型导出输出如下图所示。

3. YOLOv8 OBB项目配置(OpenCvSharp)

此处项目创建与运行演示使用dotnet工具进行,大家也可以通过Visual Studio等IDE工具进行项目创建与运行。首先讲解一下使用OpenCvSharp作为图像处理工具的代码程序。

3.1 项目创建

首先使用dotnet工具创建一个控制台应用,在CMD中输入以下指令:

  1. dotnet new console --framework net6.0 --use-program-main -o yolov8_obb_opencvsharp
  2. cd yolov8_obb_opencvsharp

项目创建后输出为:

3.2 添加项目依赖

此处以Windows平台为例安装项目依赖,首先是安装OpenVINO? C# API项目依赖,在命令行中输入以下指令即可:

  1. dotnet add package OpenVINO.CSharp.API
  2. dotnet add package OpenVINO.runtime.win
  3. dotnet add package OpenVINO.CSharp.API.Extensions
  4. dotnet add package OpenVINO.CSharp.API.Extensions.OpenCvSharp

接下来安装使用到的图像处理库OpenCvSharp,在命令行中输入以下指令即可:

  1. dotnet add package OpenCvSharp4
  2. dotnet add package OpenCvSharp4.Extensions
  3. dotnet add package OpenCvSharp4.runtime.win

添加完成项目依赖后,项目的配置文件如下所示:

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <OutputType>Exe</OutputType>
  4. <TargetFramework>net6.0</TargetFramework>
  5. <ImplicitUsings>enable</ImplicitUsings>
  6. <Nullable>enable</Nullable>
  7. </PropertyGroup>
  8. <ItemGroup>
  9. <PackageReference Include="OpenCvSharp4" Version="4.9.0.20240103" />
  10. <PackageReference Include="OpenCvSharp4.Extensions" Version="4.9.0.20240103" />
  11. <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.9.0.20240103" />
  12. <PackageReference Include="OpenVINO.CSharp.API" Version="2023.2.0.4" />
  13. <PackageReference Include="OpenVINO.CSharp.API.Extensions" Version="1.0.1" />
  14. <PackageReference Include="OpenVINO.CSharp.API.Extensions.OpenCvSharp" Version="1.0.4" />
  15. <PackageReference Include="OpenVINO.runtime.win" Version="2023.3.0.1" />
  16. </ItemGroup>
  17. </Project>

3.3 定义预测方法

Yolov8 Obb模型部署流程与方式与Yolov8 Det基本一致,其主要不同点在与其结果的后处理方式,本项目所定义的Yolov8 Obb模型推理代码如下所示:

  1. static void yolov8_obb(string model_path, string image_path, string device)
  2. {
  3. // -------- Step 1. Initialize OpenVINO Runtime Core --------
  4. Core core = new Core();
  5. // -------- Step 2. Read inference model --------
  6. Model model = core.read_model(model_path);
  7. OvExtensions.printf_model_info(model);
  8. // -------- Step 3. Loading a model to the device --------
  9. CompiledModel compiled_model = core.compile_model(model, device);
  10. // -------- Step 4. Create an infer request --------
  11. InferRequest infer_request = compiled_model.create_infer_request();
  12. // -------- Step 5. Process input images --------
  13. Mat image = new Mat(image_path); // Read image by opencvsharp
  14. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
  15. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
  16. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
  17. image.CopyTo(new Mat(max_image, roi));
  18. float factor = (float)(max_image_length / 1024.0);
  19. // -------- Step 6. Set up input data --------
  20. Tensor input_tensor = infer_request.get_input_tensor();
  21. Shape input_shape = input_tensor.get_shape();
  22. Mat input_mat = CvDnn.BlobFromImage(max_image, 1.0 / 255.0, new OpenCvSharp.Size(input_shape[2], input_shape[3]), 0, true, false);
  23. float[] input_data = new float[input_shape[1] * input_shape[2] * input_shape[3]];
  24. Marshal.Copy(input_mat.Ptr(0), input_data, 0, input_data.Length);
  25. input_tensor.set_data<float>(input_data);
  26. // -------- Step 7. Do inference synchronously --------
  27. infer_request.infer();
  28. // -------- Step 8. Get infer result data --------
  29. Tensor output_tensor = infer_request.get_output_tensor();
  30. int output_length = (int)output_tensor.get_size();
  31. float[] output_data = output_tensor.get_data<float>(output_length);
  32. // -------- Step 9. Process reault --------
  33. Mat result_data = new Mat(20, 21504, MatType.CV_32F, output_data);
  34. result_data = result_data.T();
  35. float[] d = new float[output_length];
  36. result_data.GetArray<float>(out d);
  37. // Storage results list
  38. List<Rect2d> position_boxes = new List<Rect2d>();
  39. List<int> class_ids = new List<int>();
  40. List<float> confidences = new List<float>();
  41. List<float> rotations = new List<float>();
  42. // Preprocessing output results
  43. for (int i = 0; i < result_data.Rows; i++)
  44. {
  45. Mat classes_scores = new Mat(result_data, new Rect(4, i, 15, 1));
  46. OpenCvSharp.Point max_classId_point, min_classId_point;
  47. double max_score, min_score;
  48. // Obtain the maximum value and its position in a set of data
  49. Cv2.MinMaxLoc(classes_scores, out min_score, out max_score,
  50. out min_classId_point, out max_classId_point);
  51. // Confidence level between 0 ~ 1
  52. // Obtain identification box information
  53. if (max_score > 0.25)
  54. {
  55. float cx = result_data.At<float>(i, 0);
  56. float cy = result_data.At<float>(i, 1);
  57. float ow = result_data.At<float>(i, 2);
  58. float oh = result_data.At<float>(i, 3);
  59. double x = (cx - 0.5 * ow) * factor;
  60. double y = (cy - 0.5 * oh) * factor;
  61. double width = ow * factor;
  62. double height = oh * factor;
  63. Rect2d box = new Rect2d();
  64. box.X = x;
  65. box.Y = y;
  66. box.Width = width;
  67. box.Height = height;
  68. position_boxes.Add(box);
  69. class_ids.Add(max_classId_point.X);
  70. confidences.Add((float)max_score);
  71. rotations.Add(result_data.At<float>(i, 19));
  72. }
  73. }
  74. // NMS non maximum suppression
  75. int[] indexes = new int[position_boxes.Count];
  76. CvDnn.NMSBoxes(position_boxes, confidences, 0.25f, 0.7f, out indexes);
  77. List<RotatedRect> rotated_rects = new List<RotatedRect>();
  78. for (int i = 0; i < indexes.Length; i++)
  79. {
  80. int index = indexes[i];
  81. float w = (float)position_boxes[index].Width;
  82. float h = (float)position_boxes[index].Height;
  83. float x = (float)position_boxes[index].X + w / 2;
  84. float y = (float)position_boxes[index].Y + h / 2;
  85. float r = rotations[index];
  86. float w_ = w > h ? w : h;
  87. float h_ = w > h ? h : w;
  88. r = (float)((w > h ? r : (float)(r + Math.PI / 2)) % Math.PI);
  89. RotatedRect rotate = new RotatedRect(new Point2f(x, y), new Size2f(w_, h_), (float)(r * 180.0 / Math.PI));
  90. rotated_rects.Add(rotate);
  91. }
  92. for (int i = 0; i < indexes.Length; i++)
  93. {
  94. int index = indexes[i];
  95. Point2f[] points = rotated_rects[i].Points();
  96. for (int j = 0; j < 4; j++)
  97. {
  98. Cv2.Line(image, (Point)points[j], (Point)points[(j + 1) % 4], new Scalar(255, 100, 200), 2);
  99. }
  100. Cv2.PutText(image, class_lables[class_ids[index]] + "-" + confidences[index].ToString("0.00"),
  101. (Point)points[0], HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);
  102. }
  103. string output_path = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(image_path)),
  104. Path.GetFileNameWithoutExtension(image_path) + "_result.jpg");
  105. Cv2.ImWrite(output_path, image);
  106. Slog.INFO("The result save to " + output_path);
  107. Cv2.ImShow("Result", image);
  108. Cv2.WaitKey(0);
  109. }

Yolov8 Obb模型的一条预测结果输出为 [x, y, w, h, sorce0, ···, sorce14, angle],其中前19个数据与Yolov8 Det模型的数据处理方式是一致的,主要是多了一个预测框旋转角度,因此在处理时需要同时记录旋转角度这一个数据。

3.4 预测方法调用

定义好上述方法后,便可以直接在主函数中调用该方法,只需要在主函数中增加以下代码即可:

  1. yolov8_obb("yolov8s-obb.xml", "test_image.png", "AUTO");

如果开发者自己没有进行模型下载与转换,又同时想快速体验该项目,我此处提供了在线的转换后的模型以及带预测图片,开发者可以直接在主函数中增加以下代码,便可以直接自动下载模型以及推理数据,并调用推理方法,实现程序直接运行。

  1. static void Main(string[] args)
  2. {
  3. string model_path = "";
  4. string image_path = "";
  5. string device = "AUTO";
  6. if (args.Length == 0)
  7. {
  8. if (!Directory.Exists("./model"))
  9. {
  10. Directory.CreateDirectory("./model");
  11. }
  12. if (!File.Exists("./model/yolov8s-obb.bin") && !File.Exists("./model/yolov8s-obb.bin"))
  13. {
  14. if (!File.Exists("./model/yolov8s-obb.tar"))
  15. {
  16. _ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Model/yolov8s-obb.tar",
  17. "./model/yolov8s-obb.tar").Result;
  18. }
  19. Download.unzip("./model/yolov8s-obb.tar", "./model/");
  20. }
  21. if (!File.Exists("./model/plane.png"))
  22. {
  23. _ = Download.download_file_async("https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Image/plane.png",
  24. "./model/plane.png").Result;
  25. }
  26. model_path = "./model/yolov8s-obb.xml";
  27. image_path = "./model/plane.png";
  28. }
  29. else if (args.Length >= 2)
  30. {
  31. model_path = args[0];
  32. image_path = args[1];
  33. device = args[2];
  34. }
  35. else
  36. {
  37. Console.WriteLine("Please enter the correct command parameters, for example:");
  38. Console.WriteLine("> 1. dotnet run");
  39. Console.WriteLine("> 2. dotnet run <model path> <image path> <device name>");
  40. }
  41. // -------- Get OpenVINO runtime version --------
  42. OpenVinoSharp.Version version = Ov.get_openvino_version();
  43. Slog.INFO("---- OpenVINO INFO----");
  44. Slog.INFO("Description : " + version.description);
  45. Slog.INFO("Build number: " + version.buildNumber);
  46. Slog.INFO("Predict model files: " + model_path);
  47. Slog.INFO("Predict image files: " + image_path);
  48. Slog.INFO("Inference device: " + device);
  49. Slog.INFO("Start yolov8 model inference.");
  50. yolov8_obb(model_path, image_path, device);
  51. }

备注:

上述项目中的完整代码全部放在GitHub上开源,项目链接为:

  1. https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov8/yolov8_obb_opencvsharp

4. YOLOv8 OBB项目配置(Emgu.CV)

相信有不少开发者在C#中进行图像处理时,使用的是Emgu.CV工具,因此,在此处我们同时提供了使用 Emgu.CV作为图像处理工具的YOLOv8 OBB模型部署代码。项目的创建方式与流程与上一节中一致,此处不再进行演示:

4.1 添加项目依赖

此处以Windows平台为例安装项目依赖,首先是安装OpenVINO? C# API项目依赖,在命令行中输入以下指令即可:

  1. dotnet add package OpenVINO.CSharp.API
  2. dotnet add package OpenVINO.runtime.win
  3. dotnet add package OpenVINO.CSharp.API.Extensions
  4. dotnet add package OpenVINO.CSharp.API.Extensions.EmguCV

接下来安装使用到的图像处理库Emgu.CV,在命令行中输入以下指令即可:

  1. dotnet add package Emgu.CV
  2. dotnet add package Emgu.CV.runtime.windows

添加完成项目依赖后,项目的配置文件如下所示:

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <OutputType>Exe</OutputType>
  4. <TargetFramework>net6.0</TargetFramework>
  5. <ImplicitUsings>enable</ImplicitUsings>
  6. <Nullable>enable</Nullable>
  7. </PropertyGroup>
  8. <ItemGroup>
  9. <PackageReference Include="Emgu.CV" Version="4.8.1.5350" />
  10. <PackageReference Include="Emgu.CV.runtime.windows" Version="4.8.1.5350" />
  11. <PackageReference Include="OpenVINO.CSharp.API" Version="2023.2.0.4" />
  12. <PackageReference Include="OpenVINO.CSharp.API.Extensions" Version="1.0.1" />
  13. <PackageReference Include="OpenVINO.CSharp.API.Extensions.EmguCV" Version="1.0.4.1" />
  14. <PackageReference Include="OpenVINO.runtime.win" Version="2023.3.0.1" />
  15. </ItemGroup>
  16. </Project>

4.2 定义预测方法

Yolov8 Obb模型部署流程与上一节中的流程一致,主要是替换了图像处理方式,其实现代码如下:

  1. static void yolov8_obb(string model_path, string image_path, string device)
  2. {
  3. // -------- Step 1. Initialize OpenVINO Runtime Core --------
  4. Core core = new Core();
  5. // -------- Step 2. Read inference model --------
  6. OpenVinoSharp.Model model = core.read_model(model_path);
  7. OvExtensions.printf_model_info(model);
  8. // -------- Step 3. Loading a model to the device --------
  9. CompiledModel compiled_model = core.compile_model(model, device);
  10. // -------- Step 4. Create an infer request --------
  11. InferRequest infer_request = compiled_model.create_infer_request();
  12. // -------- Step 5. Process input images --------
  13. Mat image = new Mat(image_path); // Read image by opencvsharp
  14. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
  15. Mat max_image = Mat.Zeros(max_image_length, max_image_length, DepthType.Cv8U, 3);
  16. Rectangle roi = new Rectangle(0, 0, image.Cols, image.Rows);
  17. image.CopyTo(new Mat(max_image, roi));
  18. float factor = (float)(max_image_length / 1024.0);
  19. // -------- Step 6. Set up input data --------
  20. Tensor input_tensor = infer_request.get_input_tensor();
  21. Shape input_shape = input_tensor.get_shape();
  22. Mat input_mat = DnnInvoke.BlobFromImage(max_image, 1.0 / 255.0, new Size((int)input_shape[2], (int)input_shape[3]), new MCvScalar(0), true, false);
  23. float[] input_data = new float[input_shape[1] * input_shape[2] * input_shape[3]];
  24. //Marshal.Copy(input_mat.Ptr, input_data, 0, input_data.Length);
  25. input_mat.CopyTo<float>(input_data);
  26. input_tensor.set_data<float>(input_data);
  27. // -------- Step 7. Do inference synchronously --------
  28. infer_request.infer();
  29. // -------- Step 8. Get infer result data --------
  30. Tensor output_tensor = infer_request.get_output_tensor();
  31. int output_length = (int)output_tensor.get_size();
  32. float[] output_data = output_tensor.get_data<float>(output_length);
  33. // -------- Step 9. Process reault --------
  34. Mat result_data = new Mat(20, 21504, DepthType.Cv32F, 1,
  35. Marshal.UnsafeAddrOfPinnedArrayElement(output_data, 0), 4 * 21504);
  36. result_data = result_data.T();
  37. List<Rectangle> position_boxes = new List<Rectangle>();
  38. List<int> class_ids = new List<int>();
  39. List<float> confidences = new List<float>();
  40. List<float> rotations = new List<float>();
  41. // Preprocessing output results
  42. for (int i = 0; i < result_data.Rows; i++)
  43. {
  44. Mat classes_scores = new Mat(result_data, new Rectangle(4, i, 15, 1));//GetArray(i, 5, classes_scores);
  45. Point max_classId_point = new Point(), min_classId_point = new Point();
  46. double max_score = 0, min_score = 0;
  47. CvInvoke.MinMaxLoc(classes_scores, ref min_score, ref max_score,
  48. ref min_classId_point, ref max_classId_point);
  49. if (max_score > 0.25)
  50. {
  51. Mat mat = new Mat(result_data, new Rectangle(0, i, 20, 1));
  52. float[,] data = (float[,])mat.GetData();
  53. float cx = data[0, 0];
  54. float cy = data[0, 1];
  55. float ow = data[0, 2];
  56. float oh = data[0, 3];
  57. int x = (int)((cx - 0.5 * ow) * factor);
  58. int y = (int)((cy - 0.5 * oh) * factor);
  59. int width = (int)(ow * factor);
  60. int height = (int)(oh * factor);
  61. Rectangle box = new Rectangle();
  62. box.X = x;
  63. box.Y = y;
  64. box.Width = width;
  65. box.Height = height;
  66. position_boxes.Add(box);
  67. class_ids.Add(max_classId_point.X);
  68. confidences.Add((float)max_score);
  69. rotations.Add(data[0, 19]);
  70. }
  71. }
  72. // NMS non maximum suppression
  73. int[] indexes = DnnInvoke.NMSBoxes(position_boxes.ToArray(), confidences.ToArray(), 0.5f, 0.5f);
  74. List<RotatedRect> rotated_rects = new List<RotatedRect>();
  75. for (int i = 0; i < indexes.Length; i++)
  76. {
  77. int index = indexes[i];
  78. float w = (float)position_boxes[index].Width;
  79. float h = (float)position_boxes[index].Height;
  80. float x = (float)position_boxes[index].X + w / 2;
  81. float y = (float)position_boxes[index].Y + h / 2;
  82. float r = rotations[index];
  83. float w_ = w > h ? w : h;
  84. float h_ = w > h ? h : w;
  85. r = (float)((w > h ? r : (float)(r + Math.PI / 2)) % Math.PI);
  86. RotatedRect rotate = new RotatedRect(new PointF(x, y), new SizeF(w_, h_), (float)(r * 180.0 / Math.PI));
  87. rotated_rects.Add(rotate);
  88. }
  89. for (int i = 0; i < indexes.Length; i++)
  90. {
  91. int index = indexes[i];
  92. PointF[] points = rotated_rects[i].GetVertices();
  93. for (int j = 0; j < 4; j++)
  94. {
  95. CvInvoke.Line(image, new Point((int)points[j].X, (int)points[j].Y),
  96. new Point((int)points[(j + 1) % 4].X, (int)points[(j + 1) % 4].Y), new MCvScalar(255, 100, 200), 2);
  97. }
  98. CvInvoke.PutText(image, class_lables[class_ids[index]] + "-" + confidences[index].ToString("0.00"),
  99. new Point((int)points[0].X, (int)points[0].Y), FontFace.HersheySimplex, 0.8, new MCvScalar(0, 0, 0), 2);
  100. }
  101. string output_path = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(image_path)),
  102. Path.GetFileNameWithoutExtension(image_path) + "_result.jpg");
  103. CvInvoke.Imwrite(output_path, image);
  104. Slog.INFO("The result save to " + output_path);
  105. CvInvoke.Imshow("Result", image);
  106. CvInvoke.WaitKey(0);
  107. }

备注:

上述项目中的完整代码全部放在GitHub上开源,项目链接为:

  1. https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/tree/master/model_samples/yolov8/yolov8_obb_emgucv

5. 项目编译与运行

5.1 项目文件编译

接下来输入项目编译指令进行项目编译,输入以下指令即可:

  1. dotnet build

程序编译后输出为:

5.2 项目文件运行

接下来运行编译后的程序文件,在CMD中输入以下指令,运行编译后的项目文件:

  1. dotnet run --no-build

运行后项目输出为:

6. 总结

在该项目中,我们结合之前开发的 OpenVINO? C# API 项目部署YOLOv8 OBB 模型,成功实现了旋转对象目标检测,并且根据不同开发者的使用习惯,同时提供了OpenCvSharp以及Emgu.CV两种版本,供各位开发者使用。最后如果各位开发者在使用中有任何问题,欢迎大家与我联系。

原文链接:https://www.cnblogs.com/guojin-blogs/p/17999039

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号