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;
if (segHandle == nullptr)
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
if (clsHandle == nullptr)
{
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)
{
}
else
{
}
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)
{
}
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
{
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;
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;
}