C++ (Windows)

On this page, you will learn to run the C++ samples in Mech-DLK SDK with CMake and Visual Studio on a Windows operating system.

Sample List

Two categories of samples are provided: Basic and Advanced.

  • Basic: samples using single models exported from Mech-DLK to do inference of single images and simultaneous inference of multiple images as well as obtain and visualize results.

  • Advanced: a sample demonstrating collaborative development of Mech-DLK SDK with OpenCV.

Basic

Advanced

Prerequisites

The prerequisites for the use of C++ samples in Mech-DLK SDK are as follows:

Install Required Software

In order to use the C++ samples of Mech-DLK SDK, Mech-DLK SDK, CMake, and Visual Studio must be installed.

Install Mech-DLK SDK

Please obtain the latest Mech-DLK SDK and the third-party libraries and resources it depends upon according to the Installation Guide.

Install CMake (Version 3.2 or Above)

  1. Download CMake: Select the installer to the right of Windows x64 Installer.

    cmake download
  2. During installation, select the following two options so that CMake can be added to environment variables, and a desktop icon can be created for CMake.

cmake 1

Install Visual Studio (Version 2017 or Above)

  1. Download the Visual Studio installer.

  2. During installation, please select Desktop development with C++ in the Desktop & Mobile category. Then, click Install in the lower-right corner.

sample c workload

Install OpenCV (Optional)

If you need to run the Advanced sample (ImageInferWithOpenCV), please install OpenCV by the following steps and add it to the system variables.

  1. Download and install OpenCV. Please note down the installation directory of OpenCV.

  2. After installation, you can find the following files in the directory of OpenCV:

    opencv folder files
The sample ImageInferWithOpenCV was tested with OpenCV 4.8.0, and results can be obtained.

Add Environment Variables

You can add the related environment variables by the following steps.

  1. Right-click This PC on the desktop and select Properties.

  2. Click Advanced system settings and on the Advanced tab of the pop-up System Properties dialog box, click Environment Variables to open the Environment Variables dialog box.

  3. In the System Properties box, click New and in the pop-up box of New System Variable, enter MECHDL_DIR in the text field of Variable name and xxx/mechdlk_sdk in the text field of Variable value. Then, click OK.

    If you want to run the sample ImageInferWithOpenCV, please add OpenCV to the environment variables:

    1. In the System Properties box, click New.

    2. In the pop-up box of New System Variable, enter OPENCV_DIR in the text field of Variable name and the path to the build folder of OpenCV (xxx/opencv/build) in the text field of Variable value. Then, click OK.

  4. In the System Properties box, scroll to Path and double-click it to show the Edit System Variable dialog box.

  5. Click New in the upper-right corner and add %MECHDL_DIR%. Then, click OK in the lower-right corner.

If you can find %MMIND_DLK% in the Edit System Variable dialog box, select it and click Delete on the right to remove this variable.
csharp add environment variables 1
csharp add environment variables 2

Build and Run Samples

You can build all samples at a time.

Configure Samples in CMake

  1. Double-click the CMake desktop icon to open the software.

  2. Enter or select the path to the source code and the path to build the binaries. For the path to build the binaries, you can add \build right after the path to the source code if there isn’t one and continue the configuration.

    Where is the source code

    xxx\mechdlk_sdk\samples\cpp

    Where to build the binaries

    xxx\mechdlk_sdk\samples\cpp\build

  3. Click Configure. When you first click the Configure button, a window will pop up to confirm whether you want to create the build folder. Click Yes to create the folder.

    create build cpp

    Then, you will enter the configuration page. Select the Visual Studio version and click Finish.

    configuration

    When the configuration completes, the statement Configuring done will appear in the last line of the log.

    cpp windows configuring done
    Do not select the USE_OPENCV option if you want to run Basic samples. Select the USE_OPENCV option if you want to run the Advanced sample.
  1. Click Generate to generate Visual Studio solutions. When the generation completes, the statement Generating done will appear in the last line of the log. Then, click Open Project to open the solution in Visual Studio.

    cpp windows generating done

Build Samples in Visual Studio

  1. On the Visual Studio toolbar, confirm that the solution configuration is Release. Currently, DLL files for Debug are unavailable.

  2. In the menu bar, select Build  Build Solution. An executable file is generated for each sample. The executable files are saved to the Release folder (path: xxx\mechdlk_sdk\samples\cpp\build).

    generate solution
  3. Copy and paste the resources folder under the project folder to the path xxx\mechdlk_sdk\samples\cpp\build\Release.

  4. Copy and paste all files in the 3rd_dll folder under the project folder to the path xxx\mechdlk_sdk\samples\cpp\build\Release.

  5. Copy and paste all files in the mechdlk_sdk\dll folder under the project folder to the path xxx\mechdlk_sdk\samples\cpp\build\Release.

  6. (Optional) If you need to run the Advanced sample, please copy and paste the opencv_world480.dll file in the OpenCV directory (xxx\opencv\build\x64\vc16\bin) to the path xxx\mechdlk_sdk\samples\cpp\build\Release.

Run Samples

You can run the sample in Visual Studio after building it, or you can run the sample by double-clicking the executable file.

Run a Sample in Visual Studio

  1. In the Solution Explorer panel, right-click a sample and select Set as Startup Project.

    cpp startup project
  2. Click Local Windows Debugger on the toolbar to run the sample.

Run the Executable File of the Sample

Open the Release folder and double-click the executable file named after the sample to run the sample and obtain results.

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.