int main()
{
std::wstring data_dir = L"../../../examples/data";
std::wstring trainedModelPath_seg = data_dir + L"/models/pcb_sample_model.srSeg";
std::wstring trainedModelPath_cls = data_dir + L"/models/pcb_cls_sample_model.srCls";
std::vector<std::wstring> imagePaths(4);
imagePaths[0] = data_dir + L"/images/PCB/Image-1.png";
imagePaths[1] = data_dir + L"/images/PCB/Image-2.png";
imagePaths[2] = data_dir + L"/images/PCB/Image-3.png";
imagePaths[3] = data_dir + L"/images/PCB/Image-4.png";
int nMaxImages = 32;
unsigned char* ucImageData = nullptr;
unsigned int* uiImageData = nullptr;
int verbosity = 3;
double overlayAlpha = 0.7;
int displayTime = 100;
double resizeFactor = 0.5;
int gpuIndex = 0;
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
std::vector<ClsTestResult> clsTestResults(nMaxImages);
for (size_t i = 0; i < nMaxImages; i++)
{
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
int numberOfClassesOutput = 0;
int maxStrLenOfClassName = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::vector<std::wstring> wClassNames(numberOfClassesOutput);
std::vector<std::string> classNames(numberOfClassesOutput);
std::vector<int> classColorsOutput(numberOfClassesOutput);
wchar_t* wClassNameOutput = new wchar_t[maxStrLenOfClassName];
char* classNameOutput = new char[maxStrLenOfClassName];
for (int i = 0; i < numberOfClassesOutput; i++)
{
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
wClassNames[i] = wClassNameOutput;
classNames[i] = classNameOutput;
}
delete[] wClassNameOutput;
delete[] classNameOutput;
bool isToCrop = false;
int cropSize = -1;
float marginOfCrop = 0.05;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
for (size_t i_img = 0; i_img < imagePaths.size(); i_img++)
{
int h = 0, w = 0;
if (inputImageType == eSRInputImageType::UIColorImage)
{
uiImageData = (unsigned int*)GetImageDataByInputImageType(imagePaths[i_img], inputImageType, h, w);
}
else
{
ucImageData = (unsigned char*)GetImageDataByInputImageType(imagePaths[i_img], inputImageType, h, w);
}
if (inputImageType == eSRInputImageType::ImageFilePathMultiByteChar)
{
size_t sizeOfChar = 0;
size_t sizeOfWChar = wcslen(imagePaths[i_img].c_str());
size_t bufferSize = sizeOfWChar * 2;
std::vector<char> imagePathMB(bufferSize);
sizeOfChar = wcstombs(
imagePathMB.data(),
imagePaths[i_img].c_str(),
bufferSize
);
if (sizeOfChar != sizeOfWChar)
{
std::wcout << "Saige Vision FAILED: Wrong Image path" << imagePaths[i_img] << std::endl;
continue;
}
}
else if (inputImageType == eSRInputImageType::ImageFilePathWideChar)
{
}
else if (inputImageType == eSRInputImageType::UCGrayImage)
{
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
}
{
PrintErrorType(srStatus, verbosity);
if (verbosity >= 3)
{
DisplayFailedImageOnCVWindow(imagePaths[i_img], "FAIL TO segment", "segmentation Result", displayTime, resizeFactor);
}
continue;
}
if (inputImageType == eSRInputImageType::ImageFilePathWideChar || inputImageType == eSRInputImageType::ImageFilePathMultiByteChar)
{
{
PrintErrorType(srStatus, verbosity);
continue;
}
}
long inspectionTimeOutput = 0, loadImageTimeOutput = 0;
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
if (verbosity >= 2)
{
std::cout << "====== segmentation Test Result ======" << std::endl;
std::wcout << imagePaths[i_img] << std::endl;
std::cout << " - Image width, height: " << w << ", " << h << std::endl;
std::cout << " - Image Loading time: " << loadImageTimeOutput << std::endl;
std::cout << " - Inspection time: " << inspectionTimeOutput << std::endl << std::endl;
}
std::vector<SR_SegmentedObject> segmentedObject;
int nSegmentedObjects = 0;
{
PrintErrorType(srStatus, verbosity);
continue;
}
segmentedObject.resize(nSegmentedObjects);
{
PrintErrorType(srStatus, verbosity);
continue;
}
if (verbosity >= 2)
{
std::cout << " - the number of segmented objects: " << segmentedObject.size() << std::endl;
for (int i = 0; i < segmentedObject.size(); i++)
{
std::cout << " - " << i << "-th segmented objects : " <<
"class[" << segmentedObject[i].classIdx << "], " <<
"area[" << segmentedObject[i].area << "], " <<
"width[" << segmentedObject[i].width << "], " <<
"height[" << segmentedObject[i].height << "], " <<
"x_center[" << segmentedObject[i].xCenter << "], " <<
"y_center[" << segmentedObject[i].yCenter << "]" << std::endl;
}
}
if (nSegmentedObjects > 0)
{
if (clsTestResults.size() < nSegmentedObjects)
{
clsTestResults.resize(nSegmentedObjects, nullptr);
for (size_t i = 0; i < clsTestResults.size(); i++)
{
if (clsTestResults[i] == nullptr)
}
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
}
{
PrintErrorType(srStatus, verbosity);
}
std::cout << "====== classification Test Result (Total) ======" << std::endl;
std::cout << " - Image Loading time: " << loadImageTimeOutput << std::endl;
std::cout << " - Inspection time: " << inspectionTimeOutput << std::endl;
if (verbosity >= 2)
{
for (size_t i = 0; i < nSegmentedObjects; i++)
{
std::vector<int> scoreOutput(numberOfClassesOutput);
int classOutput = -1;
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
std::cout << "====== classification Test Result (Single) ======" << std::endl;
for (int j = 0; j < numberOfClassesOutput; j++)
{
std::cout << " - " << j << "-th class score: " << scoreOutput[j] << std::endl;
}
std::cout << " - Result Class: " << classOutput << std::endl;
}
}
}
if (verbosity >= 3)
{
cv::Mat image = cv::Mat();
if (inputImageType == eSRInputImageType::UCGrayImage)
{
image = cv::Mat(h, w, CV_8UC1, ucImageData);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
image = cv::Mat(h, w, CV_8UC3, ucImageData);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
image = cv::Mat(h, w, CV_8UC4, uiImageData);
image = image.clone();
}
else
{
int isSuccess = OpenImageFile(imagePaths[i_img].c_str(), image);
if (isSuccess == 0)
{
std::cout << "Saige Vision FAILED: Fail to open image file to display" << std::endl;
continue;
}
}
if (image.channels() == 4)
{
cv::cvtColor(image, image, cv::COLOR_BGRA2BGR);
}
else if (image.channels() == 1)
{
cv::cvtColor(image, image, cv::COLOR_GRAY2BGR);
}
cv::Mat imageCheck = image.clone();
for (size_t i_o = 0; i_o < nSegmentedObjects; i_o++)
{
int classIdx = 0;
unsigned char* tmp = (unsigned char*)(&classColorsOutput[classIdx]);
int b = 0, g = 0, r = 0;
std::memcpy(&b, tmp, 1);
std::memcpy(&g, tmp + 1, 1);
std::memcpy(&r, tmp + 2, 1);
cv::Scalar overlayScalar = cv::Scalar(b, g, r);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
convertSRSegmentedObjectToOpencvFormat(segmentedObject[i_o], contours, hierarchy);
cv::drawContours(imageCheck, contours, -1, overlayScalar, -1, 8, hierarchy);
int dataType = CV_MAKETYPE(CV_8U, segmentedObject[i_o].croppedImageChannels);
cv::Mat croppedImage = cv::Mat(segmentedObject[i_o].croppedImageHeight, segmentedObject[i_o].croppedImageWidth, dataType, segmentedObject[i_o].croppedImage);
cv::putText(croppedImage, classNames[classIdx],
cv::Point(int(croppedImage.size().width * 0.1), int(croppedImage.size().height * 0.9)),
cv::FONT_HERSHEY_COMPLEX_SMALL,
2.0,
overlayScalar,
4
);
}
cv::Mat finalUnifiedImage;
double beta = 1.0 - overlayAlpha;
double gamma = 0.0;
cv::addWeighted(image, overlayAlpha, imageCheck, beta, gamma, finalUnifiedImage);
cv::namedWindow("Segmentation Contour Result", cv::WINDOW_NORMAL);
cv::resizeWindow("Segmentation Contour Result", w * resizeFactor, h * resizeFactor);
cv::imshow("Segmentation Contour Result", finalUnifiedImage);
cv::waitKey(displayTime);
for (size_t i_o = 0; i_o < nSegmentedObjects; i_o++)
{
if (segmentedObject[i_o].croppedImage != nullptr)
{
int dataType = CV_MAKETYPE(CV_8U, segmentedObject[i_o].croppedImageChannels);
cv::Mat croppedImage = cv::Mat(segmentedObject[i_o].croppedImageHeight, segmentedObject[i_o].croppedImageWidth, dataType, segmentedObject[i_o].croppedImage);
cv::namedWindow("Segmentation Cropped Image", cv::WINDOW_NORMAL);
cv::resizeWindow("Segmentation Cropped Image", croppedImage.size().width, croppedImage.size().height);
cv::imshow("Segmentation Cropped Image", croppedImage);
cv::waitKey(displayTime);
}
}
}
if (ucImageData != nullptr)
{
delete[] ucImageData;
}
if (uiImageData != nullptr)
{
delete[] uiImageData;
}
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
for (size_t i = 0; i < clsTestResults.size(); i++)
{
{
PrintErrorType(srStatus, verbosity);
return -1;
}
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::cout << "SaigeVision successfully completed, please press enter key to terminate" << std::endl;
std::cin.get();
return 0;
}
Header file for Classification Module.
eErrorType SR_PrintClsTestParameters(ClsTestHandle handle)
Print a model parameters (Classification's class information, inspection options.....
Header file of util functions for example projects.
eSRInputImageType
Data Type of input Image for example projects.
Definition: SRUtils.h:23
Header file for segmentation module.
eErrorType SR_PrintSegTestParametersWithoutBG(SegTestHandle handle)
Print a model parameters (Segmentation's class information, inspection options...)
@ SR_ONE_CLASS_CV_CONTOUR_OUTPUT
Definition: SegmentationTestDLL.h:63
@ SR_MULTI_CLASS_CV_CONTOUR_OUTPUT
Definition: SegmentationTestDLL.h:64
eErrorType SR_GetClsTestOneClassColor(ClsTestHandle handle, const int classIdx, int *classColor)
Get a color of certain class by given index.
eErrorType SR_GetClsTestOneClassNameWithWideChar(ClsTestHandle handle, const int classIdx, wchar_t *className)
Get a wide char name of certain class by given index.
eErrorType SR_GetClsTestMaxStrLenOfClassName(ClsTestHandle handle, int *len)
Get a maximum length of class name.
eErrorType SR_GetClsTestNumberOfClasses(ClsTestHandle handle, int *numberOfClasses)
Get the number of classes.
eErrorType SR_GetClsTestOneClassNameWithMultiByteChar(ClsTestHandle handle, const int classIdx, char *className)
Get a multi byte char name of certain class by given index.
void * ClsTestHandle
Classification file handler.
Definition: ClassifierTestDLL.h:56
eErrorType SR_MakeClsTestResult_ErrorType(ClsTestResult *clsTestResult)
Construct a ClsTestResult.
eErrorType SR_CreateClsTestHandleByWideCharPath_ErrorType(ClsTestHandle *clsTestHandle, const wchar_t *filePath)
Create a ClsTestHandle via wide char path.
eErrorType SR_DeleteClsTestResult(ClsTestResult clsTestResult)
Destruct a ClsTestResult.
eErrorType SR_DestroyClsTestHandle(ClsTestHandle handle)
Destruct a ClsTestHandle.
eErrorType SR_ApplyClsTestFromSegmentedObjects(ClsTestHandle handle, int numberOfObjects, SR_SegmentedObject *segmentedObjects, ClsTestResult *clsTestResults)
Apply classification algorithm to SR_SegmentedObject::croppedImage (batch-inspection).
eErrorType SR_GetClsTestInspectionTime(ClsTestResult clsTestResult, long *inspectionTimeInLong)
Get the inspection time.
eErrorType SR_GetClsTestResultScore(ClsTestResult clsTestResult, const int numClasses, int *scores, bool includeAddScore=false)
Get scores for each class.
eErrorType SR_GetClsTestLoadImageTime(ClsTestResult clsTestResult, long *imageLoadingTimeInLong)
Get the image loading time.
eErrorType SR_GetClsTestResultClass(ClsTestResult clsTestResult, int *classIdx)
Get final class index.
eErrorType SR_LoadSegTestNetworkInGPU(SegTestHandle handle, int GPUNumber, int batchSize=1)
Allocate required cpu & gpu memory.
eErrorType SR_LoadClsTestNetworkInGPU(ClsTestHandle handle, int GPUNumber, int batchSize=1)
Allocate required cpu & gpu memory.
eErrorType SR_GetSegTestIsToStoreCroppedImage(SegTestHandle handle, bool *isToStoreCroppedImage, int *defaultCropSize, eModeOfCropLargeObjects *modeOfCropLargeObjects, float *margin)
Get the parameters of Segmentation's crop object.
eErrorType SR_SetSegTestIsToStoreCroppedImage(SegTestHandle handle, bool isToStoreCroppedImage, int defaultCropSize, eModeOfCropLargeObjects modeOfCropLargeObjects=SR_CROP_BY_OBJECTSIZE_N_RESIZE_TO_CROPSIZE, float margin=0.05, bool writeOnModelFile=true)
Set the parameters of Segmentation's crop object.
void * SegTestHandle
Segmentation file handler.
Definition: SegmentationTestDLL.h:90
eErrorType SR_CreateSegTestHandleByWideCharPath_ErrorType(SegTestHandle *segTestHandle, const wchar_t *filePath)
Create a SegTestHandle via wide char path.
void * SegTestResult
Segmentation inspection results handler.
Definition: SegmentationTestDLL.h:105
eErrorType SR_MakeSegTestResult_ErrorType(SegTestResult *segTestResult)
Construct a SegTestResult.
eErrorType SR_DestroySegTestHandle(SegTestHandle handle)
Destruct a SegTestHandle.
eErrorType SR_DeleteSegTestResult(SegTestResult segTestResult)
Destruct a SegTestResult.
eErrorType SR_ApplySegTestFromImageFileByWideCharPath(SegTestHandle handle, const wchar_t *imagePath, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to image by file path.
eErrorType SR_ApplySegTestFromUnsignedIntColorArray(SegTestHandle handle, const unsigned int *imageData, const int height, const int width, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to 4-channels(BGRA) image by 8-bit data.
eErrorType SR_ApplySegTestFrom8bitCharColorArray(SegTestHandle handle, const unsigned char *imageData, const int height, const int width, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to 3-channels(BGR) image by 8-bit data.
eErrorType SR_ApplySegTestFrom8bitCharGrayArray(SegTestHandle handle, const unsigned char *imageData, const int height, const int width, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to 1-channel image by 8-bit data.
eErrorType SR_ApplySegTestFromImageFile(SegTestHandle handle, const char *imagePath, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to image by file path.
eErrorType SR_GetSegTestLoadImageTime(SegTestResult segTestResult, long *imageLoadingTimeInLong)
Get the image loading time.
eErrorType SR_GetSegTestInspectionTime(SegTestResult segTestResult, long *inspectionTimeInLong)
Get the inspection time.
eErrorType SR_GetSegTestNumberOfSegmentedObjects(SegTestResult segTestResult, int *numberOfSegmentedObjects)
Get the number of SR_SegmentedObject.
eErrorType SR_GetSegTestResultOriginalImageSize(SegTestResult segTestResult, int *width, int *height)
Get the size of inspected image.
eErrorType SR_GetSegTestSegmentedObjects(SegTestResult segTestResult, SR_SegmentedObject *segmentedObjects)
Get SR_SegmentedObject s.