Inference with OpenCV integration.
Inference with OpenCV integration.
This example demonstrates how to integrate OpenCV with the Mech-DLK SDK for image I/O and visualization workflows. It shows how to:
The example takes no command-line arguments. Image and model paths are resolved from resources/ next to the executable.
#include <vector>
#include <string>
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <windows.h>
#include <opencv2/opencv.hpp>
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;
}
{
if (cvImage.empty())
{
std::cerr << "OpenCV image is empty!" << std::endl;
}
cv::Mat rgbImage = cvImage.clone();
mmindImage.
width = rgbImage.cols;
mmindImage.
height = rgbImage.rows;
std::shared_ptr<uint8_t[]> imageData(new uint8_t[rgbImage.total() * 3]);
memcpy(imageData.get(), rgbImage.data, rgbImage.total() * 3);
mmindImage.
data = std::shared_ptr<const void>(imageData.get(),
[imageData](const void *) mutable { });
}
int main()
{
try
{
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::cout << "Loading image with OpenCV: " << imagePath.filename() << std::endl;
cv::Mat cvImage = cv::imread(imagePath.string());
if (cvImage.empty())
{
std::cerr << "Failed to load image: " << imagePath.string() << std::endl;
continue;
}
std::cout << " OpenCV image loaded: " << cvImage.cols << "x" << cvImage.rows
<< " channels: " << cvImage.channels() << std::endl;
StatusCode status = convertOpenCVToMMindImage(cvImage, mmindImage);
{
std::cerr << "Failed to convert OpenCV image to MMindImage: "
<< imagePath.filename() << std::endl;
continue;
}
std::cout <<
" Converted to MMindImage: " << mmindImage.
width <<
"x"
<< mmindImage.
height << std::endl;
images.push_back(std::move(mmindImage));
}
if (images.empty())
{
std::cerr << "No images loaded successfully" << std::endl;
return -1;
}
std::cout << "Loading inference engine..." << std::endl;
engine.
create(getPackPath().wstring());
std::cout << "Performing inference..." << std::endl;
{
return -1;
}
std::vector<MMindResult> results;
{
return -1;
}
std::cout << "Inference completed successfully!" << std::endl;
std::cout << "Number of results: " << results.size() << std::endl;
for (size_t i = 0; i < results.size(); ++i)
{
std::cout << "Result " << i << ": " << results[i].contours.size() << " contours" << std::endl;
}
for (size_t i = 0; i < images.size(); ++i)
{
std::string windowName = "OpenCV Inference Result " + std::to_string(i);
images[i].show(windowName);
}
std::cout << "Press any key to exit..." << std::endl;
cv::waitKey(0);
return 0;
}
catch (const std::exception &e)
{
std::cerr << "Exception occurred: " << e.what() << std::endl;
return -1;
}
}
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.
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.
@ kStatusCodeInvalidValue
mmind::base::StatusCode StatusCode
Represents an image structure for inference and visualization.
std::shared_ptr< const void > data
Pointer to image pixel data (BGR format, row-major)
uint32_t height
Image height in pixels.
uint32_t width
Image width in pixels.
size_t bytesPerRow
Number of bytes per row (stride/pitch)