int main()
{
int nMaxImages = 8;
std::wstring data_dir = L"../../../examples/data";
std::wstring trainedModelPath = data_dir + L"/models/textile_sample_model.srCls";
std::vector<std::wstring> imagePaths(6);
imagePaths[0] = data_dir + L"/images/Textile/class1_ok/000001.png";
imagePaths[1] = data_dir + L"/images/Textile/class1_ng/000002.png";
imagePaths[2] = data_dir + L"/images/Textile/class2_ok/000001.png";
imagePaths[3] = data_dir + L"/images/Textile/class2_ng/000002.png";
imagePaths[4] = data_dir + L"/images/Textile/class3_ok/000001.png";
imagePaths[5] = data_dir + L"/images/Textile/class3_ng/000002.png";
int verbosity = 3;
int displayTime = 100;
double resizeFactor = 1.0;
int gpuIndex = 0;
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
int nTotalImages = imagePaths.size();
std::vector<ClsTestResult> clsTestResults(nTotalImages);
for (size_t i_img = 0; i_img < nTotalImages; i_img++)
{
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
int numberOfClassesOutput = 0;
int maxStrLenOfClassName = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::vector<std::string> classNames(numberOfClassesOutput);
std::vector<std::wstring> wClassNames(numberOfClassesOutput);
std::vector<int> classColorsOutput(numberOfClassesOutput);
std::vector<int> additionalScoreOutput(numberOfClassesOutput);
char* classNameOutput = new char[maxStrLenOfClassName];
wchar_t* wClassNameOutput = new wchar_t[maxStrLenOfClassName];
for (int i = 0; i < numberOfClassesOutput; i++)
{
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
classNames[i] = classNameOutput;
wClassNames[i] = wClassNameOutput;
}
delete[] classNameOutput;
delete[] wClassNameOutput;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#ifdef SR_USE_NEW_ADDITIONAL_SCORE
std::vector<int> additionalScore(numberOfClassesOutput);
for (int i = 0; i < numberOfClassesOutput; i++)
{
additionalScore[i] = 10;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_FORWARD_TIME_LIMIT
float timeLimit = 1000;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifndef SR_USE_FORWARD_TIME_LIMIT
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::vector<int> heights(nTotalImages, 0);
std::vector<int> widths(nTotalImages, 0);
unsigned char** ucImageData = new unsigned char*[nTotalImages];
unsigned int** uiImageData = new unsigned int*[nTotalImages];
for (size_t i_img = 0; i_img < nTotalImages; i_img++)
{
uiImageData[i_img] = nullptr;
ucImageData[i_img] = nullptr;
if (inputImageType == eSRInputImageType::UIColorImage)
{
uiImageData[i_img] = (unsigned int*)GetImageDataByInputImageType(imagePaths[i_img], inputImageType, heights[i_img], widths[i_img]);
}
else
{
ucImageData[i_img] = (unsigned char*)GetImageDataByInputImageType(imagePaths[i_img], inputImageType, heights[i_img], widths[i_img]);
}
}
if (inputImageType == eSRInputImageType::UCGrayImage)
{
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
}
{
PrintErrorType(srStatus, verbosity);
std::cout << "Fail to classify" << std::endl;
}
else
{
if (verbosity >= 2)
{
long inspectionTimeOutput = 0, loadImageTimeOutput = 0;
{
PrintErrorType(srStatus, verbosity);
}
{
PrintErrorType(srStatus, verbosity);
}
std::cout << "====== classification Test Result (Total) ======" << std::endl;
std::cout << " - Total Image Loading time: " << loadImageTimeOutput << std::endl;
std::cout << " - Total Inspection time: " << inspectionTimeOutput << std::endl;
}
for (size_t i_img = 0; i_img < nTotalImages; i_img++)
{
{
PrintErrorType(srStatus, verbosity);
continue;
}
if (verbosity >= 2)
{
std::cout << "====== classification Test Result (Single) ======" << std::endl;
std::wcout << imagePaths[i_img] << std::endl;
for (int j = 0; j < numberOfClassesOutput; j++)
{
std::cout <<
" - " << j <<
"-th class score (original, with additional): " << clsResult.
scoresOriginal[j] <<
'\t' << clsResult.
scoresIncludingAdd[j] << std::endl;
}
std::cout <<
" - Result Class: " << clsResult.
classIdx << std::endl;
}
if (verbosity >= 3)
{
cv::Mat image = cv::Mat();
if (inputImageType == eSRInputImageType::UCGrayImage)
{
image = cv::Mat(heights[i_img], widths[i_img], CV_8UC1, ucImageData[i_img]);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
image = cv::Mat(heights[i_img], widths[i_img], CV_8UC3, ucImageData[i_img]);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
image = cv::Mat(heights[i_img], widths[i_img], CV_8UC4, uiImageData[i_img]);
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);
}
if (image.channels() == 1)
{
cv::cvtColor(image, image, cv::COLOR_GRAY2BGR);
}
int colorTmp = classColorsOutput[clsResult.
classIdx];
unsigned char r = (colorTmp >> 16) & 0xFF;
unsigned char g = (colorTmp >> 8) & 0xFF;
unsigned char b = (colorTmp) & 0xFF;
cv::Scalar classColor = cv::Scalar(b, g, r);
cv::putText(image, classNames[clsResult.
classIdx],
cv::Point(int(image.size().width * 0.1), int(image.size().height * 0.9)),
cv::FONT_HERSHEY_COMPLEX_SMALL,
2.0,
classColor,
4
);
cv::namedWindow("classification Result", cv::WINDOW_NORMAL);
cv::resizeWindow("classification Result", image.size().width * resizeFactor, image.size().height * resizeFactor);
cv::imshow("classification Result", image);
cv::waitKey(displayTime);
}
}
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
for (size_t i_img = 0; i_img < nTotalImages; i_img++)
{
{
PrintErrorType(srStatus, verbosity);
return -1;
}
if (ucImageData[i_img] != nullptr)
{
delete[] ucImageData[i_img];
ucImageData[i_img] = nullptr;
}
if (uiImageData[i_img] != nullptr)
{
delete[] uiImageData[i_img];
uiImageData[i_img] = nullptr;
}
}
delete[] uiImageData;
delete[] ucImageData;
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
eErrorType SR_SetClsTestAdditionalScore(ClsTestHandle handle, int numOfClasses, int *additionalScores, bool writeOnModelFile=true)
Set Additional scores.
eErrorType SR_GetClsTestAdditionalScore(ClsTestHandle handle, int *additionalScores)
Get Additional scores.
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.
eErrorType SR_SetClsTestForwardTimeLimitVariables(ClsTestHandle handle, float forwardTimeLimit, bool bIsToUseForwardTimeLimit, bool writeOnModelFile=true)
Set the Forward time limit variables.
eErrorType SR_SetClsTestIsToUseForwardTimeLimit(ClsTestHandle handle, bool bIsToUseForwardTimeLimit, bool writeOnModelFile=true)
Set the boolean whether the current model is using time limit or not.
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_ApplyClsTestFromMultipleUnsignedIntColorArrays(ClsTestHandle handle, int numberOfImages, unsigned int **imageData, int *heights, int *widths, ClsTestResult *clsTestResults)
Apply classification algorithm to 4-channels(BGRA) images by 8-bit data (batch-inspection).
eErrorType SR_ApplyClsTestFromMultiple8bitCharColorArrays(ClsTestHandle handle, int numberOfImages, unsigned char **imageData, int *heights, int *widths, ClsTestResult *clsTestResults)
Apply classification algorithm to 3-channels(BGR) images by 8-bit data (batch-inspection).
eErrorType SR_ApplyClsTestFromMultiple8bitCharGrayArrays(ClsTestHandle handle, int numberOfImages, unsigned char **imageData, int *heights, int *widths, ClsTestResult *clsTestResults)
Apply classification algorithm to 1-channel images by 8-bit data (batch-inspection).
eErrorType SR_GetClsTestInspectionTime(ClsTestResult clsTestResult, long *inspectionTimeInLong)
Get the inspection time.
eErrorType SR_GetClsTestLoadImageTime(ClsTestResult clsTestResult, long *imageLoadingTimeInLong)
Get the image loading time.
eErrorType SR_GetClsTestClassifiedResult(ClsTestResult clsTestResult, SR_ClassifiedResult *classifiedResult)
Get SR_ClassifiedResult.
eErrorType SR_LoadClsTestNetworkInGPU(ClsTestHandle handle, int GPUNumber, int batchSize=1)
Allocate required cpu & gpu memory.
Struct describing result of classification.
Definition: CommonHeaderDLL.h:289
int * scoresIncludingAdd
scores including additional scores for each class
Definition: CommonHeaderDLL.h:295
int classIdx
index of the classified result
Definition: CommonHeaderDLL.h:291
int * scoresOriginal
original scores for each class
Definition: CommonHeaderDLL.h:293