Overview
The Mech-DLK SDK (C++) is designed for the development of deep learning inference.
Quick Start
Environment Preparation
- C++ version requirement: C++17 or higher
- Compiler requirement: MSVC 2017 or later
Configure CMake
cmake_minimum_required(VERSION 3.10)
project(examples)
# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Check if OpenCV dependency path exists
if(EXISTS "D:/projects/dependencies")
set(USE_OPENCV_DEFAULT ON)
set(OpenCV_INCLUDE_DIR_DEFAULT "D:/projects/dependencies/include")
set(OpenCV_LIB_DIR_DEFAULT "D:/projects/dependencies/lib/release")
else()
set(USE_OPENCV_DEFAULT OFF)
set(OpenCV_INCLUDE_DIR_DEFAULT "")
set(OpenCV_LIB_DIR_DEFAULT "")
endif()
# Option to enable/disable OpenCV example
option(USE_OPENCV "Build infer_with_opencv example" ${USE_OPENCV_DEFAULT})
# OpenCV configuration (only used when USE_OPENCV is ON)
set(OpenCV_INCLUDE_DIR "${OpenCV_INCLUDE_DIR_DEFAULT}" CACHE PATH "OpenCV include directory")
set(OpenCV_LIB_DIR "${OpenCV_LIB_DIR_DEFAULT}" CACHE PATH "OpenCV library directory")
# Check if Halcon dependency path exists and set USE_HALCON accordingly
if(EXISTS "D:/projects/dependencies/halcon")
set(USE_HALCON_DEFAULT ON)
set(HALCON_INCLUDE_DIR_DEFAULT "D:/projects/dependencies/halcon/include;D:/projects/dependencies/halcon/include/halconcpp")
set(HALCON_LIB_DIR_DEFAULT "D:/projects/dependencies/halcon/lib/x64-win64")
else()
set(USE_HALCON_DEFAULT OFF)
set(HALCON_INCLUDE_DIR_DEFAULT "")
set(HALCON_LIB_DIR_DEFAULT "")
endif()
# Option to enable/disable Halcon example
option(USE_HALCON "Build infer_with_halcon example" ${USE_HALCON_DEFAULT})
# Halcon configuration (only used when USE_HALCON is ON)
set(HALCON_INCLUDE_DIR "${HALCON_INCLUDE_DIR_DEFAULT}" CACHE PATH "Halcon include directory")
set(HALCON_LIB_DIR "${HALCON_LIB_DIR_DEFAULT}" CACHE PATH "Halcon library directory")
# Get the project root directory
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. ABSOLUTE)
# Add cmake modules path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Include directories
include_directories(${PROJECT_ROOT}/src/cpp/include)
# Library directories
link_directories(${PROJECT_ROOT}/src/cpp/lib)
# Define the list of examples to build
set(EXAMPLE_LIST "basic" "advanced/multi_thread_infer")
# Add OpenCV example if enabled
if(USE_OPENCV)
list(APPEND EXAMPLE_LIST "advanced/infer_with_opencv")
endif()
# Add Halcon example if enabled
if(USE_HALCON)
list(APPEND EXAMPLE_LIST "advanced/infer_with_halcon")
endif()
Write Your Own C++ Application
#include <vector>
#include <filesystem>
#include <windows.h>
namespace fs = std::filesystem;
fs::path getResourcePath()
{
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);
fs::path exeDir = fs::path(exePath).parent_path();
fs::path resourcesPath = exeDir / "resources";
if (fs::exists(resourcesPath))
return resourcesPath;
return exeDir.parent_path() / "resources";
}
int main()
{
fs::path imagePath = getResourcePath() / "DefectSegmentation" / "defect_segmentation_image.jpg";
std::vector<MMindImage> images;
images.push_back(std::move(image));
fs::path modelPath = getResourcePath() / "DefectSegmentation" / "defect_segmentation_model.dlkpack";
engine.
create(modelPath.wstring());
images[0].show("Result");
return 0;
}
Defines the inference engine for Mech-DLK model packages.
StatusCode create(const std::wstring &dlkpackPath)
Creates an inference engine for the specified model package.
StatusCode load()
Loads the model into memory and prepares it for inference.
StatusCode infer(const std::vector< MMindImage > &images)
Performs inference on the input images.
StatusCode resultVisualization(std::vector< MMindImage > &images)
Draws all module results onto the input images.
Represents an image structure for inference and visualization.
StatusCode createFromPath(const std::string &imagePath)
Creates an image by loading from a file path.
Compile & Run
# Clone or download the project
cd dlk-sdk
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build a specific C++ example (or omit --target to build all)
cmake --build . --config Release --target example_basic
# Run the example (binary is placed under the build directory root)
./example_basic
Build Options
The project supports multiple build options:
# Build only C++ examples (default)
cmake .. -DBUILD_CPP_EXAMPLES=ON -DBUILD_CSHARP_WRAPPER=OFF -DBUILD_PYTHON_WRAPPER=OFF
# Build C# wrapper (Windows only)
cmake .. -DBUILD_CSHARP_WRAPPER=ON
# Build Python wrapper (Windows only)
cmake .. -DBUILD_PYTHON_WRAPPER=ON
# Build all components
cmake .. -DBUILD_CPP_EXAMPLES=ON -DBUILD_CSHARP_WRAPPER=ON -DBUILD_PYTHON_WRAPPER=ON
SDK Dependencies
The project depends on MechMind DL SDK v2:
- Include files:
cpp/include/
- Library files:
cpp/lib/
- Required libraries:
cpp/bin
Available Examples
- example_basic: Single-threaded inference with defect segmentation
- example_advanced_multi_thread_infer: Multi-threaded inference (4 threads)
- example_advanced_infer_with_opencv: Inference with OpenCV image I/O integration (built when
USE_OPENCV=ON)
- example_advanced_infer_with_halcon: Inference with MVTec Halcon image I/O integration (built when
USE_HALCON=ON)