Enable log information
This commit is contained in:
@@ -559,6 +559,130 @@ int ANSLPR_OD_Inferences_FileTest() {
|
||||
}
|
||||
|
||||
|
||||
// Run ALPR_OD inference over every image file in a given folder and log all
|
||||
// detections to the console. Mirrors ANSLPR_OD_Inferences_FileTest but batches
|
||||
// over a directory instead of a single image and does not open a display window.
|
||||
int ANSLPR_OD_FolderInferences_Test() {
|
||||
std::filesystem::path currentPath = std::filesystem::current_path();
|
||||
std::cout << "Current working directory: " << currentPath << std::endl;
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
ANSCENTER::ANSALPR* infHandle = nullptr;
|
||||
std::string licenseKey = "";
|
||||
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip";
|
||||
std::string folderPath = "E:\\Programs\\DemoAssets\\ImageSeries\\ALPR character M\\EventDebug_20260419_150142.997";
|
||||
|
||||
int engineType = 1;
|
||||
double detectionThreshold = 0.3;
|
||||
double ocrThreshold = 0.5;
|
||||
double colourThreshold = 0.5;
|
||||
|
||||
int result = CreateANSALPRHandle(&infHandle, licenseKey.c_str(), modelZipFile.c_str(), "",
|
||||
engineType, detectionThreshold, ocrThreshold, colourThreshold);
|
||||
std::cout << "Loading ANSLPR: " << result << std::endl;
|
||||
|
||||
auto startLoad = std::chrono::system_clock::now();
|
||||
int loadEngine = LoadANSALPREngineHandle(&infHandle);
|
||||
auto endLoad = std::chrono::system_clock::now();
|
||||
auto loadMs = std::chrono::duration_cast<std::chrono::milliseconds>(endLoad - startLoad).count();
|
||||
std::cout << "Init Result: " << loadEngine << " (engine load " << loadMs << " ms)" << std::endl;
|
||||
|
||||
if (!std::filesystem::exists(folderPath) || !std::filesystem::is_directory(folderPath)) {
|
||||
std::cout << "Folder does not exist or is not a directory: " << folderPath << std::endl;
|
||||
ReleaseANSALPRHandle(&infHandle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> imageFiles;
|
||||
const std::set<std::string> imageExts = { ".jpg", ".jpeg", ".png", ".bmp" };
|
||||
for (const auto& entry : std::filesystem::directory_iterator(folderPath)) {
|
||||
if (!entry.is_regular_file()) continue;
|
||||
std::string ext = entry.path().extension().string();
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
if (imageExts.count(ext)) imageFiles.push_back(entry.path());
|
||||
}
|
||||
std::sort(imageFiles.begin(), imageFiles.end());
|
||||
|
||||
std::cout << "Scanning folder: " << folderPath << std::endl;
|
||||
std::cout << "Found " << imageFiles.size() << " image(s)." << std::endl;
|
||||
|
||||
int totalDetections = 0;
|
||||
long long totalInferMs = 0;
|
||||
for (size_t i = 0; i < imageFiles.size(); ++i) {
|
||||
const auto& filePath = imageFiles[i];
|
||||
cv::Mat input = cv::imread(filePath.string(), cv::IMREAD_COLOR);
|
||||
if (input.empty()) {
|
||||
std::cout << "[" << (i + 1) << "/" << imageFiles.size() << "] "
|
||||
<< filePath.filename().string() << " <unreadable, skipped>" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string lpnResult;
|
||||
std::string jpegImage;
|
||||
cv::Mat* image = new cv::Mat(input);
|
||||
|
||||
auto t0 = std::chrono::system_clock::now();
|
||||
ANSALPR_RunInferenceComplete_CPP(&infHandle, &image, "MyCam", 0, 0, lpnResult, jpegImage);
|
||||
auto t1 = std::chrono::system_clock::now();
|
||||
auto inferMs = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
|
||||
totalInferMs += inferMs;
|
||||
|
||||
std::cout << "\n[" << (i + 1) << "/" << imageFiles.size() << "] "
|
||||
<< filePath.filename().string()
|
||||
<< " (" << input.cols << "x" << input.rows << ")"
|
||||
<< " infer=" << inferMs << "ms" << std::endl;
|
||||
std::cout << " Raw JSON: " << lpnResult << std::endl;
|
||||
|
||||
int numDetections = 0;
|
||||
if (!lpnResult.empty()) {
|
||||
try {
|
||||
pt.clear();
|
||||
std::stringstream ss;
|
||||
ss << lpnResult;
|
||||
boost::property_tree::read_json(ss, pt);
|
||||
auto resultsChild = pt.get_child_optional("results");
|
||||
if (resultsChild) {
|
||||
for (const auto& child : *resultsChild) {
|
||||
const auto& r = child.second;
|
||||
const auto class_id = GetData<int>(r, "class_id");
|
||||
const auto class_name = GetData<std::string>(r, "class_name");
|
||||
const auto x = GetData<float>(r, "x");
|
||||
const auto y = GetData<float>(r, "y");
|
||||
const auto width = GetData<float>(r, "width");
|
||||
const auto height = GetData<float>(r, "height");
|
||||
std::cout << " det[" << numDetections << "]"
|
||||
<< " id=" << class_id
|
||||
<< " name=" << class_name
|
||||
<< " bbox=(" << x << "," << y << ","
|
||||
<< width << "," << height << ")"
|
||||
<< std::endl;
|
||||
++numDetections;
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << " JSON parse error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << " => " << numDetections << " detection(s)" << std::endl;
|
||||
totalDetections += numDetections;
|
||||
delete image;
|
||||
}
|
||||
|
||||
std::cout << "\n=== ANSLPR_OD_FolderInferences_Test summary ===" << std::endl;
|
||||
std::cout << "Images processed : " << imageFiles.size() << std::endl;
|
||||
std::cout << "Total detections : " << totalDetections << std::endl;
|
||||
if (!imageFiles.empty()) {
|
||||
std::cout << "Avg inference : "
|
||||
<< (totalInferMs / static_cast<long long>(imageFiles.size())) << " ms"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
ReleaseANSALPRHandle(&infHandle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int ANSLPR_OD_INDOInferences_FileTest() {
|
||||
// Get the current working directory
|
||||
std::filesystem::path currentPath = std::filesystem::current_path();
|
||||
@@ -3955,7 +4079,8 @@ int main()
|
||||
SetConsoleCP(CP_UTF8);
|
||||
#endif
|
||||
// ANSLPR_OD_INDOInferences_FileTest();
|
||||
ANSLPR_OD_Inferences_FileTest();
|
||||
//ANSLPR_OD_Inferences_FileTest();
|
||||
ANSLPR_OD_FolderInferences_Test();
|
||||
//ANSLPR_OD_VideoTest();
|
||||
//ANSLPR_BigSize_VideoTest();
|
||||
//ANSLPR_CPU_VideoTest();
|
||||
|
||||
Reference in New Issue
Block a user