├── .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 outputs = model.outputs(); 29 | foreach (var output in outputs) 30 | { 31 | Slog.INFO(" name: " + output.get_any_name()); 32 | Slog.INFO(" type: " + output.get_element_type().c_type_string()); 33 | Slog.INFO(" shape: " + output.get_partial_shape().to_string()); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CSharpAPI.Extensions/utility/slog.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.utility 8 | { 9 | public static class Slog 10 | { 11 | public static void INFO(string msg) 12 | { 13 | Console.WriteLine("[ INFO ] " + msg); 14 | } 15 | public static void WARN(string msg) 16 | { 17 | Console.WriteLine("[ WARN ] " + msg); 18 | } 19 | public static void ERROR(string msg) 20 | { 21 | Console.WriteLine("[ ERROR ] " + msg); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CSharpAPI.Extensions/utility/utility.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.utility 8 | { 9 | public static partial class Utility 10 | { 11 | /// 12 | /// Obtain the original position of the arranged array. 13 | /// 14 | /// The original array. 15 | /// The position after arrangement. 16 | public static List argsort(List array) 17 | { 18 | int array_len = array.Count; 19 | 20 | //生成值和索引的列表 21 | List new_array = new List { }; 22 | for (int i = 0; i < array_len; i++) 23 | { 24 | new_array.Add(new float[] { array[i], i }); 25 | } 26 | //对列表按照值大到小进行排序 27 | new_array.Sort((a, b) => b[0].CompareTo(a[0])); 28 | //获取排序后的原索引 29 | List array_index = new List(); 30 | foreach (float[] item in new_array) 31 | { 32 | array_index.Add((int)item[1]); 33 | } 34 | return array_index; 35 | } 36 | /// 37 | /// Obtain the original position of the arranged array. 38 | /// 39 | /// The original array. 40 | /// The position after arrangement. 41 | public static List argsort(float[] array) 42 | { 43 | return argsort(new List(array)); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/CSharpAPI/CSharpAPI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0;net7.0;net6.0;net5.0;netcoreapp3.1;net46;net461;net462;net47;net471;net472;net48;net481 5 | True 6 | True 7 | OpenVINO.CSharp.API 8 | 9 | OpenVINO C# API 10 | 2024.3.0.2 11 | Guojin Yan 12 | Guojin Yan 13 | OpenVINO C# API 14 | Based on the C # platform, call the OpenVINO suite to deploy a deep learning model. 15 | 16 | https://github.com/guojin-yan/OpenVINO-CSharp-API 17 | https://github.com/guojin-yan/OpenVINO-CSharp-API 18 | git 19 | ../../nuget 20 | zh 21 | logo.png 22 | README.md 23 | This version is a pre release version of OpenVINO™ C# API 3.0 and its features are not yet fully developed. If there are any issues during use, please feel free to contact me. 24 | OpenVinoSharp 25 | OpenVINO_CSharp_API 26 | OpenVINO;CSharp-API;C#-API 27 | LICENSE.txt 28 | True 29 | 30 | 31 | 32 | 33 | 34 | 35 | True 36 | \ 37 | 38 | 39 | True 40 | \ 41 | 42 | 43 | True 44 | \ 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | $(DefineConstants);DOTNET_FRAMEWORK; 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/CSharpAPI/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 | 8 | namespace OpenVinoSharp 9 | { 10 | 11 | /// 12 | /// OpenVINO wrapper for .NET. 13 | /// This is the basic namespace of OpenVINO in C#, 14 | /// and all classes and methods are within this method. 15 | /// OpenVinoSharp. 16 | /// 17 | [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 18 | class NamespaceDoc 19 | { 20 | } 21 | 22 | namespace element { 23 | /// 24 | /// OpenVINO wrapper for .NET. 25 | /// Define elements in OpenVINO. 26 | /// OpenVinoSharp.element. 27 | /// 28 | [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 29 | class NamespaceDoc 30 | { 31 | } 32 | } 33 | 34 | 35 | namespace preprocess { 36 | /// 37 | /// Mainly defined the data processing methods in OpenVINO. 38 | /// OpenVinoSharp.preprocess. 39 | /// 40 | [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 41 | class NamespaceDoc 42 | { 43 | } 44 | } 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/CSharpAPI/common/version.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 9 | { 10 | /// 11 | /// [struct] Represents version information that describes plugins and the OpenVINO library 12 | /// 13 | /// ov_runtime_c#_api 14 | public struct Version 15 | { 16 | /// 17 | /// A null terminated string with build number 18 | /// 19 | public string buildNumber; 20 | /// 21 | /// A null terminated description string 22 | /// 23 | public string description; 24 | /// 25 | /// Constructs a Version. 26 | /// 27 | /// 28 | /// 29 | public Version(string buildNumber, string description) { 30 | this.buildNumber = buildNumber; 31 | this.description = description; 32 | } 33 | 34 | /// 35 | /// Convert Version to output string 36 | /// 37 | /// Output string 38 | public string to_string() 39 | { 40 | string str = ""; 41 | str += description; 42 | str += "\r\n Version : "; 43 | str += buildNumber.Substring(0, buildNumber.IndexOf("-")); 44 | str += "\r\n Build : "; 45 | str += buildNumber; 46 | return str; 47 | } 48 | } 49 | /// 50 | /// [struct] Represents version information that describes device and ov runtime library 51 | /// 52 | public struct CoreVersion 53 | { 54 | /// 55 | /// A device name 56 | /// 57 | public string device_name; 58 | /// 59 | /// The OpenVINO version. 60 | /// 61 | public Version version; 62 | } 63 | /// 64 | /// [struct] Represents version information that describes all devices and ov runtime library 65 | /// 66 | public struct CoreVersionList 67 | { 68 | /// 69 | /// An array of device versions 70 | /// 71 | public IntPtr core_version; 72 | /// 73 | /// A number of versions in the array 74 | /// 75 | public ulong size; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/dimension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using static OpenVinoSharp.Ov; 7 | using ov_dimension = OpenVinoSharp.Ov.ov_dimension; 8 | 9 | namespace OpenVinoSharp 10 | { 11 | /// 12 | /// Class representing a dimension, which may be dynamic (undetermined until runtime), 13 | /// in a shape or shape-like object. 14 | /// 15 | /// Static dimensions may be implicitly converted from value_type. 16 | /// A dynamic dimension is constructed with Dimension() or Dimension::dynamic(). 17 | public class Dimension 18 | { 19 | /// 20 | /// The ov_dimension struct. 21 | /// 22 | ov_dimension m_dimension; 23 | /// 24 | /// Construct a static dimension. 25 | /// 26 | /// Value of the dimension. 27 | public Dimension(long dimension) 28 | { 29 | m_dimension.min = dimension; 30 | m_dimension.max = dimension; 31 | } 32 | /// 33 | /// Construct a dynamic dimension with ov_dimension struct. 34 | /// 35 | /// The ov_dimension struct. 36 | public Dimension(ov_dimension dimension) 37 | { 38 | m_dimension = dimension; 39 | } 40 | /// 41 | /// Construct a dynamic dimension with bounded range 42 | /// 43 | /// The lower inclusive limit for the dimension 44 | /// The upper inclusive limit for the dimension 45 | public Dimension(long min_dimension, long max_dimension) 46 | { 47 | m_dimension.min = min_dimension; 48 | m_dimension.max = max_dimension; 49 | } 50 | /// 51 | /// Get ov_dimension struct. 52 | /// 53 | /// Return ov_dimension struct. 54 | public ov_dimension get_dimension() 55 | { 56 | return m_dimension; 57 | } 58 | /// 59 | /// Get max. 60 | /// 61 | /// Dimension max. 62 | public long get_max() 63 | { 64 | return m_dimension.max; 65 | } 66 | 67 | /// 68 | /// Get min. 69 | /// 70 | /// Dimension min. 71 | public long get_min() 72 | { 73 | return m_dimension.min; 74 | } 75 | /// 76 | /// Check this dimension whether is dynamic 77 | /// 78 | /// Boolean, true is dynamic and false is static. 79 | public bool is_dynamic() 80 | { 81 | return (m_dimension.min == 0 && m_dimension.max == -1) ? true : false; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/layout.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing.Imaging; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OpenVinoSharp 9 | { 10 | /// 11 | /// ov::Layout represents the text information of tensor's dimensions/axes. E.g. layout `NCHW` means that 4D 12 | /// tensor `{-1, 3, 480, 640}` will have: 13 | /// - 0: `N = -1`: batch dimension is dynamic 14 | /// - 1: `C = 3`: number of channels is '3' 15 | /// - 2: `H = 480`: image height is 480 16 | /// - 3: `W = 640`: image width is 640 17 | /// 18 | /// 19 | /// `ov::Layout` can be specified for: 20 | /// - Preprocessing purposes. E.g. 21 | /// - To apply normalization (means/scales) it is usually required to set 'C' dimension in a layout. 22 | /// - To resize the image to specified width/height it is needed to set 'H' and 'W' dimensions in a layout 23 | /// - To transpose image - source and target layout can be set (see 24 | /// `ov::preprocess::PreProcessSteps::convert_layout`) 25 | /// - To set/get model's batch (see `ov::get_batch`/`ov::set_batch') it is required in general to specify 'N' dimension 26 | /// in layout for appropriate inputs 27 | /// 28 | public class Layout : IDisposable 29 | { 30 | /// 31 | /// [private]Layout class pointer. 32 | /// 33 | private IntPtr m_ptr = IntPtr.Zero; 34 | /// 35 | /// [public]Layout class pointer. 36 | /// 37 | public IntPtr Ptr 38 | { 39 | get { return m_ptr; } 40 | set { m_ptr = value; } 41 | } 42 | 43 | /// 44 | /// Constructs a Layout with static or dynamic layout information based on string representation. 45 | /// 46 | /// 47 | /// The string used to construct Layout from. 48 | /// The string representation can be in the following form: 49 | /// - can define order and meaning for dimensions "NCHW" 50 | /// - partial layout specialization: 51 | /// - "NC?" defines 3 dimensional layout, first two NC, 3rd one is not defined 52 | /// - "N...C" defines layout with dynamic rank where 1st dimension is N, last one is C 53 | /// - "NC..." defines layout with dynamic rank where first two are NC, others are not 54 | /// defined 55 | /// - only order of dimensions "adbc" (0312) 56 | /// - Advanced syntax can be used for multi-character names like "[N,C,H,W,...,CustomName]" 57 | /// 58 | public Layout(string layout_desc) 59 | { 60 | sbyte[] c_layout_desc = (sbyte[])((Array)System.Text.Encoding.Default.GetBytes(layout_desc)); 61 | HandleException.handler( 62 | NativeMethods.ov_layout_create(ref c_layout_desc[0], ref m_ptr)); 63 | 64 | } 65 | 66 | /// 67 | /// Default deconstruction 68 | /// 69 | ~Layout() 70 | { 71 | Dispose(); 72 | } 73 | 74 | /// 75 | /// Release unmanaged resources. 76 | /// 77 | public void Dispose() 78 | { 79 | if (m_ptr == IntPtr.Zero) 80 | { 81 | return; 82 | } 83 | NativeMethods.ov_layout_free(m_ptr); 84 | m_ptr = IntPtr.Zero; 85 | } 86 | 87 | /// 88 | /// String representation of Layout. 89 | /// 90 | /// String representation of Layout. 91 | public string to_string() 92 | { 93 | return NativeMethods.ov_layout_to_string(m_ptr); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/node.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 9 | { 10 | /// 11 | /// Nodes are the backbone of the graph of Value dataflow. Every node has 12 | /// zero or more nodes as arguments and one value, which is either a tensor 13 | /// or a (possibly empty) tuple of values. 14 | /// 15 | public class Node : IDisposable 16 | { 17 | /// 18 | /// The node type. 19 | /// 20 | public enum NodeType 21 | { 22 | /// 23 | /// Const type. 24 | /// 25 | e_const = 0, 26 | /// 27 | /// Nomal type. 28 | /// 29 | e_nomal = 1 30 | }; 31 | /// 32 | /// [private]Node class pointer. 33 | /// 34 | public IntPtr m_ptr = IntPtr.Zero; 35 | 36 | /// 37 | /// [public]Node class pointer. 38 | /// 39 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 40 | 41 | /// 42 | /// Specify the format type of the node. 43 | /// 44 | public NodeType node_type { get; set; } 45 | 46 | /// 47 | /// Default Constructor. 48 | /// 49 | /// The pointer of node. 50 | /// The type of node. 51 | public Node(IntPtr ptr, NodeType type) 52 | { 53 | Ptr = ptr; 54 | this.node_type = type; 55 | } 56 | /// 57 | /// Default deconstruction. 58 | /// 59 | ~Node() 60 | { 61 | Dispose(); 62 | } 63 | /// 64 | /// Release unmanaged resources. 65 | /// 66 | public void Dispose() 67 | { 68 | if (m_ptr == IntPtr.Zero) 69 | { 70 | return; 71 | } 72 | if (node_type == NodeType.e_const) 73 | { 74 | NativeMethods.ov_output_const_port_free(m_ptr); 75 | } 76 | else 77 | { 78 | NativeMethods.ov_output_port_free(m_ptr); 79 | } 80 | m_ptr = IntPtr.Zero; 81 | } 82 | 83 | /// 84 | /// Get the shape. 85 | /// 86 | /// Returns the shape. 87 | public Shape get_shape() 88 | { 89 | int l = Marshal.SizeOf(typeof(Ov.ov_shape)); 90 | IntPtr shape_ptr = Marshal.AllocHGlobal(l); 91 | if (node_type == NodeType.e_const) 92 | { 93 | HandleException.handler( 94 | NativeMethods.ov_const_port_get_shape(m_ptr, shape_ptr)); 95 | } 96 | else 97 | { 98 | HandleException.handler( 99 | NativeMethods.ov_port_get_shape(m_ptr, shape_ptr)); 100 | } 101 | 102 | return new Shape(shape_ptr); 103 | } 104 | 105 | /// 106 | /// Get the partial shape. 107 | /// 108 | /// Returns the partial shape. 109 | public PartialShape get_partial_shape() 110 | { 111 | Ov.ov_partial_shape shape = new Ov.ov_partial_shape(); 112 | HandleException.handler( 113 | NativeMethods.ov_port_get_partial_shape(m_ptr, ref shape)); 114 | return new PartialShape(shape); 115 | } 116 | 117 | /// 118 | /// Get the unique name of the node. 119 | /// 120 | /// A const reference to the node's unique name. 121 | public string get_name() 122 | { 123 | ExceptionStatus status; 124 | IntPtr s_ptr = IntPtr.Zero; 125 | HandleException.handler( 126 | NativeMethods.ov_port_get_any_name(m_ptr, ref s_ptr)); 127 | string ss = Marshal.PtrToStringAnsi(s_ptr); 128 | return ss; 129 | } 130 | /// 131 | /// Checks that there is exactly one output and returns its element type. 132 | /// 133 | /// 134 | /// TODO: deprecate in favor of node->get_output_element_type(0) with a suitable check in 135 | /// the calling code, or updates to the calling code if it is making an invalid assumption 136 | /// of only one output. 137 | /// 138 | /// Data type. 139 | public OvType get_element_type() 140 | { 141 | uint data_type = 0; 142 | HandleException.handler( 143 | NativeMethods.ov_port_get_element_type(m_ptr, ref data_type)); 144 | return new OvType((ElementType)data_type); 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/node_input.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 8 | { 9 | /// 10 | /// A handle for one of a node's inputs. 11 | /// 12 | public class Input : IDisposable 13 | { 14 | /// 15 | /// The input node. 16 | /// 17 | private Node m_node; 18 | /// 19 | /// The input node port index. 20 | /// 21 | private ulong m_index = 0; 22 | /// 23 | /// Constructs a Output. 24 | /// 25 | /// The node for the input handle. 26 | /// The index of the input. 27 | public Input(Node node, ulong index) 28 | { 29 | m_node = node; 30 | m_index = index; 31 | } 32 | /// 33 | /// Default deconstruction. 34 | /// 35 | ~Input() { 36 | Dispose(); 37 | } 38 | /// 39 | /// Release unmanaged resources. 40 | /// 41 | public void Dispose() 42 | { 43 | m_node.Dispose(); 44 | } 45 | /// 46 | /// Get the node referred to by this input handle. 47 | /// 48 | /// The ouput node 49 | public Node get_node() { return m_node; } 50 | /// 51 | /// The index of the input referred to by this input handle. 52 | /// 53 | /// The index of the input. 54 | public ulong get_index() { return m_index; } 55 | /// 56 | /// The element type of the input referred to by this input handle. 57 | /// 58 | /// The element type of the input. 59 | public OvType get_element_type() { return m_node.get_element_type(); } 60 | /// 61 | /// The shape of the input referred to by this input handle. 62 | /// 63 | /// The shape of the input . 64 | public Shape get_shape() { return m_node.get_shape(); } 65 | /// 66 | /// Any tensor names associated with this input 67 | /// 68 | /// tensor names 69 | public string get_any_name() { return m_node.get_name(); } 70 | /// 71 | /// The partial shape of the input referred to by this input handle. 72 | /// 73 | /// The partial shape of the input 74 | public PartialShape get_partial_shape() { return m_node.get_partial_shape(); } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/node_output.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 8 | { 9 | /// 10 | /// A handle for one of a node's outputs. 11 | /// 12 | public class Output : IDisposable 13 | { 14 | /// 15 | /// The output node. 16 | /// 17 | private Node m_node; 18 | /// 19 | /// The output node port index. 20 | /// 21 | private ulong m_index = 0; 22 | /// 23 | /// Constructs a Output. 24 | /// 25 | /// The node for the output handle. 26 | /// The index of the output. 27 | public Output(Node node, ulong index=0) 28 | { 29 | m_node = node; 30 | m_index = index; 31 | } 32 | /// 33 | /// Default deconstruction. 34 | /// 35 | ~Output() 36 | { 37 | Dispose(); 38 | } 39 | /// 40 | /// Release unmanaged resources. 41 | /// 42 | public void Dispose() 43 | { 44 | m_node.Dispose(); 45 | } 46 | /// 47 | /// Get the node referred to by this output handle. 48 | /// 49 | /// The ouput node 50 | public Node get_node() { return m_node; } 51 | /// 52 | /// The index of the output referred to by this output handle. 53 | /// 54 | /// The index of the output. 55 | public ulong get_index() { return m_index; } 56 | /// 57 | /// The element type of the output referred to by this output handle. 58 | /// 59 | /// The element type of the output. 60 | public OvType get_element_type() { return m_node.get_element_type(); } 61 | /// 62 | /// The shape of the output referred to by this output handle. 63 | /// 64 | /// The shape of the output . 65 | public Shape get_shape(){ return m_node.get_shape(); } 66 | /// 67 | /// Any tensor names associated with this output 68 | /// 69 | /// tensor names 70 | public string get_any_name() { return m_node.get_name(); } 71 | /// 72 | /// The partial shape of the output referred to by this output handle. 73 | /// 74 | /// The partial shape of the output 75 | public PartialShape get_partial_shape() { return m_node.get_partial_shape(); } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/CSharpAPI/core/remote_context.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | namespace OpenVinoSharp 7 | { 8 | /// 9 | /// This class represents an abstraction for remote (non-CPU) accelerator device-specific inference context. 10 | /// Such context represents a scope on the device within which compiled models and remote memory tensors can exist, 11 | /// function, and exchange data. 12 | /// 13 | public class RemoteContext 14 | { 15 | /// 16 | /// [private]RemoteContext class pointer. 17 | /// 18 | private IntPtr m_ptr = IntPtr.Zero; 19 | /// 20 | /// [public]RemoteContext class pointer. 21 | /// 22 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 23 | /// 24 | /// Default Constructor 25 | /// 26 | /// RemoteContext pointer. 27 | public RemoteContext(IntPtr ptr) 28 | { 29 | if (ptr == IntPtr.Zero) 30 | { 31 | System.Diagnostics.Debug.WriteLine("RemoteContext init error : ptr is null!"); 32 | return; 33 | } 34 | Ptr = ptr; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CSharpAPI/exception/exception.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.Serialization; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OpenVinoSharp 9 | { 10 | /// 11 | /// The default exception to be thrown by OpenVINO 12 | /// 13 | [Serializable] 14 | // ReSharper disable once InconsistentNaming 15 | internal class OVException : Exception 16 | { 17 | /// 18 | /// The numeric code for error status 19 | /// 20 | public ExceptionStatus status { get; set; } 21 | 22 | 23 | /// 24 | /// A description of the error 25 | /// 26 | public string err_msg { get; set; } 27 | 28 | 29 | /// 30 | /// Constructor 31 | /// 32 | /// The numeric code for error status 33 | /// The source file name where error is encountered 34 | /// A description of the error 35 | /// The source file name where error is encountered 36 | /// The line number in the source where error is encountered 37 | public OVException(ExceptionStatus status, string err_msg) 38 | : base(err_msg) 39 | { 40 | this.status = status; 41 | this.err_msg = err_msg; 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_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 8 | { 9 | /// 10 | /// Introducing C API. 11 | /// 12 | public partial class NativeMethods 13 | { 14 | private const string dll_extern = "openvino_c"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_common.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 9 | { 10 | public partial class NativeMethods 11 | { 12 | /// 13 | /// Print the error info. 14 | /// 15 | /// a status code. 16 | /// error info. 17 | [DllImport(dll_extern, EntryPoint = "ov_get_error_info", 18 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 19 | public extern static string ov_get_error_info(int status); 20 | 21 | /// 22 | /// free char 23 | /// 24 | /// The pointer to the char to free. 25 | [DllImport(dll_extern, EntryPoint = "ov_free", 26 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 27 | public extern static void ov_free(ref char content); 28 | 29 | 30 | /// 31 | /// Get the last error msg. 32 | /// 33 | /// The last error msg. 34 | [DllImport(dll_extern, EntryPoint = "ov_get_last_err_msg", 35 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 36 | public extern static IntPtr ov_get_last_err_msg(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_dimension.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 | using ov_dimension = OpenVinoSharp.Ov.ov_dimension; 9 | 10 | namespace OpenVinoSharp 11 | { 12 | public partial class NativeMethods 13 | { 14 | 15 | 16 | 17 | /// 18 | /// Check this dimension whether is dynamic 19 | /// 20 | /// The dimension pointer that will be checked. 21 | /// Boolean, true is dynamic and false is static. 22 | [DllImport(dll_extern, EntryPoint = "ov_dimension_is_dynamic", 23 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 24 | public extern static bool ov_dimension_is_dynamic(ov_dimension dim); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_layout.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 9 | { 10 | public partial class NativeMethods 11 | { 12 | /// 13 | /// Create a layout object. 14 | /// 15 | /// The description of layout. 16 | /// The layout input pointer. 17 | /// a status code, return OK if successful 18 | [DllImport(dll_extern, EntryPoint = "ov_layout_create", 19 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 20 | public extern static ExceptionStatus ov_layout_create( 21 | ref sbyte layout_desc, 22 | ref IntPtr layout); 23 | 24 | /// 25 | /// Free layout object. 26 | /// 27 | /// The pointer of layout. 28 | [DllImport(dll_extern, EntryPoint = "ov_layout_free", 29 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 30 | public extern static void ov_layout_free(IntPtr layout); 31 | 32 | /// 33 | /// Convert layout object to a readable string. 34 | /// 35 | /// layout will be converted. 36 | /// string that describes the layout content. 37 | [DllImport(dll_extern, EntryPoint = "ov_layout_to_string", 38 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 39 | public extern static string ov_layout_to_string(IntPtr layout); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_node.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 9 | { 10 | public partial class NativeMethods 11 | { 12 | /// 13 | /// Get the shape of port object. 14 | /// 15 | /// A pointer to ov_output_const_port_t. 16 | /// tensor shape. 17 | /// Status code of the operation: OK(0) for success. 18 | [DllImport(dll_extern, EntryPoint = "ov_const_port_get_shape", 19 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 20 | public extern static ExceptionStatus ov_const_port_get_shape( 21 | IntPtr port, 22 | IntPtr tensor_shape); 23 | 24 | /// 25 | /// Get the shape of port object. 26 | /// 27 | /// A pointer to ov_output_port_t. 28 | /// A pointer to the tensor name. 29 | /// Status code of the operation: OK(0) for success. 30 | [DllImport(dll_extern, EntryPoint = "ov_const_port_get_shape", 31 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 32 | public extern static ExceptionStatus ov_port_get_shape( 33 | IntPtr port, 34 | IntPtr tensor_shape); 35 | /// 36 | /// Get the tensor name of port. 37 | /// 38 | /// A pointer to the ov_output_const_port_t. 39 | /// A pointer to the tensor name. 40 | /// Status code of the operation: OK(0) for success. 41 | [DllImport(dll_extern, EntryPoint = "ov_port_get_any_name", 42 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 43 | public extern static ExceptionStatus ov_port_get_any_name( 44 | IntPtr port, 45 | ref IntPtr tensor_name); 46 | 47 | /// 48 | /// Get the partial shape of port. 49 | /// 50 | /// A pointer to the ov_output_const_port_t. 51 | /// Partial shape. 52 | /// Status code of the operation: OK(0) for success. 53 | [DllImport(dll_extern, EntryPoint = "ov_port_get_partial_shape", 54 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 55 | public extern static ExceptionStatus ov_port_get_partial_shape( 56 | IntPtr port, 57 | ref Ov.ov_partial_shape partial_shape); 58 | 59 | /// 60 | /// Get the tensor type of port. 61 | /// 62 | /// A pointer to the ov_output_const_port_t. 63 | /// tensor type. 64 | /// Status code of the operation: OK(0) for success. 65 | [DllImport(dll_extern, EntryPoint = "ov_port_get_element_type", 66 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 67 | public extern static ExceptionStatus ov_port_get_element_type( 68 | IntPtr port, 69 | ref uint tensor_type); 70 | 71 | /// 72 | /// free port object 73 | /// 74 | /// The pointer to the instance of the ov_output_port_t to free. 75 | [DllImport(dll_extern, EntryPoint = "ov_output_port_free", 76 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 77 | public extern static void ov_output_port_free( 78 | IntPtr port); 79 | 80 | /// 81 | /// free const port 82 | /// 83 | /// The pointer to the instance of the ov_output_const_port_t to free. 84 | [DllImport(dll_extern, EntryPoint = "ov_output_const_port_free", 85 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 86 | public extern static void ov_output_const_port_free( 87 | IntPtr port); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_rank.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 | using ov_rank = OpenVinoSharp.Ov.ov_rank; 9 | namespace OpenVinoSharp 10 | { 11 | 12 | public partial class NativeMethods 13 | { 14 | 15 | /// 16 | /// Check this rank whether is dynamic 17 | /// 18 | /// The rank pointer that will be checked. 19 | /// The return value. 20 | [DllImport(dll_extern, EntryPoint = "ov_rank_is_dynamic", 21 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 22 | public extern static bool ov_rank_is_dynamic(ov_rank rank); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CSharpAPI/native_methods/ov_shape.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 9 | { 10 | public partial class NativeMethods 11 | { 12 | /// 13 | /// Initialize a fully shape object, allocate space for its dimensions 14 | /// and set its content id dims is not null. 15 | /// 16 | /// The rank value for this object, it should be more than 0(>0) 17 | /// The dimensions data for this shape object, it's size should be equal to rank. 18 | /// The input/output shape object pointer. 19 | /// Status code of the operation: OK(0) for success. 20 | [DllImport(dll_extern, EntryPoint = "ov_shape_create", 21 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 22 | public extern static ExceptionStatus ov_shape_create( 23 | long rank, 24 | ref long dims, 25 | IntPtr shape); 26 | 27 | /// 28 | /// Free a shape object's internal memory. 29 | /// 30 | /// The input shape object pointer. 31 | /// Status code of the operation: OK(0) for success. 32 | [DllImport(dll_extern, EntryPoint = "ov_shape_free", 33 | CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] 34 | public extern static ExceptionStatus ov_shape_free( 35 | IntPtr shape); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/CSharpAPI/ov/ov.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace OpenVinoSharp 7 | { /// 8 | /// Global functions under ov namespace 9 | /// 10 | public static partial class Ov 11 | { 12 | /// 13 | /// Get version of OpenVINO. 14 | /// 15 | /// Version of OpenVINO 16 | public static Version get_openvino_version() 17 | { 18 | int l = Marshal.SizeOf(typeof(Version)); 19 | IntPtr ptr = Marshal.AllocHGlobal(l); 20 | ExceptionStatus status = NativeMethods.ov_get_openvino_version(ptr); 21 | if (status != 0) 22 | { 23 | System.Diagnostics.Debug.WriteLine("ov get_openvino_version() error!"); 24 | return new Version(); 25 | } 26 | var temp = Marshal.PtrToStructure(ptr, typeof(Version)); 27 | Version version = (Version)temp; 28 | string build = string.Copy(version.buildNumber); 29 | string description = string.Copy(version.description); 30 | Version new_version = new Version(build, description); 31 | NativeMethods.ov_version_free(ptr); 32 | return new_version; 33 | } 34 | 35 | public static byte[] content_from_file(string file) 36 | { 37 | FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); 38 | 39 | long len = fs.Seek(0, SeekOrigin.End); 40 | 41 | 42 | fs.Seek(0, SeekOrigin.Begin); 43 | 44 | byte[] data = new byte[len + 1]; 45 | 46 | fs.Read(data, 0, (int)len); 47 | return data; 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/CSharpAPI/preprocess/common.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.preprocess 8 | { 9 | /// 10 | /// This enum contains enumerations for color format. 11 | /// 12 | public enum ColorFormat : uint 13 | { 14 | /// 15 | /// Undefine color format 16 | /// 17 | UNDEFINE = 0U, 18 | /// 19 | /// Image in NV12 format as single tensor 20 | /// 21 | NV12_SINGLE_PLANE, 22 | /// 23 | /// Image in NV12 format represented as separate tensors for Y and UV planes. 24 | /// 25 | NV12_TWO_PLANES, 26 | /// 27 | /// Image in I420 (YUV) format as single tensor 28 | /// 29 | I420_SINGLE_PLANE, 30 | /// 31 | /// Image in I420 format represented as separate tensors for Y, U and V planes. 32 | /// 33 | I420_THREE_PLANES, 34 | /// 35 | /// Image in RGB interleaved format (3 channels) 36 | /// 37 | RGB, 38 | /// 39 | /// Image in BGR interleaved format (3 channels) 40 | /// 41 | BGR, 42 | /// 43 | /// Image in GRAY format (1 channel) 44 | /// 45 | GRAY, 46 | /// 47 | /// Image in RGBX interleaved format (4 channels) 48 | /// 49 | RGBX, 50 | /// 51 | /// Image in BGRX interleaved format (4 channels) 52 | /// 53 | BGRX 54 | }; 55 | /// 56 | /// This enum contains codes for all preprocess resize algorithm. 57 | /// 58 | public enum ResizeAlgorithm 59 | { 60 | /// 61 | /// linear algorithm 62 | /// 63 | RESIZE_LINEAR, 64 | /// 65 | /// cubic algorithm 66 | /// 67 | RESIZE_CUBIC, 68 | /// 69 | /// nearest algorithm 70 | /// 71 | RESIZE_NEAREST 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /src/CSharpAPI/preprocess/input_info.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.preprocess 8 | { 9 | /// 10 | /// Class holding preprocessing information for one input 11 | /// From preprocessing pipeline perspective, each input can be represented as: 12 | /// - User's input parameter info (InputInfo::tensor) 13 | /// - Preprocessing steps applied to user's input (InputInfo::preprocess) 14 | /// - Model's input info, which is a final input's info after preprocessing (InputInfo::model) 15 | /// 16 | public class InputInfo : IDisposable 17 | { 18 | /// 19 | /// [private]InputInfo class pointer. 20 | /// 21 | public IntPtr m_ptr = IntPtr.Zero; 22 | 23 | /// 24 | /// [public]InputInfo class pointer. 25 | /// 26 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 27 | 28 | /// 29 | /// Default construction through InputInfo pointer. 30 | /// 31 | /// InputInfo pointer. 32 | public InputInfo(IntPtr ptr) 33 | { 34 | if (ptr == IntPtr.Zero) 35 | { 36 | throw new OVException(ExceptionStatus.GENERAL_ERROR, "The ptr is null!"); 37 | } 38 | this.m_ptr = ptr; 39 | } 40 | /// 41 | /// Default destructor 42 | /// 43 | ~InputInfo() { Dispose(); } 44 | /// 45 | /// Release unmanaged resources. 46 | /// 47 | public void Dispose() 48 | { 49 | if (m_ptr == IntPtr.Zero) 50 | { 51 | return; 52 | } 53 | NativeMethods.ov_preprocess_input_info_free(m_ptr); 54 | m_ptr = IntPtr.Zero; 55 | } 56 | 57 | /// 58 | /// Get current input tensor information with ability to change specific data 59 | /// 60 | /// Reference to current input tensor structure 61 | public InputTensorInfo tensor() 62 | { 63 | IntPtr input_tensor_ptr = IntPtr.Zero; 64 | HandleException.handler( 65 | NativeMethods.ov_preprocess_input_info_get_tensor_info(m_ptr, ref input_tensor_ptr)); 66 | return new InputTensorInfo(input_tensor_ptr); 67 | } 68 | 69 | /// 70 | /// Get current input preprocess information with ability to add more preprocessing steps 71 | /// 72 | /// Reference to current preprocess steps structure. 73 | public PreProcessSteps preprocess() 74 | { 75 | IntPtr preprocess_ptr = IntPtr.Zero; 76 | HandleException.handler( 77 | NativeMethods.ov_preprocess_input_info_get_preprocess_steps(m_ptr, ref preprocess_ptr)); 78 | return new PreProcessSteps(preprocess_ptr); 79 | } 80 | 81 | /// 82 | /// Get current input model information with ability to change original model's input data 83 | /// 84 | /// Reference to current model's input information structure. 85 | public InputModelInfo model() 86 | { 87 | IntPtr model_ptr = IntPtr.Zero; 88 | HandleException.handler( 89 | NativeMethods.ov_preprocess_input_info_get_model_info(m_ptr, ref model_ptr)); 90 | return new InputModelInfo(model_ptr); 91 | } 92 | }; 93 | } 94 | -------------------------------------------------------------------------------- /src/CSharpAPI/preprocess/input_model_info.cs: -------------------------------------------------------------------------------- 1 | using OpenVinoSharp; 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.preprocess 9 | { 10 | /// 11 | /// Information about model's input tensor. If all information is already included to loaded model, this info 12 | /// may not be needed. However it can be set to specify additional information about model, like 'layout'. 13 | /// 14 | /// 15 | /// Example of usage of model 'layout': 16 | /// Support model has input parameter with shape {1, 3, 224, 224} and user needs to resize input image to model's 17 | /// dimensions. It can be done like this 18 | /// 19 | public class InputModelInfo : IDisposable 20 | { 21 | /// 22 | /// [private]InputModelInfo class pointer. 23 | /// 24 | public IntPtr m_ptr = IntPtr.Zero; 25 | 26 | /// 27 | /// [public]InputModelInfo class pointer. 28 | /// 29 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 30 | 31 | /// 32 | /// Default construction through InputModelInfo pointer. 33 | /// 34 | /// InputModelInfo pointer. 35 | public InputModelInfo(IntPtr ptr) 36 | { 37 | if (ptr == IntPtr.Zero) 38 | { 39 | HandleException.handler(ExceptionStatus.PTR_NULL); 40 | return; 41 | } 42 | this.m_ptr = ptr; 43 | } 44 | /// 45 | /// Default destructor 46 | /// 47 | ~InputModelInfo() { Dispose(); } 48 | /// 49 | /// Release unmanaged resources 50 | /// 51 | public void Dispose() 52 | { 53 | if (m_ptr == IntPtr.Zero) 54 | { 55 | return; 56 | } 57 | NativeMethods.ov_preprocess_input_model_info_free(m_ptr); 58 | m_ptr = IntPtr.Zero; 59 | } 60 | 61 | /// 62 | /// Set layout for model's input tensor. This version allows chaining for Lvalue objects 63 | /// 64 | /// Layout for model's input tensor. 65 | /// Reference to 'this' to allow chaining with other calls in a builder-like manner 66 | public InputModelInfo set_layout(Layout layout) 67 | { 68 | HandleException.handler( 69 | NativeMethods.ov_preprocess_input_model_info_set_layout(m_ptr, layout.Ptr)); 70 | return this; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/CSharpAPI/preprocess/output_info.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.preprocess 8 | { 9 | /// 10 | /// Class holding postprocessing information for one output 11 | /// From postprocessing pipeline perspective, each output can be represented as: 12 | /// - Model's output info, (OutputInfo::model) 13 | /// - Postprocessing steps applied to user's input (OutputInfo::postprocess) 14 | /// - User's desired output parameter information, which is a final one after preprocessing (OutputInfo::tensor) 15 | /// 16 | public class OutputInfo : IDisposable 17 | { 18 | /// 19 | /// [private]OutputInfo class pointer. 20 | /// 21 | public IntPtr m_ptr = IntPtr.Zero; 22 | 23 | /// 24 | /// [public]OutputInfo class pointer. 25 | /// 26 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 27 | 28 | /// 29 | /// Default construction through OutputInfo pointer. 30 | /// 31 | /// OutputInfo pointer. 32 | public OutputInfo(IntPtr ptr) 33 | { 34 | if (ptr == IntPtr.Zero) 35 | { 36 | HandleException.handler(ExceptionStatus.PTR_NULL); 37 | return; 38 | } 39 | this.m_ptr = ptr; 40 | } 41 | /// 42 | /// Default destructor 43 | /// 44 | ~OutputInfo() { Dispose(); } 45 | /// 46 | /// Release unmanaged resources 47 | /// 48 | public void Dispose() 49 | { 50 | if (m_ptr == IntPtr.Zero) 51 | { 52 | return; 53 | } 54 | NativeMethods.ov_preprocess_output_info_free(m_ptr); 55 | m_ptr = IntPtr.Zero; 56 | } 57 | 58 | /// 59 | /// Get current output tensor information with ability to change specific data 60 | /// 61 | /// Reference to current output tensor structure 62 | public OutputTensorInfo tensor() 63 | { 64 | IntPtr output_tensor_ptr = IntPtr.Zero; 65 | HandleException.handler( 66 | NativeMethods.ov_preprocess_output_info_get_tensor_info(m_ptr, ref output_tensor_ptr)); 67 | return new OutputTensorInfo(output_tensor_ptr); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/CSharpAPI/preprocess/output_tensor_info.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.preprocess 8 | { 9 | /// 10 | /// Information about user's desired output tensor. By default, it will be initialized to same data 11 | /// (type/shape/etc) as model's output parameter. User application can override particular parameters (like 12 | /// 'element_type') according to application's data and specify appropriate conversions in post-processing steps 13 | /// 14 | public class OutputTensorInfo : IDisposable 15 | { 16 | /// 17 | /// [private]OutputTensorInfo class pointer. 18 | /// 19 | public IntPtr m_ptr = IntPtr.Zero; 20 | 21 | /// 22 | /// [public]OutputTensorInfo class pointer. 23 | /// 24 | public IntPtr Ptr { get { return m_ptr; } set { m_ptr = value; } } 25 | 26 | /// 27 | /// Default construction through OutputTensorInfo pointer. 28 | /// 29 | /// OutputTensorInfo pointer. 30 | public OutputTensorInfo(IntPtr ptr) 31 | { 32 | if (ptr == IntPtr.Zero) 33 | { 34 | HandleException.handler(ExceptionStatus.PTR_NULL); 35 | return; 36 | } 37 | this.m_ptr = ptr; 38 | } 39 | /// 40 | /// Default destructor 41 | /// 42 | ~OutputTensorInfo() { Dispose(); } 43 | /// 44 | /// Release unmanaged resources 45 | /// 46 | public void Dispose() 47 | { 48 | if (m_ptr == IntPtr.Zero) 49 | { 50 | return; 51 | } 52 | NativeMethods.ov_preprocess_output_tensor_info_free(m_ptr); 53 | m_ptr = IntPtr.Zero; 54 | } 55 | 56 | /// 57 | /// Set element type for user's desired output tensor. 58 | /// 59 | /// Element type for user's output tensor. 60 | /// Reference to 'this' to allow chaining with other calls in a builder-like manner. 61 | public OutputTensorInfo set_element_type(OvType type) 62 | { 63 | HandleException.handler( 64 | NativeMethods.ov_preprocess_output_set_element_type(m_ptr, (uint)type.get_type())); 65 | return this; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/csharp_api_extensions_emgucv_unit_tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/ppyoloe/PPYoloeDetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.Extensions.model; 3 | using OpenVinoSharp.Extensions.result; 4 | using OpenVinoSharp.Extensions.process; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | using Emgu.CV; 11 | 12 | namespace OpenVinoSharp.Extensions.model.Tests 13 | { 14 | [TestClass()] 15 | public class PPYoloeDetTests 16 | { 17 | public string model_path = "E:\\Model\\ppyoloe\\model.pdmodel"; 18 | 19 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 20 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 21 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 22 | public string image_path3 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 23 | [TestMethod()] 24 | public void PPYoloeDet_test() 25 | { 26 | Config config = new PPYoloeConfig(model_path); 27 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 28 | } 29 | 30 | [TestMethod()] 31 | public void predict_test() 32 | { 33 | Config config = new PPYoloeConfig(model_path); 34 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 35 | Mat image = CvInvoke.Imread(image_path1); 36 | 37 | DetResult result = yoloe.predict(image); 38 | Mat im = Visualize.draw_det_result(result, image); 39 | CvInvoke.Imshow("ww", im); 40 | CvInvoke.WaitKey(0); 41 | } 42 | 43 | [TestMethod()] 44 | public void predict_test1() 45 | { 46 | List images = new List() 47 | { 48 | CvInvoke.Imread(image_path), 49 | CvInvoke.Imread(image_path1), 50 | CvInvoke.Imread(image_path2), 51 | CvInvoke.Imread(image_path3) 52 | }; 53 | PPYoloeConfig config = new PPYoloeConfig(model_path); 54 | config.batch_num = 4; 55 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 56 | 57 | List result = yoloe.predict(images); 58 | Mat im = Visualize.draw_det_result(result[0], images[0]); 59 | CvInvoke.Imshow("ww", im); 60 | Mat im1 = Visualize.draw_det_result(result[1], images[1]); 61 | CvInvoke.Imshow("ww1", im1); 62 | Mat im2 = Visualize.draw_det_result(result[2], images[2]); 63 | CvInvoke.Imshow("ww2", im2); 64 | Mat im3 = Visualize.draw_det_result(result[3], images[3]); 65 | CvInvoke.Imshow("ww3", im3); 66 | CvInvoke.WaitKey(0); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/rtdetr/RtdetrDetTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 5 | using OpenVinoSharp.Extensions.process; 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.Tests 13 | { 14 | [TestClass()] 15 | public class RtdetrDetTests 16 | { 17 | public string model_path = "E:\\Model\\RT-DETR\\RTDETR\\rtdetr_r50vd_6x_coco.onnx"; 18 | public string model_path1 = "E:\\Model\\RT-DETR\\RTDETR_cropping\\rtdetr_r50vd_6x_coco.onnx"; 19 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 20 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 21 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 22 | public string image_path3 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 23 | [TestMethod()] 24 | public void RtdetrDet_test() 25 | { 26 | Config config = new RtdetrConfig(model_path); 27 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 28 | } 29 | 30 | [TestMethod()] 31 | public void predict_test() 32 | { 33 | 34 | Mat image = CvInvoke.Imread(image_path1); 35 | Config config = new RtdetrConfig(model_path); 36 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 37 | DetResult result = rtdetr.predict(image); 38 | Mat im = Visualize.draw_det_result(result, image); 39 | CvInvoke.Imshow("ww", im); 40 | CvInvoke.WaitKey(0); 41 | } 42 | [TestMethod()] 43 | public void predict_test_1() 44 | { 45 | 46 | Mat image = CvInvoke.Imread(image_path1); 47 | RtdetrConfig config = new RtdetrConfig(model_path1); 48 | config.postprcoess = false; 49 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 50 | DetResult result = rtdetr.predict(image); 51 | Mat im = Visualize.draw_det_result(result, image); 52 | CvInvoke.Imshow("ww", im); 53 | CvInvoke.WaitKey(0); 54 | } 55 | 56 | [TestMethod()] 57 | public void predict_test1() 58 | { 59 | List images = new List() 60 | { 61 | CvInvoke.Imread(image_path), 62 | CvInvoke.Imread(image_path1), 63 | CvInvoke.Imread(image_path2), 64 | CvInvoke.Imread(image_path3) 65 | }; 66 | RtdetrConfig config = new RtdetrConfig(model_path); 67 | config.batch_num = 4; 68 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 69 | List result = rtdetr.predict(images); 70 | Mat im = Visualize.draw_det_result(result[0], images[0]); 71 | CvInvoke.Imshow("ww", im); 72 | Mat im1 = Visualize.draw_det_result(result[1], images[1]); 73 | CvInvoke.Imshow("ww1", im1); 74 | Mat im2 = Visualize.draw_det_result(result[2], images[2]); 75 | CvInvoke.Imshow("ww2", im2); 76 | Mat im3 = Visualize.draw_det_result(result[3], images[3]); 77 | CvInvoke.Imshow("ww3", im3); 78 | CvInvoke.WaitKey(0); 79 | } 80 | [TestMethod()] 81 | public void predict_test1_1() 82 | { 83 | List images = new List() 84 | { 85 | CvInvoke.Imread(image_path), 86 | CvInvoke.Imread(image_path1), 87 | CvInvoke.Imread(image_path2), 88 | CvInvoke.Imread(image_path3) 89 | }; 90 | RtdetrConfig config = new RtdetrConfig(model_path1); 91 | config.postprcoess = false; 92 | config.batch_num = 4; 93 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 94 | List result = rtdetr.predict(images); 95 | Mat im = Visualize.draw_det_result(result[0], images[0]); 96 | CvInvoke.Imshow("ww", im); 97 | Mat im1 = Visualize.draw_det_result(result[1], images[1]); 98 | CvInvoke.Imshow("ww1", im1); 99 | Mat im2 = Visualize.draw_det_result(result[2], images[2]); 100 | CvInvoke.Imshow("ww2", im2); 101 | Mat im3 = Visualize.draw_det_result(result[3], images[3]); 102 | CvInvoke.Imshow("ww3", im3); 103 | CvInvoke.WaitKey(0); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov5/Yolov5DetTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 5 | using OpenVinoSharp.Extensions.process; 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.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov5DetTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov5\\yolov5s.xml"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 21 | [TestMethod()] 22 | public void Yolov5Det_test() 23 | { 24 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 25 | Yolov5Det yolo = new Yolov5Det(config); 26 | } 27 | 28 | [TestMethod()] 29 | public void predict_test() 30 | { 31 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 32 | Yolov5Det yolo = new Yolov5Det(config); 33 | Mat image = CvInvoke.Imread(image_path); 34 | DetResult result = yolo.predict(image); 35 | Mat im = Visualize.draw_det_result(result, image); 36 | CvInvoke.Imshow("ww", im); 37 | CvInvoke.WaitKey(0); 38 | Assert.IsNotNull(result); 39 | } 40 | 41 | [TestMethod()] 42 | public void predict_test1() 43 | { 44 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 45 | Yolov5Det yolo = new Yolov5Det(config); 46 | List images = new List(); 47 | images.Add(CvInvoke.Imread(image_path)); 48 | images.Add(CvInvoke.Imread(image_path1)); 49 | images.Add(CvInvoke.Imread(image_path2)); 50 | List results = yolo.predict(images); 51 | Mat im = Visualize.draw_det_result(results[0], images[0]); 52 | CvInvoke.Imshow("ww", im); 53 | CvInvoke.WaitKey(0); 54 | im = Visualize.draw_det_result(results[1], images[1]); 55 | CvInvoke.Imshow("ww", im); 56 | CvInvoke.WaitKey(0); 57 | im = Visualize.draw_det_result(results[2], images[2]); 58 | CvInvoke.Imshow("ww", im); 59 | CvInvoke.WaitKey(0); 60 | Assert.IsNotNull(results); 61 | } 62 | 63 | [TestMethod()] 64 | public void process_result_test() 65 | { 66 | Assert.Fail(); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov5/Yolov5SegTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 5 | using OpenVinoSharp.Extensions.process; 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.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov5SegTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov5\\yolov5s-seg.onnx"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 21 | 22 | [TestMethod()] 23 | public void Yolov5Seg_test() 24 | { 25 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 26 | Yolov5Seg yolo = new Yolov5Seg(config); 27 | } 28 | 29 | [TestMethod()] 30 | public void predict_test() 31 | { 32 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 33 | Yolov5Seg yolo = new Yolov5Seg(config); 34 | Mat image = CvInvoke.Imread(image_path); 35 | SegResult result = yolo.predict(image); 36 | Mat im = Visualize.draw_seg_result(result, image); 37 | CvInvoke.Imshow("ww", im); 38 | CvInvoke.WaitKey(0); 39 | Assert.IsNotNull(result); 40 | } 41 | 42 | [TestMethod()] 43 | public void predict_test1() 44 | { 45 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 46 | Yolov5Seg yolo = new Yolov5Seg(config); 47 | List images = new List(); 48 | images.Add(CvInvoke.Imread(image_path)); 49 | images.Add(CvInvoke.Imread(image_path1)); 50 | images.Add(CvInvoke.Imread(image_path2)); 51 | List results = yolo.predict(images); 52 | Mat im = Visualize.draw_seg_result(results[0], images[0]); 53 | CvInvoke.Imshow("ww", im); 54 | CvInvoke.WaitKey(0); 55 | im = Visualize.draw_seg_result(results[1], images[1]); 56 | CvInvoke.Imshow("ww", im); 57 | CvInvoke.WaitKey(0); 58 | im = Visualize.draw_seg_result(results[2], images[2]); 59 | CvInvoke.Imshow("ww", im); 60 | CvInvoke.WaitKey(0); 61 | Assert.IsNotNull(results); 62 | 63 | } 64 | 65 | [TestMethod()] 66 | public void process_result_test() 67 | { 68 | Assert.Fail(); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov8/Yolov8ClsTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using OpenVinoSharp.Extensions.result; 10 | using OpenVinoSharp.Extensions.model; 11 | 12 | namespace OpenVinoSharp.Extensions.model.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov8ClsTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-cls.xml"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_4.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_6.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_7.jpg"; 21 | [TestMethod()] 22 | public void predict_test() 23 | { 24 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 25 | Mat image = CvInvoke.Imread(image_path); 26 | ClsResult result = yolo.predict(image); 27 | result.update_lable(ImageNetOption.lables); 28 | Assert.IsNotNull(result); 29 | } 30 | 31 | [TestMethod()] 32 | public void predict_test1() 33 | { 34 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 35 | List images = new List(); 36 | images.Add(CvInvoke.Imread(image_path)); 37 | images.Add(CvInvoke.Imread(image_path1)); 38 | images.Add(CvInvoke.Imread(image_path2)); 39 | List results = yolo.predict(images); 40 | results[0].update_lable(ImageNetOption.lables); 41 | results[1].update_lable(ImageNetOption.lables); 42 | results[2].update_lable(ImageNetOption.lables); 43 | Assert.IsNotNull(results); 44 | } 45 | 46 | [TestMethod()] 47 | public void Yolov8Cls_test() 48 | { 49 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 50 | } 51 | 52 | [TestMethod()] 53 | public void Yolov8Cls_test1() 54 | { 55 | Config config = new Yolov8ClsConfig(model_path: model_xml_path); 56 | Yolov8Cls yolo = new Yolov8Cls((Yolov8ClsConfig)config); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov8/Yolov8DetTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 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.Tests 12 | { 13 | [TestClass()] 14 | public class Yolov8DetTests 15 | { 16 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s.xml"; 17 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 18 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 19 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 20 | [TestMethod()] 21 | public void Yolov8Det_test() 22 | { 23 | Yolov8Det yolo = new Yolov8Det(model_xml_path); 24 | 25 | } 26 | 27 | [TestMethod()] 28 | public void predict_test() 29 | { 30 | Yolov8Det yolo = new Yolov8Det(model_xml_path); 31 | Mat image = CvInvoke.Imread(image_path); 32 | DetResult result = yolo.predict(image); 33 | Assert.IsNotNull(result); 34 | } 35 | 36 | [TestMethod()] 37 | public void predict_test1() 38 | { 39 | Yolov8Det yolo = new Yolov8Det(model_xml_path); 40 | List images = new List(); 41 | images.Add(CvInvoke.Imread(image_path)); 42 | images.Add(CvInvoke.Imread(image_path1)); 43 | images.Add(CvInvoke.Imread(image_path2)); 44 | List results = yolo.predict(images); 45 | Assert.IsNotNull(results); 46 | } 47 | 48 | [TestMethod()] 49 | public void process_result_test() 50 | { 51 | 52 | } 53 | 54 | [TestMethod()] 55 | public void Yolov8Det_test1() 56 | { 57 | Config config = new Yolov8DetConfig(model_xml_path); 58 | Yolov8Det yolo = new Yolov8Det((Yolov8DetConfig)config); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov8/Yolov8PoseTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using OpenVinoSharp.Extensions.result; 10 | using OpenVinoSharp.Extensions.process; 11 | using static System.Net.Mime.MediaTypeNames; 12 | 13 | namespace OpenVinoSharp.Extensions.model.Tests 14 | { 15 | [TestClass()] 16 | public class Yolov8PoseTests 17 | { 18 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-pose.xml"; 19 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 20 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 21 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_10.jpg"; 22 | [TestMethod()] 23 | public void Yolov8Pose_test() 24 | { 25 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 26 | } 27 | 28 | [TestMethod()] 29 | public void predict_test() 30 | { 31 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 32 | Mat image = CvInvoke.Imread(image_path2); 33 | PoseResult result = yolo.predict(image); 34 | Mat im = Visualize.draw_poses(result, image, 0.2f); 35 | //CvInvoke.Imshow("ww", im); 36 | //CvInvoke.WaitKey(0); 37 | Assert.IsNotNull(result); 38 | } 39 | 40 | [TestMethod()] 41 | public void predict_test1() 42 | { 43 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 44 | List images = new List(); 45 | images.Add(CvInvoke.Imread(image_path)); 46 | images.Add(CvInvoke.Imread(image_path1)); 47 | images.Add(CvInvoke.Imread(image_path2)); 48 | List results = yolo.predict(images); 49 | Mat im = Visualize.draw_poses(results[0], images[0], 0.2f); 50 | //CvInvoke.Imshow("ww", im); 51 | //CvInvoke.WaitKey(0); 52 | im = Visualize.draw_poses(results[1], images[1], 0.2f); 53 | //CvInvoke.Imshow("ww", im); 54 | //CvInvoke.WaitKey(0); 55 | im = Visualize.draw_poses(results[2], images[2], 0.2f); 56 | //CvInvoke.Imshow("ww", im); 57 | //CvInvoke.WaitKey(0); 58 | Assert.IsNotNull(results); 59 | } 60 | 61 | [TestMethod()] 62 | public void process_result_test() 63 | { 64 | 65 | } 66 | 67 | [TestMethod()] 68 | public void Yolov8Pose_test1() 69 | { 70 | Config config = new Yolov8PoseConfig(model_xml_path); 71 | Yolov8Pose yolo = new Yolov8Pose((Yolov8PoseConfig)config); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_emgucv_unit_tests/model/yolov8/Yolov8SegTests.cs: -------------------------------------------------------------------------------- 1 | using Emgu.CV; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using OpenVinoSharp.Extensions.model; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using OpenVinoSharp.Extensions.result; 10 | using OpenVinoSharp.Extensions.process; 11 | 12 | namespace OpenVinoSharp.Extensions.model.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov8SegTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-seg.xml"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 21 | [TestMethod()] 22 | public void Yolov8Seg_test() 23 | { 24 | Yolov8Seg yolo = new Yolov8Seg(model_xml_path); 25 | } 26 | 27 | [TestMethod()] 28 | public void predict_test() 29 | { 30 | Yolov8Seg yolo = new Yolov8Seg(model_xml_path); 31 | Mat image = CvInvoke.Imread(image_path1); 32 | SegResult result = yolo.predict(image); 33 | //CvInvoke.Imshow("aa", result.datas[0].mask); 34 | //CvInvoke.Imshow("bb", result.datas[1].mask); 35 | //CvInvoke.Imshow("cc", result.datas[2].mask); 36 | //Mat new_mat = Visualize.draw_seg_result(result, image); 37 | //CvInvoke.Imshow("dd", new_mat); 38 | 39 | //CvInvoke.WaitKey(0); 40 | //CvInvoke.WaitKey(0); 41 | Assert.IsNotNull(result); 42 | } 43 | 44 | [TestMethod()] 45 | public void predict_test1() 46 | { 47 | Yolov8Seg yolo = new Yolov8Seg(model_xml_path); 48 | Mat image = CvInvoke.Imread(image_path1); 49 | List images = new List(); 50 | images.Add(image); 51 | List result = yolo.predict(images); 52 | //CvInvoke.Imshow("aa", result.datas[0].mask); 53 | //CvInvoke.Imshow("bb", result.datas[1].mask); 54 | //CvInvoke.Imshow("cc", result.datas[2].mask); 55 | //Mat new_mat = Visualize.draw_seg_result(result, image); 56 | //CvInvoke.Imshow("dd", new_mat); 57 | 58 | //CvInvoke.WaitKey(0); 59 | //CvInvoke.WaitKey(0); 60 | Assert.IsNotNull(result); 61 | } 62 | 63 | [TestMethod()] 64 | public void process_result_test() 65 | { 66 | 67 | } 68 | 69 | [TestMethod()] 70 | public void Yolov8Seg_test1() 71 | { 72 | Config config = new Yolov8SegConfig(model_xml_path); 73 | Yolov8Seg yolo = new Yolov8Seg((Yolov8SegConfig)config); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/csharp_api_extensions_opencvsharp_unit_tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/ppyoloe/PPYoloeDetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 5 | using OpenVinoSharp.Extensions.process; 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.Tests 13 | { 14 | [TestClass()] 15 | public class PPYoloeDetTests 16 | { 17 | public string model_path = "E:\\Model\\ppyoloe\\model.pdmodel"; 18 | 19 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 20 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 21 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 22 | public string image_path3 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 23 | [TestMethod()] 24 | public void PPYoloeDet_test() 25 | { 26 | Config config = new PPYoloeConfig(model_path); 27 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 28 | } 29 | 30 | [TestMethod()] 31 | public void predict_test() 32 | { 33 | Config config = new PPYoloeConfig(model_path); 34 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 35 | Mat image = Cv2.ImRead(image_path1); 36 | 37 | DetResult result = yoloe.predict(image); 38 | Mat im = Visualize.draw_det_result(result, image); 39 | Cv2.ImShow("ww", im); 40 | Cv2.WaitKey(0); 41 | } 42 | 43 | [TestMethod()] 44 | public void predict_test1() 45 | { 46 | List images = new List() 47 | { 48 | Cv2.ImRead(image_path), 49 | Cv2.ImRead(image_path1), 50 | Cv2.ImRead(image_path2), 51 | Cv2.ImRead(image_path3) 52 | }; 53 | PPYoloeConfig config = new PPYoloeConfig(model_path); 54 | config.batch_num = 4; 55 | PPYoloeDet yoloe = new PPYoloeDet((PPYoloeConfig)config); 56 | 57 | List result = yoloe.predict(images); 58 | Mat im = Visualize.draw_det_result(result[0], images[0]); 59 | Cv2.ImShow("ww", im); 60 | Mat im1 = Visualize.draw_det_result(result[1], images[1]); 61 | Cv2.ImShow("ww1", im1); 62 | Mat im2 = Visualize.draw_det_result(result[2], images[2]); 63 | Cv2.ImShow("ww2", im2); 64 | Mat im3 = Visualize.draw_det_result(result[3], images[3]); 65 | Cv2.ImShow("ww3", im3); 66 | Cv2.WaitKey(0); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/rtdetr/RtdetrDetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 5 | using OpenVinoSharp.Extensions.process; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using System.Runtime.InteropServices; 12 | 13 | namespace OpenVinoSharp.Extensions.model.Tests 14 | { 15 | [TestClass()] 16 | public class RtdetrDetTests 17 | { 18 | public string model_path = "E:\\Model\\RT-DETR\\RTDETR\\rtdetr_r50vd_6x_coco.onnx"; 19 | public string model_path1 = "E:\\Model\\RT-DETR\\RTDETR_cropping\\rtdetr_r50vd_6x_coco.onnx"; 20 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 21 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 22 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 23 | public string image_path3 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 24 | [TestMethod()] 25 | public void RtdetrDet_test() 26 | { 27 | Config config = new RtdetrConfig(model_path); 28 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 29 | } 30 | 31 | [TestMethod()] 32 | public void predict_test() 33 | { 34 | 35 | Mat image = Cv2.ImRead(image_path1); 36 | Config config = new RtdetrConfig(model_path); 37 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 38 | DetResult result = rtdetr.predict(image); 39 | Mat im = Visualize.draw_det_result(result, image); 40 | Cv2.ImShow("ww", im); 41 | Cv2.WaitKey(0); 42 | } 43 | [TestMethod()] 44 | public void predict_test_1() 45 | { 46 | 47 | Mat image = Cv2.ImRead(image_path1); 48 | RtdetrConfig config = new RtdetrConfig(model_path1); 49 | config.postprcoess = false; 50 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 51 | DetResult result = rtdetr.predict(image); 52 | Mat im = Visualize.draw_det_result(result, image); 53 | Cv2.ImShow("ww", im); 54 | Cv2.WaitKey(0); 55 | } 56 | 57 | [TestMethod()] 58 | public void predict_test1() 59 | { 60 | List images = new List() 61 | { 62 | Cv2.ImRead(image_path), 63 | Cv2.ImRead(image_path1), 64 | Cv2.ImRead(image_path2), 65 | Cv2.ImRead(image_path3) 66 | }; 67 | RtdetrConfig config = new RtdetrConfig(model_path); 68 | config.batch_num = 4; 69 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 70 | List result = rtdetr.predict(images); 71 | //Mat im = Visualize.draw_det_result(result[0], images[0]); 72 | //Cv2.ImShow("ww", im); 73 | //Mat im1 = Visualize.draw_det_result(result[1], images[1]); 74 | //Cv2.ImShow("ww1", im1); 75 | //Mat im2 = Visualize.draw_det_result(result[2], images[2]); 76 | //Cv2.ImShow("ww2", im2); 77 | //Mat im3 = Visualize.draw_det_result(result[3], images[3]); 78 | //Cv2.ImShow("ww3", im3); 79 | //Cv2.WaitKey(0); 80 | } 81 | [TestMethod()] 82 | public void predict_test1_1() 83 | { 84 | List images = new List() 85 | { 86 | Cv2.ImRead(image_path), 87 | Cv2.ImRead(image_path1), 88 | Cv2.ImRead(image_path2), 89 | Cv2.ImRead(image_path3) 90 | }; 91 | RtdetrConfig config = new RtdetrConfig(model_path1); 92 | config.postprcoess = false; 93 | config.batch_num = 4; 94 | RtdetrDet rtdetr = new RtdetrDet((RtdetrConfig)config); 95 | List result = rtdetr.predict(images); 96 | //Mat im = Visualize.draw_det_result(result[0], images[0]); 97 | //Cv2.ImShow("ww", im); 98 | //Mat im1 = Visualize.draw_det_result(result[1], images[1]); 99 | //Cv2.ImShow("ww1", im1); 100 | //Mat im2 = Visualize.draw_det_result(result[2], images[2]); 101 | //Cv2.ImShow("ww2", im2); 102 | //Mat im3 = Visualize.draw_det_result(result[3], images[3]); 103 | //Cv2.ImShow("ww3", im3); 104 | //Cv2.WaitKey(0); 105 | } 106 | } 107 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov5/Yolov5DetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.result; 4 | using OpenVinoSharp.Extensions.model; 5 | using OpenVinoSharp.Extensions.process; 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.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov5DetTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov5\\yolov5s.xml"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 21 | 22 | [TestMethod()] 23 | public void Yolov5Det_test() 24 | { 25 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 26 | Yolov5Det yolo = new Yolov5Det(config); 27 | } 28 | 29 | [TestMethod()] 30 | public void predict_test() 31 | { 32 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 33 | Yolov5Det yolo = new Yolov5Det(config); 34 | Mat image = Cv2.ImRead(image_path); 35 | DetResult result = yolo.predict(image); 36 | Mat im = Visualize.draw_det_result(result, image); 37 | Cv2.ImShow("ww", im); 38 | Cv2.WaitKey(0); 39 | Assert.IsNotNull(result); 40 | } 41 | 42 | [TestMethod()] 43 | public void predict_test1() 44 | { 45 | Yolov5DetConfig config = new Yolov5DetConfig(model_xml_path); 46 | Yolov5Det yolo = new Yolov5Det(config); 47 | List images = new List(); 48 | images.Add(Cv2.ImRead(image_path)); 49 | images.Add(Cv2.ImRead(image_path1)); 50 | images.Add(Cv2.ImRead(image_path2)); 51 | List results = yolo.predict(images); 52 | Mat im = Visualize.draw_det_result(results[0], images[0]); 53 | Cv2.ImShow("ww", im); 54 | Cv2.WaitKey(0); 55 | im = Visualize.draw_det_result(results[1], images[1]); 56 | Cv2.ImShow("ww", im); 57 | Cv2.WaitKey(0); 58 | im = Visualize.draw_det_result(results[2], images[2]); 59 | Cv2.ImShow("ww", im); 60 | Cv2.WaitKey(0); 61 | Assert.IsNotNull(results); 62 | } 63 | 64 | [TestMethod()] 65 | public void process_result_test() 66 | { 67 | 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov5/Yolov5SegTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.result; 4 | using OpenVinoSharp.Extensions.process; 5 | using OpenVinoSharp.Extensions.model; 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.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov5SegTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov5\\yolov5s-seg.onnx"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 21 | 22 | [TestMethod()] 23 | public void Yolov5Seg_test() 24 | { 25 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 26 | Yolov5Seg yolo = new Yolov5Seg(config); 27 | } 28 | 29 | [TestMethod()] 30 | public void predict_test() 31 | { 32 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 33 | Yolov5Seg yolo = new Yolov5Seg(config); 34 | Mat image = Cv2.ImRead(image_path1); 35 | SegResult result = yolo.predict(image); 36 | Mat im = Visualize.draw_seg_result(result, image); 37 | Cv2.ImShow("ww", im); 38 | Cv2.WaitKey(0); 39 | Assert.IsNotNull(result); 40 | } 41 | 42 | [TestMethod()] 43 | public void predict_test1() 44 | { 45 | Yolov5SegConfig config = new Yolov5SegConfig(model_xml_path); 46 | Yolov5Seg yolo = new Yolov5Seg(config); 47 | List images = new List(); 48 | images.Add(Cv2.ImRead(image_path)); 49 | images.Add(Cv2.ImRead(image_path1)); 50 | images.Add(Cv2.ImRead(image_path2)); 51 | List results = yolo.predict(images); 52 | Mat im = Visualize.draw_seg_result(results[0], images[0]); 53 | Cv2.ImShow("ww", im); 54 | Cv2.WaitKey(0); 55 | im = Visualize.draw_seg_result(results[1], images[1]); 56 | Cv2.ImShow("ww", im); 57 | Cv2.WaitKey(0); 58 | im = Visualize.draw_seg_result(results[2], images[2]); 59 | Cv2.ImShow("ww", im); 60 | Cv2.WaitKey(0); 61 | Assert.IsNotNull(results); 62 | } 63 | 64 | [TestMethod()] 65 | public void process_result_test() 66 | { 67 | 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov8/Yolov8ClsTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.result; 4 | using OpenVinoSharp.Extensions.model; 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.Tests 12 | { 13 | [TestClass()] 14 | public class Yolov8ClsTests 15 | { 16 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-cls.xml"; 17 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_4.jpg"; 18 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_6.jpg"; 19 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_7.jpg"; 20 | [TestMethod()] 21 | public void Yolov8Cls_test() 22 | { 23 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 24 | } 25 | 26 | [TestMethod()] 27 | public void predict_test() 28 | { 29 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 30 | Mat image = Cv2.ImRead(image_path); 31 | ClsResult result = yolo.predict(image); 32 | result.update_lable(ImageNetOption.lables); 33 | Assert.IsNotNull(result); 34 | } 35 | 36 | [TestMethod()] 37 | public void predict_test1() 38 | { 39 | Yolov8Cls yolo = new Yolov8Cls(model_xml_path); 40 | List images = new List(); 41 | images.Add(Cv2.ImRead(image_path)); 42 | images.Add(Cv2.ImRead(image_path1)); 43 | images.Add(Cv2.ImRead(image_path2)); 44 | List results = yolo.predict(images); 45 | results[0].update_lable(ImageNetOption.lables); 46 | results[1].update_lable(ImageNetOption.lables); 47 | results[2].update_lable(ImageNetOption.lables); 48 | Assert.IsNotNull(results); 49 | } 50 | 51 | [TestMethod()] 52 | public void Yolov8Cls_test1() 53 | { 54 | Config config = new Yolov8ClsConfig(model_path: model_xml_path); 55 | Yolov8Cls yolo = new Yolov8Cls((Yolov8ClsConfig)config); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov8/Yolov8DetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.result; 4 | using OpenVinoSharp.Extensions.model; 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.Tests 12 | { 13 | [TestClass()] 14 | public class Yolov8DetTests 15 | { 16 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s.xml"; 17 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 18 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 19 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 20 | [TestMethod()] 21 | public void Yolov8Det_test() 22 | { 23 | Yolov8Det yolo = new Yolov8Det(model_xml_path); 24 | 25 | } 26 | 27 | [TestMethod()] 28 | public void predict_test() 29 | { 30 | Yolov8Det yolo = new Yolov8Det(model_xml_path); 31 | Mat image = Cv2.ImRead(image_path); 32 | DetResult result = yolo.predict(image); 33 | Assert.IsNotNull(result); 34 | } 35 | 36 | [TestMethod()] 37 | public void predict_test1() 38 | { 39 | Config config = new Yolov8DetConfig(model_xml_path); 40 | Yolov8Det yolo = new Yolov8Det((Yolov8DetConfig)config); 41 | List images = new List(); 42 | images.Add(Cv2.ImRead(image_path)); 43 | images.Add(Cv2.ImRead(image_path1)); 44 | images.Add(Cv2.ImRead(image_path2)); 45 | List results = yolo.predict(images); 46 | Assert.IsNotNull(results); 47 | } 48 | [TestMethod()] 49 | public void process_result_test() 50 | { 51 | } 52 | 53 | [TestMethod()] 54 | public void Yolov8Det_test1() 55 | { 56 | Config config = new Yolov8DetConfig(model_xml_path); 57 | Yolov8Det yolo = new Yolov8Det((Yolov8DetConfig)config); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov8/Yolov8PoseTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.model; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using OpenVinoSharp.Extensions.result; 10 | using OpenVinoSharp.Extensions.process; 11 | 12 | namespace OpenVinoSharp.Extensions.model.Tests 13 | { 14 | [TestClass()] 15 | public class Yolov8PoseTests 16 | { 17 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-pose.xml"; 18 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 19 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_9.jpg"; 20 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_10.jpg"; 21 | [TestMethod()] 22 | public void Yolov8Pose_test() 23 | { 24 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 25 | 26 | } 27 | 28 | [TestMethod()] 29 | public void predict_test() 30 | { 31 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 32 | Mat image = Cv2.ImRead(image_path2); 33 | PoseResult result = yolo.predict(image); 34 | Mat im = Visualize.draw_poses(result, image, 0.2f); 35 | //Cv2.ImShow("ww", im); 36 | //Cv2.WaitKey(0); 37 | Assert.IsNotNull(result); 38 | } 39 | 40 | [TestMethod()] 41 | public void predict_test1() 42 | { 43 | Yolov8Pose yolo = new Yolov8Pose(model_xml_path); 44 | List images = new List(); 45 | images.Add(Cv2.ImRead(image_path)); 46 | images.Add(Cv2.ImRead(image_path1)); 47 | images.Add(Cv2.ImRead(image_path2)); 48 | List results = yolo.predict(images); 49 | Assert.IsNotNull(results); 50 | } 51 | 52 | [TestMethod()] 53 | public void process_result_test() 54 | { 55 | } 56 | 57 | [TestMethod()] 58 | public void Yolov8Pose_test1() 59 | { 60 | Config config = new Yolov8PoseConfig(model_xml_path); 61 | Yolov8Pose yolo = new Yolov8Pose((Yolov8PoseConfig)config); 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_opencvsharp_unit_tests/model/yolov8/Yolov8SegTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenCvSharp; 3 | using OpenVinoSharp.Extensions.model; 4 | using OpenVinoSharp.Extensions.result; 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.Tests 12 | { 13 | [TestClass()] 14 | public class Yolov8SegTests 15 | { 16 | public string model_xml_path = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s-seg.xml"; 17 | public string image_path = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_1.jpg"; 18 | public string image_path1 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_2.jpg"; 19 | public string image_path2 = "..\\..\\..\\..\\..\\tests\\test_data\\image\\demo_3.jpg"; 20 | [TestMethod()] 21 | public void Yolov8Seg_test() 22 | { 23 | Yolov8Seg yolo = new Yolov8Seg(model_xml_path); 24 | } 25 | 26 | [TestMethod()] 27 | public void predict_test() 28 | { 29 | Yolov8Seg yolo = new Yolov8Seg(model_xml_path); 30 | Mat image = Cv2.ImRead(image_path1); 31 | SegResult result = yolo.predict(image); 32 | Assert.IsNotNull(result); 33 | } 34 | 35 | [TestMethod()] 36 | public void predict_test1() 37 | { 38 | Config config = new Yolov8SegConfig(model_xml_path); 39 | Yolov8Seg yolo = new Yolov8Seg((Yolov8SegConfig)config); 40 | List images = new List(); 41 | images.Add(Cv2.ImRead(image_path)); 42 | images.Add(Cv2.ImRead(image_path1)); 43 | images.Add(Cv2.ImRead(image_path2)); 44 | List results = yolo.predict(images); 45 | Assert.IsNotNull(results); 46 | } 47 | 48 | [TestMethod()] 49 | public void process_result_test() 50 | { 51 | } 52 | 53 | [TestMethod()] 54 | public void Yolov8Seg_test1() 55 | { 56 | Config config = new Yolov8SegConfig(model_xml_path); 57 | Yolov8Seg yolo = new Yolov8Seg((Yolov8SegConfig)config); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_unit_tests/benchmark_app/BenchmarkTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.Extensions; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Extensions.Tests 10 | { 11 | [TestClass()] 12 | public class BenchmarkTests 13 | { 14 | [TestMethod()] 15 | public void sync_benchmark_test() 16 | { 17 | Benchmark.sync_benchmark("..\\..\\..\\..\\..\\model\\yolov8\\yolov8s-cls.xml"); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/csharp_api_extensions_unit_tests/csharp_api_extensions_unit_tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using Microsoft.VisualStudio.TestTools.UnitTesting; -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/base_test.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.Tests 8 | { 9 | public class OVBaseTest 10 | { 11 | public class TestModelInfo 12 | { 13 | public string model_xml = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s.xml"; 14 | public string model_bin = "..\\..\\..\\..\\..\\tests\\test_data\\model\\yolov8\\yolov8s.bin"; 15 | public string input_name = "images"; 16 | public string output_name = "output0"; 17 | 18 | public OvType input_type = new OvType(ElementType.F32); 19 | public Shape input_shape = new Shape(new long[] { 1, 3, 640, 640 }); 20 | 21 | public OvType output_type = new OvType(ElementType.F16); 22 | public Shape output_shape = new Shape(new long[] { 1, 84, 8400 }); 23 | } 24 | TestModelInfo model_info = new TestModelInfo(); 25 | 26 | private string device = "CPU"; 27 | public string get_model_xml_file_name() 28 | { 29 | if (!File.Exists(model_info.model_xml)) 30 | { 31 | Assert.Fail(); 32 | } 33 | return model_info.model_xml; 34 | } 35 | public string get_model_bin_file_name() 36 | { 37 | if (!File.Exists(model_info.model_bin)) 38 | { 39 | Assert.Fail(); 40 | } 41 | return model_info.model_bin; 42 | } 43 | public string get_device() 44 | { 45 | return device; 46 | } 47 | 48 | public string model_input_name() 49 | { 50 | return model_info.input_name; 51 | } 52 | public string model_output_name() 53 | { 54 | return model_info.output_name; 55 | } 56 | 57 | public Shape model_input_shape() 58 | { 59 | return model_info.input_shape; 60 | } 61 | 62 | public OvType model_input_type() 63 | { 64 | return model_info.input_type; 65 | } 66 | 67 | public Shape model_output_shape() 68 | { 69 | return model_info.output_shape; 70 | } 71 | 72 | public OvType model_output_type() 73 | { 74 | return model_info.output_type; 75 | } 76 | 77 | public byte[] content_from_file(string file) 78 | { 79 | FileStream fs = new FileStream(get_model_bin_file_name(), FileMode.Open, FileAccess.Read); 80 | 81 | long len = fs.Seek(0, SeekOrigin.End); 82 | 83 | 84 | fs.Seek(0, SeekOrigin.Begin); 85 | 86 | byte[] data = new byte[len + 1]; 87 | 88 | fs.Read(data, 0, (int)len); 89 | return data; 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/core/InputTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Tests 10 | { 11 | [TestClass()] 12 | public class InputTests : OVBaseTest 13 | { 14 | [TestMethod()] 15 | public void Input_test() 16 | { 17 | } 18 | 19 | [TestMethod()] 20 | public void Dispose_test() 21 | { 22 | 23 | } 24 | 25 | [TestMethod()] 26 | public void get_node_test() 27 | { 28 | var core = new Core(); 29 | Model model = core.read_model(get_model_xml_file_name()); 30 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 31 | Input input = model.input(); 32 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 33 | Node node = input.get_node(); 34 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 35 | input.Dispose(); 36 | model.Dispose(); 37 | core.Dispose(); 38 | } 39 | 40 | [TestMethod()] 41 | public void get_index_test() 42 | { 43 | var core = new Core(); 44 | Model model = core.read_model(get_model_xml_file_name()); 45 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 46 | Input input = model.input(); 47 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 48 | ulong index = input.get_index(); 49 | Assert.IsNotNull(index); 50 | input.Dispose(); 51 | model.Dispose(); 52 | core.Dispose(); 53 | } 54 | 55 | [TestMethod()] 56 | public void get_element_type_test() 57 | { 58 | var core = new Core(); 59 | Model model = core.read_model(get_model_xml_file_name()); 60 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 61 | Input input = model.input(); 62 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 63 | OvType type = input.get_element_type(); 64 | Assert.IsNotNull(type); 65 | input.Dispose(); 66 | model.Dispose(); 67 | core.Dispose(); 68 | } 69 | 70 | [TestMethod()] 71 | public void get_shape_test() 72 | { 73 | var core = new Core(); 74 | Model model = core.read_model(get_model_xml_file_name()); 75 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 76 | Input input = model.input(); 77 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 78 | Shape shape = input.get_shape(); 79 | Assert.IsNotNull(shape); 80 | input.Dispose(); 81 | model.Dispose(); 82 | core.Dispose(); 83 | } 84 | 85 | [TestMethod()] 86 | public void get_any_name_test() 87 | { 88 | var core = new Core(); 89 | Model model = core.read_model(get_model_xml_file_name()); 90 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 91 | Input input = model.input(); 92 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 93 | string name = input.get_any_name(); 94 | Assert.IsNotNull(name); 95 | input.Dispose(); 96 | model.Dispose(); 97 | core.Dispose(); 98 | } 99 | 100 | [TestMethod()] 101 | public void get_partial_shape_test() 102 | { 103 | var core = new Core(); 104 | Model model = core.read_model(get_model_xml_file_name()); 105 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 106 | Input input = model.input(); 107 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 108 | PartialShape shape = input.get_partial_shape(); 109 | Assert.IsNotNull(shape); 110 | input.Dispose(); 111 | model.Dispose(); 112 | core.Dispose(); 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/core/NodeTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Tests 10 | { 11 | [TestClass()] 12 | public class NodeTests : OVBaseTest 13 | { 14 | [TestMethod()] 15 | public void Node_test() 16 | { 17 | } 18 | 19 | [TestMethod()] 20 | public void Dispose_test() 21 | { 22 | } 23 | 24 | [TestMethod()] 25 | public void get_shape_test() 26 | { 27 | var core = new Core(); 28 | Model model = core.read_model(get_model_xml_file_name()); 29 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 30 | Node node = model.get_const_input(model_input_name()); 31 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 32 | Shape shape = node.get_shape(); 33 | Assert.IsNotNull(shape); 34 | node.Dispose(); 35 | model.Dispose(); 36 | core.Dispose(); 37 | } 38 | 39 | [TestMethod()] 40 | public void get_partial_shape_test() 41 | { 42 | var core = new Core(); 43 | Model model = core.read_model(get_model_xml_file_name()); 44 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 45 | Node node = model.get_const_input(model_input_name()); 46 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 47 | PartialShape shape = node.get_partial_shape(); 48 | Assert.IsNotNull(shape); 49 | node.Dispose(); 50 | model.Dispose(); 51 | core.Dispose(); 52 | } 53 | 54 | [TestMethod()] 55 | public void get_name_test() 56 | { 57 | var core = new Core(); 58 | Model model = core.read_model(get_model_xml_file_name()); 59 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 60 | Node node = model.get_const_input(model_input_name()); 61 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 62 | string name = node.get_name(); 63 | Assert.IsNotNull(name); 64 | node.Dispose(); 65 | model.Dispose(); 66 | core.Dispose(); 67 | } 68 | 69 | [TestMethod()] 70 | public void get_element_type_test() 71 | { 72 | var core = new Core(); 73 | Model model = core.read_model(get_model_xml_file_name()); 74 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 75 | Node node = model.get_const_input(model_input_name()); 76 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 77 | OvType type = node.get_element_type(); 78 | Assert.IsNotNull(type); 79 | node.Dispose(); 80 | model.Dispose(); 81 | core.Dispose(); 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/core/OutputTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Tests 10 | { 11 | [TestClass()] 12 | public class OutputTests : OVBaseTest 13 | { 14 | [TestMethod()] 15 | public void Output_test() 16 | { 17 | } 18 | 19 | [TestMethod()] 20 | public void Dispose_test() 21 | { 22 | } 23 | 24 | [TestMethod()] 25 | public void get_node_test() 26 | { 27 | var core = new Core(); 28 | Model model = core.read_model(get_model_xml_file_name()); 29 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 30 | Output input = model.output(); 31 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 32 | Node node = input.get_node(); 33 | Assert.IsTrue(node.Ptr != IntPtr.Zero); 34 | input.Dispose(); 35 | model.Dispose(); 36 | core.Dispose(); 37 | } 38 | 39 | [TestMethod()] 40 | public void get_index_test() 41 | { 42 | var core = new Core(); 43 | Model model = core.read_model(get_model_xml_file_name()); 44 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 45 | Output input = model.output(); 46 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 47 | ulong index = input.get_index(); 48 | Assert.IsNotNull(index); 49 | input.Dispose(); 50 | model.Dispose(); 51 | core.Dispose(); 52 | } 53 | 54 | [TestMethod()] 55 | public void get_element_type_test() 56 | { 57 | var core = new Core(); 58 | Model model = core.read_model(get_model_xml_file_name()); 59 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 60 | Output input = model.output(); 61 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 62 | OvType type = input.get_element_type(); 63 | Assert.IsNotNull(type); 64 | input.Dispose(); 65 | model.Dispose(); 66 | core.Dispose(); 67 | } 68 | 69 | [TestMethod()] 70 | public void get_shape_test() 71 | { 72 | var core = new Core(); 73 | Model model = core.read_model(get_model_xml_file_name()); 74 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 75 | Output input = model.output(); 76 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 77 | Shape shape = input.get_shape(); 78 | Assert.IsNotNull(shape); 79 | input.Dispose(); 80 | model.Dispose(); 81 | core.Dispose(); 82 | } 83 | 84 | [TestMethod()] 85 | public void get_any_name_test() 86 | { 87 | var core = new Core(); 88 | Model model = core.read_model(get_model_xml_file_name()); 89 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 90 | Output input = model.output(); 91 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 92 | string name = input.get_any_name(); 93 | Assert.IsNotNull(name); 94 | input.Dispose(); 95 | model.Dispose(); 96 | core.Dispose(); 97 | } 98 | 99 | [TestMethod()] 100 | public void get_partial_shape_test() 101 | { 102 | var core = new Core(); 103 | Model model = core.read_model(get_model_xml_file_name()); 104 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 105 | Output input = model.output(); 106 | Assert.IsTrue(input.get_node().Ptr != IntPtr.Zero); 107 | PartialShape shape = input.get_partial_shape(); 108 | Assert.IsNotNull(shape); 109 | input.Dispose(); 110 | model.Dispose(); 111 | core.Dispose(); 112 | } 113 | } 114 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/core/ShapeTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Tests 10 | { 11 | [TestClass()] 12 | public class ShapeTests 13 | { 14 | [TestMethod()] 15 | public void Shape_test() 16 | { 17 | } 18 | 19 | [TestMethod()] 20 | public void Shape_test1() 21 | { 22 | List data = new List() { 1, 2, 3 }; 23 | Shape shape = new Shape(data); 24 | shape.Dispose(); 25 | } 26 | 27 | [TestMethod()] 28 | public void Shape_test2() 29 | { 30 | long[] data = new long[] { 1, 2, 3 }; 31 | Shape shape = new Shape(data); 32 | shape.Dispose(); 33 | } 34 | 35 | [TestMethod()] 36 | public void Shape_test3() 37 | { 38 | Shape shape = new Shape(1,2,9); 39 | shape.Dispose(); 40 | } 41 | 42 | [TestMethod()] 43 | public void Dispose_test() 44 | { 45 | Shape shape = new Shape(1, 2, 9); 46 | shape.Dispose(); 47 | } 48 | 49 | [TestMethod()] 50 | public void to_string_test() 51 | { 52 | Shape shape = new Shape(1, 2, 9); 53 | string msg = shape.to_string(); 54 | Assert.IsNotNull(msg); 55 | shape.Dispose(); 56 | } 57 | 58 | [TestMethod()] 59 | public void data_size_test() 60 | { 61 | Shape shape = new Shape(1, 2, 9); 62 | long size = shape.data_size(); 63 | Assert.IsTrue(size == 18); 64 | shape.Dispose(); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/csharp_api_unit_tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/ov/OvTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace OpenVinoSharp.Tests 10 | { 11 | [TestClass()] 12 | public class OvTests : OVBaseTest 13 | { 14 | [TestMethod()] 15 | public void get_openvino_version_test() 16 | { 17 | Version version = Ov.get_openvino_version(); 18 | Assert.IsNotNull(version); 19 | } 20 | 21 | [TestMethod()] 22 | public void content_from_file_test() 23 | { 24 | byte[] data = Ov.content_from_file(get_model_bin_file_name()); 25 | Assert.IsTrue(data.Length>0); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/preprocess/InputInfoTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.preprocess; 3 | using OpenVinoSharp.Tests; 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.preprocess.Tests 11 | { 12 | [TestClass()] 13 | public class InputInfoTests : OVBaseTest 14 | { 15 | [TestMethod()] 16 | public void InputInfo_test() 17 | { 18 | } 19 | 20 | [TestMethod()] 21 | public void Dispose_test() 22 | { 23 | } 24 | 25 | [TestMethod()] 26 | public void tensor_test() 27 | { 28 | var core = new Core(); 29 | Model model = core.read_model(get_model_xml_file_name()); 30 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 31 | PrePostProcessor processor = new PrePostProcessor(model); 32 | Assert.IsTrue(processor.Ptr != IntPtr.Zero); 33 | InputInfo input = processor.input(); 34 | Assert.IsTrue(input.Ptr != IntPtr.Zero); 35 | InputTensorInfo input_tensor = input.tensor(); 36 | Assert.IsTrue(input_tensor.Ptr != IntPtr.Zero); 37 | input_tensor.Dispose(); 38 | input.Dispose(); 39 | processor.Dispose(); 40 | model.Dispose(); 41 | core.Dispose(); 42 | } 43 | 44 | [TestMethod()] 45 | public void preprocess_test() 46 | { 47 | var core = new Core(); 48 | Model model = core.read_model(get_model_xml_file_name()); 49 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 50 | PrePostProcessor processor = new PrePostProcessor(model); 51 | Assert.IsTrue(processor.Ptr != IntPtr.Zero); 52 | InputInfo input = processor.input(); 53 | Assert.IsTrue(input.Ptr != IntPtr.Zero); 54 | PreProcessSteps process_steps = input.preprocess(); 55 | Assert.IsTrue(process_steps.Ptr != IntPtr.Zero); 56 | process_steps.Dispose(); 57 | input.Dispose(); 58 | processor.Dispose(); 59 | model.Dispose(); 60 | core.Dispose(); 61 | } 62 | 63 | [TestMethod()] 64 | public void model_test() 65 | { 66 | var core = new Core(); 67 | Model model = core.read_model(get_model_xml_file_name()); 68 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 69 | PrePostProcessor processor = new PrePostProcessor(model); 70 | Assert.IsTrue(processor.Ptr != IntPtr.Zero); 71 | InputInfo input = processor.input(); 72 | Assert.IsTrue(input.Ptr != IntPtr.Zero); 73 | InputModelInfo model_info = input.model(); 74 | Assert.IsTrue(model_info.Ptr != IntPtr.Zero); 75 | model_info.Dispose(); 76 | input.Dispose(); 77 | processor.Dispose(); 78 | model.Dispose(); 79 | core.Dispose(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/preprocess/InputModelInfoTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.preprocess; 3 | using OpenVinoSharp.Tests; 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.preprocess.Tests 11 | { 12 | [TestClass()] 13 | public class InputModelInfoTests : OVBaseTest 14 | { 15 | [TestMethod()] 16 | public void InputModelInfo_test() 17 | { 18 | } 19 | 20 | [TestMethod()] 21 | public void Dispose_test() 22 | { 23 | } 24 | 25 | [TestMethod()] 26 | public void set_layout_test() 27 | { 28 | var core = new Core(); 29 | Model model = core.read_model(get_model_xml_file_name()); 30 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 31 | PrePostProcessor processor = new PrePostProcessor(model); 32 | Assert.IsTrue(processor.Ptr != IntPtr.Zero); 33 | InputInfo input = processor.input(); 34 | Assert.IsTrue(input.Ptr != IntPtr.Zero); 35 | InputModelInfo model_info = input.model(); 36 | Assert.IsTrue(model_info.Ptr != IntPtr.Zero); 37 | model_info.set_layout(new Layout("NCHW")); 38 | model_info.Dispose(); 39 | input.Dispose(); 40 | processor.Dispose(); 41 | model.Dispose(); 42 | core.Dispose(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/preprocess/OutputInfoTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.preprocess; 3 | using OpenVinoSharp.Tests; 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.preprocess.Tests 11 | { 12 | [TestClass()] 13 | public class OutputInfoTests : OVBaseTest 14 | { 15 | [TestMethod()] 16 | public void OutputInfo_test() 17 | { 18 | } 19 | 20 | [TestMethod()] 21 | public void Dispose_test() 22 | { 23 | } 24 | 25 | [TestMethod()] 26 | public void tensor_test() 27 | { 28 | var core = new Core(); 29 | Model model = core.read_model(get_model_xml_file_name()); 30 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 31 | PrePostProcessor processor = new PrePostProcessor(model); 32 | Assert.IsTrue(processor.Ptr != IntPtr.Zero); 33 | OutputInfo output = processor.output(); 34 | Assert.IsTrue(output.Ptr != IntPtr.Zero); 35 | OutputTensorInfo tensor_info = output.tensor(); 36 | Assert.IsTrue(tensor_info.Ptr != IntPtr.Zero); 37 | tensor_info.Dispose(); 38 | output.Dispose(); 39 | processor.Dispose(); 40 | model.Dispose(); 41 | core.Dispose(); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/preprocess/OutputTensorInfoTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.preprocess; 3 | using OpenVinoSharp.Tests; 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.preprocess.Tests 11 | { 12 | [TestClass()] 13 | public class OutputTensorInfoTests : OVBaseTest 14 | { 15 | [TestMethod()] 16 | public void OutputTensorInfo_test() 17 | { 18 | } 19 | 20 | [TestMethod()] 21 | public void Dispose_test() 22 | { 23 | } 24 | 25 | [TestMethod()] 26 | public void set_element_type_test() 27 | { 28 | var core = new Core(); 29 | Model model = core.read_model(get_model_xml_file_name()); 30 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 31 | PrePostProcessor processor = new PrePostProcessor(model); 32 | OutputInfo output = processor.output(); 33 | Assert.IsNotNull(output); 34 | OutputTensorInfo output_tensor = output.tensor(); 35 | Assert.IsNotNull(output_tensor); 36 | output_tensor.set_element_type(new OvType(ElementType.F32)); 37 | output.Dispose(); 38 | processor.Dispose(); 39 | model.Dispose(); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /tests/csharp_api_unit_tests/preprocess/PrePostProcessorTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using OpenVinoSharp.preprocess; 3 | using OpenVinoSharp.Tests; 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.preprocess.Tests 11 | { 12 | [TestClass()] 13 | public class PrePostProcessorTests : OVBaseTest 14 | { 15 | [TestMethod()] 16 | public void PrePostProcessor_test() 17 | { 18 | var core = new Core(); 19 | Model model = core.read_model(get_model_xml_file_name()); 20 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 21 | PrePostProcessor processor = new PrePostProcessor(model); 22 | processor.Dispose(); 23 | model.Dispose(); 24 | } 25 | 26 | [TestMethod()] 27 | public void Dispose_test() 28 | { 29 | } 30 | 31 | [TestMethod()] 32 | public void input_test() 33 | { 34 | var core = new Core(); 35 | Model model = core.read_model(get_model_xml_file_name()); 36 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 37 | PrePostProcessor processor = new PrePostProcessor(model); 38 | InputInfo input = processor.input(); 39 | input.Dispose(); 40 | processor.Dispose(); 41 | model.Dispose(); 42 | } 43 | 44 | [TestMethod()] 45 | public void input_test1() 46 | { 47 | var core = new Core(); 48 | Model model = core.read_model(get_model_xml_file_name()); 49 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 50 | PrePostProcessor processor = new PrePostProcessor(model); 51 | InputInfo input = processor.input(model_input_name()); 52 | input.Dispose(); 53 | processor.Dispose(); 54 | model.Dispose(); 55 | } 56 | 57 | [TestMethod()] 58 | public void input_test2() 59 | { 60 | var core = new Core(); 61 | Model model = core.read_model(get_model_xml_file_name()); 62 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 63 | PrePostProcessor processor = new PrePostProcessor(model); 64 | InputInfo input = processor.input(0); 65 | input.Dispose(); 66 | processor.Dispose(); 67 | model.Dispose(); 68 | } 69 | 70 | [TestMethod()] 71 | public void output_test() 72 | { 73 | var core = new Core(); 74 | Model model = core.read_model(get_model_xml_file_name()); 75 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 76 | PrePostProcessor processor = new PrePostProcessor(model); 77 | OutputInfo input = processor.output(); 78 | input.Dispose(); 79 | processor.Dispose(); 80 | model.Dispose(); 81 | } 82 | 83 | [TestMethod()] 84 | public void output_test1() 85 | { 86 | var core = new Core(); 87 | Model model = core.read_model(get_model_xml_file_name()); 88 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 89 | PrePostProcessor processor = new PrePostProcessor(model); 90 | OutputInfo input = processor.output(model_output_name()); 91 | input.Dispose(); 92 | processor.Dispose(); 93 | model.Dispose(); 94 | } 95 | 96 | [TestMethod()] 97 | public void output_test2() 98 | { 99 | var core = new Core(); 100 | Model model = core.read_model(get_model_xml_file_name()); 101 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 102 | PrePostProcessor processor = new PrePostProcessor(model); 103 | OutputInfo input = processor.output(0); 104 | input.Dispose(); 105 | processor.Dispose(); 106 | model.Dispose(); 107 | } 108 | 109 | [TestMethod()] 110 | public void build_test() 111 | { 112 | var core = new Core(); 113 | Model model = core.read_model(get_model_xml_file_name()); 114 | Assert.IsTrue(model.Ptr != IntPtr.Zero); 115 | PrePostProcessor processor = new PrePostProcessor(model); 116 | 117 | Tensor input_tensor = new Tensor(new OvType(ElementType.U8), new Shape(1, 640, 640, 3)); 118 | InputInfo input_info = processor.input(0); 119 | InputTensorInfo input_tensor_info = input_info.tensor(); 120 | input_tensor_info.set_from(input_tensor).set_layout(new Layout("NHWC")).set_color_format(ColorFormat.BGR); 121 | 122 | PreProcessSteps process_steps = input_info.preprocess(); 123 | process_steps.convert_color(ColorFormat.RGB).resize(ResizeAlgorithm.RESIZE_LINEAR) 124 | .convert_element_type(new OvType(ElementType.F32)).scale(255.0f).convert_layout(new Layout("NCHW")); 125 | 126 | Model new_model = processor.build(); 127 | new_model.Dispose(); 128 | processor.Dispose(); 129 | model.Dispose(); 130 | } 131 | } 132 | } --------------------------------------------------------------------------------