SaigeVision C/C++  1.4.7
SaigeVision API document for C/C++
ExampleSegRuntimeSimple.cpp

This is an example of how to inspect sample images by using the minimum number of functions of Segmentation 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/pcb_sample_model.srSeg";
// image paths to classify
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";
/***********************************************************
* 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
***********************************************************/
SegTestHandle handle = SR_CreateSegTestHandleByWideCharPath(trainedModelPath.c_str());
if (handle == nullptr)
{
std::wcout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
/***********************************************************
* 2. 클래스 정보 확인 (Optional)
* Check class information
***********************************************************/
// Print segmentation Test Parameters() by calling one fuctions
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_GetSegTestNumberOfClassesWithoutBG(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_LoadSegTestNetworkInGPU(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_ApplySegTestFromImageFile(handle, imagePathMB.data(), segTestResult, SR_MULTI_CLASS_CV_CONTOUR_OUTPUT);
}
else if (inputImageType == eSRInputImageType::ImageFilePathWideChar)
{
srStatus = SR_ApplySegTestFromImageFileByWideCharPath(handle, imagePaths[i_img].c_str(), segTestResult, SR_MULTI_CLASS_CV_CONTOUR_OUTPUT);
}
else if (inputImageType == eSRInputImageType::UCGrayImage)
{
srStatus = SR_ApplySegTestFrom8bitCharGrayArray(handle, ucImageData, h, w, segTestResult, SR_MULTI_CLASS_CV_CONTOUR_OUTPUT);
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
srStatus = SR_ApplySegTestFrom8bitCharColorArray(handle, ucImageData, h, w, segTestResult, SR_MULTI_CLASS_CV_CONTOUR_OUTPUT);
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
srStatus = SR_ApplySegTestFromUnsignedIntColorArray(handle, uiImageData, h, w, segTestResult, SR_MULTI_CLASS_CV_CONTOUR_OUTPUT);
}
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
***********************************************************/
if (inputImageType == eSRInputImageType::ImageFilePathWideChar || inputImageType == eSRInputImageType::ImageFilePathMultiByteChar)
{
// Get dimension of input image to draw results later..
srStatus = SR_GetSegTestResultOriginalImageSize(segTestResult, &w, &h);
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
}
}
long inspectionTimeOutput = 0, loadImageTimeOutput = 0;
// Get the inspection time
srStatus = SR_GetSegTestInspectionTime(segTestResult, &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
}
// Get the image-loading time
srStatus = SR_GetSegTestLoadImageTime(segTestResult, &loadImageTimeOutput);
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
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;
}
// Get Segmented objects
// segmented object after filtering (# of classes x # of objects)
int nSegmentedObjects = 0;
srStatus = SR_GetSegTestNumberOfSegmentedObjects(segTestResult, &nSegmentedObjects);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the segTestResult is not created (nullptr).
continue; // jump to next image
}
std::vector<SR_SegmentedObject> segmentedObject(nSegmentedObjects);
srStatus = SR_GetSegTestSegmentedObjects(segTestResult, segmentedObject.data());
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that the segTestResult is not created (nullptr).
continue; // jump to next image
}
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 (ucImageData != nullptr)
{
delete[] ucImageData;
}
if (uiImageData != nullptr)
{
delete[] uiImageData;
}
}
/***********************************************************
* 7. Handle 파괴, Destruct handles
***********************************************************/
srStatus = SR_DeleteSegTestResult(segTestResult);
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_DestroySegTestHandle(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;
}
SegmentationTestDLL.h
Header file for segmentation module.
SR_MakeSegTestResult
SegTestResult SR_MakeSegTestResult()
Construct a SegTestResult.
SR_MULTI_CLASS_CV_CONTOUR_OUTPUT
@ SR_MULTI_CLASS_CV_CONTOUR_OUTPUT
Definition: SegmentationTestDLL.h:64
SR_GetSegTestInspectionTime
eErrorType SR_GetSegTestInspectionTime(SegTestResult segTestResult, long *inspectionTimeInLong)
Get the inspection time.
SR_ApplySegTestFromUnsignedIntColorArray
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.
SR_ApplySegTestFromImageFileByWideCharPath
eErrorType SR_ApplySegTestFromImageFileByWideCharPath(SegTestHandle handle, const wchar_t *imagePath, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to image by file path.
GetImageDataByInputImageType
void * GetImageDataByInputImageType(const std::wstring imagePath, const eSRInputImageType inputType, int &height, int &width, int *channel=nullptr)
Get image data from a file path and image size by an allocated pointer for example projects.
Definition: SRUtils.h:138
SR_GetSegTestNumberOfSegmentedObjects
eErrorType SR_GetSegTestNumberOfSegmentedObjects(SegTestResult segTestResult, int *numberOfSegmentedObjects)
Get the number of SR_SegmentedObject.
SR_LoadSegTestNetworkInGPU
eErrorType SR_LoadSegTestNetworkInGPU(SegTestHandle handle, int GPUNumber, int batchSize=1)
Allocate required cpu & gpu memory.
SR_GetSegTestResultOriginalImageSize
eErrorType SR_GetSegTestResultOriginalImageSize(SegTestResult segTestResult, int *width, int *height)
Get the size of inspected image.
eSRInputImageType
eSRInputImageType
Data Type of input Image for example projects.
Definition: SRUtils.h:23
SR_CreateSegTestHandleByWideCharPath
SegTestHandle SR_CreateSegTestHandleByWideCharPath(const wchar_t *filePath)
Create a SegTestHandle via wide char path.
SR_DeleteSegTestResult
eErrorType SR_DeleteSegTestResult(SegTestResult segTestResult)
Destruct a SegTestResult.
SR_ApplySegTestFrom8bitCharColorArray
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.
SR_GetSegTestLoadImageTime
eErrorType SR_GetSegTestLoadImageTime(SegTestResult segTestResult, long *imageLoadingTimeInLong)
Get the image loading time.
SR_DestroySegTestHandle
eErrorType SR_DestroySegTestHandle(SegTestHandle handle)
Destruct a SegTestHandle.
eErrorType
eErrorType
Enumerator indicating error codes.
Definition: CommonHeaderDLL.h:25
SegTestHandle
void * SegTestHandle
Segmentation file handler.
Definition: SegmentationTestDLL.h:90
SR_ApplySegTestFromImageFile
eErrorType SR_ApplySegTestFromImageFile(SegTestHandle handle, const char *imagePath, SegTestResult segTestResult, int flag)
Apply segmentation algorithm to image by file path.
SRUtils.h
Header file of util functions for example projects.
SegTestResult
void * SegTestResult
Segmentation inspection results handler.
Definition: SegmentationTestDLL.h:105
SR_PrintSegTestParametersWithoutBG
eErrorType SR_PrintSegTestParametersWithoutBG(SegTestHandle handle)
Print a model parameters (Segmentation's class information, inspection options...)
SR_ApplySegTestFrom8bitCharGrayArray
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.
NO_ERROR_OUT
@ NO_ERROR_OUT
Definition: CommonHeaderDLL.h:26
SR_GetSegTestNumberOfClassesWithoutBG
eErrorType SR_GetSegTestNumberOfClassesWithoutBG(SegTestHandle handle, int *numberOfClasses)
Get the number of classes.
SR_GetSegTestSegmentedObjects
eErrorType SR_GetSegTestSegmentedObjects(SegTestResult segTestResult, SR_SegmentedObject *segmentedObjects)
Get SR_SegmentedObject s.