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

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

#include "OCRTestDLL.h"
#include "SRUtils.h"
int main()
{
std::wstring data_dir = L"../../../examples/data";
std::wstring boxModelPath = data_dir + L"/models/ocr.srOcrBox";
std::wstring textModelPath = data_dir + L"/models/ocr.srOcrText";
// image paths to classify
std::vector<std::wstring> imagePaths(3);
imagePaths[0] = data_dir + L"/images/OCR/text_01.jpg";
imagePaths[1] = data_dir + L"/images/OCR/text_02.jpg";
imagePaths[2] = data_dir + L"/images/OCR/text_03.jpg";
/***********************************************************
* 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;
void* imageData = nullptr; // image data
/***********************************************************
* 0-1. 정보 띄우기 단계 설정 (Verbosity)
* verbosity -
* 0: only warning
* 1: warning + model info
* 2: warning + model info + result info
* 3: warning + model info + result info + image display
***********************************************************/
int verbosity = 3;
/***********************************************************
* 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
***********************************************************/
OCRTestHandle handle = SR_CreateOCRTestHandleByWideCharPath(boxModelPath.c_str(), textModelPath.c_str());
if (handle == nullptr)
{
std::cout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
/***********************************************************
* 2. 클래스 정보 확인 (Optional)
* Check class information
***********************************************************/
// N/A
/***********************************************************
* 3. 검사 옵션 확인 및 수정 (Optional)
* Check inspection options and set new values
***********************************************************/
// Print all ocr test parameters
srStatus = SR_PrintOCRTestParameters(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;
}
/***********************************************************
* 4. 메모리를 GPU 및 CPU에 할당합니다.
* Allocate GPU and CPU memory
***********************************************************/
srStatus = SR_LoadOCRTestNetworkInGPU(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::ImageFilePathMultiByteChar && inputImageType != eSRInputImageType::ImageFilePathWideChar)
{
imageData = 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_ApplyOCRTestFromImageFile(handle, imagePathMB.data(), ocrTestResult);
} else if (inputImageType == eSRInputImageType::ImageFilePathWideChar)
{
srStatus = SR_ApplyOCRTestFromImageFileByWideCharPath(handle, imagePaths[i_img].c_str(), ocrTestResult);
} else if (inputImageType == eSRInputImageType::UCGrayImage)
{
srStatus = SR_ApplyOCRTestFrom8bitImageData(handle, imageData, h, w, 1, ocrTestResult);
} else if (inputImageType == eSRInputImageType::UCColorImage)
{
srStatus = SR_ApplyOCRTestFrom8bitImageData(handle, imageData, h, w, 3, ocrTestResult);
} else if (inputImageType == eSRInputImageType::UIColorImage)
{
srStatus = SR_ApplyOCRTestFrom8bitImageData(handle, imageData, h, w, 4, ocrTestResult);
}
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, loadImageTimeOutput = 0;
// Get the inspection time
srStatus = SR_GetOCRTestInspectionTime(ocrTestResult, &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_GetOCRTestLoadImageTime(ocrTestResult, &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
}
int nTextBoxes = 0;
srStatus = SR_GetOCRTestNumberOfTextBoxes(ocrTestResult, &nTextBoxes);
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
}
std::vector<SR_OCRTextBox> textBoxes(nTextBoxes);
if (nTextBoxes > 0)
{
srStatus = SR_GetOCRTestTextBoxes(ocrTestResult, textBoxes.data());
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 << "====== Detection Test Result ======" << std::endl;
std::wcout << imagePaths[i_img] << std::endl;
std::cout << " - Number of text boxes: " << nTextBoxes << std::endl;
std::cout << " - Image Loading time: " << loadImageTimeOutput << std::endl << std::endl;
std::cout << " - Inspection time: " << inspectionTimeOutput << std::endl;
std::cout << " - Text boxes information" << std::endl;
}
for (int i = 0; i < nTextBoxes; i++)
{
float x_c = textBoxes[i].box.x;
float y_c = textBoxes[i].box.y;
float width = textBoxes[i].box.width;
float height = textBoxes[i].box.height;
float angle = textBoxes[i].box.angle;
float score = textBoxes[i].box.score;
std::wstring wtext = textBoxes[i].wtext;
std::string text = textBoxes[i].text;
if (verbosity >= 2)
{
// print
printf(" - %d-th box info(x_c, y_c, w, h, angle, score): %0.3f, %0.3f, %0.3f, %0.3f, %0.3f, %0.3f\n",
i, x_c, y_c, width, height, angle, score);
wprintf(L" - %d-th box's characters: %s\n", i, wtext.c_str());
}
}
if (imageData != nullptr)
{
delete[] (unsigned char*)imageData;
}
}
/***********************************************************
* 7. Handle 파괴, Destruct handles
***********************************************************/
srStatus = SR_DeleteOCRTestResult(ocrTestResult);
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_DestroyOCRTestHandle(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;
}
SR_DestroyOCRTestHandle
eErrorType SR_DestroyOCRTestHandle(OCRTestHandle handle)
Destruct a OCRTestHandle.
SR_GetOCRTestInspectionTime
eErrorType SR_GetOCRTestInspectionTime(OCRTestResult ocrTestResult, long *inspectionTimeInLong)
Get the inspection time.
SR_ApplyOCRTestFromImageFileByWideCharPath
eErrorType SR_ApplyOCRTestFromImageFileByWideCharPath(OCRTestHandle handle, const wchar_t *imagePath, OCRTestResult ocrTestResult)
Apply OCR algorithm to image by file path.
SR_LoadOCRTestNetworkInGPU
eErrorType SR_LoadOCRTestNetworkInGPU(OCRTestHandle handle, int GPUNumber)
Allocate required cpu & gpu memory.
SR_GetOCRTestLoadImageTime
eErrorType SR_GetOCRTestLoadImageTime(OCRTestResult ocrTestResult, long *imageLoadingTimeInLong)
Get the image loading time.
OCRTestResult
void * OCRTestResult
OCR inspection results handler.
Definition: OCRTestDLL.h:67
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
eSRInputImageType
eSRInputImageType
Data Type of input Image for example projects.
Definition: SRUtils.h:23
SR_DeleteOCRTestResult
eErrorType SR_DeleteOCRTestResult(OCRTestResult ocrTestResult)
Destruct a OCRTestResult.
SR_ApplyOCRTestFrom8bitImageData
eErrorType SR_ApplyOCRTestFrom8bitImageData(OCRTestHandle handle, const void *imageData, const int height, const int width, const int channels, OCRTestResult ocrTestResult)
Apply OCR algorithm to 1 or 3 or 4 channels image by 8-bit data.
SR_MakeOCRTestResult
OCRTestResult SR_MakeOCRTestResult()
Construct a OCRTestResult.
SR_PrintOCRTestParameters
eErrorType SR_PrintOCRTestParameters(OCRTestHandle handle)
Print a model parameters (inspection options...)
SR_CreateOCRTestHandleByWideCharPath
OCRTestHandle SR_CreateOCRTestHandleByWideCharPath(const wchar_t *ocrBoxFilePath, const wchar_t *ocrTextFilePath)
Create a OCRTestHandle via wide char path.
SR_GetOCRTestNumberOfTextBoxes
eErrorType SR_GetOCRTestNumberOfTextBoxes(OCRTestResult ocrTestResult, int *numberOfOCRTextBoxes)
Get the number of SR_OCRTextBox.
SR_ApplyOCRTestFromImageFile
eErrorType SR_ApplyOCRTestFromImageFile(OCRTestHandle handle, const char *imagePath, OCRTestResult ocrTestResult)
Apply OCR algorithm to image by file path.
OCRTestDLL.h
Header file for OCR Module.
eErrorType
eErrorType
Enumerator indicating error codes.
Definition: CommonHeaderDLL.h:25
SR_GetOCRTestTextBoxes
eErrorType SR_GetOCRTestTextBoxes(OCRTestResult ocrTestResult, SR_OCRTextBox *ocrTextBoxes)
Get SR_OCRTextBox s.
SRUtils.h
Header file of util functions for example projects.
OCRTestHandle
void * OCRTestHandle
OCR file handler.
Definition: OCRTestDLL.h:52
NO_ERROR_OUT
@ NO_ERROR_OUT
Definition: CommonHeaderDLL.h:26