├── .gitattributes
├── .gitignore
├── .gitmodules
├── CONTRIBUTING.md
├── CONTRIBUTING_cn.md
├── LICENSE.txt
├── OpenVINO.sln
├── README.md
├── README_cn.md
├── docs
├── inatall
│ ├── Install_OpenVINO_CSharp_Linux_cn.md
│ ├── Install_OpenVINO_CSharp_MacOS_cn.md
│ └── Install_OpenVINO_CSharp_Windows_cn.md
├── introduce
│ └── 3_1_detailed_introduction_cn.md
└── tutorials
│ ├── macos_deploy_yolov5_guide_cn.md
│ └── windows_deploy_yolov8-obb_guide_cn.md
├── nuget
├── NuGet.png
├── README.md
└── logo.png
├── src
├── CSharp.API.Extensions.OpenCvSharp
│ ├── CSharp.API.Extensions.OpenCvSharp.csproj
│ ├── model
│ │ ├── ppyoloe
│ │ │ └── ppyoloe_det.cs
│ │ ├── predictor.cs
│ │ ├── rtdetr
│ │ │ └── rtdetr_det.cs
│ │ ├── runtime_option
│ │ │ ├── Config.cs
│ │ │ ├── dataset_option.cs
│ │ │ ├── ppyoloe_config.cs
│ │ │ ├── rtdetr_config.cs
│ │ │ ├── yolo_option.cs
│ │ │ ├── yolov5_config.cs
│ │ │ └── yolov8_config.cs
│ │ ├── yolov5
│ │ │ ├── yolov5_det.cs
│ │ │ └── yolov5_seg.cs
│ │ └── yolov8
│ │ │ ├── yolov8_cls.cs
│ │ │ ├── yolov8_det.cs
│ │ │ ├── yolov8_pose.cs
│ │ │ └── yolov8_seg.cs
│ ├── process
│ │ ├── normalize.cs
│ │ ├── permute.cs
│ │ ├── resize.cs
│ │ └── visualize.cs
│ └── result
│ │ ├── clsresult.cs
│ │ ├── detresult.cs
│ │ ├── obbresult.cs
│ │ ├── poseresult.cs
│ │ ├── result.cs
│ │ └── segresult.cs
├── CSharpAPI.Extensions.EmguCV
│ ├── CSharpAPI.Extensions.EmguCV.csproj
│ ├── model
│ │ ├── ppyoloe
│ │ │ └── ppyoloe_det.cs
│ │ ├── predictor.cs
│ │ ├── rtdetr
│ │ │ └── rtdetr_det.cs
│ │ ├── runtime_option
│ │ │ ├── Config.cs
│ │ │ ├── dataset_option.cs
│ │ │ ├── ppyoloe_config.cs
│ │ │ ├── rtdetr_config.cs
│ │ │ ├── yolo_option.cs
│ │ │ ├── yolov5_config.cs
│ │ │ └── yolov8_config.cs
│ │ ├── yolov5
│ │ │ ├── yolov5_det.cs
│ │ │ └── yolov5_seg.cs
│ │ └── yolov8
│ │ │ ├── yolov8_cls.cs
│ │ │ ├── yolov8_det.cs
│ │ │ ├── yolov8_pose.cs
│ │ │ └── yolov8_seg.cs
│ ├── process
│ │ ├── normalize.cs
│ │ ├── permute.cs
│ │ ├── resize.cs
│ │ └── visualize.cs
│ └── result
│ │ ├── clsresult.cs
│ │ ├── detresult.cs
│ │ ├── obbresult.cs
│ │ ├── poseresult.cs
│ │ ├── result.cs
│ │ └── segresult.cs
├── CSharpAPI.Extensions
│ ├── CSharpAPI.Extensions.csproj
│ ├── base.cs
│ ├── benchmark_app
│ │ ├── benchmark.cs
│ │ └── common.cs
│ ├── ov_extensions.cs
│ └── utility
│ │ ├── slog.cs
│ │ ├── utility.cs
│ │ └── utility_download.cs
└── CSharpAPI
│ ├── CSharpAPI.csproj
│ ├── base.cs
│ ├── common
│ ├── common.cs
│ ├── element_type.cs
│ ├── property.cs
│ └── version.cs
│ ├── core
│ ├── compiled_model.cs
│ ├── core.cs
│ ├── dimension.cs
│ ├── infer_request.cs
│ ├── layout.cs
│ ├── model.cs
│ ├── node.cs
│ ├── node_input.cs
│ ├── node_output.cs
│ ├── partial_shape.cs
│ ├── remote_context.cs
│ ├── shape.cs
│ └── tensor.cs
│ ├── exception
│ ├── exception.cs
│ └── handle_exception.cs
│ ├── native_methods
│ ├── ov_base.cs
│ ├── ov_common.cs
│ ├── ov_compiled_model.cs
│ ├── ov_core.cs
│ ├── ov_dimension.cs
│ ├── ov_infer_request.cs
│ ├── ov_layout.cs
│ ├── ov_model.cs
│ ├── ov_node.cs
│ ├── ov_partial_shape.cs
│ ├── ov_prepostprocess.cs
│ ├── ov_rank.cs
│ ├── ov_shape.cs
│ └── ov_tensor.cs
│ ├── ov
│ ├── ov.cs
│ └── ov_struct.cs
│ └── preprocess
│ ├── common.cs
│ ├── input_info.cs
│ ├── input_model_info.cs
│ ├── input_tensor_info.cs
│ ├── output_info.cs
│ ├── output_tensor_info.cs
│ ├── prepost_processor.cs
│ └── preprocess_steps.cs
└── tests
├── csharp_api_extensions_emgucv_unit_tests
├── csharp_api_extensions_emgucv_unit_tests.csproj
└── model
│ ├── ppyoloe
│ └── PPYoloeDetTests.cs
│ ├── rtdetr
│ └── RtdetrDetTests.cs
│ ├── yolov5
│ ├── Yolov5DetTests.cs
│ └── Yolov5SegTests.cs
│ └── yolov8
│ ├── Yolov8ClsTests.cs
│ ├── Yolov8DetTests.cs
│ ├── Yolov8PoseTests.cs
│ └── Yolov8SegTests.cs
├── csharp_api_extensions_opencvsharp_unit_tests
├── csharp_api_extensions_opencvsharp_unit_tests.csproj
└── model
│ ├── ppyoloe
│ └── PPYoloeDetTests.cs
│ ├── rtdetr
│ └── RtdetrDetTests.cs
│ ├── yolov5
│ ├── Yolov5DetTests.cs
│ └── Yolov5SegTests.cs
│ └── yolov8
│ ├── Yolov8ClsTests.cs
│ ├── Yolov8DetTests.cs
│ ├── Yolov8PoseTests.cs
│ └── Yolov8SegTests.cs
├── csharp_api_extensions_unit_tests
├── benchmark_app
│ └── BenchmarkTests.cs
└── csharp_api_extensions_unit_tests.csproj
└── csharp_api_unit_tests
├── Usings.cs
├── base_test.cs
├── core
├── CompiledModelTests.cs
├── CoreTests.cs
├── InferRequestTests.cs
├── InputTests.cs
├── ModelTests.cs
├── NodeTests.cs
├── OutputTests.cs
├── PartialShapeTests.cs
├── ShapeTests.cs
└── TensorTests.cs
├── csharp_api_unit_tests.csproj
├── ov
└── OvTests.cs
└── preprocess
├── InputInfoTests.cs
├── InputModelInfoTests.cs
├── InputTensorInfoTests.cs
├── OutputInfoTests.cs
├── OutputTensorInfoTests.cs
├── PrePostProcessorTests.cs
└── PreProcessStepsTests.cs
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 |
16 | ###############################################################################
17 | # Set the merge driver for project and solution files
18 | #
19 | # Merging from the command prompt will add diff markers to the files if there
20 | # are conflicts (Merging from VS is not affected by the settings below, in VS
21 | # the diff markers are never inserted). Diff markers may cause the following
22 | # file extensions to fail to load in VS. An alternative would be to treat
23 | # these files as binary and thus will always conflict and require user
24 | # intervention with every merge. To do so, just uncomment the entries below
25 | ###############################################################################
26 | #*.sln merge=binary
27 | #*.csproj merge=binary
28 | #*.vbproj merge=binary
29 | #*.vcxproj merge=binary
30 | #*.vcproj merge=binary
31 | #*.dbproj merge=binary
32 | #*.fsproj merge=binary
33 | #*.lsproj merge=binary
34 | #*.wixproj merge=binary
35 | #*.modelproj merge=binary
36 | #*.sqlproj merge=binary
37 | #*.wwaproj merge=binary
38 |
39 |
40 | ###############################################################################
41 | # behavior for image files
42 | #
43 | # image files are treated as binary by default.
44 | ###############################################################################
45 | #*.jpg binary
46 | #*.png binary
47 | #*.gif binary
48 |
49 |
50 | ###############################################################################
51 | # diff behavior for common document formats
52 | #
53 | # Convert binary document formats to text before diffing them. This feature
54 | # is only available from the command line. Turn it on by uncommenting the
55 | # entries below.
56 | ###############################################################################
57 | #*.doc diff=astextplain
58 | #*.DOC diff=astextplain
59 | #*.docx diff=astextplain
60 | #*.DOCX diff=astextplain
61 | #*.dot diff=astextplain
62 | #*.DOT diff=astextplain
63 | #*.pdf diff=astextplain
64 | #*.PDF diff=astextplain
65 | #*.rtf diff=astextplain
66 | #*.RTF diff=astextplain
67 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "samples"]
2 | path = samples
3 | url = https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples.git
--------------------------------------------------------------------------------
/CONTRIBUTING_cn.md:
--------------------------------------------------------------------------------
1 | # 🏅为 OpenVINO™ C# API 做贡献
2 |
3 | 欢迎大家对我们提出宝贵意见🥰!我们期待大家为 OpenVINO™ C# API 做出贡献,可以通过以下方式:
4 |
5 | - **⁉报告错误/问题**
6 |
7 | 如果您在 OpenVINO™ C# API 或其组件中遇到错误行为,您可以在 GitHub 问题跟踪器中[创建新问题](https://github.com/guojin-yan/OpenVINOSharp/issues)。
8 |
9 | - **🔖提出新的产品和功能**
10 |
11 | 如果你对 OpenVINO™ C# API 有相关建议或想分享您的想法,您可以打开一个新的[ Discussions](https://github.com/guojin-yan/OpenVINOSharp/discussions/landing) GitHub 讨论。 如果您的想法已经明确定义,您还可以创建一个功能请求问题,在这两种情况下,请提供详细说明,包括用例、优势和潜在挑战。
12 |
13 | - 🎯**修复代码错误或开发新功能**
14 |
15 | 如果您发现仓库中有代码错误或则其他内容错误,以及有新的功能或者应用案例开发,可以通过创建 [Pull requests](https://github.com/guojin-yan/OpenVINOSharp/pulls)实现,再提交时,请注意代码风格以及文档风格与代码仓保持一致。
16 |
17 | - 🕹**成为维护者**
18 |
19 | 如果你对 OpenVINO™ C# API 感兴趣,并接受该项目工作,有余力协助 OpenVINO™ C# API 库开发,可以与我联系[guojin_yjs@cumt.edu.cn](mailto:guojin_yjs@cumt.edu.cn)。
20 |
21 |
22 |
23 | ## ⭕提交拉取请求 (PR)
24 |
25 | 提交 PR 很容易😀!你可以通过提交pr提出新的产品和功能、提交代码修复等贡献,此处演示两种提交PR的方式:
26 |
27 | ### 🔻方式一:在线直接提交
28 |
29 | #### 1. 选择要更新的文件
30 |
31 | 通过在 GitHub 中单击它来选择更新,以`README.md`文件为例:
32 |
33 |

34 |
35 |
36 |
37 | #### 2. 点击“编辑此文件”
38 |
39 | 该按钮位于右上角。
40 |
41 | 
42 |
43 | 如果你没有Fork该项目,需要先Fork该项目。
44 |
45 | 
46 |
47 | #### 3. 修改文件内容
48 |
49 | 增加两个🥰符号。
50 |
51 | 
52 |
53 | 修改完文件内容后,点击**Commit changes**提交更改,并按照更改内容填写日志。
54 |
55 | 
56 |
57 | #### 4. 创建 Pull Request
58 |
59 | 修改完该文件后,修改内容只存在于修改者当前分支,需要通过 Pull Request 提交到原作者仓库才可以。点击 **Create pull request**,创建PR。
60 |
61 | 
62 |
63 | 
64 |
65 | 按要求提交后,等待代码仓库管理人员审核并通过你提交的PR。
66 |
67 | ### 🔻方式二:本地修改提交
68 |
69 | 本地修改提交适合较大的改动或增加新文件、调试代码等情况,该方法要求按装Git.
70 |
71 | #### 1. fork开源项目
72 |
73 | 找到要提交PR的项目,先将该项目fork自己的代码仓。
74 |
75 | 
76 |
77 | 
78 |
79 | #### 2. 克隆开源项目
80 |
81 | 将需要提交PR的项目克隆到本地。
82 |
83 | ```
84 | //打开CMD或者打开Git Bash Here
85 | git clone https://github.com/guojin-yan/OpenVINO-CSharp-API.git
86 | ```
87 |
88 | 
89 |
90 | #### 3.创建新的分支
91 |
92 | 提交PR时需要.为了防止在主分支上修改影响主分支代码,此处创建一个分支用于代码的修改。
93 |
94 | ```
95 | cd OpenVINO-CSharp-API // 切换到项目路径
96 | git checkout -b temp //创建名为temp的分支
97 | git branch //查看已经创建的分支 如图有temph和csharp3.0两个分支
98 | git checkout temp // 切换到分支
99 | ```
100 |
101 | 切换好分支后就可以直接根据自己需求修改项目,如上图所示。
102 |
103 | 
104 |
105 |
106 |
107 | #### 4. 修改提交项目代码
108 |
109 | 将代码修改后,执行`git status` 命令查看修改了哪些文件,接着使用`git add 修改的文件名`添加到暂存区,最后使用`git commit -m "日志信息" 文件名`提交到本地库。
110 |
111 | ```
112 | git status // 查看库状态
113 | git add 文件名 // 将修改的文件存放到暂存区
114 | git commit -m "日志信息" 文件名 // 将修改的文件提交到本地库
115 | ```
116 |
117 | 
118 |
119 | 最后将本地项目代码提交到远程GitHub上
120 |
121 | ```
122 | git push --set-upstream origin temp
123 | ```
124 |
125 | 进入GitHub项目,切换到mybranch分支,查看是否修改成功。
126 |
127 | 切换到**主分支**,将分支mybranch代码合并到主分支,查看是否可以与主分支合并成功。
128 |
129 | ```
130 | git checkout main // 切换到主分支
131 | git merge temp // 合并派生分支到主分支
132 | ```
133 |
134 | 合并成功后,将主分支推送到代码仓。
135 |
136 | ```
137 | git add . // 将修改的文件存放到暂存区
138 | git commit -m "日志信息" // 将修改的文件提交到本地库
139 | git push origin csharp3.0 // 推送到远程仓库
140 | ```
141 |
142 | 在GitHub切换到master主分支,查看是否合并成功
143 |
144 | #### 4.提交pr请求
145 |
146 | 进入自己`fork`的项目中,点击下方所指使的位置。
147 |
148 | 
149 |
150 | 点击`Create pull requests`
151 |
152 | 
153 |
154 | 最后点击`Create pull request`,提交后开源人将会收到你的合并请求。
155 |
156 | 
157 |
158 | ## ⭕编码规范
159 |
160 | 为保证项目编码风格一致,在提交PR时,要遵守该项目编码规范。
161 |
162 | ##### 🔻代码样式
163 |
164 | 我们的所有代码遵循Google 开源项目风格指南,包括C/C++。
165 |
166 | 🔸C++ 风格指南:[English](https://google.github.io/styleguide/cppguide.html)
167 |
168 | ## ⭕许可证
169 |
170 | 您所提交的贡献,默认您同意采用[Apache-2.0 license](https://github.com/PaddlePaddle/Paddle/blob/develop/LICENSE).
--------------------------------------------------------------------------------
/docs/inatall/Install_OpenVINO_CSharp_MacOS_cn.md:
--------------------------------------------------------------------------------
1 | ## 在MacOS上搭建OpenVINO™C#开发环境
2 |
3 | - [在MacOS上搭建OpenVINO™C#开发环境](#在MacOS上搭建OpenVINO™C#开发环境)
4 | - [🧩简介](#🧩简介)
5 | - [🔮安装.NET运行环境](#🔮安装.NET运行环境)
6 | - [🎈配置C#开发环境](#🎈配置C#开发环境)
7 | - [🎨创建并配置C#项目](#🎨创建并配置C#项目)
8 | - [第一步:创建OpenVINO™C#项目](#第一步:创建OpenVINO™C#项目)
9 | - [第二步:添加项目依赖](#第二步:添加项目依赖)
10 | - [🎁测试OpenVINO™C#项目](#🎁测试OpenVINO™C#项目)
11 | - [🎯总结](#🎯总结)
12 |
13 | ### 🧩简介
14 |
15 | 当前MacOS系统主要分为两个比较大的版本,一个是2020年之前的苹果系统,该MacOS系统使用的时Intel系列的CPU,另一个是2020年后推出的苹果M系列芯片的系统。在该教程中将从零开始详述在**MacOS(M2)**上搭建**OpenVINO™ CSharp**开发环境,并对 **OpenVINO™ CSharp API **环境进行简单测试。
16 |
17 | ### 🔮安装.NET运行环境
18 |
19 | **[.NET](https://learn.microsoft.com/zh-cn/dotnet/)** 是由 **Microsoft** 创建的一个免费的、跨平台的、开源开发人员平台,可以使用 C#、F# 或 Visual Basic 语言编写代码,用于构建许多不同类型的应用程序,可以在任何兼容的操作系统上(Windows、Linux、Mac OS等)运行。
20 |
21 | Microsoft官方提供了**.NET**环境的详细安装流程,大家可以参考以下文章进行安装:[在 macOS 上安装 .NET](https://learn.microsoft.com/zh-cn/dotnet/core/install/macos)。
22 |
23 | 首先访问网站[下载 .NET](https://dotnet.microsoft.com/zh-cn/download),具体下载选择如下所示,
24 |
25 | 
26 |
27 | 文件下载完后,通过双击安装文件进行环境的安装:
28 |
29 | 
30 |
31 | 打开安装文件后,如下图所示,此处无需设置其他配置,只需要按照默认步骤进行安装即可。
32 |
33 | 
34 |
35 | ### 🎈配置C#开发环境
36 |
37 | 在Linux环境下我们可以使用以下组合进行C#代码开发:
38 |
39 | - 代码构建工具:**dotnet **
40 | - 代码编辑工具:**Visual Studio Code**
41 |
42 | 在上文中我们安装**.NET**时已经同时安装了**dotnet **工具。**Visual Studio Code **是一款功能强大的代码编辑器,并且支持更多第三方插件,同时支持C#代码开发,**Visual Studio Code**安装比较简单,只需从[VS Code官网](https://code.visualstudio.com/)下载安装文件,按照默认选项完成安装。
43 |
44 | 然后配置C#编辑环境,在扩展商店中搜索C#,安装C#扩展,如下图所示。
45 |
46 | 
47 |
48 | ### 🎨创建并配置C#项目
49 |
50 | #### 第一步:创建 OpenVINO™C# 项目
51 |
52 | 使用**dotnet**创建一个测试项目,在Terminal中输入以下指令进行项目创建:
53 |
54 | ```shell
55 | dotnet new console -o test_openvino_csharp --framework net6.0
56 | ```
57 |
58 | 
59 |
60 | #### 第二步:添加项目依赖
61 |
62 | 接下来使用**Visual Studio Code**打开项目文件,在**Visual Studio Code**下方的终端窗口输入以下指令,添加``OpenVINO.CSharp.API``以及`` OpenVINO.runtime.macos-arm64``项目依赖包,其输出如下图所示:
63 |
64 | ```shell
65 | dotnet add package OpenVINO.CSharp.API
66 | dotnet add package OpenVINO.runtime.macos-arm64
67 | ```
68 |
69 | - **OpenVINO.CSharp.API**:OpenVINO™ CSharp API 项目核心程序集。
70 | - **OpenVINO.runtime.macos-arm64**:OpenVINO™ 在MacOS M系列平台运行所需依赖项。
71 | - **OpenVINO.runtime.macos-x86_64**:OpenVINO™ 在MacOS Intel CPU系列平台运行所需依赖项。
72 |
73 | 
74 |
75 | ### 🎁测试OpenVINO™C#项目
76 |
77 | 首先添加测试代码,用户可以直接将下述代码替换到上文所创建的项目中的**Program.cs**文件中。
78 |
79 | ```csharp
80 | using OpenVinoSharp;
81 | namespace test_openvino_csharp // Note: actual namespace depends on the project name.
82 | {
83 | internal class Program
84 | {
85 | static void Main(string[] args)
86 | {
87 | // -------- 测试 OpenVINO CSharp API 安装 --------
88 | OpenVinoSharp.Version version = Ov.get_openvino_version();
89 | Console.WriteLine("---- OpenVINO INFO----");
90 | Console.WriteLine("Description : " + version.description);
91 | Console.WriteLine("Build number: " + version.buildNumber);
92 | }
93 | }
94 | }
95 | ```
96 |
97 | 创建并配置好项目后,然后在终端中输入``dotnet run``运行程序即可,结果如下图所示:
98 |
99 | 
100 |
101 | 此处主要输出了OpenVINO版本信息,如果出现以下结果,说明环境配置成功。
102 |
103 | ### 🎯总结
104 |
105 | 至此,我们就完成了在MacOS上搭建OpenVINO™C#开发环境,欢迎大家使用,如需要更多信息,可以参考一下内容:
106 |
107 | - [OpenVINO™](https://github.com/openvinotoolkit/openvino)
108 | - [OpenVINO CSharp API](https://github.com/guojin-yan/OpenVINO-CSharp-API)
109 |
110 | - [OpenVINO CSharp API Samples](https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples)
--------------------------------------------------------------------------------
/docs/inatall/Install_OpenVINO_CSharp_Windows_cn.md:
--------------------------------------------------------------------------------
1 | ## 在Windows上搭建OpenVINO™C#开发环境
2 |
3 | - [在Windows上搭建OpenVINO™C#开发环境](#在Windows上搭建OpenVINO™C#开发环境)
4 | - [🧩简介](#🧩简介)
5 | - [🔮安装.NET运行环境](#🔮安装.NET运行环境)
6 | - [🎈配置C#开发环境](#🎈配置C#开发环境)
7 | - [🎨创建并配置C#项目](#🎨创建并配置C#项目)
8 | - [第一步:创建OpenVINO™C#项目](#第一步:创建OpenVINO™C#项目)
9 | - [第二步:添加项目依赖](#第二步:添加项目依赖)
10 | - [🎁测试OpenVINO™C#项目](#🎁测试OpenVINO™C#项目)
11 | - [🎯总结](#🎯总结)
12 |
13 | ### 🧩简介
14 |
15 | 本文将从零开始详述在**Windows10/11**上搭建**OpenVINO™ CSharp**开发环境,并对 **OpenVINO™ CSharp API **环境进行简单测试。
16 |
17 | ### 🔮安装.NET运行环境
18 |
19 | **[.NET](https://learn.microsoft.com/zh-cn/dotnet/)** 是由 **Microsoft** 创建的一个免费的、跨平台的、开源开发人员平台,可以使用 C#、F# 或 Visual Basic 语言编写代码,用于构建许多不同类型的应用程序,可以在任何兼容的操作系统上(Windows、Linux、Mac OS等)运行。
20 |
21 | Microsoft官方提供了**.NET**环境的详细安装流程,大家可以参考以下文章进行安装:[在 Windows 上安装 .NET](https://learn.microsoft.com/zh-cn/dotnet/core/install/windows).
22 |
23 | ### 🎈配置C#开发环境
24 |
25 | 在Windows平台创建并编译C#代码可以使用的平台比较多,最容易使用以及最简单的是使用**Visual Studio IDE**,但是**Visual Studio IDE**目前只支持Windows环境,如果想实现跨平台使用,最好的组合为:
26 |
27 | - 代码构建工具:**dotnet **
28 | - 代码编辑工具:**Visual Studio Code**
29 |
30 | 所以在此处我们将讲解使用**Visual Studio IDE**方式编译并运行项目,在Linux以及MacOS系统中讲解使用**dotnet && Visual Studio Code **组合的方式。**Visual Studio IDE**安装方式可以参考Microsoft官方提供的安装教程:
31 |
32 | - [Visual Studio 2022 IDE](https://visualstudio.microsoft.com/zh-hans/vs/)
33 | - [在 Windows 上安装 .NET](https://learn.microsoft.com/zh-cn/dotnet/core/install/windows)
34 |
35 | ### 🎨创建并配置C#项目
36 |
37 | #### 第一步:创建 OpenVINO™C# 项目
38 |
39 | 使用**Visual Studio 2022 IDE**创建一个 OpenVINO™ C# 测试项目,按照下图流程进行创建即可.
40 |
41 | 
42 |
43 | #### 第二步:添加项目依赖
44 |
45 | OpenVINO™ C# 项目所使用的依赖环境,此处可以完全使用 NuGet Package 安装所需程序集,其安装流程如下图所示:
46 |
47 | 
48 |
49 | 在此处,主要需要安装两类 NuGet 程序包,分别为:
50 |
51 | - **OpenVINO CSharp API**
52 | - **OpenVINO.CSharp.API**:OpenVINO CSharp API 项目核心程序集。
53 | - **OpenVINO.runtime.win**:OpenVINO 在Windows平台运行所需依赖项。
54 | - **OpenCvSharp**
55 | - **OpenCvSharp4**:OpenCvSharp4 项目核心程序集。
56 | - **OpenCvSharp4.runtime.win**:OpenCvSharp4 在Windows平台运行所需依赖项。
57 |
58 | 其中**OpenVINO CSharp API**是此处我们重点介绍的项目,**OpenCvSharp**是在C#中使用的一个开源视觉处理库。
59 |
60 | ### 🎁测试OpenVINO™C#项目
61 |
62 | 首先添加测试代码,用户可以直接将下述代码替换到上文所创建的项目中的**Program.cs**文件中。
63 |
64 | ```csharp
65 | using OpenCvSharp;
66 | using OpenVinoSharp;
67 | namespace test_openvino_csharp
68 | {
69 | internal class Program
70 | {
71 | static void Main(string[] args)
72 | {
73 | // -------- 测试 OpenVINO CSharp API 安装 --------
74 | OpenVinoSharp.Version version = Ov.get_openvino_version();
75 | Console.WriteLine("---- OpenVINO INFO----");
76 | Console.WriteLine("Description : " + version.description);
77 | Console.WriteLine("Build number: " + version.buildNumber);
78 |
79 | // -------- 测试 OpenCvSharp 安装--------
80 | //创建一张大小为300*300颜色为绿色的三通道彩色图像
81 | Mat img = new Mat(300, 300, MatType.CV_8UC3, new Scalar(255, 0, 0));
82 | Cv2.ImShow("img", img);
83 | Cv2.WaitKey(0);
84 | }
85 | }
86 | }
87 | ```
88 |
89 | 创建并配置好项目后,就可以直接运行该项目了,使用**Visual Studio 2022 IDE**可以直接点击运行案件运行程序,程序运行后输出如下所示:
90 |
91 | 
92 |
93 | 此处主要输出了OpenVINO版本信息,并使用OpenCvSharp绘制了一张蓝色图片,如果出现以下结果,说明环境配置成功。
94 |
95 | ### 🎯总结
96 |
97 | 至此,我们就完成了在Windows上搭建OpenVINO™C#开发环境,欢迎大家使用,如需要更多信息,可以参考一下内容:
98 |
99 | - [OpenVINO™](https://github.com/openvinotoolkit/openvino)
100 | - [OpenVINO CSharp API](https://github.com/guojin-yan/OpenVINO-CSharp-API)
101 |
102 | - [OpenVINO CSharp API Samples](https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples)
--------------------------------------------------------------------------------
/nuget/NuGet.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guojin-yan/OpenVINO-CSharp-API/2abbedfcc437d0568710eae81b6da592af7e247e/nuget/NuGet.png
--------------------------------------------------------------------------------
/nuget/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guojin-yan/OpenVINO-CSharp-API/2abbedfcc437d0568710eae81b6da592af7e247e/nuget/logo.png
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/CSharp.API.Extensions.OpenCvSharp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0;net5.0;net6.0;netcoreapp3.1;net48;net472
5 | True
6 | 12.0
7 | True
8 | OpenVinoSharp.Extensions.OpenCvSharp
9 | OpenVINO_CSharp_API_Extensions_OpenCvSharp
10 | True
11 | OpenVINO.CSharp.API.Extensions.OpenCvSharp
12 | OpenVINO C# API Extensions by OpenCvSharp
13 | 1.0.6.3
14 | Guojin Yan
15 | OpenVINO C# API Extensions by OpenCvSharp
16 | Based on the C # platform, call the OpenVINO suite to deploy a deep learning model.
17 | https://github.com/guojin-yan/OpenVINO-CSharp-API
18 | logo.png
19 | https://github.com/guojin-yan/OpenVINO-CSharp-API
20 | git
21 | This assembly is for OpenVINO CSharp API. The expansion of API mainly involves encapsulating the inference process of common models and the pre-processing and post-processing methods of data.The image processing library uses OpenCvSharp.
22 | ../../nuget
23 | README.md
24 | OpenVINO;CSharp-API;C#-API;Extensions;OpenCvSharp
25 | LICENSE.txt
26 | True
27 |
28 |
29 |
30 |
31 |
32 |
33 | True
34 | \
35 |
36 |
37 | True
38 | \
39 |
40 |
41 | True
42 | \
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/predictor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | ///
10 | /// Default model inference engine.
11 | ///
12 | public abstract class Predictor
13 | {
14 | protected Core m_core;
15 | protected Model m_model;
16 | protected CompiledModel m_compiled_model;
17 | protected InferRequest m_infer_request;
18 |
19 | protected bool m_use_gpu;
20 | public Predictor() { }
21 | ///
22 | /// Default constructor.
23 | ///
24 | /// Inference model path
25 | public Predictor(string model_path)
26 | {
27 | m_core = new Core();
28 | m_model = m_core.read_model(model_path);
29 | m_compiled_model = m_core.compile_model(m_model);
30 | m_infer_request = m_compiled_model.create_infer_request();
31 | }
32 | ///
33 | /// Parameter constructor.
34 | ///
35 | /// Inference model path.
36 | /// Inference model path device.
37 | /// The read-write property(string) to set/get the directory which will be used to store any data cached by plugins.
38 | ///
39 | ///
40 | ///
41 | public Predictor(string model_path, string device, string? cache_dir = null, bool? use_gpu = false, long[]? input_size = null)
42 | {
43 | string cache = cache_dir ?? null;
44 | m_use_gpu = use_gpu ?? false;
45 | m_core = new Core();
46 | if (cache != null)
47 | {
48 | m_core.set_property(device, Ov.cache_dir(cache_dir));
49 | }
50 | m_model = m_core.read_model(model_path);
51 | if (m_use_gpu)
52 | {
53 | if (input_size == null)
54 | {
55 | throw new ArgumentNullException("input_size");
56 | }
57 | m_model.reshape(new PartialShape(new Shape(input_size)));
58 | }
59 | m_compiled_model = m_core.compile_model(m_model, device);
60 | m_infer_request = m_compiled_model.create_infer_request();
61 | }
62 |
63 | ///
64 | /// The default model inference method is only applicable to single input and single output models.
65 | ///
66 | /// The input data.
67 | /// The input shape.
68 | /// Infer result.
69 | protected float[] infer(float[] input_data, long[] shape = null)
70 | {
71 | Tensor input_tensor = m_infer_request.get_input_tensor();
72 | if (shape != null)
73 | input_tensor.set_shape(new Shape(shape));
74 | input_tensor.set_data(input_data);
75 | m_infer_request.infer();
76 |
77 | Tensor output_tensor = m_infer_request.get_output_tensor();
78 | string s =output_tensor.get_shape().to_string();
79 | float[] result = output_tensor.get_data((int)output_tensor.get_size());
80 | return result;
81 |
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/runtime_option/Config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace OpenVinoSharp.Extensions.model
9 | {
10 | public interface Config
11 | {
12 | public void set_model(string model_path);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/runtime_option/ppyoloe_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class PPYoloeConfig : Config
10 | {
11 | public string model_path = null;
12 | public string device = "CPU";
13 | public bool use_gpu = false;
14 | public float det_thresh = 0.5f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 | public bool postprcoess = true;
20 |
21 | public PPYoloeConfig() { }
22 |
23 | public PPYoloeConfig(string model_path, string? device = null, bool? use_gpu = null,
24 | float? det_thresh = null, bool? postprcoess = null, long[]? input_size = null,
25 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
26 | {
27 | this.model_path = model_path;
28 | this.device = device ?? this.device;
29 | this.use_gpu = use_gpu ?? this.use_gpu;
30 | this.det_thresh = det_thresh ?? this.det_thresh;
31 | this.input_size = input_size ?? this.input_size;
32 | this.batch_num = batch_num ?? this.batch_num;
33 | this.cache_dir = cache_dir ?? this.cache_dir;
34 | this.categ_nums = categ_nums ?? this.categ_nums;
35 | this.postprcoess = postprcoess ?? this.postprcoess;
36 | }
37 |
38 | public void set_model(string model_path)
39 | {
40 | this.model_path = model_path;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/runtime_option/rtdetr_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class RtdetrConfig : Config
10 | {
11 | public string model_path = null;
12 | public string device = "CPU";
13 | public bool use_gpu = false;
14 | public float det_thresh = 0.5f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 | public bool postprcoess = true;
20 |
21 | public RtdetrConfig() { }
22 |
23 | public RtdetrConfig(string model_path, string? device = null, bool? use_gpu = null,
24 | float? det_thresh = null, bool? postprcoess = null, long[]? input_size = null,
25 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
26 | {
27 | this.model_path = model_path;
28 | this.device = device ?? this.device;
29 | this.use_gpu = use_gpu ?? this.use_gpu;
30 | this.det_thresh = det_thresh ?? this.det_thresh;
31 | this.input_size = input_size ?? this.input_size;
32 | this.batch_num = batch_num ?? this.batch_num;
33 | this.cache_dir = cache_dir ?? this.cache_dir;
34 | this.categ_nums = categ_nums ?? this.categ_nums;
35 | this.postprcoess = postprcoess ?? this.postprcoess;
36 | }
37 |
38 | public void set_model(string model_path)
39 | {
40 | this.model_path = model_path;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/runtime_option/yolo_option.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public struct Yolov8DetOption
10 | {
11 | public static string device = "CPU";
12 | public static bool use_gpu = false;
13 | public static float det_thresh = 0.3f;
14 | public static float det_nms_thresh = 0.5f;
15 | public static long[] input_size = { 1, 3, 640, 640 };
16 | public static int batch_num = 1;
17 | public static string cache_dir = "model/";
18 | public static int categ_nums = 80;
19 |
20 | }
21 |
22 | public struct Yolov8PoseOption
23 | {
24 | public static string device = "CPU";
25 | public static bool use_gpu = false;
26 | public static float det_thresh = 0.3f;
27 | public static float det_nms_thresh = 0.5f;
28 | public static long[] input_size = { 1, 3, 640, 640 };
29 | public static int batch_num = 1;
30 | public static string cache_dir = "model/";
31 | }
32 |
33 | public struct Yolov8ClsOption
34 | {
35 | public static string device = "CPU";
36 | public static bool use_gpu = false;
37 | public static float thresh = 0.3f;
38 | public static long[] input_size = { 1, 3, 224, 224 };
39 | public static int batch_num = 1;
40 | public static string cache_dir = "model/";
41 | public static int categ_nums = 1000;
42 | public static int result_num = 10;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/runtime_option/yolov5_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class Yolov5DetConfig : Config
10 | {
11 | public string device = "CPU";
12 | public bool use_gpu = false;
13 | public float det_thresh = 0.3f;
14 | public float det_nms_thresh = 0.45f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 |
20 | public string model_path = null;
21 |
22 | public Yolov5DetConfig() { }
23 |
24 | public Yolov5DetConfig(string model_path, string? device = null, bool? use_gpu = null,
25 | float? det_thresh = null, float? det_nms_thresh = null, long[]? input_size = null,
26 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
27 | {
28 | this.model_path = model_path;
29 | this.device = device ?? this.device;
30 | this.use_gpu = use_gpu ?? this.use_gpu;
31 | this.det_thresh = det_thresh ?? this.det_thresh;
32 | this.det_nms_thresh = det_nms_thresh ?? this.det_nms_thresh;
33 | this.input_size = input_size ?? this.input_size;
34 | this.batch_num = batch_num ?? this.batch_num;
35 | this.cache_dir = cache_dir ?? this.cache_dir;
36 | this.categ_nums = categ_nums ?? this.categ_nums;
37 | }
38 |
39 | public void set_model(string model_path)
40 | {
41 | this.model_path = model_path;
42 | }
43 | }
44 |
45 | public class Yolov5SegConfig : Config
46 | {
47 | public string device = "CPU";
48 | public bool use_gpu = false;
49 | public float det_thresh = 0.2f;
50 | public float det_nms_thresh = 0.45f;
51 | public long[] input_size = { 1, 3, 640, 640 };
52 | public int batch_num = 1;
53 | public string cache_dir = "model/";
54 | public int categ_nums = 80;
55 |
56 | public string model_path = null;
57 |
58 | public Yolov5SegConfig() { }
59 |
60 | public Yolov5SegConfig(string model_path, string? device = null, bool? use_gpu = null,
61 | float? det_thresh = null, float? det_nms_thresh = null, long[]? input_size = null,
62 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
63 | {
64 | this.model_path = model_path;
65 | this.device = device ?? this.device;
66 | this.use_gpu = use_gpu ?? this.use_gpu;
67 | this.det_thresh = det_thresh ?? this.det_thresh;
68 | this.det_nms_thresh = det_nms_thresh ?? this.det_nms_thresh;
69 | this.input_size = input_size ?? this.input_size;
70 | this.batch_num = batch_num ?? this.batch_num;
71 | this.cache_dir = cache_dir ?? this.cache_dir;
72 | this.categ_nums = categ_nums ?? this.categ_nums;
73 | }
74 |
75 | public void set_model(string model_path)
76 | {
77 | this.model_path = model_path;
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/model/yolov8/yolov8_cls.cs:
--------------------------------------------------------------------------------
1 | using OpenCvSharp;
2 | using OpenCvSharp.Dnn;
3 | using OpenVinoSharp.Extensions.process;
4 | using OpenVinoSharp.Extensions.result;
5 | using OpenVinoSharp.Extensions.utility;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Text;
10 | using System.Threading.Tasks;
11 |
12 | namespace OpenVinoSharp.Extensions.model
13 | {
14 | public class Yolov8Cls : Predictor
15 | {
16 | private int m_categ_nums;
17 | private long[] m_input_size;
18 | private int m_batch_num;
19 | private int m_result_num;
20 | public Yolov8Cls(string model_path, string? device = null, int? categ_nums = null, bool? use_gpu = null,
21 | long[]? input_size = null, int? batch_num = null, string? cache_dir = null, int? result_num = null)
22 | : base(model_path, device ?? Yolov8ClsOption.device, cache_dir ?? Yolov8ClsOption.cache_dir,
23 | use_gpu ?? Yolov8ClsOption.use_gpu, input_size ?? Yolov8ClsOption.input_size)
24 | {
25 | m_categ_nums = categ_nums ?? Yolov8ClsOption.categ_nums;
26 | m_input_size = input_size ?? Yolov8ClsOption.input_size;
27 | m_batch_num = batch_num ?? Yolov8ClsOption.batch_num;
28 | m_result_num = result_num ?? Yolov8ClsOption.result_num;
29 | }
30 |
31 | public Yolov8Cls(Yolov8ClsConfig config)
32 | : base(config.model_path, config.device, config.cache_dir,config.use_gpu, config.input_size)
33 | {
34 | m_categ_nums = config.categ_nums;
35 | m_input_size = config.input_size;
36 | m_batch_num = config.batch_num;
37 | m_result_num = config. result_num;
38 | }
39 |
40 | public ClsResult predict(Mat image)
41 | {
42 | Mat mat = new Mat();
43 | Cv2.CvtColor(image, mat, ColorConversionCodes.BGR2RGB);
44 | float factors = 0f;
45 | mat = Resize.letterbox_img(mat, (int)m_input_size[2], out factors);
46 | mat = Normalize.run(mat, true);
47 | float[] input_data = Permute.run(mat);
48 | float[] output_data = infer(input_data);
49 |
50 | List sort_result = Utility.argsort(output_data);
51 | ClsResult result = new ClsResult();
52 | for (int i = 0; i < m_result_num; ++i)
53 | {
54 | result.add(sort_result[i], output_data[sort_result[i]]);
55 | }
56 | return result;
57 | }
58 | public List predict(List images)
59 | {
60 | List results = new List();
61 | for (int beg_img_no = 0; beg_img_no < images.Count; beg_img_no += m_batch_num)
62 | {
63 | int end_img_no = Math.Min(images.Count, beg_img_no + m_batch_num);
64 | int batch_num = end_img_no - beg_img_no;
65 | List norm_img_batch = new List();
66 | float factors = 0f;
67 | for (int ino = beg_img_no; ino < end_img_no; ino++)
68 | {
69 | Mat mat = new Mat();
70 | Cv2.CvtColor(images[ino], mat, ColorConversionCodes.BGR2RGB);
71 | mat = Resize.letterbox_img(mat, (int)m_input_size[2], out factors);
72 | mat = Normalize.run(mat, true);
73 | norm_img_batch.Add(mat);
74 | }
75 | float[] input_data = PermuteBatch.run(norm_img_batch);
76 | float[] output_datas = infer(input_data, new long[] { batch_num, 3, m_input_size[2], m_input_size[3] });
77 |
78 | for (int i = 0; i < batch_num; ++i)
79 | {
80 | float[] output_data = new float[m_categ_nums];
81 | Buffer.BlockCopy(output_datas, m_categ_nums * i, output_data, 0, m_categ_nums*4);
82 | List sort_result = Utility.argsort(output_data);
83 | ClsResult result = new ClsResult();
84 | for (int j = 0; j < m_result_num; ++j)
85 | {
86 | result.add(sort_result[j], output_data[sort_result[j]]);
87 | }
88 | results.Add(result);
89 | }
90 | }
91 | return results;
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/process/normalize.cs:
--------------------------------------------------------------------------------
1 | using OpenCvSharp;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace OpenVinoSharp.Extensions.process
9 | {
10 | ///
11 | /// Normalize data classes using OpenCvSharp.
12 | ///
13 | public static class Normalize
14 | {
15 | ///
16 | /// Run normalize data classes.
17 | ///
18 | /// The image mat.
19 | /// Channel mean.
20 | /// Channel variance.
21 | /// Whether to divide by 255.
22 | /// The normalize data.
23 | public static Mat run(Mat im, float[] mean, float[] scale, bool is_scale)
24 | {
25 | double e = 1.0;
26 | if (is_scale)
27 | {
28 | e /= 255.0;
29 | }
30 | im.ConvertTo(im, MatType.CV_32FC3, e);
31 | Mat[] bgr_channels = new Mat[3];
32 | Cv2.Split(im, out bgr_channels);
33 | for (var i = 0; i < bgr_channels.Length; i++)
34 | {
35 | bgr_channels[i].ConvertTo(bgr_channels[i], MatType.CV_32FC1, 1.0 * scale[i],
36 | (0.0 - mean[i]) * scale[i]);
37 | }
38 | Mat re = new Mat();
39 | Cv2.Merge(bgr_channels, re);
40 | return re;
41 | }
42 | ///
43 | /// Run normalize data classes.
44 | ///
45 | /// The image mat.
46 | /// Whether to divide by 255.
47 | /// The normalize data.
48 | public static Mat run(Mat im, bool is_scale)
49 | {
50 | double e = 1.0;
51 | if (is_scale)
52 | {
53 | e /= 255.0;
54 | }
55 | im.ConvertTo(im, MatType.CV_32FC3, e);
56 | return im;
57 | }
58 |
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/process/permute.cs:
--------------------------------------------------------------------------------
1 |
2 | using OpenCvSharp;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Runtime.InteropServices;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace OpenVinoSharp.Extensions.process
11 | {
12 | public static class Permute
13 | {
14 | public static float[] run(Mat im)
15 | {
16 | int rh = im.Rows;
17 | int rw = im.Cols;
18 | int rc = im.Channels();
19 | float[] res = new float[rh * rw * rc];
20 |
21 | GCHandle resultHandle = default;
22 | try
23 | {
24 | resultHandle = GCHandle.Alloc(res, GCHandleType.Pinned);
25 | IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
26 | for (int i = 0; i < rc; ++i)
27 | {
28 | using Mat dest = new(rh, rw, MatType.CV_32FC1, resultPtr + i * rh * rw * sizeof(float));
29 | Cv2.ExtractChannel(im, dest, i);
30 | }
31 | }
32 | finally
33 | {
34 | resultHandle.Free();
35 | }
36 | return res;
37 |
38 | }
39 |
40 | }
41 | public static class PermuteBatch
42 | {
43 | public static float[] run(List imgs)
44 | {
45 | int rh = imgs[0].Rows;
46 | int rw = imgs[0].Cols;
47 | int rc = imgs[0].Channels();
48 | float[] res = new float[rh * rw * rc * imgs.Count];
49 |
50 | GCHandle resultHandle = default;
51 | resultHandle = GCHandle.Alloc(res, GCHandleType.Pinned);
52 | IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
53 | try
54 | {
55 | for (int j = 0; j < imgs.Count; j++)
56 | {
57 | for (int i = 0; i < rc; ++i)
58 | {
59 | using Mat dest = new(rh, rw, MatType.CV_32FC1, resultPtr + (j * rc + i) * rh * rw * sizeof(float));
60 | Cv2.ExtractChannel(imgs[j], dest, i);
61 | }
62 | }
63 | }
64 | finally
65 | {
66 | resultHandle.Free();
67 | }
68 | return res;
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/CSharp.API.Extensions.OpenCvSharp/process/resize.cs:
--------------------------------------------------------------------------------
1 | using OpenCvSharp;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Drawing;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 | using Size = OpenCvSharp.Size;
9 |
10 | namespace OpenVinoSharp.Extensions.process
11 | {
12 | public static class Resize
13 | {
14 | public static Mat imgtype0(Mat img, string limit_type, int limit_side_len,
15 | out float ratio_h, out float ratio_w)
16 | {
17 | int w = img.Cols;
18 | int h = img.Rows;
19 | float ratio = 1.0f;
20 | if (limit_type == "min")
21 | {
22 | int min_wh = Math.Min(h, w);
23 | if (min_wh < limit_side_len)
24 | {
25 | if (h < w)
26 | {
27 | ratio = (float)limit_side_len / (float)h;
28 | }
29 | else
30 | {
31 | ratio = (float)limit_side_len / (float)w;
32 | }
33 | }
34 | }
35 | else
36 | {
37 | int max_wh = Math.Max(h, w);
38 | if (max_wh > limit_side_len)
39 | {
40 | if (h > w)
41 | {
42 | ratio = (float)(limit_side_len) / (float)(h);
43 | }
44 | else
45 | {
46 | ratio = (float)(limit_side_len) / (float)(w);
47 | }
48 | }
49 | }
50 |
51 | int resize_h = (int)((float)(h) * ratio);
52 | int resize_w = (int)((float)(w) * ratio);
53 |
54 | //int resize_h = 960;
55 | //int resize_w = 960;
56 |
57 | resize_h = Math.Max((int)(Math.Round((float)(resize_h) / 32.0f) * 32), 32);
58 | resize_w = Math.Max((int)(Math.Round((float)(resize_w) / 32.0f) * 32), 32);
59 |
60 | Mat resize_img = new Mat();
61 | Cv2.Resize(img, resize_img, new Size(resize_w, resize_h));
62 | ratio_h = (float)(resize_h) / (float)(h);
63 | ratio_w = (float)(resize_w) / (float)(w);
64 | return resize_img;
65 | }
66 |
67 | public static Mat cls_img(Mat img, List cls_image_shape)
68 | {
69 | int imgC, imgH, imgW;
70 | imgC = cls_image_shape[0];
71 | imgH = cls_image_shape[1];
72 | imgW = cls_image_shape[2];
73 |
74 | float ratio = (float)img.Cols / (float)img.Rows;
75 | int resize_w, resize_h;
76 | if (Math.Ceiling(imgH * ratio) > imgW)
77 | resize_w = imgW;
78 | else
79 | resize_w = (int)(Math.Ceiling(imgH * ratio));
80 | Mat resize_img = new Mat();
81 | Cv2.Resize(img, resize_img, new Size(resize_w, imgH), 0.0f, 0.0f, InterpolationFlags.Linear);
82 | return resize_img;
83 | }
84 |
85 |
86 | public static Mat crnn_img(Mat img, float wh_ratio, int[] rec_image_shape)
87 | {
88 | int imgC, imgH, imgW;
89 | imgC = rec_image_shape[0];
90 | imgH = rec_image_shape[1];
91 | imgW = rec_image_shape[2];
92 |
93 | imgW = (int)(imgH * wh_ratio);
94 |
95 | float ratio = (float)(img.Cols) / (float)(img.Rows);
96 | int resize_w, resize_h;
97 |
98 | if (Math.Ceiling(imgH * ratio) > imgW)
99 | resize_w = imgW;
100 | else
101 | resize_w = (int)(Math.Ceiling(imgH * ratio));
102 | Mat resize_img = new Mat();
103 | Cv2.Resize(img, resize_img, new Size(resize_w, imgH), 0.0f, 0.0f, InterpolationFlags.Linear);
104 | Cv2.CopyMakeBorder(resize_img, resize_img, 0, 0, 0, (int)(imgW - resize_img.Cols), BorderTypes.Constant, new Scalar(127, 127, 127));
105 | return resize_img;
106 | }
107 |
108 | public static Mat letterbox_img(Mat image, int length, out float scales)
109 | {
110 | int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
111 | Mat max_image = Mat.Zeros(max_image_length, max_image_length, MatType.CV_8UC3);
112 | max_image = max_image * 255;
113 | Rect roi = new Rect(0, 0, image.Cols, image.Rows);
114 | image.CopyTo(new Mat(max_image, roi));
115 | Mat resize_img = new Mat();
116 | Cv2.Resize(max_image, resize_img, new Size(length, length), 0.0f, 0.0f, InterpolationFlags.Linear);
117 | scales = (float)((float)max_image_length / (float)length);
118 | return resize_img;
119 | }
120 |
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/CSharpAPI.Extensions.EmguCV.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0;net5.0;net6.0;netcoreapp3.1;net48
5 | True
6 | 12.0
7 | True
8 | OpenVinoSharp.Extensions.EmguCV
9 | OpenVINO_CSharp_API_Extensions_EmguCV
10 | True
11 | OpenVINO.CSharp.API.Extensions.EmguCV
12 | OpenVINO C# API Extensions by EmguCV
13 | 1.0.6.1
14 | Guojin Yan
15 | OpenVINO C# API Extensions by EmguCV
16 | Based on the C # platform, call the OpenVINO suite to deploy a deep learning model.
17 | https://github.com/guojin-yan/OpenVINO-CSharp-API
18 | logo.png
19 | https://github.com/guojin-yan/OpenVINO-CSharp-API
20 | git
21 | This assembly is for OpenVINO CSharp API. The expansion of API mainly involves encapsulating the inference process of common models and the pre-processing and post-processing methods of data.The image processing library uses EmguCV.
22 | ../../nuget
23 | README.md
24 | OpenVINO;CSharp-API;C#-API;Extensions;EmguCV
25 | True
26 | LICENSE.txt
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | True
38 | \
39 |
40 |
41 | True
42 | \
43 |
44 |
45 | True
46 | \
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/predictor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | ///
10 | /// Default model inference engine.
11 | ///
12 | public abstract class Predictor
13 | {
14 | protected Core m_core;
15 | protected Model m_model;
16 | protected CompiledModel m_compiled_model;
17 | protected InferRequest m_infer_request;
18 |
19 | protected bool m_use_gpu;
20 | public Predictor() { }
21 | ///
22 | /// Default constructor.
23 | ///
24 | /// Inference model path
25 | public Predictor(string model_path)
26 | {
27 | m_core = new Core();
28 | m_model = m_core.read_model(model_path);
29 | m_compiled_model = m_core.compile_model(m_model);
30 | m_infer_request = m_compiled_model.create_infer_request();
31 | }
32 | ///
33 | /// Parameter constructor.
34 | ///
35 | /// Inference model path.
36 | /// Inference model path device.
37 | /// The read-write property(string) to set/get the directory which will be used to store any data cached by plugins.
38 | ///
39 | ///
40 | ///
41 | public Predictor(string model_path, string device, string? cache_dir = null, bool? use_gpu = false, long[]? input_size = null)
42 | {
43 | string cache = cache_dir ?? null;
44 | m_use_gpu = use_gpu ?? false;
45 | m_core = new Core();
46 | if (cache != null)
47 | {
48 | m_core.set_property(device, Ov.cache_dir(cache_dir));
49 | }
50 | m_model = m_core.read_model(model_path);
51 | if (m_use_gpu)
52 | {
53 | if (input_size == null)
54 | {
55 | throw new ArgumentNullException("input_size");
56 | }
57 | m_model.reshape(new PartialShape(new Shape(input_size)));
58 | }
59 | m_compiled_model = m_core.compile_model(m_model, device);
60 | m_infer_request = m_compiled_model.create_infer_request();
61 | }
62 |
63 | ///
64 | /// The default model inference method is only applicable to single input and single output models.
65 | ///
66 | /// The input data.
67 | /// The input shape.
68 | /// Infer result.
69 | protected float[] infer(float[] input_data, long[] shape = null)
70 | {
71 | Tensor input_tensor = m_infer_request.get_input_tensor();
72 | if (shape != null)
73 | input_tensor.set_shape(new Shape(shape));
74 | input_tensor.set_data(input_data);
75 | m_infer_request.infer();
76 |
77 | Tensor output_tensor = m_infer_request.get_output_tensor();
78 | string s =output_tensor.get_shape().to_string();
79 | float[] result = output_tensor.get_data((int)output_tensor.get_size());
80 | return result;
81 |
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/runtime_option/Config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace OpenVinoSharp.Extensions.model
9 | {
10 | public interface Config
11 | {
12 | public void set_model(string model_path);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/runtime_option/ppyoloe_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class PPYoloeConfig : Config
10 | {
11 | public string model_path = null;
12 | public string device = "CPU";
13 | public bool use_gpu = false;
14 | public float det_thresh = 0.5f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 | public bool postprcoess = true;
20 |
21 | public PPYoloeConfig() { }
22 |
23 | public PPYoloeConfig(string model_path, string? device = null, bool? use_gpu = null,
24 | float? det_thresh = null, bool? postprcoess = null, long[]? input_size = null,
25 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
26 | {
27 | this.model_path = model_path;
28 | this.device = device ?? this.device;
29 | this.use_gpu = use_gpu ?? this.use_gpu;
30 | this.det_thresh = det_thresh ?? this.det_thresh;
31 | this.input_size = input_size ?? this.input_size;
32 | this.batch_num = batch_num ?? this.batch_num;
33 | this.cache_dir = cache_dir ?? this.cache_dir;
34 | this.categ_nums = categ_nums ?? this.categ_nums;
35 | this.postprcoess = postprcoess ?? this.postprcoess;
36 | }
37 |
38 | public void set_model(string model_path)
39 | {
40 | this.model_path = model_path;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/runtime_option/rtdetr_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class RtdetrConfig : Config
10 | {
11 | public string model_path = null;
12 | public string device = "CPU";
13 | public bool use_gpu = false;
14 | public float det_thresh = 0.5f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 | public bool postprcoess = true;
20 |
21 | public RtdetrConfig() { }
22 |
23 | public RtdetrConfig(string model_path, string? device = null, bool? use_gpu = null,
24 | float? det_thresh = null, bool? postprcoess = null, long[]? input_size = null,
25 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
26 | {
27 | this.model_path = model_path;
28 | this.device = device ?? this.device;
29 | this.use_gpu = use_gpu ?? this.use_gpu;
30 | this.det_thresh = det_thresh ?? this.det_thresh;
31 | this.input_size = input_size ?? this.input_size;
32 | this.batch_num = batch_num ?? this.batch_num;
33 | this.cache_dir = cache_dir ?? this.cache_dir;
34 | this.categ_nums = categ_nums ?? this.categ_nums;
35 | this.postprcoess = postprcoess ?? this.postprcoess;
36 | }
37 |
38 | public void set_model(string model_path)
39 | {
40 | this.model_path = model_path;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/runtime_option/yolo_option.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public struct Yolov8DetOption
10 | {
11 | public static string device = "CPU";
12 | public static bool use_gpu = false;
13 | public static float det_thresh = 0.3f;
14 | public static float det_nms_thresh = 0.5f;
15 | public static long[] input_size = { 1, 3, 640, 640 };
16 | public static int batch_num = 1;
17 | public static string cache_dir = "model/";
18 | public static int categ_nums = 80;
19 |
20 | }
21 |
22 | public struct Yolov8PoseOption
23 | {
24 | public static string device = "CPU";
25 | public static bool use_gpu = false;
26 | public static float det_thresh = 0.3f;
27 | public static float det_nms_thresh = 0.5f;
28 | public static long[] input_size = { 1, 3, 640, 640 };
29 | public static int batch_num = 1;
30 | public static string cache_dir = "model/";
31 | }
32 |
33 | public struct Yolov8ClsOption
34 | {
35 | public static string device = "CPU";
36 | public static bool use_gpu = false;
37 | public static float thresh = 0.3f;
38 | public static long[] input_size = { 1, 3, 224, 224 };
39 | public static int batch_num = 1;
40 | public static string cache_dir = "model/";
41 | public static int categ_nums = 1000;
42 | public static int result_num = 10;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/runtime_option/yolov5_config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions.model
8 | {
9 | public class Yolov5DetConfig : Config
10 | {
11 | public string device = "CPU";
12 | public bool use_gpu = false;
13 | public float det_thresh = 0.3f;
14 | public float det_nms_thresh = 0.45f;
15 | public long[] input_size = { 1, 3, 640, 640 };
16 | public int batch_num = 1;
17 | public string cache_dir = "model/";
18 | public int categ_nums = 80;
19 |
20 | public string model_path = null;
21 |
22 | public Yolov5DetConfig() { }
23 |
24 | public Yolov5DetConfig(string model_path, string? device = null, bool? use_gpu = null,
25 | float? det_thresh = null, float? det_nms_thresh = null, long[]? input_size = null,
26 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
27 | {
28 | this.model_path = model_path;
29 | this.device = device ?? this.device;
30 | this.use_gpu = use_gpu ?? this.use_gpu;
31 | this.det_thresh = det_thresh ?? this.det_thresh;
32 | this.det_nms_thresh = det_nms_thresh ?? this.det_nms_thresh;
33 | this.input_size = input_size ?? this.input_size;
34 | this.batch_num = batch_num ?? this.batch_num;
35 | this.cache_dir = cache_dir ?? this.cache_dir;
36 | this.categ_nums = categ_nums ?? this.categ_nums;
37 | }
38 |
39 | public void set_model(string model_path)
40 | {
41 | this.model_path = model_path;
42 | }
43 | }
44 |
45 | public class Yolov5SegConfig : Config
46 | {
47 | public string device = "CPU";
48 | public bool use_gpu = false;
49 | public float det_thresh = 0.2f;
50 | public float det_nms_thresh = 0.45f;
51 | public long[] input_size = { 1, 3, 640, 640 };
52 | public int batch_num = 1;
53 | public string cache_dir = "model/";
54 | public int categ_nums = 80;
55 |
56 | public string model_path = null;
57 |
58 | public Yolov5SegConfig() { }
59 |
60 | public Yolov5SegConfig(string model_path, string? device = null, bool? use_gpu = null,
61 | float? det_thresh = null, float? det_nms_thresh = null, long[]? input_size = null,
62 | int? batch_num = null, string? cache_dir = null, int? categ_nums = null)
63 | {
64 | this.model_path = model_path;
65 | this.device = device ?? this.device;
66 | this.use_gpu = use_gpu ?? this.use_gpu;
67 | this.det_thresh = det_thresh ?? this.det_thresh;
68 | this.det_nms_thresh = det_nms_thresh ?? this.det_nms_thresh;
69 | this.input_size = input_size ?? this.input_size;
70 | this.batch_num = batch_num ?? this.batch_num;
71 | this.cache_dir = cache_dir ?? this.cache_dir;
72 | this.categ_nums = categ_nums ?? this.categ_nums;
73 | }
74 |
75 | public void set_model(string model_path)
76 | {
77 | this.model_path = model_path;
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/model/yolov8/yolov8_cls.cs:
--------------------------------------------------------------------------------
1 | using Emgu.CV;
2 | using OpenVinoSharp.Extensions.process;
3 | using OpenVinoSharp.Extensions.result;
4 | using OpenVinoSharp.Extensions.utility;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace OpenVinoSharp.Extensions.model
12 | {
13 | public class Yolov8Cls : Predictor
14 | {
15 | private int m_categ_nums;
16 | private long[] m_input_size;
17 | private int m_batch_num;
18 | private int m_result_num;
19 | public Yolov8Cls(string model_path, string? device = null, int? categ_nums = null, bool? use_gpu = null,
20 | long[]? input_size = null, int? batch_num = null, string? cache_dir = null, int? result_num = null)
21 | : base(model_path, device ?? Yolov8ClsOption.device, cache_dir ?? Yolov8ClsOption.cache_dir,
22 | use_gpu ?? Yolov8ClsOption.use_gpu, input_size ?? Yolov8ClsOption.input_size)
23 | {
24 | m_categ_nums = categ_nums ?? Yolov8ClsOption.categ_nums;
25 | m_input_size = input_size ?? Yolov8ClsOption.input_size;
26 | m_batch_num = batch_num ?? Yolov8ClsOption.batch_num;
27 | m_result_num = result_num ?? Yolov8ClsOption.result_num;
28 | }
29 | public Yolov8Cls(Yolov8ClsConfig config)
30 | : base(config.model_path, config.device, config.cache_dir, config.use_gpu, config.input_size)
31 | {
32 | m_categ_nums = config.categ_nums;
33 | m_input_size = config.input_size;
34 | m_batch_num = config.batch_num;
35 | m_result_num = config.result_num;
36 | }
37 | public ClsResult predict(Mat image)
38 | {
39 | Mat mat = new Mat();
40 | CvInvoke.CvtColor(image, mat, Emgu.CV.CvEnum.ColorConversion.Bgr2Rgb);
41 | float factors = 0f;
42 | mat = Resize.letterbox_img(mat, (int)m_input_size[2], out factors);
43 | mat = Normalize.run(mat, true);
44 | float[] input_data = Permute.run(mat);
45 | float[] output_data = infer(input_data);
46 |
47 | List sort_result = Utility.argsort(output_data);
48 | ClsResult result = new ClsResult();
49 | for (int i = 0; i < m_result_num; ++i)
50 | {
51 | result.add(sort_result[i], output_data[sort_result[i]]);
52 | }
53 | return result;
54 | }
55 | public List predict(List images)
56 | {
57 | List results = new List();
58 | for (int beg_img_no = 0; beg_img_no < images.Count; beg_img_no += m_batch_num)
59 | {
60 | int end_img_no = Math.Min(images.Count, beg_img_no + m_batch_num);
61 | int batch_num = end_img_no - beg_img_no;
62 | List norm_img_batch = new List();
63 | float factors = 0f;
64 | for (int ino = beg_img_no; ino < end_img_no; ino++)
65 | {
66 | Mat mat = new Mat();
67 | CvInvoke.CvtColor(images[ino], mat, Emgu.CV.CvEnum.ColorConversion.Bgr2Rgb);
68 | mat = Resize.letterbox_img(mat, (int)m_input_size[2], out factors);
69 | mat = Normalize.run(mat, true);
70 | norm_img_batch.Add(mat);
71 | }
72 | float[] input_data = PermuteBatch.run(norm_img_batch);
73 | float[] output_datas = infer(input_data, new long[] { batch_num, 3, m_input_size[2], m_input_size[3] });
74 |
75 | for (int i = 0; i < batch_num; ++i)
76 | {
77 | float[] output_data = new float[m_categ_nums];
78 | Buffer.BlockCopy(output_datas, m_categ_nums * i, output_data, 0, m_categ_nums*4);
79 | List sort_result = Utility.argsort(output_data);
80 | ClsResult result = new ClsResult();
81 | for (int j = 0; j < m_result_num; ++j)
82 | {
83 | result.add(sort_result[j], output_data[sort_result[j]]);
84 | }
85 | results.Add(result);
86 | }
87 | }
88 | return results;
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/process/normalize.cs:
--------------------------------------------------------------------------------
1 | using Emgu.CV;
2 | using Emgu.CV.CvEnum;
3 | using Emgu.CV.Util;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 |
10 | namespace OpenVinoSharp.Extensions.process
11 | {
12 | ///
13 | /// Normalize data classes using EmguCV.
14 | ///
15 | public static class Normalize
16 | {
17 | ///
18 | /// Run normalize data classes.
19 | ///
20 | /// The image mat.
21 | /// Channel mean.
22 | /// Channel variance.
23 | /// Whether to divide by 255.
24 | /// The normalize data.
25 | public static Mat run(Mat im, float[] mean, float[] scale, bool is_scale)
26 | {
27 | double e = 1.0;
28 | if (is_scale)
29 | {
30 | e /= 255.0;
31 | }
32 | im.ConvertTo(im, DepthType.Cv32F, e);
33 | VectorOfMat bgr_channels = new VectorOfMat();
34 |
35 | CvInvoke.Split(im, bgr_channels);
36 | for (var i = 0; i < bgr_channels.Size; i++)
37 | {
38 | bgr_channels[i].ConvertTo(bgr_channels[i], DepthType.Cv32F, 1.0 * scale[i],
39 | (0.0 - mean[i]) * scale[i]);
40 | }
41 | Mat re = new Mat();
42 | CvInvoke.Merge(bgr_channels, re);
43 | return re;
44 | }
45 | ///
46 | /// Run normalize data classes.
47 | ///
48 | /// The image mat.
49 | /// Whether to divide by 255.
50 | /// The normalize data.
51 | public static Mat run(Mat im, bool is_scale)
52 | {
53 | double e = 1.0;
54 | if (is_scale)
55 | {
56 | e /= 255.0;
57 | }
58 | im.ConvertTo(im, DepthType.Cv32F, e);
59 | return im;
60 | }
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/process/permute.cs:
--------------------------------------------------------------------------------
1 | using Emgu.CV;
2 | using Emgu.CV.CvEnum;
3 | using Emgu.CV.Structure;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Runtime.InteropServices;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace OpenVinoSharp.Extensions.process
12 | {
13 | public static class Permute
14 | {
15 | public static float[] run(Mat im)
16 | {
17 | int rh = im.Rows;
18 | int rw = im.Cols;
19 | int rc = 3;
20 | int len = rh * rw;
21 | float[] res = new float[rh * rw * rc];
22 |
23 | //// 创建单通道图像以存储提取的通道
24 | //Mat c_0 = new Mat(im.Size, DepthType.Cv32F,1);
25 | //Mat c_1 = new Mat(im.Size, DepthType.Cv32F, 1);
26 | //Mat c_2 = new Mat(im.Size, DepthType.Cv32F, 1);
27 |
28 | //// 提取通道
29 | //CvInvoke.ExtractChannel(im, c_0, 0);
30 | //CvInvoke.ExtractChannel(im, c_1, 1);
31 | //CvInvoke.ExtractChannel(im, c_2, 2);
32 |
33 | //// 创建一个一维数组来存储所有通道的数据
34 |
35 | //// 将每个通道的数据复制到一维数组中
36 | //Buffer.BlockCopy(c_0.GetData(), 0, res, 0, len);
37 | //Buffer.BlockCopy(c_1.GetData(), 0, res, len, len);
38 | //Buffer.BlockCopy(c_2.GetData(), 0, res, len * 2, len);
39 |
40 | //return res;
41 | GCHandle resultHandle = default;
42 | try
43 | {
44 | resultHandle = GCHandle.Alloc(res, GCHandleType.Pinned);
45 | IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
46 | for (int i = 0; i < rc; ++i)
47 | {
48 | using Mat dest = new Mat(rh, rw, DepthType.Cv32F, 1, resultPtr + i * rh * rw * 4, rw * 4);
49 | CvInvoke.ExtractChannel(im, dest, i);
50 | }
51 | }
52 | finally
53 | {
54 | resultHandle.Free();
55 | }
56 | return res;
57 | }
58 |
59 |
60 | }
61 | public static class PermuteBatch
62 | {
63 | public static float[] run(List imgs)
64 | {
65 | int rh = imgs[0].Rows;
66 | int rw = imgs[0].Cols;
67 | int rc = 3;
68 | int len = rh * rw;
69 | float[] res = new float[rh * rw * rc * imgs.Count];
70 |
71 | GCHandle resultHandle = default;
72 | resultHandle = GCHandle.Alloc(res, GCHandleType.Pinned);
73 | IntPtr resultPtr = resultHandle.AddrOfPinnedObject();
74 | try
75 | {
76 | for (int j = 0; j < imgs.Count; j++)
77 | {
78 | for (int i = 0; i < rc; ++i)
79 | {
80 | using Mat dest = new Mat(rh, rw, DepthType.Cv32F, 1, resultPtr + (i + j * rc) * rh * rw * 4, rw * 4);
81 | CvInvoke.ExtractChannel(imgs[j], dest, i);
82 | }
83 | }
84 | }
85 | finally
86 | {
87 | resultHandle.Free();
88 | }
89 | return res;
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions.EmguCV/process/resize.cs:
--------------------------------------------------------------------------------
1 | using Emgu.CV.CvEnum;
2 | using Emgu.CV;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Drawing;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 | using Emgu.CV.Structure;
10 |
11 | namespace OpenVinoSharp.Extensions.process
12 | {
13 | public static class Resize
14 | {
15 | public static Mat imgtype0(Mat img, string limit_type, int limit_side_len,
16 | out float ratio_h, out float ratio_w)
17 | {
18 | int w = img.Cols;
19 | int h = img.Rows;
20 | float ratio = 1.0f;
21 | if (limit_type == "min")
22 | {
23 | int min_wh = Math.Min(h, w);
24 | if (min_wh < limit_side_len)
25 | {
26 | if (h < w)
27 | {
28 | ratio = (float)limit_side_len / (float)h;
29 | }
30 | else
31 | {
32 | ratio = (float)limit_side_len / (float)w;
33 | }
34 | }
35 | }
36 | else
37 | {
38 | int max_wh = Math.Max(h, w);
39 | if (max_wh > limit_side_len)
40 | {
41 | if (h > w)
42 | {
43 | ratio = (float)(limit_side_len) / (float)(h);
44 | }
45 | else
46 | {
47 | ratio = (float)(limit_side_len) / (float)(w);
48 | }
49 | }
50 | }
51 |
52 | int resize_h = (int)((float)(h) * ratio);
53 | int resize_w = (int)((float)(w) * ratio);
54 |
55 | //int resize_h = 960;
56 | //int resize_w = 960;
57 |
58 | resize_h = Math.Max((int)(Math.Round((float)(resize_h) / 32.0f) * 32), 32);
59 | resize_w = Math.Max((int)(Math.Round((float)(resize_w) / 32.0f) * 32), 32);
60 |
61 | Mat resize_img = new Mat();
62 | CvInvoke.Resize(img, resize_img, new Size(resize_w, resize_h));
63 | ratio_h = (float)(resize_h) / (float)(h);
64 | ratio_w = (float)(resize_w) / (float)(w);
65 | return resize_img;
66 | }
67 |
68 | public static Mat cls_img(Mat img, List cls_image_shape)
69 | {
70 | int imgC, imgH, imgW;
71 | imgC = cls_image_shape[0];
72 | imgH = cls_image_shape[1];
73 | imgW = cls_image_shape[2];
74 |
75 | float ratio = (float)img.Cols / (float)img.Rows;
76 | int resize_w, resize_h;
77 | if (Math.Ceiling(imgH * ratio) > imgW)
78 | resize_w = imgW;
79 | else
80 | resize_w = (int)(Math.Ceiling(imgH * ratio));
81 | Mat resize_img = new Mat();
82 | CvInvoke.Resize(img, resize_img, new Size(resize_w, imgH), 0.0f, 0.0f, Inter.Linear);
83 | return resize_img;
84 | }
85 |
86 |
87 | public static Mat crnn_img(Mat img, float wh_ratio, int[] rec_image_shape)
88 | {
89 | int imgC, imgH, imgW;
90 | imgC = rec_image_shape[0];
91 | imgH = rec_image_shape[1];
92 | imgW = rec_image_shape[2];
93 |
94 | imgW = (int)(imgH * wh_ratio);
95 |
96 | float ratio = (float)(img.Cols) / (float)(img.Rows);
97 | int resize_w, resize_h;
98 |
99 | if (Math.Ceiling(imgH * ratio) > imgW)
100 | resize_w = imgW;
101 | else
102 | resize_w = (int)(Math.Ceiling(imgH * ratio));
103 | Mat resize_img = new Mat();
104 | CvInvoke.Resize(img, resize_img, new Size(resize_w, imgH), 0.0f, 0.0f, Inter.Linear);
105 | CvInvoke.CopyMakeBorder(resize_img, resize_img, 0, 0, 0, (int)(imgW - resize_img.Cols), BorderType.Constant, new MCvScalar(127, 127, 127));
106 | return resize_img;
107 | }
108 |
109 | public static Mat letterbox_img(Mat image, int length, out float scales)
110 | {
111 | int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
112 | Mat max_image = Mat.Zeros(max_image_length, max_image_length, DepthType.Cv8U, 3);
113 | Rectangle roi = new Rectangle(0, 0, image.Cols, image.Rows);
114 | image.CopyTo(new Mat(max_image, roi));
115 | float[] factors = new float[4];
116 | factors[0] = factors[1] = (float)(max_image_length / 640.0);
117 | factors[2] = image.Rows;
118 | factors[3] = image.Cols;
119 | Mat resize_img = new Mat();
120 | CvInvoke.Resize(max_image, resize_img, new Size(length, length), 0.0f, 0.0f, Inter.Linear);
121 | scales = (float)((float)max_image_length / (float)length);
122 | return resize_img;
123 | }
124 |
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions/CSharpAPI.Extensions.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0;net5.0;net6.0;netcoreapp3.1;net48;net472
5 | True
6 | 12.0
7 | True
8 | OpenVinoSharp.Extensions
9 | OpenVINO_CSharp_API_Extensions
10 | True
11 | OpenVINO.CSharp.API.Extensions
12 | OpenVINO C# API Extensions by OpenCvSharp
13 | 1.0.2
14 | Guojin Yan
15 | OpenVINO C# API Extensions
16 | Based on the C # platform, call the OpenVINO suite to deploy a deep learning model.
17 | https://github.com/guojin-yan/OpenVINO-CSharp-API
18 | https://github.com/guojin-yan/OpenVINO-CSharp-API
19 | git
20 | This assembly is for OpenVINO CSharp API. The expansion of API mainly involves encapsulating the inference process of common models and the pre-processing and post-processing methods of data.
21 | ../../nuget
22 | logo.png
23 | README.md
24 | OpenVINO;CSharp-API;C#-API;Extensions;
25 | LICENSE.txt
26 | True
27 |
28 |
29 |
30 |
31 | True
32 | \
33 |
34 |
35 | True
36 | \
37 |
38 |
39 | True
40 | \
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions/base.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace OpenVinoSharp.Extensions
8 | {
9 | ///
10 | /// Customized OpenVINO application extension class, mainly
11 | /// including self-defined classes and methods.
12 | ///
13 | [System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
14 | class NamespaceDoc
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions/benchmark_app/benchmark.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 | using OpenVinoSharp.Extensions.utility;
9 |
10 | namespace OpenVinoSharp.Extensions
11 | {
12 | ///
13 | /// Model performance evaluation class
14 | ///
15 | public static class Benchmark
16 | {
17 | ///
18 | /// This sample demonstrates how to estimate performance of a model using Synchronous Inference Request API.
19 | ///
20 | /// The inference model path.
21 | /// The device name, default="CPU".
22 | /// Running status code.
23 | public static int sync_benchmark(string model_path, string device_name = "CPU")
24 | {
25 | try
26 | {
27 | Slog.INFO("OpenVINO");
28 | Version version = Ov.get_openvino_version();
29 | Slog.INFO(version.description + " " + version.buildNumber);
30 |
31 | // Optimize for latency. Most of the devices are configured for latency by default,
32 | // but there are exceptions like GNA
33 | Dictionary latency = new Dictionary();
34 | latency.Add("PERFORMANCE_HINT", "1");
35 |
36 |
37 | // Create ov::Core and use it to compile a model.
38 | // Select the device by providing the name as the second parameter to CLI.
39 | // Using MULTI device is pointless in sync scenario
40 | // because only one instance of ov::InferRequest is used
41 | Core core = new Core();
42 | Model model = core.read_model(model_path);
43 | CompiledModel compiled_model = core.compile_model(model, device_name, latency);
44 | InferRequest infer_request = compiled_model.create_infer_request();
45 | foreach (var input in compiled_model.inputs())
46 | {
47 | Common.fill_tensor_random(infer_request.get_tensor(input));
48 | }
49 | // Fill input data for the infer_request
50 | infer_request.infer();
51 | // Benchmark for seconds_to_run seconds and at least niter iterations
52 | int niter = 10;
53 | List latencies = new List();
54 |
55 | TimeSpan seconds_to_run = TimeSpan.FromSeconds(10);
56 | DateTime start = DateTime.Now;
57 | var time_point = start;
58 | var time_point_to_finish = start + seconds_to_run;
59 |
60 | while (time_point < time_point_to_finish || latencies.Count() < niter)
61 | {
62 | infer_request.infer();
63 | var iter_end = DateTime.Now;
64 | latencies.Add((iter_end - time_point).TotalMilliseconds);
65 | time_point = iter_end;
66 | }
67 |
68 | var end = time_point;
69 | double duration = (end - start).TotalMilliseconds;
70 | // Report results
71 | Slog.INFO("Count: " + latencies.Count.ToString() + " iterations");
72 | Slog.INFO("Duration: " + duration + " ms");
73 | Slog.INFO("Latency:");
74 | int percent = 50;
75 | new LatencyMetrics(latencies, "", percent).write_to_slog();
76 | Slog.INFO("Throughput: " + (latencies.Count * 1000 / duration).ToString("0.00") + "FPS");
77 | }
78 | catch (Exception ex) {
79 | Slog.INFO(ex.Message);
80 | return 1;
81 | }
82 | return 0;
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/CSharpAPI.Extensions/ov_extensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using OpenVinoSharp.Extensions.utility;
7 | namespace OpenVinoSharp.Extensions
8 | {
9 | public static class OvExtensions
10 | {
11 | ///
12 | /// Print the input and output information of the model
13 | ///
14 | /// The openvino model.
15 | public static void printf_model_info(Model model)
16 | {
17 | Slog.INFO("Inference Model");
18 | Slog.INFO(" Model name: " + model.get_friendly_name());
19 | Slog.INFO(" Input:");
20 | List inputs = model.inputs();
21 | foreach (var input in inputs)
22 | {
23 | Slog.INFO(" name: " + input.get_any_name());
24 | Slog.INFO(" type: " + input.get_element_type().c_type_string());
25 | Slog.INFO(" shape: " + input.get_partial_shape().to_string());
26 | }
27 | Slog.INFO(" Output:");
28 | List