Basic inference example.
Basic inference example.
This example demonstrates the basic usage of the Mech-DLK SDK for performing inference with a pre-trained deep learning model. It shows how to:
The example takes no command-line arguments. It reads images and the model package from resources/DefectSegmentation/ (located next to the executable or in its parent directory).
#include <vector>
#include <string>
#include <iostream>
#include <filesystem>
#include <algorithm>
#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))
{
std::cout << "Resources path to use: " << resourcesPath << std::endl;
return resourcesPath;
}
resourcesPath = exeDir.parent_path() / "resources";
std::cout << "Resources path to use: " << resourcesPath << std::endl;
return resourcesPath;
}
fs::path getPackPath()
{
return getResourcePath() / "DefectSegmentation" / "defect_segmentation_model.dlkpack";
}
std::vector<fs::path> getImagePaths()
{
fs::path imageDir = getResourcePath() / "DefectSegmentation";
std::vector<fs::path> imagePaths;
std::vector<std::string> imageExtensions = {".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif"};
if (fs::exists(imageDir) && fs::is_directory(imageDir))
{
for (const auto &entry : fs::directory_iterator(imageDir))
{
if (entry.is_regular_file())
{
std::string ext = entry.path().extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (std::find(imageExtensions.begin(), imageExtensions.end(), ext) != imageExtensions.end())
{
imagePaths.push_back(entry.path());
}
}
}
std::sort(imagePaths.begin(), imagePaths.end());
}
return imagePaths;
}
int main()
{
std::vector<fs::path> imagePaths = getImagePaths();
if (imagePaths.empty())
{
std::cerr << "No images found in DefectSegmentation directory" << std::endl;
return -1;
}
std::cout << "Found " << imagePaths.size() << " image(s)" << std::endl;
std::vector<MMindImage> images;
for (const auto &imagePath : imagePaths)
{
{
std::cerr << "Failed to load image: " << imagePath.filename() << std::endl;
continue;
}
images.push_back(std::move(image));
std::cout << "Loaded: " << imagePath.filename() << std::endl;
}
if (images.empty())
{
std::cerr << "No images loaded successfully" << std::endl;
return -1;
}
engine.
create(getPackPath().wstring());
std::cout << "Available module names:" << std::endl;
for (const auto& name : moduleNames) {
std::cout << " " << name << std::endl;
}
{
return -1;
}
std::vector<MMindResult> results;
{
return -1;
}
std::cout << "\nInference Results:" << std::endl;
for (size_t i = 0; i < results.size(); ++i)
{
std::cout << "Result " << i << ": " << results[i].contours.size() << " contours, "
<< results[i].bboxes.size() << " bboxes" << std::endl;
for (size_t j = 0; j < results[i].contours.size(); ++j)
{
const auto &contour = results[i].contours[j];
std::cout << " Contour " << j << " blob value:" << std::endl;
std::cout << " Area: " << blobValue.area << std::endl;
std::cout << " Circularity: " << blobValue.circularity << std::endl;
std::cout << " Center: (" << blobValue.centerX << ", " << blobValue.centerY << ")" << std::endl;
std::cout << " Width: " << blobValue.width << std::endl;
std::cout << " Height: " << blobValue.height << std::endl;
std::cout << " AspectRatio: " << blobValue.aspectRatio << std::endl;
}
}
for (size_t i = 0; i < images.size(); ++i)
{
std::string savePath = "result_" + std::to_string(i) + ".jpg";
{
std::cout << "Result image saved to: " << savePath << std::endl;
}
else
{
std::cerr << "Failed to save result image " << i << ": "
}
}
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.
std::vector< std::string > moduleNames() const
Gets the list of all module names in the loaded model package.
StatusCode getModuleResult(const std::string &moduleName, std::vector< MMindResult > &results)
Gets inference results for a specific module.
std::string statusCodeToString(const StatusCode statusCode)
Converts the status code to the corresponding string.
MBlobValue blobValueFromContour(const MContour &contour)
Computes region feature values (area, width, height, aspect ratio, etc.) for a contour.
mmind::base::StatusCode StatusCode
Represents an image structure for inference and visualization.
StatusCode createFromPath(const std::string &imagePath)
Creates an image by loading from a file path.