This is an example of how to inspect sample images by using all the functions of Detection module. More details about this example.
int main()
{
std::wstring data_dir = L"../../../examples/data";
std::wstring trainedModelPath = data_dir + L"/models/lego_sample_model.srDet";
std::vector<std::wstring> imagePaths(5);
imagePaths[0] = data_dir + L"/images/Lego/sample1.png";
imagePaths[1] = data_dir + L"/images/Lego/sample2.png";
imagePaths[2] = data_dir + L"/images/Lego/sample3.png";
imagePaths[3] = data_dir + L"/images/Lego/sample4.png";
imagePaths[4] = data_dir + L"/images/Lego/sample5.png";
unsigned char* ucImageData = nullptr;
unsigned int* uiImageData = 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 numberOfClassesOutput = 0;
int maxStrLenOfClassName = 0;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
std::vector<std::string> classNames(numberOfClassesOutput);
std::vector<std::wstring> wClassNames(numberOfClassesOutput);
std::vector<int> classColorsOutput(numberOfClassesOutput);
std::vector<int> scoreThresholdOutput(numberOfClassesOutput);
std::vector<int> pixelSizeThresholdOutput(numberOfClassesOutput);
char* classNameOutput = new char[maxStrLenOfClassName];
wchar_t* wClassNameOutput = new wchar_t[maxStrLenOfClassName];
for (int i = 0; i < numberOfClassesOutput; i++)
{
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
classNames[i] = classNameOutput;
wClassNames[i] = wClassNameOutput;
}
delete[] classNameOutput;
delete[] wClassNameOutput;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#ifdef SR_USE_NEW_SCORE_THRESHOLD
std::vector<int> scoreThresholdInput(numberOfClassesOutput);
for (int i = 0; i < numberOfClassesOutput; i++)
{
scoreThresholdInput[i] = 50;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_NEW_PIXEL_SIZE_THRESHOLD
std::vector<int> pixelSizeThresholdInput(numberOfClassesOutput);
for (int i = 0; i < numberOfClassesOutput; i++)
{
pixelSizeThresholdInput[i] = 0;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_USE_FORWARD_TIME_LIMIT
float timeLimit = 1100;
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifndef SR_USE_FORWARD_TIME_LIMIT
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifdef SR_FILTER_OUTPUT_BY_MAX_NUMBER
std::vector<int> maxNumberOfDetectionBoxes(numberOfClassesOutput);
for (size_t i = 0; i < numberOfClassesOutput; i++)
{
maxNumberOfDetectionBoxes[i] = 5;
}
{
PrintErrorType(srStatus, verbosity);
return -1;
}
#endif
#ifndef SR_FILTER_OUTPUT_BY_MAX_NUMBER
std::vector<int> maxNumberOfDetectionBoxes(numberOfClassesOutput);
for (size_t i = 0; i < numberOfClassesOutput; i++)
{
maxNumberOfDetectionBoxes[i] = -1;
}
{
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::UIColorImage)
{
}
else
{
}
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;
}
int numberOfBoxesOutput = 0;
long inspectionTimeOutput = 0, loadImageTimeOutput = 0;
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
{
PrintErrorType(srStatus, verbosity);
continue;
}
std::vector<SR_DetectedBox> detectedBoxes(numberOfBoxesOutput);
{
PrintErrorType(srStatus, verbosity);
continue;
}
cv::Mat image = cv::Mat();
if (verbosity >= 3)
{
if (inputImageType == eSRInputImageType::UCGrayImage)
{
image = cv::Mat(h, w, CV_8UC1, ucImageData);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UCColorImage)
{
image = cv::Mat(h, w, CV_8UC3, ucImageData);
image = image.clone();
}
else if (inputImageType == eSRInputImageType::UIColorImage)
{
image = cv::Mat(h, w, CV_8UC4, uiImageData);
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 final detected boxes: " << numberOfBoxesOutput << std::endl;
#ifdef SR_FILTER_OUTPUT_BY_MAX_NUMBER
int* numberOfBoxesBeforeFiltered = new int[numberOfClassesOutput];
std::cout << " - Number of raw detected boxes before filtering: " << std::endl;
for (size_t i_c = 0; i_c < numberOfClassesOutput; i_c++)
{
std::cout << " - " << classNames[i_c] << ": " << numberOfBoxesBeforeFiltered[i_c] << std::endl;
}
delete[] numberOfBoxesBeforeFiltered;
#endif
std::cout << " - Image Loading time: " << loadImageTimeOutput << std::endl << std::endl;
std::cout << " - Inspection time: " << inspectionTimeOutput << std::endl;
std::cout << " - Detected boxes information" << std::endl;
}
for (int i = 0; i < numberOfBoxesOutput; i++)
{
int xmin = static_cast<int>(detectedBoxes[i].box.x_tl);
int ymin = static_cast<int>(detectedBoxes[i].box.y_tl);
int xmax = static_cast<int>(detectedBoxes[i].box.x_br);
int ymax = static_cast<int>(detectedBoxes[i].box.y_br);
if (verbosity >= 2)
{
printf(" - %d-th box info(label, score, x_topLeft, y_topLeft, x_bottomRight, y_bottomRight): %d, %0.2f, %d, %d, %d, %d\n",
i, detectedBoxes[i].box.classIdx, detectedBoxes[i].box.score, xmin, ymin, xmax, ymax);
}
if (verbosity >= 3)
{
int colorTmp = classColorsOutput[detectedBoxes[i].box.classIdx];
unsigned char r = (colorTmp >> 16) & 0xFF;
unsigned char g = (colorTmp >> 8) & 0xFF;
unsigned char b = (colorTmp) & 0xFF;
cv::Scalar classColor = cv::Scalar(b, g, r);
cv::Rect rect(xmin, ymin, xmax - xmin, ymax - ymin);
cv::rectangle(image, rect, classColor, 2, 8, 0);
cv::putText(image, classNames[detectedBoxes[i].box.classIdx], cv::Point(xmin, ymin), 2, 1.2, classColor, 3);
}
}
if (verbosity >= 3)
{
cv::namedWindow("Detection Result", cv::WINDOW_NORMAL);
cv::resizeWindow("Detection Result", image.size().width * resizeFactor, image.size().height * resizeFactor);
cv::imshow("Detection Result", image);
cv::waitKey(displayTime);
}
if (ucImageData != nullptr)
{
delete[] ucImageData;
}
if (uiImageData != nullptr)
{
delete[] uiImageData;
}
}
{
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;
}