SaigeVision C/C++ 1.5.0
SaigeVision API document for C/C++
 
Loading...
Searching...
No Matches
ExampleClsRuntimeSimple.cpp

This is an example of how to inspect sample images by using the minimum number of functions of cls module. More details about this example.

#include "SRUtils.h"
int main()
{
std::wstring data_dir = L"../../../examples/data";
// trained model path
std::wstring trainedModelPath = data_dir + L"/models/textile_sample_model.srCls";
// image paths to classify
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";
/***********************************************************
* 0. 이미지 인풋 타입 설정
* inputImageType -
* ImageFilePathMultiByteChar: from file path (multi byte char)
* ImageFilePathWideChar: from file path (wide char)
* UCGrayImage: from memory(8bit unsigned char gray image)
* UCColorImage: from memory(8bit unsigned char color image)
* UIColorImage: from memory(32bit unsigned int color image)
***********************************************************/
//eSRInputImageType inputImageType = eSRInputImageType::ImageFilePathMultiByteChar;
eSRInputImageType inputImageType = eSRInputImageType::ImageFilePathWideChar;
//eSRInputImageType inputImageType = eSRInputImageType::UCGrayImage;
//eSRInputImageType inputImageType = eSRInputImageType::UCColorImage;
//eSRInputImageType inputImageType = eSRInputImageType::UIColorImage;
unsigned char* ucImageData = nullptr; // unsigned char image data
unsigned int* uiImageData = nullptr; // unsigned int image data
/***********************************************************
* 0-1. 정보 띄우기 단계 설정 (Verbosity)
* verbosity -
* 0: only warning
* 1: warning + model info
* 2: warning + model info + result info
***********************************************************/
int verbosity = 2;
/***********************************************************
* 0-2. GPU 번호 설정
* gpuIndex - 사용하고자 하는 gpu의 번호
* Multi GPU 가 가능한 License의 경우에 한하여 gpuIndex를
* 0~3까지 설정해 줄 수 있다. 아닌 경우 gpuIndex는 0을 사용하여야 한다.
***********************************************************/
int gpuIndex = 0;
/***********************************************************
* 1. 모델 파일 및 검사 결과 handle 생성,
* Create handles for model files and inspection results
***********************************************************/
ClsTestHandle handle = nullptr;
srStatus = SR_CreateClsTestHandleByWideCharPath_ErrorType(&handle, trainedModelPath.c_str());
if (srStatus != NO_ERROR_OUT || handle == nullptr)
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
ClsTestResult clsTestResult = nullptr;
srStatus = SR_MakeClsTestResult_ErrorType(&clsTestResult);
if (srStatus != NO_ERROR_OUT || clsTestResult == nullptr)
{
PrintErrorType(srStatus, verbosity);
return -1;
}
/***********************************************************
* 2. 클래스 정보 확인 (Optional)
* Check class information
***********************************************************/
// Print all classification Test Parameters
srStatus = SR_PrintClsTestParameters(handle);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the handle is not created.
return -1;
}
int numberOfClassesOutput = 0;
// Get the number of classes
srStatus = SR_GetClsTestNumberOfClasses(handle, &numberOfClassesOutput);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that input pointer is not allocated (nullptr).
return -1;
}
/***********************************************************
* 3. 검사 옵션 확인 및 수정 (Optional)
* Check inspection options and set new values
***********************************************************/
// pass
/***********************************************************
* 4. 메모리를 GPU 및 CPU에 할당합니다.
* Allocate GPU and CPU memory
***********************************************************/
srStatus = SR_LoadClsTestNetworkInGPU(handle, gpuIndex);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the inserted index is not applicable.
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);
}
/***********************************************************
* 5. 이미지 검사하기, Inspect images
***********************************************************/
if (inputImageType == eSRInputImageType::ImageFilePathMultiByteChar)
{
// Check if given image path(UNICODE) can be converted to ASCII CODE without any loss.
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;
}
srStatus = SR_ApplyClsTestFromImageFile(handle, imagePathMB.data(), clsTestResult);
}
else if (inputImageType == eSRInputImageType::ImageFilePathWideChar)
{
srStatus = SR_ApplyClsTestFromImageFileByWideCharPath(handle, imagePaths[i_img].c_str(), clsTestResult);
}
else if (inputImageType == eSRInputImageType::UCGrayImage)
{
srStatus = SR_ApplyClsTestFrom8bitCharGrayArray(handle, ucImageData, h, w, clsTestResult);
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
srStatus = SR_ApplyClsTestFrom8bitCharColorArray(handle, ucImageData, h, w, clsTestResult);
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
srStatus = SR_ApplyClsTestFromUnsignedIntColorArray(handle, uiImageData, h, w, clsTestResult);
}
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the input data is not loaded successfully.
continue; // jump to next image
}
/***********************************************************
* 6. 검사 결과 받아오기, Retrieve inspection results
***********************************************************/
long inspectionTimeOutput = 0;
srStatus = SR_GetClsTestClassifiedResult(clsTestResult, &clsResult);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that input pointer is not allocated (nullptr).
continue; // jump to next image
}
// Get the inspection time
srStatus = SR_GetClsTestInspectionTime(clsTestResult, &inspectionTimeOutput);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the input pointer is not allocated (nullptr).
continue; // jump to next image
}
// Display and draw
if (verbosity >= 2)
{
std::cout << "====== classification Test Result ======" << 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;
std::cout << " - Inspection time: " << inspectionTimeOutput << std::endl << std::endl;
}
if (ucImageData != nullptr)
{
delete[] ucImageData;
}
if (uiImageData != nullptr)
{
delete[] uiImageData;
}
}
/***********************************************************
* 7. Handle 파괴, Destruct handles
***********************************************************/
srStatus = SR_DeleteClsTestResult(clsTestResult);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the input pointer is not allocated (nullptr).
return -1;
}
srStatus = SR_DestroyClsTestHandle(handle);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the input pointer is not allocated (nullptr).
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.....
eErrorType
Enumerator indicating error codes.
Definition: CommonHeaderDLL.h:30
@ NO_ERROR_OUT
Definition: CommonHeaderDLL.h:31
Header file of util functions for example projects.
eSRInputImageType
Data Type of input Image for example projects.
Definition: SRUtils.h:23
eErrorType SR_GetClsTestNumberOfClasses(ClsTestHandle handle, int *numberOfClasses)
Get the number of classes.
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.
void * ClsTestResult
Classification inspection results handler.
Definition: ClassifierTestDLL.h:71
eErrorType SR_DeleteClsTestResult(ClsTestResult clsTestResult)
Destruct a ClsTestResult.
eErrorType SR_DestroyClsTestHandle(ClsTestHandle handle)
Destruct a ClsTestHandle.
eErrorType SR_ApplyClsTestFrom8bitCharColorArray(ClsTestHandle handle, const unsigned char *imageData, const int height, const int width, ClsTestResult clsTestResult)
Apply classification algorithm to 3-channels(BGR) image by 8-bit data.
eErrorType SR_ApplyClsTestFromImageFileByWideCharPath(ClsTestHandle handle, const wchar_t *imagePath, ClsTestResult clsTestResult)
Apply classification algorithm to image by file path.
eErrorType SR_ApplyClsTestFromUnsignedIntColorArray(ClsTestHandle handle, const unsigned int *imageData, const int height, const int width, ClsTestResult clsTestResult)
Apply classification algorithm to 4-channels(BGRA) image by 8-bit data.
eErrorType SR_ApplyClsTestFromImageFile(ClsTestHandle handle, const char *imagePath, ClsTestResult clsTestResult)
Apply classification algorithm to image by file path.
eErrorType SR_ApplyClsTestFrom8bitCharGrayArray(ClsTestHandle handle, const unsigned char *imageData, const int height, const int width, ClsTestResult clsTestResult)
Apply classification algorithm to 1-channel image by 8-bit data.
eErrorType SR_GetClsTestInspectionTime(ClsTestResult clsTestResult, long *inspectionTimeInLong)
Get the inspection 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