This is an example of how to inspect sample images by using all the functions of OCR module. More details about this example.
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";
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";
void* imageData = nullptr;
int verbosity = 3;
int displayTime = 100;
double resizeFactor = 1.0;
int gpuIndex = 0;
if (handle == nullptr)
{
std::cout << "Saige Vision failed: Please check the file path of trained model." << std::endl;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
int scoreThreshold = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
int areaThreshold = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
int maxInspectionWidth = 0, maxInspectionHeight = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#ifdef SR_USE_NEW_SCORE_THRESHOLD
int newScoreThreshold = 50;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_NEW_AREA_THRESHOLD
int newAreaThreshold = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_NEW_MAX_INSPECTION_SIZE
int newMaxInspectionHeight = 1000;
int newMaxInspectionWidth = 1000;
{
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;
}
for (size_t i_img = 0; i_img < imagePaths.size(); i_img++)
{
int h = 0, w = 0;
if (inputImageType != eSRInputImageType::ImageFilePathMultiByteChar && inputImageType != eSRInputImageType::ImageFilePathWideChar)
{
}
if (inputImageType == eSRInputImageType::ImageFilePathMultiByteChar)
{
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;
}
} else if (inputImageType == eSRInputImageType::ImageFilePathWideChar)
{
} else if (inputImageType == eSRInputImageType::UCGrayImage)
{
} else if (inputImageType == eSRInputImageType::UCColorImage)
{
} else if (inputImageType == eSRInputImageType::UIColorImage)
{
}
{
PrintErrorType(srStatus, verbosity);
if (verbosity >= 3)
{
}
continue;
}
long inspectionTimeOutput = 0, loadImageTimeOutput = 0;
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
int nTextBoxes = 0;
{
PrintErrorType(srStatus, verbosity);
continue;
}
std::vector<SR_OCRTextBox> textBoxes(nTextBoxes);
if (nTextBoxes > 0)
{
{
PrintErrorType(srStatus, verbosity);
continue;
}
}
cv::Mat image = cv::Mat();
if (verbosity >= 3)
{
if (inputImageType == eSRInputImageType::UCGrayImage)
{
image = cv::Mat(h, w, CV_8UC1, imageData);
image = image.clone();
} else if (inputImageType == eSRInputImageType::UCColorImage)
{
image = cv::Mat(h, w, CV_8UC3, imageData);
image = image.clone();
} else if (inputImageType == eSRInputImageType::UIColorImage)
{
image = cv::Mat(h, w, CV_8UC4, imageData);
image = image.clone();
} else
{
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)
{
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 (verbosity >= 3)
{
cv::Scalar boxColor = cv::Scalar(0, 0, 255);
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;
}
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::cout << "SaigeVision successfully completed, please press enter key to terminate" << std::endl;
std::cin.get();
return 0;
}