English | [中文](../../cn/faq/use_sdk_on_windows.md) # Using the FastDeploy C++ SDK on Windows Platform ## Contents - [Using the FastDeploy C++ SDK on Windows Platform](#using-the-fastdeploy-c-sdk-on-windows-platform) - [Contents](#contents) - [1. Environment Dependent](#1-environment-dependent) - [2. Download FastDeploy Windows 10 C++ SDK](#2-download-fastdeploy-windows-10-c-sdk) - [2.1 Download the Pre-built Library or Build the Latest SDK from Source](#21-download-the-pre-built-library-or-build-the-latest-sdk-from-source) - [2.2 Prepare Model Files and Test Images](#22-prepare-model-files-and-test-images) - [3. Various ways to use C++ SDK on Windows Platform](#3-various-ways-to-use-c-sdk-on-windows-platform) - [3.1 SDK usage method 1:Using the C++ SDK from the Command Line](#31-sdk-usage-method-1using-the-c-sdk-from-the-command-line) - [3.1.1 Build PPYOLOE on Windows Platform](#311-build-ppyoloe-on-windows-platform) - [3.1.2 Run Demo](#312-run-demo) - [3.2 SDK usage method 2: Visual Studio 2019 creates sln project using C++ SDK](#32-sdk-usage-method-2-visual-studio-2019-creates-sln-project-using-c-sdk) - [3.2.1 Step 1:Visual Studio 2019 creates sln project project](#321-step-1visual-studio-2019-creates-sln-project-project) - [3.2.2 Step 2:Copy the code of infer\_ppyoloe.cc from examples to the project](#322-step-2copy-the-code-of-infer_ppyoloecc-from-examples-to-the-project) - [3.2.3 Step 3:Set the project configuration to "Release x64" configuration](#323-step-3set-the-project-configuration-to-release-x64-configuration) - [3.2.4 Step 4:Configure Include Header File Path](#324-step-4configure-include-header-file-path) - [3.2.5 Step 5:Configure Lib Path and Add Library Files](#325-step-5configure-lib-path-and-add-library-files) - [3.2.6 Step 6:Build the Project and Run to Get the Result](#326-step-6build-the-project-and-run-to-get-the-result) - [3.3 Visual Studio 2019 Create CMake project using C++ SDK](#33-visual-studio-2019-create-cmake-project-using-c-sdk) - [3.3.1 Step 1: Visual Studio 2019 Creates a CMake Project](#331-step-1-visual-studio-2019-creates-a-cmake-project) - [3.3.2 Step 2:Configure FastDeploy C++ SDK in CMakeLists](#332-step-2configure-fastdeploy-c-sdk-in-cmakelists) - [3.3.3 Step 3:Generate project cache and Modify CMakeSetting.json Configuration](#333-step-3generate-project-cache-and-modify-cmakesettingjson-configuration) - [3.3.4 Step 4:Generate executable file, Run to Get the Result](#334-step-4generate-executable-file-run-to-get-the-result) - [4. Multiple methods to Configure the Required Dependencies for the Exe Runtime](#4-multiple-methods-to-configure-the-required-dependencies-for-the-exe-runtime) - [4.1 Use method 1:Use Fastdeploy\_init.bat for Configuration (Recommended)](#41--use-method-1use-fastdeploy_initbat-for-configuration-recommended) - [4.1.1 fastdeploy\_init.bat User's Manual](#411-fastdeploy_initbat-users-manual) - [4.1.2 fastdeploy\_init.bat View all dll, lib and include paths in the SDK](#412-fastdeploy_initbat-view-all-dll-lib-and-include-paths-in-the-sdk) - [4.1.3 fastdeploy\_init.bat Installs all the dlls in the SDK to the specified directory](#413-fastdeploy_initbat-installs-all-the-dlls-in-the-sdk-to-the-specified-directory) - [4.1.4 fastdeploy\_init.bat Configures SDK Environment Variables](#414-fastdeploy_initbat-configures-sdk-environment-variables) - [4.2 Use method 2:Modify CMakeLists.txt, One Line of Command Configuration (Recommended)](#42--use-method-2modify-cmakeliststxt-one-line-of-command-configuration-recommended) - [4.3 Use method 3:Command Line Setting Environment Variables](#43--use-method-3command-line-setting-environment-variables) - [4.4 Use method 4:Manually Copy the Dependency Library to the Exe Directory](#44-use-method-4manually-copy-the-dependency-library-to-the-exe-directory) ## 1. Environment Dependent
- cmake >= 3.12 - Visual Studio 2019 - cuda >= 11.2 (WITH_GPU=ON) - cudnn >= 8.0 (WITH_GPU=ON) ## 2. Download FastDeploy Windows 10 C++ SDK ### 2.1 Download the Pre-built Library or Build the Latest SDK from Source The compiled FastDeploy Windows 10 C++ SDK can be downloaded from the link below, and the examples code is included in the SDK. ```text https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-win-x64-gpu-0.2.1.zip ``` Please refer to source code compilation: [build_and_install](../build_and_install) ### 2.2 Prepare Model Files and Test Images Model files and test images can be downloaded from the link below and unzipped ```text https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz # (please unzip it after downloading) https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg ``` ## 3. Various ways to use C++ SDK on Windows Platform ### 3.1 SDK usage method 1:Using the C++ SDK from the Command Line #### 3.1.1 Build PPYOLOE on Windows Platform Open `x64 Native Tools Command Prompt for VS 2019` command tool on Winodws, cd to the demo path of ppyoloe: ```bat cd fastdeploy-win-x64-gpu-0.2.1\examples\vision\detection\paddledetection\cpp ``` ```bat mkdir build && cd build cmake .. -G "Visual Studio 16 2019" -A x64 -DFASTDEPLOY_INSTALL_DIR=%cd%\..\..\..\..\..\..\..\fastdeploy-win-x64-gpu-0.2.1 -DCUDA_DIRECTORY="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.2" ``` Then Run ```bat msbuild infer_demo.sln /m:4 /p:Configuration=Release /p:Platform=x64 ``` #### 3.1.2 Run Demo ```bat cd Release infer_ppyoloe_demo.exe ppyoloe_crn_l_300e_coco 000000014439.jpg 0 # CPU infer_ppyoloe_demo.exe ppyoloe_crn_l_300e_coco 000000014439.jpg 1 # GPU infer_ppyoloe_demo.exe ppyoloe_crn_l_300e_coco 000000014439.jpg 2 # GPU + TensorRT ``` In particular, for the configuration method of the dependency library required by the exe runtime, please refer to the chapter: [Multiple methods to configure the dependency library required by the exe runtime](#CommandLineDeps) ### 3.2 SDK usage method 2: Visual Studio 2019 creates sln project using C++ SDK This section is for non-CMake users and describes how to create a sln project in Visual Studio 2019 to use FastDeploy C++ SDK. CMake users please read the next section directly. In addition, this section is a special thanks to "Awake to the Southern Sky" for his tutorial on FastDeploy: [How to deploy PaddleDetection target detection model on Windows using FastDeploy C++](https://www.bilibili.com/read/cv18807232). #### 3.2.1 Step 1:Visual Studio 2019 creates sln project project (1) Open Visual Studio 2019 and click on "Create New Project" -> click on "Console Program" to create a new sln project.   (2)Click "Create" and an empty sln project is created. We copy the code of infer_ppyoloe directly from examples here.  #### 3.2.2 Step 2:Copy the code of infer_ppyoloe.cc from examples to the project (1)Copy the code of infer_ppyoloe.cc from examples to the project and replace it directly, the path to copy the code is: ```bat fastdeploy-win-x64-gpu-0.2.1\examples\vision\detection\paddledetection\cpp ```  #### 3.2.3 Step 3:Set the project configuration to "Release x64" configuration  #### 3.2.4 Step 4:Configure Include Header File Path (1)Configure the header file include path: select the project with the mouse, and then right-click to pop down the menu, in which click "Properties"。  (2)In the pop-up property page, select: C/C++ -> General -> Additional Include Directories, and then add the paths to the fastdeploy and opencv headers. As: ```bat D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\include D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\third_libs\install\opencv-win-x64-3.4.16\build\include ``` Note that if you compile the latest SDK or version >0.2.1, the directory structure of the dependency library has changed, and the opencv path needs to be modified appropriately. For example: ```bat D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\third_libs\install\opencv\build\include ```  Developers need to make slight modifications according to their actual sdk path. #### 3.2.5 Step 5:Configure Lib Path and Add Library Files (1)In the property page, select: Linker -> General -> Additional Libraries Directory, then add the lib paths for fastdeploy and opencv. As: ```bat D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\lib D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\third_libs\install\opencv-win-x64-3.4.16\build\x64\vc15\lib ``` Note that if you compile the latest SDK or version >0.2.1, the directory structure of the dependency library has changed, and the opencv path needs to be modified appropriately. For example: ```bat D:\qiuyanjun\fastdeploy_build\built\fastdeploy-win-x64-gpu-0.2.1\third_libs\install\opencv\build\include ```  (2)Adding library files: only fastdeploy.lib and opencv_world3416.lib are needed  #### 3.2.6 Step 6:Build the Project and Run to Get the Result (1)Click on the menu bar "Generate" -> "Generate Solution"   Compile successfully, you can see the exe saved in: ```bat D:\qiuyanjun\fastdeploy_test\infer_ppyoloe\x64\Release\infer_ppyoloe.exe ``` (2)Execute the executable file and get the inference result. First you need to copy all the dlls to the directory where the exe is located. At the same time, you also need to download and extract the pyoloe model files and test images, and then copy them to the directory where the exe is located. Special note, the exe needs to run when the dependency library configuration method, please refer to the section: [various methods to configure the exe to run the required dependency library](#CommandLineDeps).  ### 3.3 Visual Studio 2019 Create CMake project using C++ SDK This section is for CMake users and describes how to create CMake projects in Visual Studio 2019 using the FastDeploy C++ SDK. #### 3.3.1 Step 1: Visual Studio 2019 Creates a CMake Project (1)Open Visual Studio 2019, click "Create New Project" -> click "CMake" to create a CMake project. Take PPYOLOE as an example to illustrate how to use FastDeploy C++ SDK in Visual Studio 2019 IDE.    (2)Open the project and find that Visual Stuio 2019 has generated some basic files for us, including CMakeLists.txt. infer_ppyoloe.h header file is not actually used here, we can just delete it.  #### 3.3.2 Step 2:Configure FastDeploy C++ SDK in CMakeLists (1)After the project is created, we need to add the infer_ppyoloe inference source code and modify CMakeLists.txt as follows:  (2)The code of infer_ppyoloe.cpp can be copied directly from the code in examples: - [examples/vision/detection/paddledetection/cpp/infer_ppyoloe.cc](../../../examples/vision/detection/paddledetection/cpp/infer_ppyoloe.cc) (3)CMakeLists.txt mainly includes the configuration of the path of FastDeploy C++ SDK, if it is the GPU version of the SDK, you also need to configure CUDA_DIRECTORY as the installation path of CUDA, the configuration of CMakeLists.txt is as follows: ```cmake project(infer_ppyoloe_demo C CXX) cmake_minimum_required(VERSION 3.12) # Only support "Release" mode now set(CMAKE_BUILD_TYPE "Release") # Set FastDeploy install dir set(FASTDEPLOY_INSTALL_DIR "D:/qiuyanjun/fastdeploy-win-x64-gpu-0.2.1" CACHE PATH "Path to downloaded or built fastdeploy sdk.") # Set CUDA_DIRECTORY (CUDA 11.x) for GPU SDK set(CUDA_DIRECTORY "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7" CACHE PATH "Path to installed CUDA Toolkit.") include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) include_directories(${FASTDEPLOY_INCS}) add_executable(infer_ppyoloe_demo ${PROJECT_SOURCE_DIR}/infer_ppyoloe.cpp) target_link_libraries(infer_ppyoloe_demo ${FASTDEPLOY_LIBS}) # Optional: install all DLLs to binary dir. install_fastdeploy_libraries(${CMAKE_CURRENT_BINARY_DIR}/Release) ``` Note that the `install_fastdeploy_libraries` function is only valid with the latest code compiled for the SDK or version >0.2.1. #### 3.3.3 Step 3:Generate project cache and Modify CMakeSetting.json Configuration (1)Click on "CMakeLists.txt" -> right click on "Generate Cache":  We found that the cache has been successfully generated, but since the default is Debug mode when opening the project, we found that the exe and cache save path is still in Debug mode. We can first modify the CMake settings to Release. (2)Click "CMakeLists.txt"->right-click "cmake settings for infer_ppyoloe_demo", enter the CMakeSettings.json settings panel, change the Debug setting to Release.  Also set CMake Builder to "Visual Studio 16 2019 Win64".  (3)Click Save CMake Cache to switch to Release configuration:  (4):(4.1)Click "CMakeLists.txt"->right click "CMake Cache for x64-Release only"->"Click to delete cache"; (4.2) Click "CMakeLists.txt"->"Generate cache"; (4.3) If you find the option to delete cache is grayed out in step 1, you can directly click "CMakeLists. txt"->"Generate", if it fails, you can try again (4.1) and (4.2)  Finally, you can see that the configuration has successfully generated the CMake cache in Relase mode.  #### 3.3.4 Step 4:Generate executable file, Run to Get the Result (1)Click "CMakeLists.txt"->"Generate". You can find that infer_ppyoloe_demo.exe has been successfully generated and saved in the `out/build/x64-Release/Release` directory.  (2)Execute the executable file and get the inference result. First you need to copy all the dlls to the directory where the exe is located, here we can add a command in CMakeLists.txt to install all the dlls in FastDeploy to the specified directory. Note that this method is only valid for the latest code compiled SDK or version >0.2.1. For other configuration methods, please refer to the section: [Multiple Methods to Configure Dependencies for exe Runtime].(#CommandLineDeps) ```cmake install_fastdeploy_libraries(${CMAKE_CURRENT_BINARY_DIR}/Release) ``` (3)At the same time, you also need to download and unzip the pyoloe model files and test images and copy them to the directory where the exe is located. After preparation, the directory structure is as follows:  (4)Finally, the following command is executed to obtain the inference results: ```bat D:\xxxinfer_ppyoloe\out\build\x64-Release\Release>infer_ppyoloe_demo.exe ppyoloe_crn_l_300e_coco 000000014439.jpg 0 [INFO] fastdeploy/runtime.cc(304)::fastdeploy::Runtime::Init Runtime initialized with Backend::OPENVINO in Device::CPU. DetectionResult: [xmin, ymin, xmax, ymax, score, label_id] 415.047180,89.311569, 506.009613, 283.863098, 0.950423, 0 163.665710,81.914932, 198.585342, 166.760895, 0.896433, 0 581.788635,113.027618, 612.623474, 198.521713, 0.842596, 0 267.217224,89.777306, 298.796051, 169.361526, 0.837951, 0 ...... 153.301407,123.233757, 177.130539, 164.558350, 0.066697, 60 505.887604,140.919601, 523.167236, 151.875336, 0.084912, 67 Visualized result saved in ./vis_result.jpg ``` Open the saved image to view the visualization results at: