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

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

#include "OCRTestDLL.h"
#include "SRUtils.h"
/* define this variable when user want to use new score threshold other than preset values. */
//#define SR_USE_NEW_SCORE_THRESHOLD
/* define this variable when user want to use new area threshold other than preset values. */
//#define SR_USE_NEW_AREA_THRESHOLD
/* define this variable when user want to use new max inspection size. */
//#define SR_USE_NEW_MAX_INSPECTION_SIZE
/* define this variable when user want to limit the forward time of classfication algorithm. */
//#define SR_USE_FORWARD_TIME_LIMIT
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
* displayTime - 결과 이미지 display 하는 시간 (milliseconds)
* resizeFactor - 결과 이미지 display 하는 스케일 (display 크기 = 원본 크기 * resizeFactor)
***********************************************************/
int verbosity = 3;
int displayTime = 100;
double resizeFactor = 1.0;
/***********************************************************
* 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;
}
// Get the minimum score threshold for each class
int scoreThreshold = 0;
srStatus = SR_GetOCRTestScoreThreshold(handle, &scoreThreshold);
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;
}
// Get the minimum pixel-size threshold for each class
int areaThreshold = 0;
srStatus = SR_GetOCRTestAreaThreshold(handle, &areaThreshold);
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;
}
// Get the max inspection size
int maxInspectionWidth = 0, maxInspectionHeight = 0;
srStatus = SR_GetOCRTestMaxInspectionSize(handle, &maxInspectionHeight, &maxInspectionWidth);
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;
}
#ifdef SR_USE_NEW_SCORE_THRESHOLD
int newScoreThreshold = 50;
srStatus = SR_SetOCRTestScoreThreshold(handle, newScoreThreshold, true);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_NEW_AREA_THRESHOLD
int newAreaThreshold = 0;
srStatus = SR_SetOCRTestAreaThreshold(handle, newAreaThreshold, true);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_NEW_MAX_INSPECTION_SIZE
/*
학습에서 사용한 이미지 크기와 검사에서 사용하는 이미지 크기가 다를 경우 아래 함수를 호출하여
검사 이미지 사이즈에 맞게 최대 검사 사이즈를 설정하여야 합니다.
최대 검사 사이즈 보다 큰 이미지가 들어올 경우 최대 검사 사이즈로 리사이즈된 후 검사됩니다.
단 이 함수는 SR_LoadSegTestNetworkInGPU 함수를 호출하기 전 호출되어야 합니다.
SR_GetSegTestMaxInspectionSize 함수를 통해 현재 설정되어 있는 최대 검사 사이즈를 확인할 수 있습니다.
*/
int newMaxInspectionHeight = 1000;
int newMaxInspectionWidth = 1000;
srStatus = SR_SetOCRTestMaxInspectionSize(handle, newMaxInspectionHeight, newMaxInspectionWidth, true);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that SR_LoadOCRTestNetwork is already called.
return -1;
}
#endif
#ifdef SR_USE_FORWARD_TIME_LIMIT
float timeLimit = 1000; // milliseconds
srStatus = SR_SetOCRTestForwardTimeLimitVariables(handle, timeLimit, true, true);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that handle is not created.
return -1;
}
#endif
#ifndef SR_USE_FORWARD_TIME_LIMIT
srStatus = SR_SetOCRTestIsToUseForwardTimeLimit(handle, false, true);
if (srStatus != NO_ERROR_OUT)
{
PrintErrorType(srStatus, verbosity);
// if you get this error, it's possible that handle is not created.
return -1;
}
#endif
/***********************************************************
* 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.
if (verbosity >= 3)
{
DisplayFailedImageOnCVWindow(imagePaths[i_img], "FAIL TO OCR", "OCR Result", displayTime, resizeFactor);
}
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 and draw
cv::Mat image = cv::Mat();
if (verbosity >= 3)
{
if (inputImageType == eSRInputImageType::UCGrayImage)
{
image = cv::Mat(h, w, CV_8UC1, imageData);
image = image.clone(); /* to prevent modifying raw data */
} else if (inputImageType == eSRInputImageType::UCColorImage)
{
image = cv::Mat(h, w, CV_8UC3, imageData);
image = image.clone(); /* to prevent modifying raw data */
} else if (inputImageType == eSRInputImageType::UIColorImage)
{
image = cv::Mat(h, w, CV_8UC4, imageData);
image = image.clone(); /* to prevent modifying raw data */
} 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);
}
}
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());
}
// plot
if (verbosity >= 3)
{
cv::Scalar boxColor = cv::Scalar(0, 0, 255);
// draw rectangle
cv::RotatedRect rRect = cv::RotatedRect(cv::Point2f(x_c, y_c), cv::Size2f(width, height), angle);
cv::Point2f vertices[4];
rRect.points(vertices);
for (int j = 0; j < 4; j++)
cv::line(image, vertices[j], vertices[(j + 1) % 4], boxColor, 4);
cv::putText(image, text, cv::Point(x_c + width / 2, y_c), 2, 1., cv::Scalar(255, 255, 255), 2);
}
}
if (verbosity >= 3)
{
cv::namedWindow("OCR Result", cv::WINDOW_NORMAL);
cv::resizeWindow("OCR Result", image.size().width * resizeFactor, image.size().height * resizeFactor);
cv::imshow("OCR Result", image);
cv::waitKey(displayTime);
}
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_GetOCRTestMaxInspectionSize
eErrorType SR_GetOCRTestMaxInspectionSize(OCRTestHandle handle, int *maxInspectionHeight, int *maxInspectionWidth)
Get OCR's max inspection size.
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
SR_SetOCRTestAreaThreshold
eErrorType SR_SetOCRTestAreaThreshold(OCRTestHandle handle, int areaThreshold, bool writeOnModelFile=true)
Get OCR's area threshold.
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_SetOCRTestScoreThreshold
eErrorType SR_SetOCRTestScoreThreshold(OCRTestHandle handle, int scoreThreshold, bool writeOnModelFile=true)
Set OCR's score threshold.
SR_GetOCRTestAreaThreshold
eErrorType SR_GetOCRTestAreaThreshold(OCRTestHandle handle, int *areaThreshold)
Get OCR's area threshold.
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_GetOCRTestScoreThreshold
eErrorType SR_GetOCRTestScoreThreshold(OCRTestHandle handle, int *scoreThreshold)
Get OCR's score threshold.
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_SetOCRTestIsToUseForwardTimeLimit
eErrorType SR_SetOCRTestIsToUseForwardTimeLimit(OCRTestHandle handle, bool bIsToUseForwardTimeLimit, bool writeOnModelFile=true)
Set the boolean whether the current model is using time limit or not.
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.
SR_SetOCRTestForwardTimeLimitVariables
eErrorType SR_SetOCRTestForwardTimeLimitVariables(OCRTestHandle handle, float forwardTimeLimit, bool bIsToUseForwardTimeLimit, bool writeOnModelFile=true)
Set the Forward time limit variables.
OCRTestDLL.h
Header file for OCR Module.
eErrorType
eErrorType
Enumerator indicating error codes.
Definition: CommonHeaderDLL.h:25
DisplayFailedImageOnCVWindow
void DisplayFailedImageOnCVWindow(std::wstring &imagePath, std::string failPhrase, std::string windowName, int displayTime, const float resizeFactor)
Display failed image on opencv window for example projects.
Definition: SRUtils.h:273
SR_GetOCRTestTextBoxes
eErrorType SR_GetOCRTestTextBoxes(OCRTestResult ocrTestResult, SR_OCRTextBox *ocrTextBoxes)
Get SR_OCRTextBox s.
SRUtils.h
Header file of util functions for example projects.
SR_SetOCRTestMaxInspectionSize
eErrorType SR_SetOCRTestMaxInspectionSize(OCRTestHandle handle, int maxInspectionHeight, int maxInspectionWidth, bool writeOnModelFile=true)
Set OCR's max inspection size.
OCRTestHandle
void * OCRTestHandle
OCR file handler.
Definition: OCRTestDLL.h:52
NO_ERROR_OUT
@ NO_ERROR_OUT
Definition: CommonHeaderDLL.h:26
OpenImageFile
bool OpenImageFile(const std::wstring imagePath, cv::Mat &image, bool asCV8U=false)
Open image file for example projects.
Definition: SRUtils.h:74