5232 lines
231 KiB
C++
5232 lines
231 KiB
C++
#define NOMINMAX
|
|
#include <iostream>
|
|
#include <opencv2/imgcodecs.hpp>
|
|
#include <opencv2/opencv.hpp>
|
|
#include <opencv2/dnn.hpp>
|
|
#include "boost/property_tree/ptree.hpp"
|
|
#include "boost/property_tree/json_parser.hpp"
|
|
#include "boost/foreach.hpp"
|
|
#include "boost/optional.hpp"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <iostream>
|
|
#include <opencv2/dnn.hpp>
|
|
#include <opencv2/imgproc.hpp>
|
|
#include <opencv2/highgui.hpp>
|
|
#include <opencv2/objdetect.hpp>
|
|
#include "ANSLPR.h"
|
|
#include "ANSLPR_CPU.h"
|
|
#include "ANSODEngine.h"
|
|
#include "ANSOpenCV.h"
|
|
#include "ANSRTSP.h"
|
|
#include "ANSVideoPlayer.h"
|
|
#include "ANSFilePlayer.h"
|
|
#include <filesystem>
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <atomic>
|
|
#include <random>
|
|
#include <chrono>
|
|
#include <deque>
|
|
#include <set>
|
|
#include <map>
|
|
#include <cuda_runtime.h>
|
|
#include "EPLoader.h"
|
|
|
|
// Decode \\uXXXX (literal backslash-u-hex) sequences back to UTF-8.
|
|
// VectorDetectionToJsonString double-escapes Unicode for LabVIEW compatibility,
|
|
// so JSON strings contain literal "\u54c1" text instead of actual Unicode chars.
|
|
static std::string DecodeUnicodeEscapes(const std::string& input) {
|
|
std::string result;
|
|
result.reserve(input.size());
|
|
size_t i = 0;
|
|
while (i < input.size()) {
|
|
if (i + 5 < input.size() && input[i] == '\\' && input[i + 1] == 'u') {
|
|
// Parse 4 hex digits
|
|
std::string hex = input.substr(i + 2, 4);
|
|
char* end = nullptr;
|
|
uint32_t cp = static_cast<uint32_t>(strtoul(hex.c_str(), &end, 16));
|
|
if (end == hex.c_str() + 4) {
|
|
// Check for surrogate pair (\\uD800-DBFF followed by \\uDC00-DFFF)
|
|
if (cp >= 0xD800 && cp <= 0xDBFF && i + 11 < input.size()
|
|
&& input[i + 6] == '\\' && input[i + 7] == 'u') {
|
|
std::string hex2 = input.substr(i + 8, 4);
|
|
uint32_t cp2 = static_cast<uint32_t>(strtoul(hex2.c_str(), &end, 16));
|
|
if (end == hex2.c_str() + 4 && cp2 >= 0xDC00 && cp2 <= 0xDFFF) {
|
|
cp = 0x10000 + ((cp - 0xD800) << 10) + (cp2 - 0xDC00);
|
|
i += 12;
|
|
} else {
|
|
i += 6;
|
|
}
|
|
} else {
|
|
i += 6;
|
|
}
|
|
// Encode codepoint as UTF-8
|
|
if (cp < 0x80) {
|
|
result += static_cast<char>(cp);
|
|
} else if (cp < 0x800) {
|
|
result += static_cast<char>(0xC0 | (cp >> 6));
|
|
result += static_cast<char>(0x80 | (cp & 0x3F));
|
|
} else if (cp < 0x10000) {
|
|
result += static_cast<char>(0xE0 | (cp >> 12));
|
|
result += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
|
|
result += static_cast<char>(0x80 | (cp & 0x3F));
|
|
} else {
|
|
result += static_cast<char>(0xF0 | (cp >> 18));
|
|
result += static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
|
|
result += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
|
|
result += static_cast<char>(0x80 | (cp & 0x3F));
|
|
}
|
|
} else {
|
|
result += input[i++];
|
|
}
|
|
} else {
|
|
result += input[i++];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template<typename T>
|
|
T GetOptionalValue(const boost::property_tree::ptree& pt, std::string attribute, T defaultValue) {
|
|
if (pt.count(attribute)) {
|
|
return pt.get<T>(attribute);
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
template <typename T>
|
|
T GetData(const boost::property_tree::ptree& pt, const std::string& key)
|
|
{
|
|
T ret;
|
|
if (boost::optional<T> data = pt.get_optional<T>(key))
|
|
{
|
|
ret = data.get();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int ANSLPR_CPU_VideoTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle = new ANSCENTER::ANSALPR_CPU();
|
|
infHandle->SetALPRCheckerEnabled(true);
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = currentPath.string() + "\\ANS_GenericALPR_v1.0.zip";
|
|
modelZipFile = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericALPR_v1.0.zip";
|
|
std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\ALRP\\ALPR1.mp4";
|
|
std::string lpnResult;
|
|
bool result = infHandle->Initialize(licenseKey, modelZipFile, "",0.5, 0.5);
|
|
std::cout << "Loading ANSLRP:" << result << std::endl;
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
printf("could not read this video file...\n");
|
|
return -1;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) // if not success, break loop
|
|
{
|
|
std::cout << "\n Cannot read the video file. please check your video.\n";
|
|
break;
|
|
}
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
infHandle->Inference(frame, lpnResult);
|
|
std::string detectionResult = lpnResult;
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(frame, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(frame, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 0.6, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ANSLPR", frame);
|
|
if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
|
|
{
|
|
std::cout << "End of program faces.\n";
|
|
}
|
|
}
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
int ANSLPR_BigSize_VideoTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle = new ANSCENTER::ANSALPR_CPU();
|
|
infHandle->SetALPRCheckerEnabled(true);
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = currentPath.string() + "\\ANS_GenericALPR_v1.0.zip";
|
|
modelZipFile = "C:\\ProgramData\\ANSCENTER\\Shared\\ANS_GenericALPR_v1.0.zip";
|
|
std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\ALRP\\3725.mp4";
|
|
std::string lpnResult;
|
|
bool result = infHandle->Initialize(licenseKey, modelZipFile, "",0.5,0.5);
|
|
std::cout << "Loading ANSLRP:" << result << std::endl;
|
|
infHandle->LoadEngine();
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
printf("could not read this video file...\n");
|
|
return -1;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) // if not success, break loop
|
|
{
|
|
std::cout << "\n Cannot read the video file. please check your video.\n";
|
|
break;
|
|
}
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
infHandle->Inference(frame, lpnResult,"MyCam");
|
|
std::string detectionResult = lpnResult;
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(frame, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(frame, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 1.2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
cv::resize(frame, frame, cv::Size(frame.cols / 2, frame.rows / 2)); // to half size or even smaller
|
|
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ANSLPR", frame);
|
|
if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
|
|
{
|
|
std::cout << "End of program faces.\n";
|
|
}
|
|
}
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
}
|
|
|
|
std::string readJsonFile(const std::string& filePath) {
|
|
boost::property_tree::ptree pt;
|
|
boost::property_tree::read_json(filePath, pt);
|
|
|
|
std::ostringstream oss;
|
|
boost::property_tree::write_json(oss, pt, false);
|
|
|
|
return oss.str();
|
|
}
|
|
unsigned char* CVMatToBytes(cv::Mat image, unsigned int& bufferLengh)
|
|
{
|
|
int size = int(image.total() * image.elemSize());
|
|
std::cout << "size:" << size << std::endl;
|
|
unsigned char* bytes = new unsigned char[size]; // you will have to delete[] that later
|
|
std::memcpy(bytes, image.data, size * sizeof(unsigned char));
|
|
bufferLengh = size * sizeof(unsigned char);
|
|
return bytes;
|
|
}
|
|
int ANSLPR_CPU_Inferences_FileTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle;
|
|
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_GenericALPR_v1.0.zip";
|
|
std::string imageFilePath = "C:\\Projects\\ANSVIS\\Documentation\\TestImages\\ALPR\\LP1.jpg";
|
|
std::string StrBox = readJsonFile("C:\\Projects\\ANLS\\Documents\\bboxStr.json");
|
|
|
|
int result = CreateANSALPRHandle(& infHandle, "", modelZipFile.c_str(), "",0,0.5,0.5,0);
|
|
std::cout << "Init Result:" << result << std::endl;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
unsigned int bufferLength = 0;
|
|
|
|
cv::Mat input = cv::imread(imageFilePath, cv::IMREAD_COLOR);
|
|
cv::Mat frame = input;
|
|
unsigned char* jpeg_string = CVMatToBytes(frame, bufferLength);
|
|
int height = frame.rows;
|
|
int width = frame.cols;
|
|
auto start = std::chrono::system_clock::now();
|
|
std::string detectionResult = ANSALPR_RunInferenceBinaryInCroppedImages(&infHandle, jpeg_string, width, height, StrBox.c_str());
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(frame, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(frame, cv::format("%s:%d", class_name, class_id), cv::Point(x, y - 5),
|
|
0, 0.6, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
//cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
//cv::imshow("ANSLPR", frame);
|
|
//cv::waitKey(0);
|
|
//cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
}
|
|
int ANSLPR_CV_VideoTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle;
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = currentPath.string() + "\\ANS_GenericALPR_v1.0.zip";
|
|
modelZipFile = "C:\\Programs\\DemoAssets\\ModelsForANSVIS\\ANS_GenericALPR_v1.1.zip";
|
|
std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\ALRP\\3725.mp4";
|
|
std::string lpnResult;
|
|
int result = CreateANSALPRHandle(& infHandle, licenseKey.c_str(), modelZipFile.c_str(), "",0,0.5,0.5,0);
|
|
std::cout << "Loading ANSLRP:" << result << std::endl;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
printf("could not read this video file...\n");
|
|
return -1;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) // if not success, break loop
|
|
{
|
|
std::cout << "\n Cannot read the video file. please check your video.\n";
|
|
break;
|
|
}
|
|
auto start = std::chrono::system_clock::now();
|
|
std::string jpegImage;
|
|
|
|
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
|
|
image = new cv::Mat(frame); // ✅ Allocate the image
|
|
ANSALPR_RunInferenceComplete_CPP(& infHandle, &image, "MyCam", 0, 0, lpnResult, jpegImage);
|
|
std::string detectionResult = lpnResult;
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(frame, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(frame, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 1.2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
cv::resize(frame, frame, cv::Size(1920,1080)); // to half size or even smaller
|
|
delete image;
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ANSLPR", frame);
|
|
if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
|
|
{
|
|
std::cout << "End of program faces.\n";
|
|
}
|
|
}
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
|
|
}
|
|
int ANSLPR_OD_VideoTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle;
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile ="C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";// "C:\\Projects\\ANSVIS\\Models\\ANS_ALPR_v1.2.zip";// "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip";
|
|
|
|
std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\day.mp4";//
|
|
std::string lpnResult;
|
|
int engineType = 1;
|
|
double detectionThreshold = 0.5;
|
|
double ocrThreshold = 0.5;
|
|
double detectionColourThreshold = 0.5;
|
|
int result = CreateANSALPRHandle(&infHandle, licenseKey.c_str(), modelZipFile.c_str(), "", engineType, detectionThreshold, ocrThreshold, detectionColourThreshold);
|
|
std::cout << "Loading ANSLRP:" << result << std::endl;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
int loadEngine = LoadANSALPREngineHandle(&infHandle);
|
|
std::cout << "Loading ANSLRP:" << loadEngine << std::endl;
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
printf("could not read this video file...\n");
|
|
return -1;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) // if not success, break loop
|
|
{
|
|
std::cout << "\n Cannot read the video file. please check your video.\n";
|
|
break;
|
|
}
|
|
auto start = std::chrono::system_clock::now();
|
|
std::string jpegImage;
|
|
|
|
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
|
|
image = new cv::Mat(frame); // ✅ Allocate the image
|
|
ANSALPR_RunInferenceComplete_CPP(&infHandle, &image, "MyCam", 0, 0, lpnResult, jpegImage);
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
|
|
std::string detectionResult = lpnResult;
|
|
//std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(frame, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(frame, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 1.2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
|
|
cv::resize(frame, frame, cv::Size(1920, 1080)); // to half size or even smaller
|
|
delete image;
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ANSLPR", frame);
|
|
if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
|
|
{
|
|
std::cout << "End of program faces.\n";
|
|
}
|
|
}
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
|
|
}
|
|
|
|
int ANSLPR_OD_Inferences_FileTest() {
|
|
// Get the current working directory
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle;
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip";
|
|
std::string imageFilePath = "E:\\Programs\\DemoAssets\\Images\\864.jpg";//;"E:\\Programs\\DemoAssets\\Images\\ALPRTest\\WrongOrder\\1109.jpg";//20250912_213850.717.jpg; 20250912_213850.511.jpg;//20250912_213850.411.jpg;//20250912_213850.261.jpg(65H115912:0.73) cororect (20250912_213850.071.jpg: 65H115833)
|
|
std::string lpnResult;
|
|
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 ANSLRP:" << result << std::endl;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
int loadEngine = LoadANSALPREngineHandle(&infHandle);
|
|
std::cout << "Init Result:" << result << std::endl;
|
|
auto end = std::chrono::system_clock::now();
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
printf("Time to load engine = %lld ms\n", static_cast<long long int>(elapsed.count()));
|
|
unsigned int bufferLength = 0;
|
|
std::string jpegImage;
|
|
|
|
cv::Mat input = cv::imread(imageFilePath, cv::IMREAD_COLOR);
|
|
|
|
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
|
|
image = new cv::Mat(input); // ✅ Allocate the image
|
|
ANSALPR_RunInferenceComplete_CPP(&infHandle, &image, "MyCam", 0, 0, lpnResult, jpegImage);
|
|
std::string detectionResult = lpnResult;
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(input, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(input, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 1.2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
delete image;
|
|
cv::resize(input, input, cv::Size(1920, 1080)); // to half size or even smaller
|
|
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
|
|
cv::imshow("ANSLPR", input);
|
|
|
|
cv::waitKey(0);
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return 0;
|
|
}
|
|
|
|
|
|
// 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:\\Projects\\ANSVIS\\Models\\ANS_ALPR_v1.3.zip";//""C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip";//
|
|
std::string folderPath = "E:\\Programs\\DemoAssets\\ImageSeries\\ALPR character M";
|
|
|
|
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;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
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::recursive_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 (recursive): " << 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();
|
|
// Print the current working directory
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
boost::property_tree::ptree root;
|
|
boost::property_tree::ptree detectionObjects;
|
|
boost::property_tree::ptree pt;
|
|
ANSCENTER::ANSALPR* infHandle;
|
|
std::string licenseKey = "";
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.4_NVIDIAGeForceRTX4070LaptopGPU.zip";//"E:\\Programs\\DemoAssets\\ModelsForANSVIS\\ANS_ALPR_IND_v1.1.zip";
|
|
std::string imageFilePath = "E:\\Programs\\DemoAssets\\Images\\ALPRTest\\alpr_loi\\702.jpg";//"E:\\Programs\\TrainingWorkingStation\\IndoALPR\\Indonesian License Plate Dataset\\data\\train075.jpg";//20250912_213850.717.jpg; 20250912_213850.511.jpg;//20250912_213850.411.jpg;//20250912_213850.261.jpg(65H115912:0.73) cororect (20250912_213850.071.jpg: 65H115833)
|
|
|
|
//"E:\\Programs\\DemoAssets\\Images\\ALPRTest\\WrongOrder\\4389.jpg";///
|
|
std::string lpnResult;
|
|
int engineType = 1;
|
|
double detectionThreshold = 0.3;
|
|
double ocrThreshold = 0.5;
|
|
int result = CreateANSALPRHandle(&infHandle, licenseKey.c_str(), modelZipFile.c_str(), "", engineType, detectionThreshold, ocrThreshold, 0.5);
|
|
std::cout << "Loading ANSLRP:" << result << std::endl;
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
int loadEngine = LoadANSALPREngineHandle(&infHandle);
|
|
std::cout << "Init Result:" << result << std::endl;
|
|
unsigned int bufferLength = 0;
|
|
std::string jpegImage;
|
|
|
|
cv::Mat input = cv::imread(imageFilePath, cv::IMREAD_COLOR);
|
|
|
|
cv::Mat* image = nullptr; // ✅ Use a pointer to hold the allocated image
|
|
image = new cv::Mat(input); // ✅ Allocate the image
|
|
ANSALPR_RunInferenceComplete_CPP(&infHandle, &image, "MyCam", 0, 0, lpnResult, jpegImage);
|
|
std::string detectionResult = lpnResult;
|
|
std::cout << "Result:" << detectionResult;
|
|
if (!detectionResult.empty()) {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss.clear();
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
|
{
|
|
const boost::property_tree::ptree& result = child.second;
|
|
const auto class_id = GetData<int>(result, "class_id");
|
|
const auto class_name = GetData<std::string>(result, "class_name");
|
|
const auto x = GetData<float>(result, "x");
|
|
const auto y = GetData<float>(result, "y");
|
|
const auto width = GetData<float>(result, "width");
|
|
const auto height = GetData<float>(result, "height");
|
|
cv::rectangle(input, cv::Rect(x, y, width, height), 123, 2);
|
|
cv::putText(input, cv::format("%s", class_name), cv::Point(x, y - 5),
|
|
0, 1.2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::system_clock::now();
|
|
delete image;
|
|
cv::resize(input, input, cv::Size(1920, 1080)); // to half size or even smaller
|
|
cv::namedWindow("ANSLPR", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ANSLPR", input);
|
|
cv::waitKey(0);
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return 0;
|
|
}
|
|
// ============================================================================
|
|
// Multi-GPU ALPR Stress Test — 4 parallel RTSP→ALPR tasks
|
|
//
|
|
// Purpose: Diagnose why dual RTX 5080 performs worse than single RTX 3050.
|
|
// Each task has its own RTSP reader + ALPR engine. Tasks 0-1 read stream A,
|
|
// tasks 2-3 read stream B. All 4 run in parallel threads.
|
|
//
|
|
// The display composites all 4 views into a single resizable window with a
|
|
// log panel at the bottom showing per-task stats and GPU diagnostics.
|
|
// ============================================================================
|
|
|
|
// Thread-safe logger: collects timestamped messages for on-screen log + file
|
|
static const char* LOG_FILE_PATH = "C:\\Temp\\ALPRdebug.txt";
|
|
|
|
class ThreadSafeLog {
|
|
public:
|
|
void init() {
|
|
std::lock_guard<std::mutex> lk(m_mtx);
|
|
m_file.open(LOG_FILE_PATH, std::ios::out | std::ios::trunc);
|
|
if (m_file.is_open()) {
|
|
auto now = std::chrono::system_clock::now();
|
|
auto t = std::chrono::system_clock::to_time_t(now);
|
|
char timeBuf[64];
|
|
struct tm lt;
|
|
localtime_s(<, &t);
|
|
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", <);
|
|
m_file << "================================================================\n";
|
|
m_file << " ANSLPR Multi-GPU Stress Test Debug Log\n";
|
|
m_file << " Started: " << timeBuf << "\n";
|
|
m_file << " Log file: " << LOG_FILE_PATH << "\n";
|
|
m_file << "================================================================\n\n";
|
|
m_file.flush();
|
|
}
|
|
}
|
|
void add(const std::string& msg) {
|
|
std::lock_guard<std::mutex> lk(m_mtx);
|
|
// Full timestamp for file: HH:MM:SS.mmm
|
|
auto now = std::chrono::system_clock::now();
|
|
auto t = std::chrono::system_clock::to_time_t(now);
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
now.time_since_epoch()).count() % 1000;
|
|
struct tm lt;
|
|
localtime_s(<, &t);
|
|
char ts[32];
|
|
snprintf(ts, sizeof(ts), "[%02d:%02d:%02d.%03lld] ",
|
|
lt.tm_hour, lt.tm_min, lt.tm_sec, static_cast<long long>(ms));
|
|
std::string line = std::string(ts) + msg;
|
|
m_lines.push_back(line);
|
|
if (m_lines.size() > 200) m_lines.pop_front();
|
|
// Write to file immediately (flush so user can read mid-run)
|
|
if (m_file.is_open()) {
|
|
m_file << line << "\n";
|
|
m_file.flush();
|
|
}
|
|
}
|
|
std::deque<std::string> getRecent(size_t n) {
|
|
std::lock_guard<std::mutex> lk(m_mtx);
|
|
size_t start = (m_lines.size() > n) ? m_lines.size() - n : 0;
|
|
return std::deque<std::string>(m_lines.begin() + start, m_lines.end());
|
|
}
|
|
void close() {
|
|
std::lock_guard<std::mutex> lk(m_mtx);
|
|
if (m_file.is_open()) m_file.close();
|
|
}
|
|
private:
|
|
std::mutex m_mtx;
|
|
std::deque<std::string> m_lines;
|
|
std::ofstream m_file;
|
|
};
|
|
|
|
// Per-task shared state (written by worker thread, read by display thread)
|
|
struct TaskState {
|
|
std::mutex mtx;
|
|
cv::Mat displayFrame; // latest frame with detections drawn
|
|
double fps = 0.0;
|
|
double inferenceMs = 0.0;
|
|
int frameCount = 0;
|
|
int detectionCount= 0;
|
|
std::string lastPlate;
|
|
bool engineLoaded = false;
|
|
bool streamOk = false;
|
|
std::string statusMsg = "Initializing...";
|
|
// GPU resource tracking (set during engine load)
|
|
int gpuDeviceId = -1; // which GPU this task's engine landed on
|
|
size_t vramUsedBytes = 0; // VRAM consumed by this task's engine
|
|
// Grab/Inference timing (updated by worker thread)
|
|
double lastGrabMs = 0.0;
|
|
double lastInfMs = 0.0;
|
|
};
|
|
|
|
// Snapshot of GPU state for real-time monitoring
|
|
struct GpuSnapshot {
|
|
int deviceId = 0;
|
|
std::string name;
|
|
size_t totalMiB = 0;
|
|
size_t freeMiB = 0;
|
|
size_t usedMiB = 0;
|
|
};
|
|
|
|
// Safe check: is CUDA runtime available? (prevents crash on CPU-only PCs)
|
|
static bool IsCudaAvailable() {
|
|
static int cached = -1;
|
|
if (cached < 0) {
|
|
HMODULE h = LoadLibraryA("nvcuda.dll");
|
|
cached = (h != nullptr) ? 1 : 0;
|
|
if (h) FreeLibrary(h);
|
|
}
|
|
return cached == 1;
|
|
}
|
|
|
|
// Query current GPU VRAM usage for all devices
|
|
static std::vector<GpuSnapshot> QueryGpuVram() {
|
|
std::vector<GpuSnapshot> snapshots;
|
|
if (!IsCudaAvailable()) return snapshots;
|
|
int deviceCount = 0;
|
|
if (cudaGetDeviceCount(&deviceCount) != cudaSuccess) return snapshots;
|
|
for (int i = 0; i < deviceCount; i++) {
|
|
cudaDeviceProp prop;
|
|
cudaGetDeviceProperties(&prop, i);
|
|
int prevDevice;
|
|
cudaGetDevice(&prevDevice);
|
|
cudaSetDevice(i);
|
|
size_t freeMem = 0, totalMem = 0;
|
|
cudaMemGetInfo(&freeMem, &totalMem);
|
|
cudaSetDevice(prevDevice);
|
|
|
|
GpuSnapshot s;
|
|
s.deviceId = i;
|
|
s.name = prop.name;
|
|
s.totalMiB = totalMem / (1024 * 1024);
|
|
s.freeMiB = freeMem / (1024 * 1024);
|
|
s.usedMiB = s.totalMiB - s.freeMiB;
|
|
snapshots.push_back(s);
|
|
}
|
|
return snapshots;
|
|
}
|
|
|
|
// Measure per-GPU free VRAM (returns array indexed by device)
|
|
static std::vector<size_t> GetPerGpuFreeMiB() {
|
|
std::vector<size_t> result;
|
|
if (!IsCudaAvailable()) return result;
|
|
int deviceCount = 0;
|
|
if (cudaGetDeviceCount(&deviceCount) != cudaSuccess) return result;
|
|
int prevDevice;
|
|
cudaGetDevice(&prevDevice);
|
|
for (int i = 0; i < deviceCount; i++) {
|
|
cudaSetDevice(i);
|
|
size_t freeMem = 0, totalMem = 0;
|
|
cudaMemGetInfo(&freeMem, &totalMem);
|
|
result.push_back(freeMem / (1024 * 1024));
|
|
}
|
|
cudaSetDevice(prevDevice);
|
|
return result;
|
|
}
|
|
|
|
static std::atomic<bool> g_running{true};
|
|
static ThreadSafeLog g_log;
|
|
|
|
// Log GPU info using CUDA runtime
|
|
static void LogGpuInfo() {
|
|
if (!IsCudaAvailable()) {
|
|
g_log.add("No NVIDIA GPU detected — running in CPU mode");
|
|
printf("[GPU] No NVIDIA GPU detected — running in CPU mode\n");
|
|
return;
|
|
}
|
|
int deviceCount = 0;
|
|
cudaError_t err = cudaGetDeviceCount(&deviceCount);
|
|
if (err != cudaSuccess) {
|
|
g_log.add("CUDA ERROR: cudaGetDeviceCount failed: " + std::string(cudaGetErrorString(err)));
|
|
printf("[GPU] CUDA ERROR: %s\n", cudaGetErrorString(err));
|
|
return;
|
|
}
|
|
printf("============================================================\n");
|
|
printf(" GPU DEVICE REPORT — %d device(s) detected\n", deviceCount);
|
|
printf("============================================================\n");
|
|
g_log.add("GPU DEVICE REPORT: " + std::to_string(deviceCount) + " device(s)");
|
|
|
|
for (int i = 0; i < deviceCount; i++) {
|
|
cudaDeviceProp prop;
|
|
cudaGetDeviceProperties(&prop, i);
|
|
size_t freeMem = 0, totalMem = 0;
|
|
cudaSetDevice(i);
|
|
cudaMemGetInfo(&freeMem, &totalMem);
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
" GPU[%d] %s | SM %d.%d | VRAM: %.0f MiB total, %.0f MiB free",
|
|
i, prop.name, prop.major, prop.minor,
|
|
totalMem / 1048576.0, freeMem / 1048576.0);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
" GPU[%d] PCIe Bus %d, Device %d | Async Engines: %d | Concurrent Kernels: %d",
|
|
i, prop.pciBusID, prop.pciDeviceID,
|
|
prop.asyncEngineCount, prop.concurrentKernels);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
printf("============================================================\n");
|
|
}
|
|
|
|
// Global inference mutex: serializes inference on non-NVIDIA GPUs (DirectML/OpenVINO).
|
|
// DirectML is not thread-safe when multiple ORT sessions run concurrently on the
|
|
// same integrated GPU — causes access violations on 4K frames.
|
|
// On NVIDIA, each task has its own CUDA context so no serialization needed.
|
|
static std::mutex g_inferenceMutex;
|
|
|
|
// Worker thread: reads RTSP frames and runs ALPR inference
|
|
// RTSP client and ALPR engine are pre-created on the main thread to avoid
|
|
// race conditions in CreateANSRTSPHandle / CreateANSALPRHandle.
|
|
// Takes rtspClientPtr (pointer to array slot) + streamGuard mutex so the
|
|
// CHAOS thread can safely destroy+recreate the stream without use-after-free.
|
|
static void ALPRWorkerThread(int taskId,
|
|
ANSCENTER::ANSRTSPClient** rtspClientPtr,
|
|
std::mutex* streamGuard,
|
|
ANSCENTER::ANSALPR* alprHandle,
|
|
TaskState& state) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", taskId);
|
|
std::string prefix(tag);
|
|
|
|
g_log.add(prefix + " Worker thread started");
|
|
printf("%s Worker thread started\n", tag);
|
|
|
|
// --- Main inference loop ---
|
|
int width = 0, height = 0;
|
|
int64_t pts = 0;
|
|
int emptyFrames = 0;
|
|
std::string cameraId = "Cam" + std::to_string(taskId);
|
|
|
|
// FPS tracking with sliding window
|
|
std::deque<std::chrono::steady_clock::time_point> fpsTimestamps;
|
|
|
|
// Timing accumulators for periodic benchmarking
|
|
double totalGrabMs = 0, totalInfMs = 0;
|
|
int grabCount = 0, infCount = 0;
|
|
double maxGrabMs = 0, maxInfMs = 0;
|
|
auto benchStart = std::chrono::steady_clock::now();
|
|
|
|
bool hwDecodeLogged = false;
|
|
|
|
while (g_running.load()) {
|
|
// Lock the stream guard to prevent CHAOS from destroying the client
|
|
// while we're mid-frame-grab or mid-inference.
|
|
std::unique_lock<std::mutex> streamLock(*streamGuard);
|
|
|
|
// Re-read the client pointer each iteration — CHAOS may have
|
|
// destroyed+recreated it, so our old pointer could be dangling.
|
|
ANSCENTER::ANSRTSPClient* rtspClient = *rtspClientPtr;
|
|
if (rtspClient == nullptr) {
|
|
streamLock.unlock();
|
|
emptyFrames++;
|
|
if (emptyFrames % 100 == 1) {
|
|
g_log.add(prefix + " Stream destroyed by CHAOS, waiting... (count=" + std::to_string(emptyFrames) + ")");
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
continue;
|
|
}
|
|
|
|
// Read frame from RTSP via ANSCV
|
|
auto grabStart = std::chrono::steady_clock::now();
|
|
cv::Mat* framePtr = nullptr;
|
|
GetRTSPCVImage(&rtspClient, width, height, pts, &framePtr);
|
|
auto grabEnd = std::chrono::steady_clock::now();
|
|
double grabMs = std::chrono::duration<double, std::milli>(grabEnd - grabStart).count();
|
|
|
|
if (framePtr == nullptr || framePtr->empty()) {
|
|
emptyFrames++;
|
|
if (emptyFrames % 100 == 1) {
|
|
g_log.add(prefix + " Empty frame (count=" + std::to_string(emptyFrames) + ")");
|
|
}
|
|
if (emptyFrames > 300) {
|
|
g_log.add(prefix + " Too many empty frames (reconnect disabled for long test)");
|
|
// ReconnectRTSP(&rtspClient); // Disabled for VRAM stability testing
|
|
emptyFrames = 0;
|
|
}
|
|
streamLock.unlock();
|
|
if (framePtr) delete framePtr;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
emptyFrames = 0;
|
|
|
|
// Log HW decode status once after first successful frame
|
|
if (!hwDecodeLogged) {
|
|
hwDecodeLogged = true;
|
|
int hwActive = rtspClient->IsHWDecodingActive() ? 1 : 0;
|
|
bool isCuda = rtspClient->IsCudaHWAccel();
|
|
int hwGpu = rtspClient->GetHWDecodingGpuIndex();
|
|
char hwBuf[256];
|
|
const char* hwType = !hwActive ? "INACTIVE (software/CPU)"
|
|
: isCuda ? "ACTIVE (CUDA/NVDEC zero-copy)"
|
|
: "ACTIVE (D3D11VA/NVDEC cpu-nv12)";
|
|
snprintf(hwBuf, sizeof(hwBuf), "%s HW Decode: %s (GPU index: %d)",
|
|
tag, hwType, hwGpu);
|
|
g_log.add(hwBuf);
|
|
printf("%s\n", hwBuf);
|
|
}
|
|
totalGrabMs += grabMs;
|
|
grabCount++;
|
|
if (grabMs > maxGrabMs) maxGrabMs = grabMs;
|
|
|
|
// Run ALPR inference
|
|
bool isNvidia = (ANSCENTER::EPLoader::Current().type == ANSCENTER::EngineType::NVIDIA_GPU);
|
|
fprintf(stderr, "[Worker T%d] frame %d: calling inference %dx%d...\n",
|
|
taskId, state.frameCount + 1, framePtr->cols, framePtr->rows);
|
|
auto infStart = std::chrono::steady_clock::now();
|
|
std::string lpnResult, jpegImage;
|
|
{
|
|
std::unique_lock<std::mutex> infLock(g_inferenceMutex, std::defer_lock);
|
|
if (!isNvidia) infLock.lock();
|
|
ANSALPR_RunInferenceComplete_CPP(&alprHandle, &framePtr, cameraId.c_str(), 0, 0, lpnResult, jpegImage);
|
|
}
|
|
fprintf(stderr, "[Worker T%d] frame %d: inference done, result len=%zu\n",
|
|
taskId, state.frameCount + 1, lpnResult.size());
|
|
|
|
// Release stream lock — inference is done, CHAOS can now safely destroy.
|
|
streamLock.unlock();
|
|
auto infEnd = std::chrono::steady_clock::now();
|
|
double infMs = std::chrono::duration<double, std::milli>(infEnd - infStart).count();
|
|
totalInfMs += infMs;
|
|
infCount++;
|
|
if (infMs > maxInfMs) maxInfMs = infMs;
|
|
|
|
// Parse detections and draw on frame
|
|
cv::Mat display = framePtr->clone();
|
|
int detCount = 0;
|
|
std::string lastPlateText;
|
|
|
|
if (!lpnResult.empty()) {
|
|
try {
|
|
boost::property_tree::ptree pt;
|
|
std::stringstream ss(lpnResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& det = child.second;
|
|
const auto class_name = GetData<std::string>(det, "class_name");
|
|
const auto x = GetData<float>(det, "x");
|
|
const auto y = GetData<float>(det, "y");
|
|
const auto w = GetData<float>(det, "width");
|
|
const auto h = GetData<float>(det, "height");
|
|
cv::rectangle(display, cv::Rect((int)x, (int)y, (int)w, (int)h),
|
|
cv::Scalar(0, 255, 0), 2);
|
|
cv::putText(display, class_name, cv::Point((int)x, (int)y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2);
|
|
lastPlateText = class_name;
|
|
detCount++;
|
|
}
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
// Update FPS (sliding window over last 2 seconds)
|
|
auto now = std::chrono::steady_clock::now();
|
|
fpsTimestamps.push_back(now);
|
|
while (!fpsTimestamps.empty() &&
|
|
std::chrono::duration<double>(now - fpsTimestamps.front()).count() > 2.0) {
|
|
fpsTimestamps.pop_front();
|
|
}
|
|
double fps = fpsTimestamps.size() / 2.0;
|
|
|
|
// Draw OSD on frame
|
|
char osd[128];
|
|
snprintf(osd, sizeof(osd), "Task%d | %.1f FPS | Inf: %.0f ms | #%d",
|
|
taskId, fps, infMs, state.frameCount + 1);
|
|
cv::putText(display, osd, cv::Point(10, 30),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 2);
|
|
|
|
// Update shared state
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
state.displayFrame = display;
|
|
state.fps = fps;
|
|
state.inferenceMs = infMs;
|
|
state.lastGrabMs = grabMs;
|
|
state.lastInfMs = infMs;
|
|
state.frameCount++;
|
|
state.detectionCount += detCount;
|
|
if (!lastPlateText.empty()) state.lastPlate = lastPlateText;
|
|
}
|
|
|
|
// Periodic logging (every 100 frames)
|
|
if ((state.frameCount % 100) == 0) {
|
|
double avgGrab = grabCount > 0 ? totalGrabMs / grabCount : 0;
|
|
double avgInf = infCount > 0 ? totalInfMs / infCount : 0;
|
|
double elapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - benchStart).count();
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
"%s Frame %d | FPS=%.1f | Grab: avg=%.1fms max=%.0fms | Inf: avg=%.1fms max=%.0fms | "
|
|
"GrabPct=%.0f%% InfPct=%.0f%% | Det=%d",
|
|
tag, state.frameCount, fps,
|
|
avgGrab, maxGrabMs,
|
|
avgInf, maxInfMs,
|
|
(totalGrabMs / (elapsed * 1000.0)) * 100.0,
|
|
(totalInfMs / (elapsed * 1000.0)) * 100.0,
|
|
state.detectionCount);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
|
|
// Reset accumulators
|
|
totalGrabMs = totalInfMs = 0;
|
|
maxGrabMs = maxInfMs = 0;
|
|
grabCount = infCount = 0;
|
|
benchStart = std::chrono::steady_clock::now();
|
|
}
|
|
|
|
delete framePtr;
|
|
}
|
|
|
|
g_log.add(prefix + " Worker loop exited");
|
|
}
|
|
|
|
// =============================================================================
|
|
// ANSLPR_SingleTask_Test — 1 stream, 1 AI task. For isolating DirectML/ORT
|
|
// issues on non-NVIDIA GPUs. If this works but 2-task crashes, it's concurrency.
|
|
// =============================================================================
|
|
int ANSLPR_SingleTask_Test() {
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
g_log.init();
|
|
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ANSLPR Single-Task Test — 1 Stream, 1 AI Task\n");
|
|
printf(" Press ESC to stop\n");
|
|
printf(" Log file: %s\n", LOG_FILE_PATH);
|
|
printf("============================================================\n\n");
|
|
|
|
g_log.add("============================================================");
|
|
g_log.add(" ANSLPR Single-Task Test — 1 Stream, 1 AI Task");
|
|
g_log.add("============================================================");
|
|
|
|
const std::string streamUrl = "rtsp://admin:admin123@103.156.0.133:8010/cam/realmonitor?channel=1&subtype=0";
|
|
g_log.add("Stream: " + streamUrl);
|
|
|
|
// --- Create RTSP client ---
|
|
ANSCENTER::ANSRTSPClient* rtspClient = nullptr;
|
|
printf("[Stream0] Creating RTSP handle...\n");
|
|
int rtspResult = CreateANSRTSPHandle(&rtspClient, "", "", "", streamUrl.c_str());
|
|
if (rtspResult != 1 || rtspClient == nullptr) {
|
|
printf("[Stream0] FAILED to create RTSP handle\n");
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -1;
|
|
}
|
|
SetRTSPImageQuality(&rtspClient, 0);
|
|
SetRTSPHWDecoding(&rtspClient, -1); // Force software decoding
|
|
StartRTSP(&rtspClient);
|
|
g_log.add("[Stream0] RTSP started (software decode)");
|
|
|
|
// --- Create single ALPR engine ---
|
|
ANSCENTER::ANSALPR* alprHandle = nullptr;
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
printf("[Task0] Creating ALPR handle...\n");
|
|
auto engineStart = std::chrono::steady_clock::now();
|
|
int createResult = CreateANSALPRHandle(&alprHandle, "", modelZipFile.c_str(), "",
|
|
1, 0.5, 0.5, 0.5);
|
|
if (createResult != 1 || alprHandle == nullptr) {
|
|
printf("[Task0] FAILED to create ALPR handle (result=%d)\n", createResult);
|
|
StopRTSP(&rtspClient); ReleaseANSRTSPHandle(&rtspClient);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -1;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandle, 1);
|
|
|
|
printf("[Task0] Loading ALPR engine...\n");
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandle);
|
|
auto engineEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
if (loadResult != 1) {
|
|
printf("[Task0] FAILED to load ALPR engine (result=%d)\n", loadResult);
|
|
ReleaseANSALPRHandle(&alprHandle);
|
|
StopRTSP(&rtspClient); ReleaseANSRTSPHandle(&rtspClient);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -1;
|
|
}
|
|
printf("[Task0] Engine loaded in %.0f ms\n", loadMs);
|
|
g_log.add("[Task0] Engine loaded in " + std::to_string((int)loadMs) + " ms");
|
|
|
|
// --- Single-task worker + display ---
|
|
TaskState state;
|
|
state.engineLoaded = true;
|
|
state.streamOk = true;
|
|
state.statusMsg = "Running";
|
|
|
|
std::mutex streamGuard;
|
|
std::thread worker(ALPRWorkerThread, 0, &rtspClient, &streamGuard, alprHandle, std::ref(state));
|
|
|
|
const int cellW = 800, cellH = 600;
|
|
const int logPanelH = 80;
|
|
std::string windowTitle = "ANSLPR Single-Task Test";
|
|
cv::namedWindow(windowTitle, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowTitle, cellW, cellH + logPanelH);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
|
|
while (g_running.load()) {
|
|
cv::Mat canvas(cellH + logPanelH, cellW, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
std::string lastPlate;
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
if (!state.displayFrame.empty())
|
|
cv::resize(state.displayFrame, cell, cv::Size(cellW, cellH));
|
|
fps = state.fps;
|
|
infMs = state.inferenceMs;
|
|
fCount = state.frameCount;
|
|
dCount = state.detectionCount;
|
|
lastPlate = state.lastPlate;
|
|
}
|
|
|
|
if (cell.empty())
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 40, cellW, 40), cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar[256];
|
|
snprintf(bar, sizeof(bar), "T0 | %.1f FPS | %.0fms | F:%d | D:%d | %s",
|
|
fps, infMs, fCount, dCount, lastPlate.empty() ? "-" : lastPlate.c_str());
|
|
cv::putText(cell, bar, cv::Point(5, cellH - 12),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);
|
|
cell.copyTo(canvas(cv::Rect(0, 0, cellW, cellH)));
|
|
|
|
cv::Mat logPanel = canvas(cv::Rect(0, cellH, cellW, logPanelH));
|
|
logPanel.setTo(cv::Scalar(20, 20, 20));
|
|
auto elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - testStart).count();
|
|
char header[256];
|
|
snprintf(header, sizeof(header), "Elapsed: %.0fs | 1 camera, 1 AI task | %.1f FPS | Press ESC to stop",
|
|
elapsed, fps);
|
|
cv::putText(logPanel, header, cv::Point(10, 20),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(200, 200, 0), 1);
|
|
|
|
cv::imshow(windowTitle, canvas);
|
|
if (cv::waitKey(30) == 27) {
|
|
g_log.add("ESC pressed — stopping...");
|
|
printf("\nESC pressed — stopping...\n");
|
|
g_running.store(false);
|
|
}
|
|
}
|
|
|
|
if (worker.joinable()) worker.join();
|
|
|
|
printf("\n============================================================\n");
|
|
printf(" FINAL SUMMARY\n");
|
|
printf(" Frames: %d | Detections: %d | FPS: %.1f | InfMs: %.0f\n",
|
|
state.frameCount, state.detectionCount, state.fps, state.inferenceMs);
|
|
printf("============================================================\n");
|
|
|
|
ReleaseANSALPRHandle(&alprHandle);
|
|
StopRTSP(&rtspClient);
|
|
ReleaseANSRTSPHandle(&rtspClient);
|
|
g_log.close();
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return 0;
|
|
}
|
|
|
|
// =============================================================================
|
|
// ANSLPR_CPU_StressTest — Lightweight 2-task stress test for CPU-only PCs
|
|
// Uses ANSALPR_OD (engineType=1) which auto-falls-back to ONNX Runtime on CPU.
|
|
// No VRAM tracking, no NVDEC alignment, no chaos thread.
|
|
// =============================================================================
|
|
int ANSLPR_CPU_StressTest() {
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
g_log.init();
|
|
|
|
const int NUM_STREAMS = 2;
|
|
const int NUM_TASKS = 2;
|
|
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ANSLPR CPU Stress Test — %d Parallel ALPR Tasks\n", NUM_TASKS);
|
|
printf(" Press ESC to stop\n");
|
|
printf(" Log file: %s\n", LOG_FILE_PATH);
|
|
printf("============================================================\n\n");
|
|
|
|
g_log.add("============================================================");
|
|
g_log.add(" ANSLPR CPU Stress Test — " + std::to_string(NUM_TASKS) + " Tasks");
|
|
g_log.add("============================================================");
|
|
|
|
// --- RTSP URLs (2 camera streams) ---
|
|
const std::string streamUrls[NUM_STREAMS] = {
|
|
"rtsp://admin:admin123@103.156.0.133:8010/cam/realmonitor?channel=1&subtype=0",
|
|
"rtsp://nhathuocngoclinh.zapto.org:600/rtsp/streaming?channel=01&subtype=0"
|
|
};
|
|
const int taskStreamMap[NUM_TASKS] = { 0, 1 };
|
|
|
|
for (int i = 0; i < NUM_STREAMS; i++)
|
|
g_log.add("Stream " + std::to_string(i) + ": " + streamUrls[i]);
|
|
|
|
// --- Task states ---
|
|
TaskState taskStates[NUM_TASKS];
|
|
|
|
// --- Create RTSP clients (software decoding) ---
|
|
ANSCENTER::ANSRTSPClient* rtspClients[NUM_STREAMS] = {};
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
printf("[Stream%d] Creating RTSP handle...\n", s);
|
|
int result = CreateANSRTSPHandle(&rtspClients[s], "", "", "", streamUrls[s].c_str());
|
|
if (result != 1 || rtspClients[s] == nullptr) {
|
|
printf("[Stream%d] FAILED to create RTSP handle\n", s);
|
|
g_log.add("[Stream" + std::to_string(s) + "] RTSP create FAILED");
|
|
rtspClients[s] = nullptr;
|
|
continue;
|
|
}
|
|
SetRTSPImageQuality(&rtspClients[s], 0);
|
|
SetRTSPHWDecoding(&rtspClients[s], -1); // HW_DECODING_DISABLE: force software decoding
|
|
StartRTSP(&rtspClients[s]);
|
|
g_log.add("[Stream" + std::to_string(s) + "] RTSP started (software decode)");
|
|
}
|
|
|
|
// --- Create ALPR engines (engineType=1 → ANSALPR_OD, auto CPU/GPU) ---
|
|
ANSCENTER::ANSALPR* alprHandles[NUM_TASKS] = {};
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
int engineType = 1; // ANSALPR_OD: auto CPU/GPU
|
|
double detThresh = 0.5, ocrThresh = 0.5, colThresh = 0.5;
|
|
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", i);
|
|
int streamIdx = taskStreamMap[i];
|
|
if (rtspClients[streamIdx] == nullptr) {
|
|
printf("%s Skipped — Stream%d not available\n", tag, streamIdx);
|
|
continue;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].streamOk = true;
|
|
taskStates[i].statusMsg = "Loading ALPR engine...";
|
|
}
|
|
|
|
printf("%s Creating ALPR handle...\n", tag);
|
|
auto engineStart = std::chrono::steady_clock::now();
|
|
int createResult = CreateANSALPRHandle(&alprHandles[i], "", modelZipFile.c_str(), "",
|
|
engineType, detThresh, ocrThresh, colThresh);
|
|
if (createResult != 1 || alprHandles[i] == nullptr) {
|
|
printf("%s FAILED to create ALPR handle (result=%d)\n", tag, createResult);
|
|
g_log.add(std::string(tag) + " ALPR create FAILED");
|
|
continue;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandles[i], 1);
|
|
|
|
printf("%s Loading ALPR engine...\n", tag);
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandles[i]);
|
|
auto engineEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
|
|
if (loadResult != 1) {
|
|
printf("%s FAILED to load ALPR engine (result=%d)\n", tag, loadResult);
|
|
g_log.add(std::string(tag) + " Engine load FAILED");
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
alprHandles[i] = nullptr;
|
|
continue;
|
|
}
|
|
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), "%s Engine loaded in %.0f ms (Stream%d)", tag, loadMs, streamIdx);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].engineLoaded = true;
|
|
taskStates[i].statusMsg = "Running";
|
|
}
|
|
}
|
|
|
|
// --- Launch worker threads ---
|
|
std::mutex streamGuards[NUM_STREAMS];
|
|
std::thread workers[NUM_TASKS];
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
int streamIdx = taskStreamMap[i];
|
|
if (rtspClients[streamIdx] && alprHandles[i]) {
|
|
workers[i] = std::thread(ALPRWorkerThread, i,
|
|
&rtspClients[streamIdx],
|
|
&streamGuards[streamIdx],
|
|
alprHandles[i],
|
|
std::ref(taskStates[i]));
|
|
}
|
|
}
|
|
|
|
// --- Display loop ---
|
|
const int cellW = 640, cellH = 480;
|
|
const int logPanelH = 120;
|
|
const int gridCols = 2, gridRows = 1;
|
|
std::string windowTitle = "ANSLPR CPU Stress Test";
|
|
cv::namedWindow(windowTitle, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowTitle, cellW * gridCols, cellH * gridRows + logPanelH);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
|
|
while (g_running.load()) {
|
|
cv::Mat canvas(cellH * gridRows + logPanelH, cellW * gridCols, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
int col = i % gridCols, row = i / gridCols;
|
|
cv::Rect roi(col * cellW, row * cellH, cellW, cellH);
|
|
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
std::string statusMsg, lastPlate;
|
|
bool engineLoaded = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (!taskStates[i].displayFrame.empty())
|
|
cv::resize(taskStates[i].displayFrame, cell, cv::Size(cellW, cellH));
|
|
fps = taskStates[i].fps;
|
|
infMs = taskStates[i].inferenceMs;
|
|
fCount = taskStates[i].frameCount;
|
|
dCount = taskStates[i].detectionCount;
|
|
statusMsg = taskStates[i].statusMsg;
|
|
lastPlate = taskStates[i].lastPlate;
|
|
engineLoaded = taskStates[i].engineLoaded;
|
|
}
|
|
|
|
if (cell.empty()) {
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
cv::putText(cell, "Task " + std::to_string(i) + ": " + statusMsg,
|
|
cv::Point(20, cellH / 2),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(100, 100, 255), 2);
|
|
}
|
|
|
|
// Status bar
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 40, cellW, 40), cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar[256];
|
|
snprintf(bar, sizeof(bar), "T%d(S%d) | %.1f FPS | %.0fms | F:%d | D:%d | %s",
|
|
i, taskStreamMap[i], fps, infMs, fCount, dCount,
|
|
lastPlate.empty() ? "-" : lastPlate.c_str());
|
|
cv::Scalar barColor = engineLoaded ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 100, 255);
|
|
cv::putText(cell, bar, cv::Point(5, cellH - 12),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, barColor, 1);
|
|
cell.copyTo(canvas(roi));
|
|
}
|
|
|
|
// Grid line
|
|
if (gridCols > 1)
|
|
cv::line(canvas, cv::Point(cellW, 0), cv::Point(cellW, cellH * gridRows),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
|
|
// Log panel
|
|
cv::Rect logRoi(0, cellH * gridRows, cellW * gridCols, logPanelH);
|
|
cv::Mat logPanel = canvas(logRoi);
|
|
logPanel.setTo(cv::Scalar(20, 20, 20));
|
|
|
|
auto elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - testStart).count();
|
|
double totalFps = 0;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
totalFps += taskStates[i].fps;
|
|
}
|
|
char header[256];
|
|
snprintf(header, sizeof(header),
|
|
"Elapsed: %.0fs | %d cameras, %d AI tasks | Total: %.1f FPS | Press ESC to stop",
|
|
elapsed, NUM_STREAMS, NUM_TASKS, totalFps);
|
|
cv::putText(logPanel, header, cv::Point(10, 20),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(200, 200, 0), 1);
|
|
|
|
// Per-task summary
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
char tLine[256];
|
|
snprintf(tLine, sizeof(tLine),
|
|
"T%d(S%d): FPS=%.1f Inf=%.0fms Frames=%d Det=%d",
|
|
i, taskStreamMap[i], taskStates[i].fps, taskStates[i].inferenceMs,
|
|
taskStates[i].frameCount, taskStates[i].detectionCount);
|
|
cv::putText(logPanel, tLine, cv::Point(10, 42 + i * 18),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(200, 200, 200), 1);
|
|
}
|
|
|
|
// Recent log
|
|
auto recentLogs = g_log.getRecent(3);
|
|
int logY = 42 + NUM_TASKS * 18 + 5;
|
|
for (const auto& line : recentLogs) {
|
|
if (logY > logPanelH - 5) break;
|
|
std::string display = (line.size() > 120) ? line.substr(0, 117) + "..." : line;
|
|
cv::putText(logPanel, display, cv::Point(10, logY),
|
|
cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(140, 140, 140), 1);
|
|
logY += 15;
|
|
}
|
|
|
|
cv::imshow(windowTitle, canvas);
|
|
int key = cv::waitKey(30);
|
|
if (key == 27) {
|
|
g_log.add("ESC pressed — stopping...");
|
|
printf("\nESC pressed — stopping...\n");
|
|
g_running.store(false);
|
|
}
|
|
}
|
|
|
|
// --- Wait for workers ---
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (workers[i].joinable()) workers[i].join();
|
|
}
|
|
|
|
// --- Final summary ---
|
|
double totalElapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
printf("\n============================================================\n");
|
|
printf(" FINAL SUMMARY (runtime: %.0fs)\n", totalElapsed);
|
|
printf("============================================================\n");
|
|
double totalFpsFinal = 0;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " Task %d (Stream %d): %d frames, %d detections, FPS=%.1f, InfMs=%.0f",
|
|
i, taskStreamMap[i], taskStates[i].frameCount, taskStates[i].detectionCount,
|
|
taskStates[i].fps, taskStates[i].inferenceMs);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
totalFpsFinal += taskStates[i].fps;
|
|
}
|
|
printf(" Total throughput: %.1f FPS\n", totalFpsFinal);
|
|
printf("============================================================\n");
|
|
|
|
// --- Cleanup ---
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (alprHandles[i]) ReleaseANSALPRHandle(&alprHandles[i]);
|
|
}
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (rtspClients[s]) {
|
|
StopRTSP(&rtspClients[s]);
|
|
ReleaseANSRTSPHandle(&rtspClients[s]);
|
|
}
|
|
}
|
|
|
|
g_log.close();
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return 0;
|
|
}
|
|
|
|
int ANSLPR_MultiGPU_StressTest() {
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
|
|
// --- Initialize log file ---
|
|
g_log.init();
|
|
|
|
printf("\n");
|
|
// --- Auto-detect GPU availability (safe on CPU-only PCs without CUDA runtime) ---
|
|
int gpuCount = 0;
|
|
bool hasGpu = false;
|
|
if (IsCudaAvailable()) {
|
|
cudaGetDeviceCount(&gpuCount);
|
|
hasGpu = (gpuCount > 0);
|
|
}
|
|
const char* modeStr = hasGpu ? "GPU (NVIDIA CUDA)" : "CPU (Software Decoding)";
|
|
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ANSLPR Multi-Engine Stress Test — 5 Parallel ALPR Tasks\n");
|
|
printf(" Mode: %s\n", modeStr);
|
|
printf(" (4 cameras, 5 AI tasks — Task 4 shares Stream 2)\n");
|
|
printf(" Press ESC to stop\n");
|
|
printf(" Log file: %s\n", LOG_FILE_PATH);
|
|
printf("============================================================\n\n");
|
|
|
|
g_log.add("============================================================");
|
|
g_log.add(" ANSLPR Multi-Engine Stress Test — 5 Parallel ALPR Tasks");
|
|
g_log.add(" Mode: " + std::string(modeStr));
|
|
g_log.add("============================================================");
|
|
|
|
// --- Log GPU info for diagnostics (safe on CPU — prints "no GPU found") ---
|
|
LogGpuInfo();
|
|
|
|
// --- RTSP URLs (4 independent camera streams) ---
|
|
const std::string rtspUrl0 = "rtsp://admin:admin123@103.156.0.133:8010/cam/realmonitor?channel=1&subtype=0";
|
|
const std::string rtspUrl1 = "rtsp://cafe2471.ddns.net:600/rtsp/streaming?channel=01&subtype=0";
|
|
const std::string rtspUrl2 = "rtsp://nhathuocngoclinh.zapto.org:600/rtsp/streaming?channel=01&subtype=0";
|
|
const std::string rtspUrl3 = "rtsp://bnunitttd.ddns.net:554/rtsp/streaming?channel=01&subtype=0";
|
|
|
|
g_log.add("Stream 0: " + rtspUrl0);
|
|
g_log.add("Stream 1: " + rtspUrl1);
|
|
g_log.add("Stream 2: " + rtspUrl2);
|
|
g_log.add("Stream 3: " + rtspUrl3);
|
|
|
|
// =========================================================================
|
|
// Architecture: Camera Process + AI Task Process (mimics LabVIEW)
|
|
// -----------------------------------------------------------------------
|
|
// Camera Process: 4 independent RTSP streams acquire frames from cameras.
|
|
// AI Task Process: 5 AI tasks subscribe to camera streams and run inference
|
|
// in parallel. Multiple tasks can share one camera stream.
|
|
// Task 4 subscribes to Stream 2 (nhathuocngoclinh) to demonstrate the
|
|
// shared-camera subscription model used in LabVIEW.
|
|
// =========================================================================
|
|
|
|
const int NUM_STREAMS = 4;
|
|
const int NUM_TASKS = 5;
|
|
|
|
// --- Task states ---
|
|
TaskState taskStates[NUM_TASKS];
|
|
|
|
// =========================================================================
|
|
// CAMERA PROCESS: Create 4 independent RTSP readers (one per camera).
|
|
// These form the camera acquisition layer that AI tasks subscribe to.
|
|
// =========================================================================
|
|
ANSCENTER::ANSRTSPClient* rtspClients[NUM_STREAMS] = {};
|
|
const std::string streamUrls[NUM_STREAMS] = { rtspUrl0, rtspUrl1, rtspUrl2, rtspUrl3 };
|
|
// Map: task index -> stream index
|
|
// Tasks 0-3 map 1:1 to streams 0-3.
|
|
// Task 4 subscribes to Stream 2 (nhathuocngoclinh) — shared camera.
|
|
const int taskStreamMap[NUM_TASKS] = { 0, 1, 2, 3, 2 };
|
|
|
|
// Log task-to-stream subscription mapping
|
|
g_log.add("--- AI Task -> Camera Stream subscription ---");
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
g_log.add(" Task " + std::to_string(i) + " -> Stream " + std::to_string(taskStreamMap[i])
|
|
+ " (" + streamUrls[taskStreamMap[i]] + ")");
|
|
}
|
|
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
printf("[Stream%d] Creating RTSP handle for %s...\n", s, streamUrls[s].c_str());
|
|
g_log.add("[Stream" + std::to_string(s) + "] Creating RTSP handle for " + streamUrls[s]);
|
|
int rtspResult = CreateANSRTSPHandle(&rtspClients[s], "", "", "", streamUrls[s].c_str());
|
|
if (rtspResult != 1 || rtspClients[s] == nullptr) {
|
|
printf("[Stream%d] FAILED to create RTSP handle (result=%d)\n", s, rtspResult);
|
|
g_log.add("[Stream" + std::to_string(s) + "] RTSP create FAILED");
|
|
rtspClients[s] = nullptr;
|
|
continue;
|
|
}
|
|
SetRTSPImageQuality(&rtspClients[s], 0);
|
|
if (hasGpu) SetRTSPHWDecoding(&rtspClients[s], 7); // CUDA HW decode only with GPU
|
|
StartRTSP(&rtspClients[s]);
|
|
g_log.add("[Stream" + std::to_string(s) + "] RTSP started");
|
|
}
|
|
|
|
// =========================================================================
|
|
// AI TASK PROCESS: Create 5 ALPR engines sequentially.
|
|
// Each AI task gets its own engine and subscribes to a camera stream.
|
|
// Task 4 shares Stream 2 (nhathuocngoclinh) with Task 2 — demonstrating
|
|
// the LabVIEW pattern where multiple AI tasks subscribe to one camera.
|
|
// =========================================================================
|
|
ANSCENTER::ANSALPR* alprHandles[NUM_TASKS] = {};
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
int engineType = 1; // ANSALPR_OD: auto-detects GPU/CPU, uses ONNX Runtime on CPU
|
|
double detThresh = 0.5, ocrThresh = 0.5, colThresh = 0.5;
|
|
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", i);
|
|
|
|
int streamIdx = taskStreamMap[i];
|
|
if (rtspClients[streamIdx] == nullptr) {
|
|
printf("%s Skipped — Stream%d not available\n", tag, streamIdx);
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Stream not available";
|
|
continue;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].streamOk = true;
|
|
taskStates[i].statusMsg = "Loading ALPR engine...";
|
|
}
|
|
|
|
printf("%s Creating ALPR handle (engineType=%d)...\n", tag, engineType);
|
|
g_log.add(std::string(tag) + " Creating ALPR handle...");
|
|
auto engineStart = std::chrono::steady_clock::now();
|
|
int createResult = CreateANSALPRHandle(&alprHandles[i], "", modelZipFile.c_str(), "",
|
|
engineType, detThresh, ocrThresh, colThresh);
|
|
if (createResult != 1 || alprHandles[i] == nullptr) {
|
|
printf("%s FAILED to create ALPR handle (result=%d)\n", tag, createResult);
|
|
g_log.add(std::string(tag) + " ALPR create FAILED");
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "ALPR create failed";
|
|
continue;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandles[i], 1);
|
|
|
|
printf("%s Loading ALPR engine (%s)...\n", tag, hasGpu ? "TensorRT" : "CPU");
|
|
g_log.add(std::string(tag) + " Loading ALPR engine...");
|
|
|
|
// Snapshot VRAM before engine load to measure consumption (GPU only)
|
|
std::vector<size_t> vramBefore;
|
|
if (hasGpu) vramBefore = GetPerGpuFreeMiB();
|
|
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandles[i]);
|
|
auto engineEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
|
|
if (loadResult != 1) {
|
|
printf("%s FAILED to load ALPR engine (result=%d)\n", tag, loadResult);
|
|
g_log.add(std::string(tag) + " Engine load FAILED");
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
alprHandles[i] = nullptr;
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Engine load failed";
|
|
continue;
|
|
}
|
|
|
|
int bestGpu = -1;
|
|
size_t maxDelta = 0;
|
|
|
|
if (hasGpu) {
|
|
// Snapshot VRAM after engine load — find which GPU lost the most VRAM
|
|
auto vramAfter = GetPerGpuFreeMiB();
|
|
size_t gpuCnt = vramBefore.size() < vramAfter.size() ? vramBefore.size() : vramAfter.size();
|
|
bestGpu = 0;
|
|
for (size_t g = 0; g < gpuCnt; g++) {
|
|
size_t delta = (vramBefore[g] > vramAfter[g]) ? (vramBefore[g] - vramAfter[g]) : 0;
|
|
if (delta > maxDelta) {
|
|
maxDelta = delta;
|
|
bestGpu = (int)g;
|
|
}
|
|
}
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
"%s Engine loaded in %.0f ms | GPU[%d] | VRAM used: %zu MiB (Stream%d)",
|
|
tag, loadMs, bestGpu, maxDelta, streamIdx);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
|
|
// Log per-GPU VRAM state after this engine load
|
|
for (size_t g = 0; g < vramAfter.size(); g++) {
|
|
size_t total = 0;
|
|
auto gpus = QueryGpuVram();
|
|
if (g < gpus.size()) total = gpus[g].totalMiB;
|
|
char vbuf[256];
|
|
snprintf(vbuf, sizeof(vbuf),
|
|
" GPU[%zu] VRAM: %zu MiB free (of %zu MiB)",
|
|
g, vramAfter[g], total);
|
|
printf("%s\n", vbuf);
|
|
g_log.add(vbuf);
|
|
}
|
|
} else {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), "%s Engine loaded in %.0f ms (CPU mode, Stream%d)",
|
|
tag, loadMs, streamIdx);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].engineLoaded = true;
|
|
taskStates[i].statusMsg = "Running";
|
|
taskStates[i].gpuDeviceId = bestGpu;
|
|
taskStates[i].vramUsedBytes = maxDelta * 1024 * 1024;
|
|
}
|
|
}
|
|
|
|
// --- Align NVDEC decode GPU with inference GPU for NV12 zero-copy ---
|
|
// (GPU only — software decoding on CPU doesn't use NVDEC)
|
|
if (hasGpu)
|
|
// Each stream should decode on the same GPU as its inference engine to enable
|
|
// direct NVDEC→TensorRT zero-copy (0.5ms vs 17ms preprocess per frame).
|
|
//
|
|
// Strategy: For each stream, count how many tasks run on each GPU (vote).
|
|
// Pick the GPU with the most tasks → maximises the number of NV12 zero-copy hits.
|
|
// If tied, prefer to keep the current decode GPU to avoid a reconnect.
|
|
// Additional tie-breaker: distribute streams across GPUs for decode load balance.
|
|
{
|
|
int streamPreferredGpu[NUM_STREAMS];
|
|
for (int s = 0; s < NUM_STREAMS; s++) streamPreferredGpu[s] = -1;
|
|
|
|
// Track how many streams have already been assigned to each GPU (for tie-breaking)
|
|
std::map<int, int> gpuStreamCount;
|
|
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (!rtspClients[s]) continue;
|
|
|
|
// Count votes: how many tasks on this stream use each GPU
|
|
std::map<int, int> gpuVotes;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (taskStreamMap[i] == s && alprHandles[i]) {
|
|
gpuVotes[taskStates[i].gpuDeviceId]++;
|
|
}
|
|
}
|
|
if (gpuVotes.empty()) continue;
|
|
|
|
// Find the GPU with the most votes
|
|
int currentGpu = rtspClients[s]->GetHWDecodingGpuIndex();
|
|
int bestGpu = -1;
|
|
int bestVotes = 0;
|
|
for (auto& [gpu, votes] : gpuVotes) {
|
|
if (votes > bestVotes) {
|
|
bestVotes = votes;
|
|
bestGpu = gpu;
|
|
} else if (votes == bestVotes) {
|
|
// Tie-break 1: prefer current decode GPU (avoids reconnect)
|
|
if (gpu == currentGpu && bestGpu != currentGpu) {
|
|
bestGpu = gpu;
|
|
}
|
|
// Tie-break 2: prefer GPU with fewer streams assigned (load balance)
|
|
else if (bestGpu != currentGpu && gpu != currentGpu) {
|
|
if (gpuStreamCount[gpu] < gpuStreamCount[bestGpu]) {
|
|
bestGpu = gpu;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
streamPreferredGpu[s] = bestGpu;
|
|
gpuStreamCount[bestGpu]++;
|
|
|
|
char buf[512];
|
|
std::string voteStr;
|
|
for (auto& [gpu, votes] : gpuVotes) {
|
|
if (!voteStr.empty()) voteStr += ", ";
|
|
voteStr += "GPU[" + std::to_string(gpu) + "]=" + std::to_string(votes);
|
|
}
|
|
snprintf(buf, sizeof(buf),
|
|
"[Stream%d] GPU vote: {%s} -> preferred GPU[%d] (current: GPU[%d])",
|
|
s, voteStr.c_str(), bestGpu, currentGpu);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
}
|
|
|
|
// Apply alignment: reconnect streams whose NVDEC is on the wrong GPU.
|
|
// IMPORTANT: If currentGpu == -1, the decoder hasn't initialized yet.
|
|
// Do NOT reconnect — it disrupts the initial RTSP handshake and causes
|
|
// 80+ seconds of empty frames. Just set preferredGpu; the decoder will
|
|
// use it when it naturally initializes.
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (rtspClients[s] && streamPreferredGpu[s] >= 0) {
|
|
int currentGpu = rtspClients[s]->GetHWDecodingGpuIndex();
|
|
if (currentGpu < 0) {
|
|
// Decoder not yet initialized — set preferred GPU without reconnect
|
|
SetRTSPHWDecoding(&rtspClients[s], 7, streamPreferredGpu[s]);
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
"[Stream%d] NVDEC not yet initialized (GPU[-1]) -- set preferred GPU[%d] (no reconnect)",
|
|
s, streamPreferredGpu[s]);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
} else if (currentGpu != streamPreferredGpu[s]) {
|
|
// Decoder is active on wrong GPU — reconnect disabled for VRAM stability testing
|
|
SetRTSPHWDecoding(&rtspClients[s], 7, streamPreferredGpu[s]);
|
|
// ReconnectRTSP(&rtspClients[s]); // Disabled for long test
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
"[Stream%d] NVDEC GPU realigned: GPU[%d] -> GPU[%d] (reconnected for zero-copy)",
|
|
s, currentGpu, streamPreferredGpu[s]);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
} else {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
"[Stream%d] NVDEC GPU already on GPU[%d] (zero-copy OK)",
|
|
s, currentGpu);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Enable deep pipeline benchmarking on all ALPR handles ---
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (alprHandles[i]) {
|
|
alprHandles[i]->ActivateDebugger(true);
|
|
}
|
|
}
|
|
g_log.add("Debug benchmarking ENABLED on all ALPR handles");
|
|
|
|
// --- Per-stream mutex: prevents CHAOS from destroying a stream while a
|
|
// worker is mid-frame-grab or mid-inference (use-after-free fix). ---
|
|
std::mutex streamGuards[NUM_STREAMS];
|
|
|
|
// --- Launch worker threads ---
|
|
// Each AI task subscribes to its camera stream via taskStreamMap.
|
|
// Tasks sharing a stream (e.g. Task 2 & Task 4 on Stream 2) both get
|
|
// the same RTSP client pointer and share the stream's mutex guard.
|
|
g_log.add("Launching " + std::to_string(NUM_TASKS) + " worker threads...");
|
|
std::thread workers[NUM_TASKS];
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
int streamIdx = taskStreamMap[i];
|
|
if (rtspClients[streamIdx] && alprHandles[i]) {
|
|
workers[i] = std::thread(ALPRWorkerThread, i,
|
|
&rtspClients[streamIdx],
|
|
&streamGuards[streamIdx],
|
|
alprHandles[i],
|
|
std::ref(taskStates[i]));
|
|
}
|
|
}
|
|
|
|
// =========================================================================
|
|
// Camera Chaos Thread — simulates camera errors / reconnects
|
|
// Mimics LabVIEW behavior: cameras randomly go into Error/Recovering
|
|
// state, triggering Stop/Reconnect/Destroy+Recreate cycles that cause
|
|
// CUDA cleanup (cuArrayDestroy, cuMemFree) while inference is running.
|
|
// This is the exact scenario that triggers the nvcuda64 SRW lock deadlock.
|
|
// =========================================================================
|
|
std::atomic<bool> chaosEnabled{false}; // Disabled for VRAM stability long test
|
|
std::thread chaosThread([&]() {
|
|
std::mt19937 rng(std::random_device{}());
|
|
|
|
// Wait 10 seconds for system to stabilize before starting chaos
|
|
for (int i = 0; i < 100 && g_running.load(); i++) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
}
|
|
|
|
g_log.add("[CHAOS] Camera chaos thread started — every 10s, stop/destroy/recreate one camera (round-robin)");
|
|
printf("[CHAOS] Camera chaos thread started — 10s interval, round-robin across %d streams\n", NUM_STREAMS);
|
|
|
|
int chaosCount = 0;
|
|
int nextStream = 0; // Round-robin: cycle through streams 0,1,2,3,0,1,...
|
|
while (g_running.load() && chaosEnabled.load()) {
|
|
// Fixed 10-second interval between chaos events
|
|
for (int s = 0; s < 100 && g_running.load(); s++) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
}
|
|
if (!g_running.load()) break;
|
|
|
|
int streamIdx = nextStream;
|
|
nextStream = (nextStream + 1) % NUM_STREAMS;
|
|
chaosCount++;
|
|
|
|
char buf[512];
|
|
auto chaosStart = std::chrono::steady_clock::now();
|
|
|
|
// Lock stream guard: wait for any in-flight inference to finish
|
|
// before touching the RTSP client. This prevents use-after-free
|
|
// when CHAOS destroys a stream while a worker is mid-inference.
|
|
std::unique_lock<std::mutex> chaosLock(streamGuards[streamIdx]);
|
|
|
|
// Always use full DESTROY + RECREATE cycle.
|
|
// Reconnect() reuses internal player state which can leave stale
|
|
// CUDA resources and cause freezes. A clean destroy + recreate
|
|
// guarantees a fresh decoder/player with no leftover state.
|
|
{
|
|
bool wasAlive = (rtspClients[streamIdx] != nullptr);
|
|
|
|
snprintf(buf, sizeof(buf), "[CHAOS #%d] Stream%d: DESTROY + RECREATE (%s)",
|
|
chaosCount, streamIdx,
|
|
wasAlive ? "camera was running" : "camera was already offline");
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
|
|
// Stop and release old handle if it exists
|
|
if (rtspClients[streamIdx]) {
|
|
StopRTSP(&rtspClients[streamIdx]);
|
|
ReleaseANSRTSPHandle(&rtspClients[streamIdx]);
|
|
rtspClients[streamIdx] = nullptr;
|
|
}
|
|
|
|
// Release lock during offline sleep — worker sees nullptr and skips
|
|
int offlineMs = 500 + (rng() % 2500); // 0.5 - 3 seconds offline
|
|
chaosLock.unlock();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(offlineMs));
|
|
chaosLock.lock();
|
|
|
|
// Recreate the RTSP handle (under lock again)
|
|
int result = CreateANSRTSPHandle(&rtspClients[streamIdx], "", "", "",
|
|
streamUrls[streamIdx].c_str());
|
|
if (result == 1 && rtspClients[streamIdx]) {
|
|
SetRTSPImageQuality(&rtspClients[streamIdx], 0);
|
|
if (hasGpu) SetRTSPHWDecoding(&rtspClients[streamIdx], 7);
|
|
StartRTSP(&rtspClients[streamIdx]);
|
|
|
|
auto chaosEnd = std::chrono::steady_clock::now();
|
|
double chaosMs = std::chrono::duration<double, std::milli>(chaosEnd - chaosStart).count();
|
|
snprintf(buf, sizeof(buf), "[CHAOS #%d] Stream%d: RECREATED in %.0f ms (offline %d ms)",
|
|
chaosCount, streamIdx, chaosMs, offlineMs);
|
|
} else {
|
|
snprintf(buf, sizeof(buf), "[CHAOS #%d] Stream%d: RECREATE FAILED (result=%d)",
|
|
chaosCount, streamIdx, result);
|
|
}
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
}
|
|
}
|
|
|
|
g_log.add("[CHAOS] Camera chaos thread stopped (total events: " + std::to_string(chaosCount) + ")");
|
|
printf("[CHAOS] Camera chaos thread stopped (total events: %d)\n", chaosCount);
|
|
});
|
|
|
|
// --- Display loop (main thread) ---
|
|
// 3x2 grid layout: 5 tasks displayed in 3 columns x 2 rows
|
|
const int cellW = 480, cellH = 360; // Smaller cells for 3-column layout
|
|
const int logPanelH = 220;
|
|
const int gridCols = 3, gridRows = 2;
|
|
std::string windowTitle = hasGpu ? "ANSLPR Multi-GPU Stress Test" : "ANSLPR CPU Stress Test";
|
|
cv::namedWindow(windowTitle, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowTitle, cellW * gridCols, cellH * gridRows + logPanelH);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
auto lastGpuSnapshot = std::chrono::steady_clock::now();
|
|
int snapshotCount = 0;
|
|
|
|
while (g_running.load()) {
|
|
// --- Periodic GPU/perf snapshot every 10 seconds (written to log file) ---
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
if (std::chrono::duration<double>(now2 - lastGpuSnapshot).count() >= 10.0) {
|
|
lastGpuSnapshot = now2;
|
|
snapshotCount++;
|
|
double elapsedSec = std::chrono::duration<double>(now2 - testStart).count();
|
|
g_log.add("---- PERIODIC SNAPSHOT #" + std::to_string(snapshotCount)
|
|
+ " (elapsed " + std::to_string((int)elapsedSec) + "s) ----");
|
|
// GPU VRAM
|
|
auto gpuSnap = QueryGpuVram();
|
|
for (const auto& gs : gpuSnap) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" GPU[%d] %s | Used: %zu/%zu MiB (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
g_log.add(buf);
|
|
}
|
|
// Per-task stats
|
|
double totalFpsSnap = 0;
|
|
for (int t = 0; t < NUM_TASKS; t++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[t].mtx);
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" T%d(S%d): GPU[%d] VRAM=%zuMiB FPS=%.1f GrabMs=%.0f InfMs=%.0f Frames=%d Det=%d",
|
|
t, taskStreamMap[t], taskStates[t].gpuDeviceId,
|
|
taskStates[t].vramUsedBytes / (1024 * 1024),
|
|
taskStates[t].fps, taskStates[t].lastGrabMs, taskStates[t].inferenceMs,
|
|
taskStates[t].frameCount, taskStates[t].detectionCount);
|
|
g_log.add(buf);
|
|
totalFpsSnap += taskStates[t].fps;
|
|
}
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS", totalFpsSnap);
|
|
g_log.add(buf);
|
|
// Multi-GPU check
|
|
std::set<int> gpusUsed;
|
|
for (int t = 0; t < NUM_TASKS; t++) {
|
|
if (taskStates[t].gpuDeviceId >= 0) gpusUsed.insert(taskStates[t].gpuDeviceId);
|
|
}
|
|
if (gpusUsed.size() > 1) {
|
|
g_log.add(" MULTI-GPU: YES — tasks distributed across " + std::to_string(gpusUsed.size()) + " GPUs");
|
|
} else if (!gpusUsed.empty()) {
|
|
g_log.add(" MULTI-GPU: NO — all tasks on GPU[" + std::to_string(*gpusUsed.begin()) + "]");
|
|
}
|
|
g_log.add("---- END SNAPSHOT ----");
|
|
}
|
|
// Build 3x2 grid + log panel (5 tasks: 3 cols x 2 rows, cell [1][2] empty)
|
|
cv::Mat canvas(cellH * gridRows + logPanelH, cellW * gridCols, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
// Place each task's frame in its cell
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
int row = i / gridCols, col = i % gridCols;
|
|
cv::Rect roi(col * cellW, row * cellH, cellW, cellH);
|
|
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
int gpuId = -1;
|
|
size_t vramMiB = 0;
|
|
std::string statusMsg, lastPlate;
|
|
bool engineLoaded = false, streamOk = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (!taskStates[i].displayFrame.empty()) {
|
|
cv::resize(taskStates[i].displayFrame, cell, cv::Size(cellW, cellH));
|
|
}
|
|
fps = taskStates[i].fps;
|
|
infMs = taskStates[i].inferenceMs;
|
|
fCount = taskStates[i].frameCount;
|
|
dCount = taskStates[i].detectionCount;
|
|
statusMsg = taskStates[i].statusMsg;
|
|
lastPlate = taskStates[i].lastPlate;
|
|
engineLoaded = taskStates[i].engineLoaded;
|
|
streamOk = taskStates[i].streamOk;
|
|
gpuId = taskStates[i].gpuDeviceId;
|
|
vramMiB = taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
|
|
if (cell.empty()) {
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
cv::putText(cell, "Task " + std::to_string(i) + ": " + statusMsg,
|
|
cv::Point(20, cellH / 2),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(100, 100, 255), 2);
|
|
}
|
|
|
|
// Draw status bar at bottom of each cell (2 lines)
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 50, cellW, 50), cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar1[256], bar2[256];
|
|
snprintf(bar1, sizeof(bar1), "T%d(S%d) | %.1f FPS | %.0fms | F:%d | D:%d | %s",
|
|
i, taskStreamMap[i], fps, infMs, fCount, dCount,
|
|
lastPlate.empty() ? "-" : lastPlate.c_str());
|
|
if (!hasGpu) {
|
|
snprintf(bar2, sizeof(bar2), "CPU mode (software decoding)");
|
|
} else if (gpuId >= 0) {
|
|
snprintf(bar2, sizeof(bar2), "GPU[%d] | VRAM: %zu MiB", gpuId, vramMiB);
|
|
} else {
|
|
snprintf(bar2, sizeof(bar2), "GPU: N/A");
|
|
}
|
|
cv::Scalar barColor = engineLoaded ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 100, 255);
|
|
cv::putText(cell, bar1, cv::Point(5, cellH - 28),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, barColor, 1);
|
|
cv::putText(cell, bar2, cv::Point(5, cellH - 8),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(0, 200, 255), 1);
|
|
|
|
cell.copyTo(canvas(roi));
|
|
}
|
|
|
|
// Draw grid lines
|
|
for (int c = 1; c < gridCols; c++)
|
|
cv::line(canvas, cv::Point(c * cellW, 0), cv::Point(c * cellW, cellH * gridRows),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
for (int r = 1; r < gridRows; r++)
|
|
cv::line(canvas, cv::Point(0, r * cellH), cv::Point(cellW * gridCols, r * cellH),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
|
|
// --- Log panel at bottom ---
|
|
cv::Rect logRoi(0, cellH * gridRows, cellW * gridCols, logPanelH);
|
|
cv::Mat logPanel = canvas(logRoi);
|
|
logPanel.setTo(cv::Scalar(20, 20, 20));
|
|
|
|
// Elapsed time header
|
|
auto elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - testStart).count();
|
|
char header[256];
|
|
snprintf(header, sizeof(header),
|
|
"Elapsed: %.0fs | %d cameras, %d AI tasks | Press ESC to stop",
|
|
elapsed, NUM_STREAMS, NUM_TASKS);
|
|
cv::putText(logPanel, header, cv::Point(10, 18),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(200, 200, 0), 1);
|
|
|
|
// Aggregate stats + per-task GPU summary
|
|
double totalFps = 0;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
totalFps += taskStates[i].fps;
|
|
}
|
|
// Build dynamic task-GPU summary string
|
|
std::string taskGpuStr;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (i > 0) taskGpuStr += " ";
|
|
taskGpuStr += "T" + std::to_string(i) + "(S" + std::to_string(taskStreamMap[i])
|
|
+ "):GPU" + std::to_string(taskStates[i].gpuDeviceId);
|
|
}
|
|
char aggLine[512];
|
|
snprintf(aggLine, sizeof(aggLine), "Total: %.1f FPS | %s",
|
|
totalFps, taskGpuStr.c_str());
|
|
cv::putText(logPanel, aggLine, cv::Point(10, 38),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(0, 255, 255), 1);
|
|
|
|
// Real-time GPU VRAM monitor (query every frame — cheap call)
|
|
auto gpuSnaps = QueryGpuVram();
|
|
int gpuLineY = 58;
|
|
for (const auto& gs : gpuSnaps) {
|
|
// Count tasks on this GPU and their total VRAM
|
|
int tasksOnGpu = 0;
|
|
size_t taskVramMiB = 0;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (taskStates[i].gpuDeviceId == gs.deviceId) {
|
|
tasksOnGpu++;
|
|
taskVramMiB += taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
}
|
|
char gpuLine[256];
|
|
snprintf(gpuLine, sizeof(gpuLine),
|
|
"GPU[%d] %s | Used: %zu/%zu MiB | Tasks: %d (engine VRAM: %zu MiB)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
tasksOnGpu, taskVramMiB);
|
|
cv::putText(logPanel, gpuLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(100, 255, 100), 1);
|
|
gpuLineY += 18;
|
|
}
|
|
|
|
// Per-task resource line (shows which stream each task subscribes to)
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
char tLine[256];
|
|
snprintf(tLine, sizeof(tLine),
|
|
"T%d(S%d): GPU[%d] VRAM=%zuMiB FPS=%.1f Inf=%.0fms Frames=%d Det=%d",
|
|
i, taskStreamMap[i], taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].fps, taskStates[i].inferenceMs,
|
|
taskStates[i].frameCount, taskStates[i].detectionCount);
|
|
cv::putText(logPanel, tLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(200, 200, 200), 1);
|
|
gpuLineY += 16;
|
|
}
|
|
|
|
// Recent log lines (remaining space)
|
|
auto recentLogs = g_log.getRecent(4);
|
|
for (const auto& line : recentLogs) {
|
|
if (gpuLineY > logPanelH - 5) break;
|
|
std::string display = (line.size() > 130) ? line.substr(0, 127) + "..." : line;
|
|
cv::putText(logPanel, display, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(140, 140, 140), 1);
|
|
gpuLineY += 15;
|
|
}
|
|
|
|
cv::imshow(windowTitle, canvas);
|
|
int key = cv::waitKey(30);
|
|
if (key == 27) { // ESC
|
|
g_log.add("ESC pressed — stopping all tasks...");
|
|
printf("\nESC pressed — stopping...\n");
|
|
g_running.store(false);
|
|
}
|
|
}
|
|
|
|
// --- Stop chaos thread ---
|
|
chaosEnabled.store(false);
|
|
if (chaosThread.joinable()) chaosThread.join();
|
|
|
|
// --- Wait for all workers ---
|
|
printf("Waiting for %d worker threads to finish...\n", NUM_TASKS);
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (workers[i].joinable()) workers[i].join();
|
|
}
|
|
|
|
// --- Print final summary (console + log file) ---
|
|
double totalElapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
|
|
g_log.add("================================================================");
|
|
g_log.add(" FINAL PERFORMANCE SUMMARY");
|
|
g_log.add(" " + std::to_string(NUM_STREAMS) + " cameras, " + std::to_string(NUM_TASKS) + " AI tasks");
|
|
g_log.add(" Total runtime: " + std::to_string((int)totalElapsed) + " seconds");
|
|
g_log.add("================================================================");
|
|
|
|
printf("\n============================================================\n");
|
|
printf(" FINAL PERFORMANCE SUMMARY (runtime: %.0fs)\n", totalElapsed);
|
|
printf(" %d cameras, %d AI tasks\n", NUM_STREAMS, NUM_TASKS);
|
|
printf("============================================================\n");
|
|
|
|
double totalFpsFinal = 0;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
" Task %d (Stream %d): GPU[%d] | VRAM=%zuMiB | %d frames, %d detections, FPS=%.1f, InfMs=%.0f",
|
|
i, taskStreamMap[i], taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].frameCount, taskStates[i].detectionCount,
|
|
taskStates[i].fps, taskStates[i].inferenceMs);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
totalFpsFinal += taskStates[i].fps;
|
|
}
|
|
|
|
auto finalGpu = QueryGpuVram();
|
|
for (const auto& gs : finalGpu) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " GPU[%d] %s: %zu/%zu MiB used (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
// Multi-GPU verdict
|
|
std::set<int> finalGpusUsed;
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (taskStates[i].gpuDeviceId >= 0) finalGpusUsed.insert(taskStates[i].gpuDeviceId);
|
|
}
|
|
{
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS across %d tasks (%d cameras)",
|
|
totalFpsFinal, NUM_TASKS, NUM_STREAMS);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
if (finalGpusUsed.size() > 1) {
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " MULTI-GPU: YES — tasks on %zu different GPUs", finalGpusUsed.size());
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
} else if (!finalGpusUsed.empty()) {
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " MULTI-GPU: NO — all tasks on GPU[%d] only", *finalGpusUsed.begin());
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
g_log.add(" DIAGNOSIS: Engine pool sees only 1 GPU. On dual-GPU systems, check:");
|
|
g_log.add(" 1. Both GPUs visible to CUDA (nvidia-smi shows 2 devices)");
|
|
g_log.add(" 2. TRT engine files are compatible with both GPU architectures");
|
|
g_log.add(" 3. No CUDA_VISIBLE_DEVICES env var restricting GPU access");
|
|
}
|
|
|
|
// Log shared-camera subscription info
|
|
g_log.add(" Camera subscription: Task 2 and Task 4 both subscribe to Stream 2 (nhathuocngoclinh)");
|
|
|
|
printf("============================================================\n");
|
|
g_log.add("================================================================");
|
|
g_log.add(" Log saved to: " + std::string(LOG_FILE_PATH));
|
|
g_log.add("================================================================");
|
|
|
|
// --- Release all handles (sequentially on main thread) ---
|
|
for (int i = 0; i < NUM_TASKS; i++) {
|
|
if (alprHandles[i]) {
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
}
|
|
}
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (rtspClients[s]) {
|
|
StopRTSP(&rtspClients[s]);
|
|
ReleaseANSRTSPHandle(&rtspClients[s]);
|
|
}
|
|
}
|
|
|
|
g_log.close();
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
|
|
return 0;
|
|
}
|
|
|
|
// =============================================================================
|
|
// VideoPlayer-based worker thread for SimulatedCam stress test
|
|
// Same structure as ALPRWorkerThread but uses ANSVideoPlayer instead of ANSRTSP
|
|
// =============================================================================
|
|
static void ALPRWorkerThread_VideoPlayer(int taskId,
|
|
ANSCENTER::ANSVIDEOPLAYER* vpClient,
|
|
ANSCENTER::ANSALPR* alprHandle,
|
|
TaskState& state) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", taskId);
|
|
std::string prefix(tag);
|
|
|
|
g_log.add(prefix + " Worker thread started");
|
|
printf("%s Worker thread started\n", tag);
|
|
|
|
int width = 0, height = 0;
|
|
int64_t pts = 0;
|
|
int emptyFrames = 0;
|
|
std::string cameraId = "Cam" + std::to_string(taskId);
|
|
|
|
// FPS tracking with sliding window
|
|
std::deque<std::chrono::steady_clock::time_point> fpsTimestamps;
|
|
|
|
// Timing accumulators for periodic benchmarking
|
|
double totalGrabMs = 0, totalInfMs = 0;
|
|
int grabCount = 0, infCount = 0;
|
|
double maxGrabMs = 0, maxInfMs = 0;
|
|
auto benchStart = std::chrono::steady_clock::now();
|
|
|
|
while (g_running.load()) {
|
|
// Read frame from VideoPlayer
|
|
auto grabStart = std::chrono::steady_clock::now();
|
|
cv::Mat* framePtr = nullptr;
|
|
GetVideoPlayerCVImage(&vpClient, width, height, pts, &framePtr);
|
|
auto grabEnd = std::chrono::steady_clock::now();
|
|
double grabMs = std::chrono::duration<double, std::milli>(grabEnd - grabStart).count();
|
|
|
|
if (framePtr == nullptr || framePtr->empty()) {
|
|
emptyFrames++;
|
|
if (emptyFrames % 100 == 1) {
|
|
g_log.add(prefix + " Empty frame (count=" + std::to_string(emptyFrames) + ")");
|
|
}
|
|
if (emptyFrames > 300) {
|
|
g_log.add(prefix + " Too many empty frames, attempting reconnect...");
|
|
ReconnectVideoPlayer(&vpClient);
|
|
emptyFrames = 0;
|
|
}
|
|
if (framePtr) delete framePtr;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
emptyFrames = 0;
|
|
|
|
totalGrabMs += grabMs;
|
|
grabCount++;
|
|
if (grabMs > maxGrabMs) maxGrabMs = grabMs;
|
|
|
|
// Run ALPR inference
|
|
auto infStart = std::chrono::steady_clock::now();
|
|
std::string lpnResult, jpegImage;
|
|
// Pass framePtr directly — NOT a copy. ANSGpuFrameRegistry::lookup()
|
|
// matches by cv::Mat* pointer, so `new cv::Mat(*framePtr)` would create
|
|
// a different pointer the registry doesn't know, breaking NV12 zero-copy.
|
|
ANSALPR_RunInferenceComplete_CPP(&alprHandle, &framePtr, cameraId.c_str(), 0, 0, lpnResult, jpegImage);
|
|
auto infEnd = std::chrono::steady_clock::now();
|
|
double infMs = std::chrono::duration<double, std::milli>(infEnd - infStart).count();
|
|
totalInfMs += infMs;
|
|
infCount++;
|
|
if (infMs > maxInfMs) maxInfMs = infMs;
|
|
|
|
// Parse detections and draw on frame
|
|
cv::Mat display = framePtr->clone();
|
|
int detCount = 0;
|
|
std::string lastPlateText;
|
|
|
|
if (!lpnResult.empty()) {
|
|
try {
|
|
boost::property_tree::ptree pt;
|
|
std::stringstream ss(lpnResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& det = child.second;
|
|
const auto class_name = GetData<std::string>(det, "class_name");
|
|
const auto x = GetData<float>(det, "x");
|
|
const auto y = GetData<float>(det, "y");
|
|
const auto w = GetData<float>(det, "width");
|
|
const auto h = GetData<float>(det, "height");
|
|
cv::rectangle(display, cv::Rect((int)x, (int)y, (int)w, (int)h),
|
|
cv::Scalar(0, 255, 0), 2);
|
|
cv::putText(display, class_name, cv::Point((int)x, (int)y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2);
|
|
lastPlateText = class_name;
|
|
detCount++;
|
|
}
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
// Update FPS (sliding window over last 2 seconds)
|
|
auto now = std::chrono::steady_clock::now();
|
|
fpsTimestamps.push_back(now);
|
|
while (!fpsTimestamps.empty() &&
|
|
std::chrono::duration<double>(now - fpsTimestamps.front()).count() > 2.0) {
|
|
fpsTimestamps.pop_front();
|
|
}
|
|
double fps = fpsTimestamps.size() / 2.0;
|
|
|
|
// Draw OSD on frame
|
|
char osd[128];
|
|
snprintf(osd, sizeof(osd), "Task%d | %.1f FPS | Inf: %.0f ms | #%d",
|
|
taskId, fps, infMs, state.frameCount + 1);
|
|
cv::putText(display, osd, cv::Point(10, 30),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 2);
|
|
|
|
// Update shared state
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
state.displayFrame = display;
|
|
state.fps = fps;
|
|
state.inferenceMs = infMs;
|
|
state.lastGrabMs = grabMs;
|
|
state.lastInfMs = infMs;
|
|
state.frameCount++;
|
|
state.detectionCount += detCount;
|
|
if (!lastPlateText.empty()) state.lastPlate = lastPlateText;
|
|
}
|
|
|
|
// Periodic logging (every 100 frames)
|
|
if ((state.frameCount % 100) == 0) {
|
|
double avgGrab = grabCount > 0 ? totalGrabMs / grabCount : 0;
|
|
double avgInf = infCount > 0 ? totalInfMs / infCount : 0;
|
|
double elapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - benchStart).count();
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
"%s Frame %d | FPS=%.1f | Grab: avg=%.1fms max=%.0fms | Inf: avg=%.1fms max=%.0fms | "
|
|
"GrabPct=%.0f%% InfPct=%.0f%% | Det=%d",
|
|
tag, state.frameCount, fps,
|
|
avgGrab, maxGrabMs,
|
|
avgInf, maxInfMs,
|
|
(totalGrabMs / (elapsed * 1000.0)) * 100.0,
|
|
(totalInfMs / (elapsed * 1000.0)) * 100.0,
|
|
state.detectionCount);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
|
|
// Reset accumulators
|
|
totalGrabMs = totalInfMs = 0;
|
|
maxGrabMs = maxInfMs = 0;
|
|
grabCount = infCount = 0;
|
|
benchStart = std::chrono::steady_clock::now();
|
|
}
|
|
|
|
delete framePtr;
|
|
}
|
|
|
|
g_log.add(prefix + " Worker loop exited");
|
|
}
|
|
|
|
// =============================================================================
|
|
// ANSLPR_MultiGPU_StressTest_SimulatedCam
|
|
// Same structure as ANSLPR_MultiGPU_StressTest but uses local video files
|
|
// via ANSVideoPlayer instead of live RTSP streams.
|
|
// =============================================================================
|
|
int ANSLPR_MultiGPU_StressTest_SimulatedCam() {
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
|
|
// --- Initialize log file ---
|
|
g_log.init();
|
|
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ANSLPR Multi-GPU Stress Test (Simulated Cam)\n");
|
|
printf(" Using local video files via ANSVideoPlayer\n");
|
|
printf(" Press ESC to stop\n");
|
|
printf(" Log file: %s\n", LOG_FILE_PATH);
|
|
printf("============================================================\n\n");
|
|
|
|
g_log.add("============================================================");
|
|
g_log.add(" ANSLPR Multi-GPU Stress Test (Simulated Cam)");
|
|
g_log.add(" Using ANSVideoPlayer with local video files");
|
|
g_log.add("============================================================");
|
|
|
|
// --- Log GPU info for diagnostics ---
|
|
LogGpuInfo();
|
|
|
|
// --- Video file paths (4 files, one per task) ---
|
|
const std::string videoFile0 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day.mp4";
|
|
const std::string videoFile1 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_1.mp4";
|
|
const std::string videoFile2 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_2.mp4";
|
|
const std::string videoFile3 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_3.mp4";
|
|
|
|
g_log.add("Video 0: " + videoFile0);
|
|
g_log.add("Video 1: " + videoFile1);
|
|
g_log.add("Video 2: " + videoFile2);
|
|
g_log.add("Video 3: " + videoFile3);
|
|
|
|
// --- Task states ---
|
|
TaskState taskStates[4];
|
|
|
|
// =========================================================================
|
|
// Create 4 VideoPlayer readers — one per task
|
|
// =========================================================================
|
|
const int NUM_STREAMS = 4;
|
|
ANSCENTER::ANSVIDEOPLAYER* vpClients[NUM_STREAMS] = {};
|
|
const std::string videoFiles[NUM_STREAMS] = { videoFile0, videoFile1, videoFile2, videoFile3 };
|
|
const int taskStreamMap[4] = { 0, 1, 2, 3 };
|
|
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
printf("[Stream%d] Creating VideoPlayer for %s\n", s, videoFiles[s].c_str());
|
|
g_log.add("[Stream" + std::to_string(s) + "] Creating VideoPlayer for " + videoFiles[s]);
|
|
int result = CreateANSVideoPlayerHandle(&vpClients[s], "", videoFiles[s].c_str());
|
|
if (result != 1 || vpClients[s] == nullptr) {
|
|
printf("[Stream%d] FAILED to create VideoPlayer (result=%d)\n", s, result);
|
|
g_log.add("[Stream" + std::to_string(s) + "] VideoPlayer create FAILED");
|
|
vpClients[s] = nullptr;
|
|
continue;
|
|
}
|
|
// Don't call StartVideoPlayer here — play() will be called just before worker threads
|
|
// launch, so the video doesn't play to completion during the ~16s engine loading phase.
|
|
SetVideoPlayerDisplayResolution(&vpClients[s], 1920, 1080);
|
|
g_log.add("[Stream" + std::to_string(s) + "] VideoPlayer created (display: 1920x1080)");
|
|
}
|
|
|
|
// =========================================================================
|
|
// Create 4 ALPR engines sequentially
|
|
// =========================================================================
|
|
ANSCENTER::ANSALPR* alprHandles[4] = {};
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
int engineType = 1; // NVIDIA_GPU
|
|
double detThresh = 0.5, ocrThresh = 0.5, colThresh = 0.5;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", i);
|
|
|
|
int streamIdx = taskStreamMap[i];
|
|
if (vpClients[streamIdx] == nullptr) {
|
|
printf("%s Skipped — Stream%d not available\n", tag, streamIdx);
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Stream not available";
|
|
continue;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].streamOk = true;
|
|
taskStates[i].statusMsg = "Loading ALPR engine...";
|
|
}
|
|
|
|
printf("%s Creating ALPR handle (engineType=%d)...\n", tag, engineType);
|
|
g_log.add(std::string(tag) + " Creating ALPR handle...");
|
|
auto engineStart = std::chrono::steady_clock::now();
|
|
int createResult = CreateANSALPRHandle(&alprHandles[i], "", modelZipFile.c_str(), "",
|
|
engineType, detThresh, ocrThresh, colThresh);
|
|
if (createResult != 1 || alprHandles[i] == nullptr) {
|
|
printf("%s FAILED to create ALPR handle (result=%d)\n", tag, createResult);
|
|
g_log.add(std::string(tag) + " ALPR create FAILED");
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "ALPR create failed";
|
|
continue;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandles[i], 1);
|
|
|
|
printf("%s Loading ALPR engine (TensorRT)...\n", tag);
|
|
g_log.add(std::string(tag) + " Loading ALPR engine...");
|
|
|
|
// Snapshot VRAM before engine load to measure consumption
|
|
auto vramBefore = GetPerGpuFreeMiB();
|
|
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandles[i]);
|
|
auto engineEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
|
|
if (loadResult != 1) {
|
|
printf("%s FAILED to load ALPR engine (result=%d)\n", tag, loadResult);
|
|
g_log.add(std::string(tag) + " Engine load FAILED");
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
alprHandles[i] = nullptr;
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Engine load failed";
|
|
continue;
|
|
}
|
|
|
|
// Snapshot VRAM after engine load — find which GPU lost the most VRAM
|
|
auto vramAfter = GetPerGpuFreeMiB();
|
|
int bestGpu = 0;
|
|
size_t maxDelta = 0;
|
|
size_t gpuCount = vramBefore.size() < vramAfter.size() ? vramBefore.size() : vramAfter.size();
|
|
for (size_t g = 0; g < gpuCount; g++) {
|
|
size_t delta = (vramBefore[g] > vramAfter[g]) ? (vramBefore[g] - vramAfter[g]) : 0;
|
|
if (delta > maxDelta) {
|
|
maxDelta = delta;
|
|
bestGpu = (int)g;
|
|
}
|
|
}
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
"%s Engine loaded in %.0f ms | GPU[%d] | VRAM used: %zu MiB (Video%d)",
|
|
tag, loadMs, bestGpu, maxDelta, streamIdx);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
|
|
// Log per-GPU VRAM state after this engine load
|
|
for (size_t g = 0; g < vramAfter.size(); g++) {
|
|
size_t total = 0;
|
|
if (g < vramBefore.size()) {
|
|
auto gpus = QueryGpuVram();
|
|
if (g < gpus.size()) total = gpus[g].totalMiB;
|
|
}
|
|
char vbuf[256];
|
|
snprintf(vbuf, sizeof(vbuf),
|
|
" GPU[%zu] VRAM: %zu MiB free (of %zu MiB)",
|
|
g, vramAfter[g], total);
|
|
printf("%s\n", vbuf);
|
|
g_log.add(vbuf);
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].engineLoaded = true;
|
|
taskStates[i].statusMsg = "Running";
|
|
taskStates[i].gpuDeviceId = bestGpu;
|
|
taskStates[i].vramUsedBytes = maxDelta * 1024 * 1024;
|
|
}
|
|
}
|
|
|
|
// --- No NVDEC realignment needed — ANSVideoPlayer uses cv::VideoCapture (CPU decode) ---
|
|
|
|
// --- Enable deep pipeline benchmarking on all ALPR handles ---
|
|
for (int i = 0; i < 4; i++) {
|
|
if (alprHandles[i]) {
|
|
alprHandles[i]->ActivateDebugger(true);
|
|
}
|
|
}
|
|
g_log.add("Debug benchmarking ENABLED on all ALPR handles");
|
|
|
|
// --- Start video playback NOW (just before workers need frames) ---
|
|
// This avoids the video playing to completion during the ~16s engine loading phase.
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (vpClients[s]) {
|
|
StartVideoPlayer(&vpClients[s]);
|
|
g_log.add("[Stream" + std::to_string(s) + "] VideoPlayer play() started");
|
|
}
|
|
}
|
|
|
|
// --- Launch worker threads ---
|
|
g_log.add("Launching worker threads...");
|
|
std::thread workers[4];
|
|
for (int i = 0; i < 4; i++) {
|
|
int streamIdx = taskStreamMap[i];
|
|
if (vpClients[streamIdx] && alprHandles[i]) {
|
|
workers[i] = std::thread(ALPRWorkerThread_VideoPlayer, i,
|
|
vpClients[streamIdx], alprHandles[i],
|
|
std::ref(taskStates[i]));
|
|
}
|
|
}
|
|
|
|
// --- Display loop (main thread) ---
|
|
const int cellW = 640, cellH = 480;
|
|
const int logPanelH = 200;
|
|
const char* windowName = "ANSLPR Multi-GPU Stress Test (Simulated Cam)";
|
|
cv::namedWindow(windowName, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowName, cellW * 2, cellH * 2 + logPanelH);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
auto lastGpuSnapshot = std::chrono::steady_clock::now();
|
|
int snapshotCount = 0;
|
|
|
|
while (g_running.load()) {
|
|
// --- Periodic GPU/perf snapshot every 10 seconds ---
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
if (std::chrono::duration<double>(now2 - lastGpuSnapshot).count() >= 10.0) {
|
|
lastGpuSnapshot = now2;
|
|
snapshotCount++;
|
|
double elapsedSec = std::chrono::duration<double>(now2 - testStart).count();
|
|
g_log.add("---- PERIODIC SNAPSHOT #" + std::to_string(snapshotCount)
|
|
+ " (elapsed " + std::to_string((int)elapsedSec) + "s) ----");
|
|
auto gpuSnap = QueryGpuVram();
|
|
for (const auto& gs : gpuSnap) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" GPU[%d] %s | Used: %zu/%zu MiB (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
g_log.add(buf);
|
|
}
|
|
double totalFpsSnap = 0;
|
|
for (int t = 0; t < 4; t++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[t].mtx);
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" T%d: GPU[%d] VRAM=%zuMiB FPS=%.1f GrabMs=%.0f InfMs=%.0f Frames=%d Det=%d",
|
|
t, taskStates[t].gpuDeviceId,
|
|
taskStates[t].vramUsedBytes / (1024 * 1024),
|
|
taskStates[t].fps, taskStates[t].lastGrabMs, taskStates[t].inferenceMs,
|
|
taskStates[t].frameCount, taskStates[t].detectionCount);
|
|
g_log.add(buf);
|
|
totalFpsSnap += taskStates[t].fps;
|
|
}
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS", totalFpsSnap);
|
|
g_log.add(buf);
|
|
std::set<int> gpusUsed;
|
|
for (int t = 0; t < 4; t++) {
|
|
if (taskStates[t].gpuDeviceId >= 0) gpusUsed.insert(taskStates[t].gpuDeviceId);
|
|
}
|
|
if (gpusUsed.size() > 1) {
|
|
g_log.add(" MULTI-GPU: YES — tasks distributed across " + std::to_string(gpusUsed.size()) + " GPUs");
|
|
} else if (!gpusUsed.empty()) {
|
|
g_log.add(" MULTI-GPU: NO — all tasks on GPU[" + std::to_string(*gpusUsed.begin()) + "]");
|
|
}
|
|
g_log.add("---- END SNAPSHOT ----");
|
|
}
|
|
|
|
// Build 2x2 grid + log panel
|
|
cv::Mat canvas(cellH * 2 + logPanelH, cellW * 2, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
int row = i / 2, col = i % 2;
|
|
cv::Rect roi(col * cellW, row * cellH, cellW, cellH);
|
|
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
int gpuId = -1;
|
|
size_t vramMiB = 0;
|
|
std::string statusMsg, lastPlate;
|
|
bool engineLoaded = false, streamOk = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (!taskStates[i].displayFrame.empty()) {
|
|
cv::resize(taskStates[i].displayFrame, cell, cv::Size(cellW, cellH));
|
|
}
|
|
fps = taskStates[i].fps;
|
|
infMs = taskStates[i].inferenceMs;
|
|
fCount = taskStates[i].frameCount;
|
|
dCount = taskStates[i].detectionCount;
|
|
statusMsg = taskStates[i].statusMsg;
|
|
lastPlate = taskStates[i].lastPlate;
|
|
engineLoaded = taskStates[i].engineLoaded;
|
|
streamOk = taskStates[i].streamOk;
|
|
gpuId = taskStates[i].gpuDeviceId;
|
|
vramMiB = taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
|
|
if (cell.empty()) {
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
cv::putText(cell, "Task " + std::to_string(i) + ": " + statusMsg,
|
|
cv::Point(20, cellH / 2),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(100, 100, 255), 2);
|
|
}
|
|
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 50, cellW, 50), cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar1[256], bar2[256];
|
|
snprintf(bar1, sizeof(bar1), "T%d | %.1f FPS | %.0fms | Frames:%d | Det:%d | %s",
|
|
i, fps, infMs, fCount, dCount,
|
|
lastPlate.empty() ? "-" : lastPlate.c_str());
|
|
if (gpuId >= 0) {
|
|
snprintf(bar2, sizeof(bar2), "GPU[%d] | VRAM: %zu MiB", gpuId, vramMiB);
|
|
} else {
|
|
snprintf(bar2, sizeof(bar2), "GPU: N/A");
|
|
}
|
|
cv::Scalar barColor = engineLoaded ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 100, 255);
|
|
cv::putText(cell, bar1, cv::Point(5, cellH - 28),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, barColor, 1);
|
|
cv::putText(cell, bar2, cv::Point(5, cellH - 8),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(0, 200, 255), 1);
|
|
|
|
cell.copyTo(canvas(roi));
|
|
|
|
cv::line(canvas, cv::Point(cellW, 0), cv::Point(cellW, cellH * 2),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
cv::line(canvas, cv::Point(0, cellH), cv::Point(cellW * 2, cellH),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
}
|
|
|
|
// --- Log panel at bottom ---
|
|
cv::Rect logRoi(0, cellH * 2, cellW * 2, logPanelH);
|
|
cv::Mat logPanel = canvas(logRoi);
|
|
logPanel.setTo(cv::Scalar(20, 20, 20));
|
|
|
|
auto elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - testStart).count();
|
|
char header[128];
|
|
snprintf(header, sizeof(header),
|
|
"Elapsed: %.0fs | Simulated Cam (VideoPlayer) | Press ESC to stop", elapsed);
|
|
cv::putText(logPanel, header, cv::Point(10, 18),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(200, 200, 0), 1);
|
|
|
|
double totalFps = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
totalFps += taskStates[i].fps;
|
|
}
|
|
char aggLine[256];
|
|
snprintf(aggLine, sizeof(aggLine), "Total throughput: %.1f FPS | T0:GPU%d T1:GPU%d T2:GPU%d T3:GPU%d",
|
|
totalFps,
|
|
taskStates[0].gpuDeviceId, taskStates[1].gpuDeviceId,
|
|
taskStates[2].gpuDeviceId, taskStates[3].gpuDeviceId);
|
|
cv::putText(logPanel, aggLine, cv::Point(10, 38),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 255), 1);
|
|
|
|
auto gpuSnaps = QueryGpuVram();
|
|
int gpuLineY = 58;
|
|
for (const auto& gs : gpuSnaps) {
|
|
int tasksOnGpu = 0;
|
|
size_t taskVramMiB = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (taskStates[i].gpuDeviceId == gs.deviceId) {
|
|
tasksOnGpu++;
|
|
taskVramMiB += taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
}
|
|
char gpuLine[256];
|
|
snprintf(gpuLine, sizeof(gpuLine),
|
|
"GPU[%d] %s | Used: %zu/%zu MiB | Tasks: %d (engine VRAM: %zu MiB)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
tasksOnGpu, taskVramMiB);
|
|
cv::putText(logPanel, gpuLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(100, 255, 100), 1);
|
|
gpuLineY += 18;
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
char tLine[256];
|
|
snprintf(tLine, sizeof(tLine),
|
|
"T%d: GPU[%d] VRAM=%zuMiB FPS=%.1f Inf=%.0fms Frames=%d Det=%d",
|
|
i, taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].fps, taskStates[i].inferenceMs,
|
|
taskStates[i].frameCount, taskStates[i].detectionCount);
|
|
cv::putText(logPanel, tLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(200, 200, 200), 1);
|
|
gpuLineY += 16;
|
|
}
|
|
|
|
auto recentLogs = g_log.getRecent(4);
|
|
for (const auto& line : recentLogs) {
|
|
if (gpuLineY > logPanelH - 5) break;
|
|
std::string display = (line.size() > 130) ? line.substr(0, 127) + "..." : line;
|
|
cv::putText(logPanel, display, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(140, 140, 140), 1);
|
|
gpuLineY += 15;
|
|
}
|
|
|
|
cv::imshow(windowName, canvas);
|
|
int key = cv::waitKey(30);
|
|
if (key == 27) { // ESC
|
|
g_log.add("ESC pressed — stopping all tasks...");
|
|
printf("\nESC pressed — stopping...\n");
|
|
g_running.store(false);
|
|
}
|
|
}
|
|
|
|
// --- Wait for all workers ---
|
|
printf("Waiting for worker threads to finish...\n");
|
|
for (int i = 0; i < 4; i++) {
|
|
if (workers[i].joinable()) workers[i].join();
|
|
}
|
|
|
|
// --- Print final summary ---
|
|
double totalElapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
|
|
g_log.add("================================================================");
|
|
g_log.add(" FINAL PERFORMANCE SUMMARY (Simulated Cam)");
|
|
g_log.add(" Total runtime: " + std::to_string((int)totalElapsed) + " seconds");
|
|
g_log.add("================================================================");
|
|
|
|
printf("\n============================================================\n");
|
|
printf(" FINAL PERFORMANCE SUMMARY — Simulated Cam (runtime: %.0fs)\n", totalElapsed);
|
|
printf("============================================================\n");
|
|
|
|
double totalFpsFinal = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
" Task %d: GPU[%d] | VRAM=%zuMiB | %d frames, %d detections, FPS=%.1f, InfMs=%.0f",
|
|
i, taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].frameCount, taskStates[i].detectionCount,
|
|
taskStates[i].fps, taskStates[i].inferenceMs);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
totalFpsFinal += taskStates[i].fps;
|
|
}
|
|
|
|
auto finalGpu = QueryGpuVram();
|
|
for (const auto& gs : finalGpu) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " GPU[%d] %s: %zu/%zu MiB used (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
std::set<int> finalGpusUsed;
|
|
for (int i = 0; i < 4; i++) {
|
|
if (taskStates[i].gpuDeviceId >= 0) finalGpusUsed.insert(taskStates[i].gpuDeviceId);
|
|
}
|
|
{
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS across 4 tasks", totalFpsFinal);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
if (finalGpusUsed.size() > 1) {
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " MULTI-GPU: YES — tasks on %zu different GPUs", finalGpusUsed.size());
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
} else if (!finalGpusUsed.empty()) {
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " MULTI-GPU: NO — all tasks on GPU[%d] only", *finalGpusUsed.begin());
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
printf("============================================================\n");
|
|
g_log.add("================================================================");
|
|
g_log.add(" Log saved to: " + std::string(LOG_FILE_PATH));
|
|
g_log.add("================================================================");
|
|
|
|
// --- Release all handles ---
|
|
for (int i = 0; i < 4; i++) {
|
|
if (alprHandles[i]) {
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
}
|
|
}
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (vpClients[s]) {
|
|
StopVideoPlayer(&vpClients[s]);
|
|
ReleaseANSVideoPlayerHandle(&vpClients[s]);
|
|
}
|
|
}
|
|
|
|
g_log.close();
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
|
|
return 0;
|
|
}
|
|
|
|
// =============================================================================
|
|
// Worker thread for FilePlayer-based stress test (uses ANSFILEPLAYER)
|
|
// Key difference from VideoPlayer worker: uses GetFilePlayerCVImage/ReconnectFilePlayer
|
|
// =============================================================================
|
|
static void ALPRWorkerThread_FilePlayer(int taskId,
|
|
ANSCENTER::ANSFILEPLAYER* fpClient,
|
|
ANSCENTER::ANSALPR* alprHandle,
|
|
TaskState& state) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", taskId);
|
|
std::string prefix(tag);
|
|
|
|
g_log.add(prefix + " Worker thread started");
|
|
printf("%s Worker thread started\n", tag);
|
|
|
|
int width = 0, height = 0;
|
|
int64_t pts = 0;
|
|
int emptyFrames = 0;
|
|
std::string cameraId = "Cam" + std::to_string(taskId);
|
|
|
|
std::deque<std::chrono::steady_clock::time_point> fpsTimestamps;
|
|
double totalGrabMs = 0, totalInfMs = 0;
|
|
int grabCount = 0, infCount = 0;
|
|
double maxGrabMs = 0, maxInfMs = 0;
|
|
auto benchStart = std::chrono::steady_clock::now();
|
|
|
|
while (g_running.load()) {
|
|
auto grabStart = std::chrono::steady_clock::now();
|
|
cv::Mat* framePtr = nullptr;
|
|
GetFilePlayerCVImage(&fpClient, width, height, pts, &framePtr);
|
|
auto grabEnd = std::chrono::steady_clock::now();
|
|
double grabMs = std::chrono::duration<double, std::milli>(grabEnd - grabStart).count();
|
|
|
|
if (framePtr == nullptr || framePtr->empty()) {
|
|
emptyFrames++;
|
|
if (emptyFrames % 100 == 1) {
|
|
g_log.add(prefix + " Empty frame (count=" + std::to_string(emptyFrames) + ")");
|
|
}
|
|
if (emptyFrames > 300) {
|
|
g_log.add(prefix + " Too many empty frames, attempting reconnect...");
|
|
ReconnectFilePlayer(&fpClient);
|
|
emptyFrames = 0;
|
|
}
|
|
if (framePtr) delete framePtr;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
emptyFrames = 0;
|
|
|
|
totalGrabMs += grabMs;
|
|
grabCount++;
|
|
if (grabMs > maxGrabMs) maxGrabMs = grabMs;
|
|
|
|
auto infStart = std::chrono::steady_clock::now();
|
|
std::string lpnResult, jpegImage;
|
|
// Pass framePtr directly — NOT a copy. ANSGpuFrameRegistry::lookup()
|
|
// matches by cv::Mat* pointer, so `new cv::Mat(*framePtr)` would create
|
|
// a different pointer the registry doesn't know, breaking NV12 zero-copy.
|
|
ANSALPR_RunInferenceComplete_CPP(&alprHandle, &framePtr, cameraId.c_str(), 0, 0, lpnResult, jpegImage);
|
|
auto infEnd = std::chrono::steady_clock::now();
|
|
double infMs = std::chrono::duration<double, std::milli>(infEnd - infStart).count();
|
|
totalInfMs += infMs;
|
|
infCount++;
|
|
if (infMs > maxInfMs) maxInfMs = infMs;
|
|
|
|
cv::Mat display = framePtr->clone();
|
|
int detCount = 0;
|
|
std::string lastPlateText;
|
|
|
|
if (!lpnResult.empty()) {
|
|
try {
|
|
boost::property_tree::ptree pt;
|
|
std::stringstream ss(lpnResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& det = child.second;
|
|
const auto class_name = GetData<std::string>(det, "class_name");
|
|
const auto x = GetData<float>(det, "x");
|
|
const auto y = GetData<float>(det, "y");
|
|
const auto w = GetData<float>(det, "width");
|
|
const auto h = GetData<float>(det, "height");
|
|
cv::rectangle(display, cv::Rect((int)x, (int)y, (int)w, (int)h),
|
|
cv::Scalar(0, 255, 0), 2);
|
|
cv::putText(display, class_name, cv::Point((int)x, (int)y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2);
|
|
lastPlateText = class_name;
|
|
detCount++;
|
|
}
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
fpsTimestamps.push_back(now);
|
|
while (!fpsTimestamps.empty() &&
|
|
std::chrono::duration<double>(now - fpsTimestamps.front()).count() > 2.0) {
|
|
fpsTimestamps.pop_front();
|
|
}
|
|
double fps = fpsTimestamps.size() / 2.0;
|
|
|
|
char osd[128];
|
|
snprintf(osd, sizeof(osd), "Task%d | %.1f FPS | Inf: %.0f ms | #%d",
|
|
taskId, fps, infMs, state.frameCount + 1);
|
|
cv::putText(display, osd, cv::Point(10, 30),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 255), 2);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
state.displayFrame = display;
|
|
state.fps = fps;
|
|
state.inferenceMs = infMs;
|
|
state.lastGrabMs = grabMs;
|
|
state.lastInfMs = infMs;
|
|
state.frameCount++;
|
|
state.detectionCount += detCount;
|
|
if (!lastPlateText.empty()) state.lastPlate = lastPlateText;
|
|
}
|
|
|
|
if ((state.frameCount % 100) == 0) {
|
|
double avgGrab = grabCount > 0 ? totalGrabMs / grabCount : 0;
|
|
double avgInf = infCount > 0 ? totalInfMs / infCount : 0;
|
|
double elapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - benchStart).count();
|
|
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
"%s Frame %d | FPS=%.1f | Grab: avg=%.1fms max=%.0fms | Inf: avg=%.1fms max=%.0fms | "
|
|
"GrabPct=%.0f%% InfPct=%.0f%% | Det=%d",
|
|
tag, state.frameCount, fps,
|
|
avgGrab, maxGrabMs,
|
|
avgInf, maxInfMs,
|
|
(totalGrabMs / (elapsed * 1000.0)) * 100.0,
|
|
(totalInfMs / (elapsed * 1000.0)) * 100.0,
|
|
state.detectionCount);
|
|
g_log.add(buf);
|
|
printf("%s\n", buf);
|
|
|
|
totalGrabMs = totalInfMs = 0;
|
|
maxGrabMs = maxInfMs = 0;
|
|
grabCount = infCount = 0;
|
|
benchStart = std::chrono::steady_clock::now();
|
|
}
|
|
|
|
delete framePtr;
|
|
}
|
|
|
|
g_log.add(prefix + " Worker loop exited");
|
|
}
|
|
|
|
// =============================================================================
|
|
// ANSLPR_MultiGPU_StressTest_FilePlayer
|
|
// Same as SimulatedCam but uses ANSFILEPLAYER (loops video continuously).
|
|
// =============================================================================
|
|
int ANSLPR_MultiGPU_StressTest_FilePlayer() {
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
g_log.init();
|
|
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ANSLPR Multi-GPU Stress Test (FilePlayer — looping)\n");
|
|
printf(" Using local video files via ANSFilePlayer (HW decode)\n");
|
|
printf(" Press ESC to stop\n");
|
|
printf(" Log file: %s\n", LOG_FILE_PATH);
|
|
printf("============================================================\n\n");
|
|
|
|
g_log.add("============================================================");
|
|
g_log.add(" ANSLPR Multi-GPU Stress Test (FilePlayer — looping)");
|
|
g_log.add(" Using ANSFilePlayer with HW decode + NV12 zero-copy");
|
|
g_log.add("============================================================");
|
|
|
|
LogGpuInfo();
|
|
|
|
const std::string videoFile0 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day.mp4";
|
|
const std::string videoFile1 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_1.mp4";
|
|
const std::string videoFile2 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_2.mp4";
|
|
const std::string videoFile3 = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day_3.mp4";
|
|
|
|
g_log.add("Video 0: " + videoFile0);
|
|
g_log.add("Video 1: " + videoFile1);
|
|
g_log.add("Video 2: " + videoFile2);
|
|
g_log.add("Video 3: " + videoFile3);
|
|
|
|
TaskState taskStates[4];
|
|
|
|
// =========================================================================
|
|
// Create 4 FilePlayer readers
|
|
// =========================================================================
|
|
const int NUM_STREAMS = 4;
|
|
ANSCENTER::ANSFILEPLAYER* fpClients[NUM_STREAMS] = {};
|
|
const std::string videoFiles[NUM_STREAMS] = { videoFile0, videoFile1, videoFile2, videoFile3 };
|
|
const int taskStreamMap[4] = { 0, 1, 2, 3 };
|
|
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
printf("[Stream%d] Creating FilePlayer for %s\n", s, videoFiles[s].c_str());
|
|
g_log.add("[Stream" + std::to_string(s) + "] Creating FilePlayer for " + videoFiles[s]);
|
|
int result = CreateANSFilePlayerHandle(&fpClients[s], "", videoFiles[s].c_str());
|
|
if (result != 1 || fpClients[s] == nullptr) {
|
|
printf("[Stream%d] FAILED to create FilePlayer (result=%d)\n", s, result);
|
|
g_log.add("[Stream" + std::to_string(s) + "] FilePlayer create FAILED");
|
|
fpClients[s] = nullptr;
|
|
continue;
|
|
}
|
|
// Don't start yet — start after engines are loaded
|
|
SetFilePlayerDisplayResolution(&fpClients[s], 1920, 1080);
|
|
g_log.add("[Stream" + std::to_string(s) + "] FilePlayer created (display: 1920x1080)");
|
|
}
|
|
|
|
// =========================================================================
|
|
// Create 4 ALPR engines sequentially
|
|
// =========================================================================
|
|
ANSCENTER::ANSALPR* alprHandles[4] = {};
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
int engineType = 1;
|
|
double detThresh = 0.5, ocrThresh = 0.5, colThresh = 0.5;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
char tag[32];
|
|
snprintf(tag, sizeof(tag), "[Task%d]", i);
|
|
|
|
int streamIdx = taskStreamMap[i];
|
|
if (fpClients[streamIdx] == nullptr) {
|
|
printf("%s Skipped — Stream%d not available\n", tag, streamIdx);
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Stream not available";
|
|
continue;
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].streamOk = true;
|
|
taskStates[i].statusMsg = "Loading ALPR engine...";
|
|
}
|
|
|
|
printf("%s Creating ALPR handle (engineType=%d)...\n", tag, engineType);
|
|
g_log.add(std::string(tag) + " Creating ALPR handle...");
|
|
auto engineStart = std::chrono::steady_clock::now();
|
|
int createResult = CreateANSALPRHandle(&alprHandles[i], "", modelZipFile.c_str(), "",
|
|
engineType, detThresh, ocrThresh, colThresh);
|
|
if (createResult != 1 || alprHandles[i] == nullptr) {
|
|
printf("%s FAILED to create ALPR handle (result=%d)\n", tag, createResult);
|
|
g_log.add(std::string(tag) + " ALPR create FAILED");
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "ALPR create failed";
|
|
continue;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandles[i], 1);
|
|
|
|
printf("%s Loading ALPR engine (TensorRT)...\n", tag);
|
|
g_log.add(std::string(tag) + " Loading ALPR engine...");
|
|
|
|
auto vramBefore = GetPerGpuFreeMiB();
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandles[i]);
|
|
auto engineEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
|
|
if (loadResult != 1) {
|
|
printf("%s FAILED to load ALPR engine (result=%d)\n", tag, loadResult);
|
|
g_log.add(std::string(tag) + " Engine load FAILED");
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
alprHandles[i] = nullptr;
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].statusMsg = "Engine load failed";
|
|
continue;
|
|
}
|
|
|
|
auto vramAfter = GetPerGpuFreeMiB();
|
|
int bestGpu = 0;
|
|
size_t maxDelta = 0;
|
|
for (size_t g = 0; g < vramBefore.size() && g < vramAfter.size(); g++) {
|
|
size_t delta = (vramBefore[g] > vramAfter[g]) ? vramBefore[g] - vramAfter[g] : 0;
|
|
if (delta > maxDelta) { maxDelta = delta; bestGpu = (int)g; }
|
|
}
|
|
|
|
char ebuf[256];
|
|
snprintf(ebuf, sizeof(ebuf), "%s Engine loaded in %d ms | GPU[%d] | VRAM used: %zu MiB (Video%d)",
|
|
tag, (int)loadMs, bestGpu, maxDelta, i);
|
|
printf("%s\n", ebuf);
|
|
g_log.add(ebuf);
|
|
|
|
for (size_t g = 0; g < vramAfter.size(); g++) {
|
|
size_t total = 0;
|
|
cudaDeviceProp prop;
|
|
if (cudaGetDeviceProperties(&prop, (int)g) == cudaSuccess) {
|
|
total = prop.totalGlobalMem / (1024 * 1024);
|
|
}
|
|
char vbuf[128];
|
|
snprintf(vbuf, sizeof(vbuf), " GPU[%zu] VRAM: %zu MiB free (of %zu MiB)", g, vramAfter[g], total);
|
|
printf("%s\n", vbuf);
|
|
g_log.add(vbuf);
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
taskStates[i].engineLoaded = true;
|
|
taskStates[i].statusMsg = "Running";
|
|
taskStates[i].gpuDeviceId = bestGpu;
|
|
taskStates[i].vramUsedBytes = maxDelta * 1024 * 1024;
|
|
}
|
|
}
|
|
|
|
// --- Enable debug benchmarking ---
|
|
for (int i = 0; i < 4; i++) {
|
|
if (alprHandles[i]) {
|
|
alprHandles[i]->ActivateDebugger(true);
|
|
}
|
|
}
|
|
g_log.add("Debug benchmarking ENABLED on all ALPR handles");
|
|
|
|
// --- Start video playback NOW (just before workers need frames) ---
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (fpClients[s]) {
|
|
StartFilePlayer(&fpClients[s]);
|
|
g_log.add("[Stream" + std::to_string(s) + "] FilePlayer play() started");
|
|
}
|
|
}
|
|
|
|
// --- Launch worker threads ---
|
|
g_log.add("Launching worker threads...");
|
|
std::thread workers[4];
|
|
for (int i = 0; i < 4; i++) {
|
|
int streamIdx = taskStreamMap[i];
|
|
if (fpClients[streamIdx] && alprHandles[i]) {
|
|
workers[i] = std::thread(ALPRWorkerThread_FilePlayer, i,
|
|
fpClients[streamIdx], alprHandles[i],
|
|
std::ref(taskStates[i]));
|
|
}
|
|
}
|
|
|
|
// --- Display loop (main thread) ---
|
|
const int cellW = 640, cellH = 480;
|
|
const int logPanelH = 200;
|
|
const char* windowName = "ANSLPR Stress Test (FilePlayer — looping)";
|
|
cv::namedWindow(windowName, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowName, cellW * 2, cellH * 2 + logPanelH);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
auto lastGpuSnapshot = std::chrono::steady_clock::now();
|
|
int snapshotCount = 0;
|
|
|
|
while (g_running.load()) {
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
if (std::chrono::duration<double>(now2 - lastGpuSnapshot).count() >= 10.0) {
|
|
lastGpuSnapshot = now2;
|
|
snapshotCount++;
|
|
double elapsedSec = std::chrono::duration<double>(now2 - testStart).count();
|
|
g_log.add("---- PERIODIC SNAPSHOT #" + std::to_string(snapshotCount)
|
|
+ " (elapsed " + std::to_string((int)elapsedSec) + "s) ----");
|
|
auto gpuSnap = QueryGpuVram();
|
|
for (const auto& gs : gpuSnap) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" GPU[%d] %s | Used: %zu/%zu MiB (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
g_log.add(buf);
|
|
}
|
|
double totalFpsSnap = 0;
|
|
for (int t = 0; t < 4; t++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[t].mtx);
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf),
|
|
" T%d: GPU[%d] VRAM=%zuMiB FPS=%.1f GrabMs=%.0f InfMs=%.0f Frames=%d Det=%d",
|
|
t, taskStates[t].gpuDeviceId,
|
|
taskStates[t].vramUsedBytes / (1024 * 1024),
|
|
taskStates[t].fps, taskStates[t].lastGrabMs, taskStates[t].inferenceMs,
|
|
taskStates[t].frameCount, taskStates[t].detectionCount);
|
|
g_log.add(buf);
|
|
totalFpsSnap += taskStates[t].fps;
|
|
}
|
|
char buf[128];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS", totalFpsSnap);
|
|
g_log.add(buf);
|
|
std::set<int> gpusUsed;
|
|
for (int t = 0; t < 4; t++) {
|
|
if (taskStates[t].gpuDeviceId >= 0) gpusUsed.insert(taskStates[t].gpuDeviceId);
|
|
}
|
|
if (gpusUsed.size() > 1) {
|
|
g_log.add(" MULTI-GPU: YES — tasks distributed across " + std::to_string(gpusUsed.size()) + " GPUs");
|
|
} else if (!gpusUsed.empty()) {
|
|
g_log.add(" MULTI-GPU: NO — all tasks on GPU[" + std::to_string(*gpusUsed.begin()) + "]");
|
|
}
|
|
g_log.add("---- END SNAPSHOT ----");
|
|
}
|
|
|
|
// Build 2x2 grid + log panel
|
|
cv::Mat canvas(cellH * 2 + logPanelH, cellW * 2, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
int row = i / 2, col = i % 2;
|
|
cv::Rect roi(col * cellW, row * cellH, cellW, cellH);
|
|
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
int gpuId = -1;
|
|
size_t vramMiB = 0;
|
|
std::string statusMsg, lastPlate;
|
|
bool engineLoaded = false, streamOk = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (!taskStates[i].displayFrame.empty()) {
|
|
cv::resize(taskStates[i].displayFrame, cell, cv::Size(cellW, cellH));
|
|
}
|
|
fps = taskStates[i].fps;
|
|
infMs = taskStates[i].inferenceMs;
|
|
fCount = taskStates[i].frameCount;
|
|
dCount = taskStates[i].detectionCount;
|
|
statusMsg = taskStates[i].statusMsg;
|
|
lastPlate = taskStates[i].lastPlate;
|
|
engineLoaded = taskStates[i].engineLoaded;
|
|
streamOk = taskStates[i].streamOk;
|
|
gpuId = taskStates[i].gpuDeviceId;
|
|
vramMiB = taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
|
|
if (cell.empty()) {
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
cv::putText(cell, "Task " + std::to_string(i) + ": " + statusMsg,
|
|
cv::Point(20, cellH / 2),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(100, 100, 255), 2);
|
|
}
|
|
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 50, cellW, 50), cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar1[256], bar2[256];
|
|
snprintf(bar1, sizeof(bar1), "T%d | %.1f FPS | %.0fms | Frames:%d | Det:%d | %s",
|
|
i, fps, infMs, fCount, dCount,
|
|
lastPlate.empty() ? "-" : lastPlate.c_str());
|
|
if (gpuId >= 0) {
|
|
snprintf(bar2, sizeof(bar2), "GPU[%d] | VRAM: %zu MiB", gpuId, vramMiB);
|
|
} else {
|
|
snprintf(bar2, sizeof(bar2), "GPU: N/A");
|
|
}
|
|
cv::Scalar barColor = engineLoaded ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 100, 255);
|
|
cv::putText(cell, bar1, cv::Point(5, cellH - 28),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, barColor, 1);
|
|
cv::putText(cell, bar2, cv::Point(5, cellH - 8),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(0, 200, 255), 1);
|
|
|
|
cell.copyTo(canvas(roi));
|
|
|
|
cv::line(canvas, cv::Point(cellW, 0), cv::Point(cellW, cellH * 2),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
cv::line(canvas, cv::Point(0, cellH), cv::Point(cellW * 2, cellH),
|
|
cv::Scalar(100, 100, 100), 1);
|
|
}
|
|
|
|
// Log panel
|
|
cv::Rect logRoi(0, cellH * 2, cellW * 2, logPanelH);
|
|
cv::Mat logPanel = canvas(logRoi);
|
|
logPanel.setTo(cv::Scalar(20, 20, 20));
|
|
|
|
auto elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - testStart).count();
|
|
char header[128];
|
|
snprintf(header, sizeof(header),
|
|
"Elapsed: %.0fs | FilePlayer (looping, HW decode) | Press ESC to stop", elapsed);
|
|
cv::putText(logPanel, header, cv::Point(10, 18),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(200, 200, 0), 1);
|
|
|
|
double totalFps = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
totalFps += taskStates[i].fps;
|
|
}
|
|
char aggLine[256];
|
|
snprintf(aggLine, sizeof(aggLine), "Total throughput: %.1f FPS | T0:GPU%d T1:GPU%d T2:GPU%d T3:GPU%d",
|
|
totalFps,
|
|
taskStates[0].gpuDeviceId, taskStates[1].gpuDeviceId,
|
|
taskStates[2].gpuDeviceId, taskStates[3].gpuDeviceId);
|
|
cv::putText(logPanel, aggLine, cv::Point(10, 38),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 255), 1);
|
|
|
|
auto gpuSnaps = QueryGpuVram();
|
|
int gpuLineY = 58;
|
|
for (const auto& gs : gpuSnaps) {
|
|
int tasksOnGpu = 0;
|
|
size_t taskVramMiB = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
if (taskStates[i].gpuDeviceId == gs.deviceId) {
|
|
tasksOnGpu++;
|
|
taskVramMiB += taskStates[i].vramUsedBytes / (1024 * 1024);
|
|
}
|
|
}
|
|
char gpuLine[256];
|
|
snprintf(gpuLine, sizeof(gpuLine),
|
|
"GPU[%d] %s | Used: %zu/%zu MiB | Tasks: %d (engine VRAM: %zu MiB)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
tasksOnGpu, taskVramMiB);
|
|
cv::putText(logPanel, gpuLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.45, cv::Scalar(100, 255, 100), 1);
|
|
gpuLineY += 18;
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
std::lock_guard<std::mutex> lk(taskStates[i].mtx);
|
|
char tLine[256];
|
|
snprintf(tLine, sizeof(tLine),
|
|
"T%d: GPU[%d] VRAM=%zuMiB FPS=%.1f Inf=%.0fms Frames=%d Det=%d",
|
|
i, taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].fps, taskStates[i].inferenceMs,
|
|
taskStates[i].frameCount, taskStates[i].detectionCount);
|
|
cv::putText(logPanel, tLine, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(200, 200, 200), 1);
|
|
gpuLineY += 16;
|
|
}
|
|
|
|
auto recentLogs = g_log.getRecent(4);
|
|
for (const auto& line : recentLogs) {
|
|
if (gpuLineY > logPanelH - 5) break;
|
|
std::string display = (line.size() > 130) ? line.substr(0, 127) + "..." : line;
|
|
cv::putText(logPanel, display, cv::Point(10, gpuLineY),
|
|
cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(140, 140, 140), 1);
|
|
gpuLineY += 15;
|
|
}
|
|
|
|
cv::imshow(windowName, canvas);
|
|
int key = cv::waitKey(30);
|
|
if (key == 27) {
|
|
g_log.add("ESC pressed — stopping all tasks...");
|
|
printf("\nESC pressed — stopping...\n");
|
|
g_running.store(false);
|
|
}
|
|
}
|
|
|
|
// --- Wait for all workers ---
|
|
printf("Waiting for worker threads to finish...\n");
|
|
for (int i = 0; i < 4; i++) {
|
|
if (workers[i].joinable()) workers[i].join();
|
|
}
|
|
|
|
// --- Final summary ---
|
|
double totalElapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
|
|
g_log.add("================================================================");
|
|
g_log.add(" FINAL PERFORMANCE SUMMARY (FilePlayer — looping)");
|
|
g_log.add(" Total runtime: " + std::to_string((int)totalElapsed) + " seconds");
|
|
g_log.add("================================================================");
|
|
|
|
printf("\n============================================================\n");
|
|
printf(" FINAL PERFORMANCE SUMMARY — FilePlayer (runtime: %.0fs)\n", totalElapsed);
|
|
printf("============================================================\n");
|
|
|
|
double totalFpsFinal = 0;
|
|
for (int i = 0; i < 4; i++) {
|
|
char buf[512];
|
|
snprintf(buf, sizeof(buf),
|
|
" Task %d: GPU[%d] | VRAM=%zuMiB | %d frames, %d detections, FPS=%.1f, InfMs=%.0f",
|
|
i, taskStates[i].gpuDeviceId,
|
|
taskStates[i].vramUsedBytes / (1024 * 1024),
|
|
taskStates[i].frameCount, taskStates[i].detectionCount,
|
|
taskStates[i].fps, taskStates[i].inferenceMs);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
totalFpsFinal += taskStates[i].fps;
|
|
}
|
|
|
|
auto finalGpu = QueryGpuVram();
|
|
for (const auto& gs : finalGpu) {
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " GPU[%d] %s: %zu/%zu MiB used (%.1f%%)",
|
|
gs.deviceId, gs.name.c_str(), gs.usedMiB, gs.totalMiB,
|
|
gs.totalMiB > 0 ? 100.0 * gs.usedMiB / gs.totalMiB : 0.0);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
{
|
|
char buf[256];
|
|
snprintf(buf, sizeof(buf), " Total throughput: %.1f FPS across 4 tasks", totalFpsFinal);
|
|
printf("%s\n", buf);
|
|
g_log.add(buf);
|
|
}
|
|
|
|
printf("============================================================\n");
|
|
g_log.add("================================================================");
|
|
|
|
// --- Release all handles ---
|
|
for (int i = 0; i < 4; i++) {
|
|
if (alprHandles[i]) {
|
|
ReleaseANSALPRHandle(&alprHandles[i]);
|
|
}
|
|
}
|
|
for (int s = 0; s < NUM_STREAMS; s++) {
|
|
if (fpClients[s]) {
|
|
StopFilePlayer(&fpClients[s]);
|
|
ReleaseANSFilePlayerHandle(&fpClients[s]);
|
|
}
|
|
}
|
|
|
|
g_log.close();
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
|
|
return 0;
|
|
}
|
|
|
|
// ANSLPR_OD_CPU_VideoTest — Uses ANSALPR_OD (engineType=1) on Intel CPU/iGPU.
|
|
// ANSALPR_OD auto-detects hardware (OpenVINO on Intel, DirectML on AMD, etc.)
|
|
// No CUDA calls — safe on non-NVIDIA systems.
|
|
int ANSLPR_OD_CPU_VideoTest() {
|
|
std::cout << "\n============================================================" << std::endl;
|
|
std::cout << " ANSLPR CPU/iGPU Test (ANSALPR_OD with auto-detect)" << std::endl;
|
|
std::cout << "============================================================\n" << std::endl;
|
|
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";
|
|
std::string videoFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4";
|
|
|
|
std::cout << "Model: " << modelZipFile << std::endl;
|
|
std::cout << "Video: " << videoFilePath << std::endl;
|
|
|
|
ANSCENTER::ANSALPR* infHandle = nullptr;
|
|
int engineType = 1; // ANSALPR_OD (auto-detects HW internally)
|
|
double detThresh = 0.5, ocrThresh = 0.5, colThresh = 0.5;
|
|
|
|
// Step 1: Create handle
|
|
std::cout << "[LPR-CPU] Step 1: Creating handle..." << std::endl;
|
|
int createResult = CreateANSALPRHandle(&infHandle, "", modelZipFile.c_str(), "",
|
|
engineType, detThresh, ocrThresh, colThresh);
|
|
std::cout << "[LPR-CPU] CreateANSALPRHandle result: " << createResult << std::endl;
|
|
if (createResult != 1 || infHandle == nullptr) {
|
|
std::cerr << "[LPR-CPU] FAILED: CreateANSALPRHandle returned " << createResult << std::endl;
|
|
return -1;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
// Step 2: Load engine
|
|
std::cout << "[LPR-CPU] Step 2: Loading engine..." << std::endl;
|
|
int loadResult = LoadANSALPREngineHandle(&infHandle);
|
|
std::cout << "[LPR-CPU] LoadANSALPREngineHandle result: " << loadResult << std::endl;
|
|
if (loadResult != 1) {
|
|
std::cerr << "[LPR-CPU] FAILED: LoadANSALPREngineHandle returned " << loadResult << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -2;
|
|
}
|
|
|
|
// Step 3: Open video
|
|
std::cout << "[LPR-CPU] Step 3: Opening video..." << std::endl;
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
std::cerr << "[LPR-CPU] FAILED: Could not open video: " << videoFilePath << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -3;
|
|
}
|
|
|
|
int totalFrames = static_cast<int>(capture.get(cv::CAP_PROP_FRAME_COUNT));
|
|
std::cout << "[LPR-CPU] Video opened: " << totalFrames << " frames" << std::endl;
|
|
|
|
// Step 4: Run inference
|
|
std::cout << "[LPR-CPU] Step 4: Running inference..." << std::endl;
|
|
boost::property_tree::ptree pt;
|
|
int frameIndex = 0;
|
|
int totalDetections = 0;
|
|
double totalInferenceMs = 0.0;
|
|
int maxFrames = 200;
|
|
|
|
while (frameIndex < maxFrames) {
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) {
|
|
std::cout << "[LPR-CPU] End of video at frame " << frameIndex << std::endl;
|
|
break;
|
|
}
|
|
frameIndex++;
|
|
|
|
unsigned int bufferLength = 0;
|
|
unsigned char* jpeg_bytes = CVMatToBytes(frame, bufferLength);
|
|
int height = frame.rows;
|
|
int width = frame.cols;
|
|
|
|
auto start = std::chrono::system_clock::now();
|
|
std::string detectionResult = ANSALPR_RunInferenceBinary(&infHandle, jpeg_bytes, width, height);
|
|
auto end = std::chrono::system_clock::now();
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
totalInferenceMs += static_cast<double>(elapsed.count());
|
|
|
|
delete[] jpeg_bytes;
|
|
|
|
if (!detectionResult.empty()) {
|
|
try {
|
|
pt.clear();
|
|
std::stringstream ss;
|
|
ss << detectionResult;
|
|
boost::property_tree::read_json(ss, pt);
|
|
int detCount = 0;
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& r = child.second;
|
|
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 w = GetData<float>(r, "width");
|
|
const auto h = GetData<float>(r, "height");
|
|
detCount++;
|
|
cv::rectangle(frame, cv::Rect(x, y, w, h), cv::Scalar(0, 255, 0), 2);
|
|
cv::putText(frame, class_name, cv::Point(x, y - 5),
|
|
0, 0.6, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
|
|
}
|
|
totalDetections += detCount;
|
|
}
|
|
catch (...) {}
|
|
}
|
|
|
|
if (frameIndex % 10 == 0) {
|
|
double avgSoFar = totalInferenceMs / frameIndex;
|
|
std::cout << "[LPR-CPU] Frame " << frameIndex << "/" << maxFrames
|
|
<< " | Time: " << elapsed.count() << "ms"
|
|
<< " | Avg: " << static_cast<int>(avgSoFar) << "ms"
|
|
<< " | Detections: " << totalDetections << std::endl;
|
|
}
|
|
|
|
cv::imshow("ANSLPR CPU Test", frame);
|
|
if (cv::waitKey(1) == 27) break;
|
|
}
|
|
|
|
// Summary
|
|
double avgMs = (frameIndex > 0) ? (totalInferenceMs / frameIndex) : 0.0;
|
|
std::cout << "\n=== LPR CPU Test Summary ===" << std::endl;
|
|
std::cout << "Frames processed: " << frameIndex << std::endl;
|
|
std::cout << "Total detections: " << totalDetections << std::endl;
|
|
std::cout << "Avg inference: " << avgMs << " ms/frame" << std::endl;
|
|
std::cout << "Total time: " << totalInferenceMs << " ms" << std::endl;
|
|
std::cout << (frameIndex > 0 ? "[LPR-CPU] PASSED" : "[LPR-CPU] FAILED") << std::endl;
|
|
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return (frameIndex > 0) ? 0 : -4;
|
|
}
|
|
|
|
// ── ANSALPR_OCR test: Japanese license plate detection using ANSONNXOCR ──
|
|
// Render UTF-8 text onto a cv::Mat using Windows GDI (supports CJK/Unicode).
|
|
// cv::putText only handles ASCII — Japanese characters render as '?'.
|
|
#ifdef WIN32
|
|
static void putTextUnicode(cv::Mat& img, const std::string& text, cv::Point org,
|
|
double fontScale, cv::Scalar color, int thickness) {
|
|
int wlen = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, nullptr, 0);
|
|
std::wstring wtext(wlen - 1, 0);
|
|
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, &wtext[0], wlen);
|
|
|
|
HDC hdc = CreateCompatibleDC(nullptr);
|
|
int fontHeight = (int)(fontScale * 30);
|
|
|
|
HFONT hFont = CreateFontW(fontHeight, 0, 0, 0,
|
|
(thickness > 2) ? FW_BOLD : FW_NORMAL,
|
|
FALSE, FALSE, FALSE,
|
|
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
|
|
ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Yu Gothic UI");
|
|
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
|
|
|
|
SIZE sz;
|
|
GetTextExtentPoint32W(hdc, wtext.c_str(), (int)wtext.size(), &sz);
|
|
|
|
BITMAPINFO bmi = {};
|
|
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
bmi.bmiHeader.biWidth = sz.cx;
|
|
bmi.bmiHeader.biHeight = -sz.cy;
|
|
bmi.bmiHeader.biPlanes = 1;
|
|
bmi.bmiHeader.biBitCount = 32;
|
|
bmi.bmiHeader.biCompression = BI_RGB;
|
|
void* bits = nullptr;
|
|
HBITMAP hBmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &bits, nullptr, 0);
|
|
HBITMAP hOldBmp = (HBITMAP)SelectObject(hdc, hBmp);
|
|
|
|
SetBkMode(hdc, TRANSPARENT);
|
|
SetTextColor(hdc, RGB((int)color[2], (int)color[1], (int)color[0]));
|
|
TextOutW(hdc, 0, 0, wtext.c_str(), (int)wtext.size());
|
|
|
|
cv::Mat textImg(sz.cy, sz.cx, CV_8UC4, bits);
|
|
for (int row = 0; row < sz.cy; ++row) {
|
|
for (int col = 0; col < sz.cx; ++col) {
|
|
cv::Vec4b px = textImg.at<cv::Vec4b>(row, col);
|
|
if (px[0] != 0 || px[1] != 0 || px[2] != 0) {
|
|
int dy = org.y + row;
|
|
int dx = org.x + col;
|
|
if (dy >= 0 && dy < img.rows && dx >= 0 && dx < img.cols) {
|
|
img.at<cv::Vec3b>(dy, dx) = cv::Vec3b(px[0], px[1], px[2]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SelectObject(hdc, hOldBmp);
|
|
SelectObject(hdc, hOldFont);
|
|
DeleteObject(hBmp);
|
|
DeleteObject(hFont);
|
|
DeleteDC(hdc);
|
|
}
|
|
#endif
|
|
|
|
int ALPR_OCR_Test() {
|
|
std::cout << "=== ALPR_OCR_Test: Japanese License Plate (ANSALPR_OCR) ===" << std::endl;
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
|
|
ANSCENTER::ANSALPR* infHandle = nullptr;
|
|
std::string licenseKey = "";
|
|
std::string modelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_GenericALPR_v2.0.zip";
|
|
std::string imagePath = "C:\\Programs\\ModelTraining\\JLPD\\data\\test6.jpg";
|
|
|
|
int engineType = 2; // ANSALPR_OCR
|
|
double detectionThreshold = 0.3;
|
|
double ocrThreshold = 0.5;
|
|
double colourThreshold = 0.0; // No colour detection for this test
|
|
|
|
// Step 1: Create handle
|
|
int createResult = CreateANSALPRHandle(&infHandle, licenseKey.c_str(),
|
|
modelFilePath.c_str(), "", engineType, detectionThreshold, ocrThreshold, colourThreshold);
|
|
std::cout << "CreateANSALPRHandle result: " << createResult << std::endl;
|
|
if (!createResult || !infHandle) {
|
|
std::cerr << "Failed to create ANSALPR_OCR handle" << std::endl;
|
|
return -1;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
// Step 2: Set country to Japan
|
|
ANSALPR_SetCountry(&infHandle, 5); // JAPAN = 5
|
|
std::cout << "Country set to JAPAN" << std::endl;
|
|
|
|
// Step 3: Load engine
|
|
auto engineStart = std::chrono::high_resolution_clock::now();
|
|
int loadResult = LoadANSALPREngineHandle(&infHandle);
|
|
auto engineEnd = std::chrono::high_resolution_clock::now();
|
|
double engineMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
std::cout << "LoadANSALPREngineHandle result: " << loadResult << " (" << engineMs << " ms)" << std::endl;
|
|
if (!loadResult) {
|
|
std::cerr << "Failed to load ANSALPR_OCR engine" << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -2;
|
|
}
|
|
|
|
// Step 4: Load image
|
|
cv::Mat input = cv::imread(imagePath, cv::IMREAD_COLOR);
|
|
if (input.empty()) {
|
|
std::cerr << "Failed to load image: " << imagePath << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -3;
|
|
}
|
|
std::cout << "Image loaded: " << input.cols << "x" << input.rows << std::endl;
|
|
|
|
cv::Mat frame = input.clone();
|
|
int width = frame.cols;
|
|
int height = frame.rows;
|
|
|
|
// Convert to raw BGR bytes for RunInferenceBinary
|
|
unsigned int bufferLength = static_cast<unsigned int>(frame.total() * frame.elemSize());
|
|
unsigned char* imageBytes = new unsigned char[bufferLength];
|
|
std::memcpy(imageBytes, frame.data, bufferLength);
|
|
|
|
// Step 5: Warmup run
|
|
auto warmupStart = std::chrono::high_resolution_clock::now();
|
|
std::string detectionResult = ANSALPR_RunInferenceBinary(&infHandle, imageBytes, width, height);
|
|
auto warmupEnd = std::chrono::high_resolution_clock::now();
|
|
double warmupMs = std::chrono::duration<double, std::milli>(warmupEnd - warmupStart).count();
|
|
std::cout << "Warmup inference: " << warmupMs << " ms" << std::endl;
|
|
std::cout << "ALPR Result: " << detectionResult << std::endl;
|
|
|
|
// Step 6: Benchmark
|
|
const int benchmarkIterations = 10;
|
|
std::vector<double> times;
|
|
times.reserve(benchmarkIterations);
|
|
for (int i = 0; i < benchmarkIterations; ++i) {
|
|
auto t0 = std::chrono::high_resolution_clock::now();
|
|
std::string result = ANSALPR_RunInferenceBinary(&infHandle, imageBytes, width, height);
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
times.push_back(ms);
|
|
std::cout << " Run " << (i + 1) << "/" << benchmarkIterations << ": " << ms << " ms" << std::endl;
|
|
}
|
|
std::sort(times.begin(), times.end());
|
|
double sum = std::accumulate(times.begin(), times.end(), 0.0);
|
|
double avg = sum / benchmarkIterations;
|
|
double median = (benchmarkIterations % 2 == 0)
|
|
? (times[benchmarkIterations / 2 - 1] + times[benchmarkIterations / 2]) / 2.0
|
|
: times[benchmarkIterations / 2];
|
|
std::cout << "\n=== Benchmark (" << benchmarkIterations << " runs) ===" << std::endl;
|
|
std::cout << " Avg: " << avg << " ms" << std::endl;
|
|
std::cout << " Median: " << median << " ms" << std::endl;
|
|
std::cout << " Min: " << times.front() << " ms" << std::endl;
|
|
std::cout << " Max: " << times.back() << " ms" << std::endl;
|
|
std::cout << " FPS: " << (1000.0 / avg) << std::endl;
|
|
|
|
delete[] imageBytes;
|
|
|
|
// Step 7: Draw results on image
|
|
if (!detectionResult.empty()) {
|
|
try {
|
|
boost::property_tree::ptree pt;
|
|
std::stringstream ss(detectionResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& res = child.second;
|
|
const auto class_name_raw = GetData<std::string>(res, "class_name");
|
|
const std::string class_name = DecodeUnicodeEscapes(class_name_raw);
|
|
const auto x = GetData<int>(res, "x");
|
|
const auto y = GetData<int>(res, "y");
|
|
const auto w = GetData<int>(res, "width");
|
|
const auto h = GetData<int>(res, "height");
|
|
|
|
cv::rectangle(frame, cv::Rect(x, y, w, h), cv::Scalar(0, 255, 0), 2);
|
|
|
|
std::string extraInfo = GetOptionalValue<std::string>(res, "extra_info", "");
|
|
std::cout << " Plate: " << class_name << std::endl;
|
|
if (!extraInfo.empty()) {
|
|
std::cout << " extra_info: " << extraInfo << std::endl;
|
|
}
|
|
|
|
#ifdef WIN32
|
|
{
|
|
int textH = (int)(1.5 * 30);
|
|
int ty = y - 5 - textH;
|
|
if (ty < 0) ty = y + 3;
|
|
putTextUnicode(frame, class_name, cv::Point(x, ty),
|
|
1.5, cv::Scalar(0, 0, 255), 3);
|
|
}
|
|
#else
|
|
cv::putText(frame, class_name, cv::Point(x, y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
#endif
|
|
}
|
|
}
|
|
catch (const std::exception& e) {
|
|
std::cerr << "JSON parse error: " << e.what() << std::endl;
|
|
}
|
|
}
|
|
|
|
// Step 8: Display result
|
|
cv::Mat display;
|
|
double scale = std::min(1920.0 / frame.cols, 1080.0 / frame.rows);
|
|
if (scale < 1.0) {
|
|
cv::resize(frame, display, cv::Size(), scale, scale);
|
|
} else {
|
|
display = frame;
|
|
}
|
|
cv::namedWindow("ALPR_OCR_Test", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ALPR_OCR_Test", display);
|
|
cv::waitKey(0);
|
|
|
|
// Cleanup
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
cv::destroyAllWindows();
|
|
frame.release();
|
|
input.release();
|
|
|
|
std::cout << "=== ALPR_OCR_Test complete ===" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
int ALPR_OCR_VideoTest() {
|
|
std::cout << "=== ALPR_OCR_VideoTest: ANSALPR_OCR engine on video ===" << std::endl;
|
|
std::filesystem::path currentPath = std::filesystem::current_path();
|
|
std::cout << "Current working directory: " << currentPath << std::endl;
|
|
|
|
ANSCENTER::ANSALPR* infHandle = nullptr;
|
|
std::string licenseKey = "";
|
|
std::string modelFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_GenericALPR_v2.0.zip";
|
|
std::string videoFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\HM1.mp4";
|
|
|
|
int engineType = 2; // ANSALPR_OCR
|
|
double detectionThreshold = 0.3;
|
|
double ocrThreshold = 0.5;
|
|
double colourThreshold = 0.0;
|
|
|
|
// Step 1: Create handle
|
|
int createResult = CreateANSALPRHandle(&infHandle, licenseKey.c_str(),
|
|
modelFilePath.c_str(), "", engineType, detectionThreshold, ocrThreshold, colourThreshold);
|
|
std::cout << "CreateANSALPRHandle result: " << createResult << std::endl;
|
|
if (!createResult || !infHandle) {
|
|
std::cerr << "Failed to create ANSALPR_OCR handle" << std::endl;
|
|
return -1;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
// Step 2: Set country (VIETNAM = 0 — adjust to match the dataset if needed).
|
|
// Country codes match country.txt parsing in ANSALPR_OCR::Initialize:
|
|
// 0 = VIETNAM, 1 = CHINA, 2 = AUSTRALIA, 3 = USA, 4 = INDONESIA, 5 = JAPAN.
|
|
// Setting VIETNAM here also activates ANSALPR_OCR's Vietnam-only
|
|
// post-processing (alnum filter + uppercase + plate-format validation
|
|
// against the 11 standard Vietnam patterns).
|
|
ANSALPR_SetCountry(&infHandle, 0);
|
|
std::cout << "Country set to VIETNAM" << std::endl;
|
|
|
|
// Step 3: Load engine
|
|
auto engineStart = std::chrono::high_resolution_clock::now();
|
|
int loadResult = LoadANSALPREngineHandle(&infHandle);
|
|
auto engineEnd = std::chrono::high_resolution_clock::now();
|
|
double engineMs = std::chrono::duration<double, std::milli>(engineEnd - engineStart).count();
|
|
std::cout << "LoadANSALPREngineHandle result: " << loadResult << " (" << engineMs << " ms)" << std::endl;
|
|
if (!loadResult) {
|
|
std::cerr << "Failed to load ANSALPR_OCR engine" << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -2;
|
|
}
|
|
|
|
// Step 4: Open video
|
|
cv::VideoCapture capture(videoFilePath);
|
|
if (!capture.isOpened()) {
|
|
std::cerr << "Could not open video file: " << videoFilePath << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -3;
|
|
}
|
|
|
|
boost::property_tree::ptree pt;
|
|
int frameIdx = 0;
|
|
|
|
while (true) {
|
|
cv::Mat frame;
|
|
if (!capture.read(frame)) {
|
|
std::cout << "\nEnd of video stream — restarting from beginning.\n";
|
|
capture.set(cv::CAP_PROP_POS_FRAMES, 0);
|
|
if (!capture.read(frame)) {
|
|
// Some backends need a full reopen to rewind cleanly.
|
|
capture.release();
|
|
capture.open(videoFilePath);
|
|
if (!capture.isOpened() || !capture.read(frame)) {
|
|
std::cerr << "Failed to restart video — stopping.\n";
|
|
break;
|
|
}
|
|
}
|
|
frameIdx = 0;
|
|
}
|
|
++frameIdx;
|
|
|
|
int width = frame.cols;
|
|
int height = frame.rows;
|
|
|
|
// Convert to raw BGR bytes for ANSALPR_RunInferenceBinary
|
|
unsigned int bufferLength = static_cast<unsigned int>(frame.total() * frame.elemSize());
|
|
unsigned char* imageBytes = new unsigned char[bufferLength];
|
|
std::memcpy(imageBytes, frame.data, bufferLength);
|
|
|
|
auto t0 = std::chrono::high_resolution_clock::now();
|
|
std::string detectionResult = ANSALPR_RunInferenceBinary(&infHandle, imageBytes, width, height);
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
double inferMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
delete[] imageBytes;
|
|
|
|
printf("Frame %d: %.2f ms (%.1f FPS)\n", frameIdx, inferMs,
|
|
inferMs > 0.0 ? (1000.0 / inferMs) : 0.0);
|
|
|
|
// Draw detections
|
|
if (!detectionResult.empty()) {
|
|
try {
|
|
pt.clear();
|
|
std::stringstream ss(detectionResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) {
|
|
const boost::property_tree::ptree& res = child.second;
|
|
const auto class_name_raw = GetData<std::string>(res, "class_name");
|
|
const std::string class_name = DecodeUnicodeEscapes(class_name_raw);
|
|
const auto x = GetData<int>(res, "x");
|
|
const auto y = GetData<int>(res, "y");
|
|
const auto w = GetData<int>(res, "width");
|
|
const auto h = GetData<int>(res, "height");
|
|
|
|
cv::rectangle(frame, cv::Rect(x, y, w, h), cv::Scalar(0, 255, 0), 2);
|
|
|
|
std::string extraInfo = GetOptionalValue<std::string>(res, "extra_info", "");
|
|
if (!class_name.empty()) {
|
|
std::cout << " Plate: " << class_name;
|
|
if (!extraInfo.empty()) std::cout << " (" << extraInfo << ")";
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
#ifdef WIN32
|
|
{
|
|
int textH = (int)(1.5 * 30);
|
|
int ty = y - 5 - textH;
|
|
if (ty < 0) ty = y + 3;
|
|
putTextUnicode(frame, class_name, cv::Point(x, ty),
|
|
1.5, cv::Scalar(0, 0, 255), 3);
|
|
}
|
|
#else
|
|
cv::putText(frame, class_name, cv::Point(x, y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
#endif
|
|
}
|
|
}
|
|
catch (const std::exception& e) {
|
|
std::cerr << "JSON parse error: " << e.what() << std::endl;
|
|
}
|
|
}
|
|
|
|
// Display (fit to 1920x1080)
|
|
cv::Mat display;
|
|
double scale = std::min(1920.0 / frame.cols, 1080.0 / frame.rows);
|
|
if (scale < 1.0) {
|
|
cv::resize(frame, display, cv::Size(), scale, scale);
|
|
} else {
|
|
display = frame;
|
|
}
|
|
cv::namedWindow("ALPR_OCR_VideoTest", cv::WINDOW_AUTOSIZE);
|
|
cv::imshow("ALPR_OCR_VideoTest", display);
|
|
if (cv::waitKey(1) == 27) { // ESC to exit
|
|
std::cout << "ESC pressed — stopping.\n";
|
|
break;
|
|
}
|
|
}
|
|
|
|
capture.release();
|
|
cv::destroyAllWindows();
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
|
|
std::cout << "=== ALPR_OCR_VideoTest complete ===" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
// ALPR_Helmet_Parallel_VideoTest — ANSVIS-style multi-task simulation.
|
|
//
|
|
// Combines the FilePlayer + multi-task fan-out from
|
|
// CustomModel_StressTest_FilePlayer (ANSODEngine-UnitTest) with the
|
|
// ANSALPR_OCR pipeline from ALPR_OCR_VideoTest, plus an OD-then-batched-
|
|
// ALPR pipeline that mirrors how ANSVIS chains a custom detector into
|
|
// ALPR for vehicle / scene-aware plate reading.
|
|
//
|
|
// Task A (ALPR) : ANSALPR_OCR engine (engineType = 2, country = VN)
|
|
// on FULL frames via ANSALPR_RunInferenceBinary —
|
|
// independent worker, runs in parallel with B.
|
|
// Task B (Helmet : custom detector (modelType = 16) via
|
|
// + batch ALPR) RunInferenceComplete_CPP. After OD completes,
|
|
// any detection whose class name looks like a
|
|
// license plate is collected into a cv::Rect list
|
|
// and submitted to alprHandle->RunInferencesBatch
|
|
// (the same engine method ANSALPR_RunInferencesBatch_LV
|
|
// wraps for LabVIEW — see HelmetParallelWorker for
|
|
// why we don't go through the _LV entry directly).
|
|
//
|
|
// alprHandle is shared between A and B; ANSALPR's internal refcount
|
|
// (ALPRHandleGuard / AcquireALPRHandle in ANSLPR/dllmain.cpp) makes
|
|
// concurrent calls safe.
|
|
//
|
|
// Per-frame flow per task (mirrors LabVIEW camera→AI plumbing):
|
|
// GetFilePlayerCVImage → ANSCV_CloneImage_S → run inference on clone
|
|
// → (helmet only) batch ALPR on LP boxes
|
|
// → draw on clone → ANSCV_ReleaseImage_S
|
|
//
|
|
// Press ESC to stop. Edit the CONFIG block below to point at your
|
|
// video / model paths.
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
namespace {
|
|
|
|
struct ParallelTaskState {
|
|
std::mutex mtx;
|
|
cv::Mat displayFrame;
|
|
double fps = 0.0;
|
|
double inferenceMs = 0.0;
|
|
double grabMs = 0.0;
|
|
int frameCount = 0;
|
|
int detectionCount = 0;
|
|
std::string statusMsg = "Initializing";
|
|
std::string lastDetection;
|
|
bool engineLoaded = false;
|
|
};
|
|
|
|
static std::atomic<bool> g_parallelRunning{ true };
|
|
|
|
// ============================================================================
|
|
// AMD/DML root-cause diagnostics — instrumentation shared by both workers.
|
|
// Goal: capture enough state per GPU call to differentiate Intel/NVIDIA-OK
|
|
// vs AMD-fails behaviour without touching engine code.
|
|
//
|
|
// Signals captured:
|
|
// - Concurrent inference depth (atomic counter incremented at GPU-call
|
|
// entry, decremented at exit). On NVIDIA you typically see depth=2
|
|
// routinely (true concurrent kernels). On AMD/DirectML the D3D12
|
|
// driver tends to serialise, so depth>1 here means queue contention —
|
|
// compare with NVIDIA logs to see the difference.
|
|
// - Per-call rolling latency stats (n / min / mean / p99 / max) for each
|
|
// GPU-bound entry point. Progressive degradation precedes the AMD
|
|
// 0xE0434352 device-loss; this exposes it.
|
|
// - Stuck-call watchdog (>2 s) — Windows TDR fires at 2 s, so anything
|
|
// flagged here is a TDR precursor.
|
|
// - SLOW(>1s) tag on individual calls.
|
|
// - Thread ID + tag on every entry/exit so we can correlate ALPR vs
|
|
// Helmet workers in the log timeline.
|
|
// - Periodic 5-second stats snapshot to ANS_DBG + stdout.
|
|
// ============================================================================
|
|
|
|
static std::atomic<int> g_dmlInflight{ 0 }; // currently-executing GPU calls
|
|
static std::atomic<int> g_dmlPeakDepth{ 0 }; // high-water mark of dmlInflight
|
|
static std::atomic<int64_t> g_alprFullFrameCalls{ 0 };
|
|
static std::atomic<int64_t> g_helmetOdCalls{ 0 };
|
|
static std::atomic<int64_t> g_batchAlprCalls{ 0 };
|
|
static std::atomic<int64_t> g_slowCalls{ 0 }; // count of calls >1s
|
|
static std::atomic<int64_t> g_stuckEvents{ 0 }; // count of watchdog STUCK logs
|
|
|
|
// Each in-flight GPU call registers itself here; the watchdog scans the list
|
|
// every 500 ms and screams about any entry that's been alive >2 s.
|
|
struct InflightCall {
|
|
const char* tag;
|
|
DWORD tid;
|
|
std::chrono::steady_clock::time_point t0;
|
|
};
|
|
static std::mutex g_inflightMtx;
|
|
static std::vector<InflightCall> g_inflight;
|
|
|
|
// Bounded rolling latency histogram per call type.
|
|
struct LatencyStats {
|
|
static constexpr size_t kCap = 500;
|
|
std::mutex mtx;
|
|
std::deque<double> ms;
|
|
void Add(double v) {
|
|
std::lock_guard<std::mutex> lk(mtx);
|
|
ms.push_back(v);
|
|
if (ms.size() > kCap) ms.pop_front();
|
|
}
|
|
void Snapshot(size_t& n, double& mn, double& mx,
|
|
double& mean, double& p99) {
|
|
std::lock_guard<std::mutex> lk(mtx);
|
|
n = ms.size();
|
|
if (n == 0) { mn = mx = mean = p99 = 0.0; return; }
|
|
mn = *std::min_element(ms.begin(), ms.end());
|
|
mx = *std::max_element(ms.begin(), ms.end());
|
|
double s = 0.0; for (double v : ms) s += v;
|
|
mean = s / static_cast<double>(n);
|
|
std::vector<double> sorted(ms.begin(), ms.end());
|
|
std::sort(sorted.begin(), sorted.end());
|
|
size_t idx = std::min(n - 1, static_cast<size_t>(static_cast<double>(n) * 0.99));
|
|
p99 = sorted[idx];
|
|
}
|
|
};
|
|
static LatencyStats g_alprFullLatency;
|
|
static LatencyStats g_helmetOdLatency;
|
|
static LatencyStats g_batchAlprLatency;
|
|
|
|
// RAII scope around a single GPU-bound call. Logs entry+exit, registers
|
|
// in the watchdog list, and feeds the rolling latency histogram on exit.
|
|
class GpuCallScope {
|
|
public:
|
|
GpuCallScope(const char* tag, LatencyStats* stats)
|
|
: tag_(tag), stats_(stats),
|
|
t0_(std::chrono::steady_clock::now()),
|
|
tid_(GetCurrentThreadId())
|
|
{
|
|
const int depth = ++g_dmlInflight;
|
|
// Update peak depth observed (CAS loop — lock-free).
|
|
int prev = g_dmlPeakDepth.load(std::memory_order_relaxed);
|
|
while (depth > prev &&
|
|
!g_dmlPeakDepth.compare_exchange_weak(prev, depth)) {}
|
|
ANS_DBG("ALPRParallel", "GPU ENTER %s tid=%lu depth=%d",
|
|
tag_, static_cast<unsigned long>(tid_), depth);
|
|
std::lock_guard<std::mutex> lk(g_inflightMtx);
|
|
g_inflight.push_back({ tag_, tid_, t0_ });
|
|
}
|
|
~GpuCallScope() {
|
|
const auto t1 = std::chrono::steady_clock::now();
|
|
const double ms =
|
|
std::chrono::duration<double, std::milli>(t1 - t0_).count();
|
|
const int newDepth = --g_dmlInflight;
|
|
if (stats_) stats_->Add(ms);
|
|
if (ms > 1000.0) {
|
|
g_slowCalls.fetch_add(1, std::memory_order_relaxed);
|
|
ANS_DBG("ALPRParallel",
|
|
"GPU SLOW(>1s) %s tid=%lu ms=%.1f depth-after=%d slowTotal=%lld",
|
|
tag_, static_cast<unsigned long>(tid_), ms, newDepth,
|
|
g_slowCalls.load());
|
|
} else {
|
|
ANS_DBG("ALPRParallel", "GPU exit %s tid=%lu ms=%.1f depth-after=%d",
|
|
tag_, static_cast<unsigned long>(tid_), ms, newDepth);
|
|
}
|
|
std::lock_guard<std::mutex> lk(g_inflightMtx);
|
|
for (auto it = g_inflight.begin(); it != g_inflight.end(); ++it) {
|
|
if (it->tid == tid_ && it->t0 == t0_) {
|
|
g_inflight.erase(it);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
GpuCallScope(const GpuCallScope&) = delete;
|
|
GpuCallScope& operator=(const GpuCallScope&) = delete;
|
|
private:
|
|
const char* tag_;
|
|
LatencyStats* stats_;
|
|
std::chrono::steady_clock::time_point t0_;
|
|
DWORD tid_;
|
|
};
|
|
|
|
// Watchdog thread: scans g_inflight every 500 ms and emits a STUCK log
|
|
// for any call alive >2 s. Two seconds is the default Windows TDR
|
|
// threshold (HKLM\...\GraphicsDrivers\TdrDelay), so anything caught
|
|
// here is by definition the precursor to a 0xE0434352 device-loss.
|
|
static void GpuWatchdogThread() {
|
|
ANS_DBG("ALPRParallel", "Watchdog thread started (TDR threshold=2000ms)");
|
|
while (g_parallelRunning.load()) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
const auto now = std::chrono::steady_clock::now();
|
|
std::lock_guard<std::mutex> lk(g_inflightMtx);
|
|
for (const auto& c : g_inflight) {
|
|
const double ms =
|
|
std::chrono::duration<double, std::milli>(now - c.t0).count();
|
|
if (ms > 2000.0) {
|
|
g_stuckEvents.fetch_add(1, std::memory_order_relaxed);
|
|
ANS_DBG("ALPRParallel",
|
|
"WATCHDOG STUCK %s tid=%lu inflight=%.0fms (>=2000ms TDR)",
|
|
c.tag, static_cast<unsigned long>(c.tid), ms);
|
|
}
|
|
}
|
|
}
|
|
ANS_DBG("ALPRParallel", "Watchdog thread finished");
|
|
}
|
|
|
|
// Periodic stats dumper. Owned by the main test loop; the display thread
|
|
// calls Maybe() every render frame and gets a snapshot logged ~every 5 s.
|
|
struct StatsSnapshotter {
|
|
void Maybe(std::chrono::steady_clock::time_point now) {
|
|
if (now - last_ < std::chrono::seconds(5)) return;
|
|
last_ = now;
|
|
size_t n; double mn, mx, mean, p99;
|
|
char buf[1024];
|
|
char* p = buf;
|
|
char* const end = buf + sizeof(buf);
|
|
p += snprintf(p, end - p,
|
|
"STATS dmlInflight=%d peakDepth=%d "
|
|
"alprFullCalls=%lld helmetOdCalls=%lld batchAlprCalls=%lld "
|
|
"slowCalls=%lld stuckEvents=%lld | ",
|
|
g_dmlInflight.load(), g_dmlPeakDepth.load(),
|
|
g_alprFullFrameCalls.load(),
|
|
g_helmetOdCalls.load(),
|
|
g_batchAlprCalls.load(),
|
|
g_slowCalls.load(),
|
|
g_stuckEvents.load());
|
|
g_alprFullLatency.Snapshot(n, mn, mx, mean, p99);
|
|
p += snprintf(p, end - p,
|
|
"alprFull(n=%zu mn=%.0f mean=%.0f p99=%.0f mx=%.0f) ",
|
|
n, mn, mean, p99, mx);
|
|
g_helmetOdLatency.Snapshot(n, mn, mx, mean, p99);
|
|
p += snprintf(p, end - p,
|
|
"helmetOD(n=%zu mn=%.0f mean=%.0f p99=%.0f mx=%.0f) ",
|
|
n, mn, mean, p99, mx);
|
|
g_batchAlprLatency.Snapshot(n, mn, mx, mean, p99);
|
|
p += snprintf(p, end - p,
|
|
"batchALPR(n=%zu mn=%.0f mean=%.0f p99=%.0f mx=%.0f)",
|
|
n, mn, mean, p99, mx);
|
|
ANS_DBG("ALPRParallel", "%s", buf);
|
|
printf("[ALPRParallel] %s\n", buf);
|
|
}
|
|
private:
|
|
std::chrono::steady_clock::time_point last_ =
|
|
std::chrono::steady_clock::now();
|
|
};
|
|
static StatsSnapshotter g_statsSnap;
|
|
|
|
// ALPR worker: pulls frames from FilePlayer, runs ANSALPR_RunInferenceBinary,
|
|
// parses the JSON, draws boxes + plate text, updates display state.
|
|
static void ALPRParallelWorker(ANSCENTER::ANSFILEPLAYER* fpClient,
|
|
ANSCENTER::ANSALPR* alprHandle,
|
|
ParallelTaskState& state) {
|
|
const char* tag = "[ALPR ]";
|
|
printf("%s Worker thread started\n", tag);
|
|
ANS_DBG("ALPRParallel", "ALPR worker started: fpClient=%p alprHandle=%p",
|
|
(void*)fpClient, (void*)alprHandle);
|
|
|
|
int width = 0, height = 0;
|
|
int64_t pts = 0;
|
|
int emptyFrames = 0;
|
|
boost::property_tree::ptree pt;
|
|
std::deque<std::chrono::steady_clock::time_point> fpsTimestamps;
|
|
|
|
while (g_parallelRunning.load()) {
|
|
auto grabStart = std::chrono::steady_clock::now();
|
|
cv::Mat* framePtr = nullptr;
|
|
GetFilePlayerCVImage(&fpClient, width, height, pts, &framePtr);
|
|
auto grabEnd = std::chrono::steady_clock::now();
|
|
double grabMs = std::chrono::duration<double, std::milli>(grabEnd - grabStart).count();
|
|
|
|
if (!framePtr || framePtr->empty()) {
|
|
emptyFrames++;
|
|
if (emptyFrames > 500) {
|
|
printf("%s Too many empty frames (%d), stopping\n", tag, emptyFrames);
|
|
ANS_DBG("ALPRParallel", "ALPR worker stopping: too many empty frames=%d", emptyFrames);
|
|
break;
|
|
}
|
|
if (framePtr) ANSCV_ReleaseImage_S(&framePtr);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
emptyFrames = 0;
|
|
|
|
cv::Mat* clonedImage = nullptr;
|
|
int cloneResult = ANSCV_CloneImage_S(&framePtr, &clonedImage);
|
|
if (cloneResult != 1 || !clonedImage) {
|
|
printf("%s CloneImage failed (result=%d)\n", tag, cloneResult);
|
|
ANS_DBG("ALPRParallel", "ANSCV_CloneImage_S failed: result=%d", cloneResult);
|
|
ANSCV_ReleaseImage_S(&framePtr);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
ANSCV_ReleaseImage_S(&framePtr);
|
|
|
|
// Pack BGR bytes for ANSALPR_RunInferenceBinary (same as ALPR_OCR_VideoTest).
|
|
const int frameW = clonedImage->cols;
|
|
const int frameH = clonedImage->rows;
|
|
const unsigned int bufferLength =
|
|
static_cast<unsigned int>(clonedImage->total() * clonedImage->elemSize());
|
|
std::vector<unsigned char> imageBytes(bufferLength);
|
|
std::memcpy(imageBytes.data(), clonedImage->data, bufferLength);
|
|
|
|
auto infStart = std::chrono::steady_clock::now();
|
|
std::string detectionResult;
|
|
{
|
|
GpuCallScope gpu("alprFullFrame", &g_alprFullLatency);
|
|
detectionResult = ANSALPR_RunInferenceBinary(&alprHandle,
|
|
imageBytes.data(), frameW, frameH);
|
|
}
|
|
g_alprFullFrameCalls.fetch_add(1, std::memory_order_relaxed);
|
|
auto infEnd = std::chrono::steady_clock::now();
|
|
double infMs = std::chrono::duration<double, std::milli>(infEnd - infStart).count();
|
|
|
|
// Draw on the clone for display.
|
|
cv::Mat display = clonedImage->clone();
|
|
std::string lastDet;
|
|
int detCount = 0;
|
|
if (!detectionResult.empty()) {
|
|
try {
|
|
pt.clear();
|
|
std::stringstream ss(detectionResult);
|
|
boost::property_tree::read_json(ss, pt);
|
|
auto resultsChild = pt.get_child_optional("results");
|
|
if (resultsChild) {
|
|
for (const auto& child : *resultsChild) {
|
|
const auto& res = child.second;
|
|
const auto class_name_raw = GetData<std::string>(res, "class_name");
|
|
const std::string class_name = DecodeUnicodeEscapes(class_name_raw);
|
|
const auto x = GetData<int>(res, "x");
|
|
const auto y = GetData<int>(res, "y");
|
|
const auto w = GetData<int>(res, "width");
|
|
const auto h = GetData<int>(res, "height");
|
|
cv::rectangle(display, cv::Rect(x, y, w, h),
|
|
cv::Scalar(0, 255, 0), 2);
|
|
#ifdef WIN32
|
|
{
|
|
int textH = (int)(1.2 * 30);
|
|
int ty = y - 5 - textH;
|
|
if (ty < 0) ty = y + 3;
|
|
putTextUnicode(display, class_name, cv::Point(x, ty),
|
|
1.2, cv::Scalar(0, 0, 255), 2);
|
|
}
|
|
#else
|
|
cv::putText(display, class_name, cv::Point(x, y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8,
|
|
cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
#endif
|
|
if (!class_name.empty()) lastDet = class_name;
|
|
++detCount;
|
|
}
|
|
}
|
|
} catch (const std::exception& e) {
|
|
std::cerr << tag << " JSON parse error: " << e.what() << std::endl;
|
|
ANS_DBG("ALPRParallel", "JSON parse error: %s", e.what());
|
|
}
|
|
}
|
|
|
|
ANSCV_ReleaseImage_S(&clonedImage);
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
fpsTimestamps.push_back(now);
|
|
while (!fpsTimestamps.empty() &&
|
|
std::chrono::duration<double>(now - fpsTimestamps.front()).count() > 2.0) {
|
|
fpsTimestamps.pop_front();
|
|
}
|
|
double fps = fpsTimestamps.size() / 2.0;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
if (!display.empty()) state.displayFrame = display;
|
|
state.fps = fps;
|
|
state.inferenceMs = infMs;
|
|
state.grabMs = grabMs;
|
|
state.frameCount += 1;
|
|
state.detectionCount += detCount;
|
|
if (!lastDet.empty()) state.lastDetection = lastDet;
|
|
}
|
|
|
|
if ((state.frameCount % 200) == 0) {
|
|
printf("%s %d frames | %.1f FPS | Inf: %.0f ms | Det: %d\n",
|
|
tag, state.frameCount, fps, infMs, state.detectionCount);
|
|
}
|
|
}
|
|
|
|
printf("%s Worker thread finished (%d frames)\n", tag, state.frameCount);
|
|
ANS_DBG("ALPRParallel", "ALPR worker finished: frames=%d detections=%d",
|
|
state.frameCount, state.detectionCount);
|
|
}
|
|
|
|
// Case-insensitive substring match on a class name — keeps the LP filter
|
|
// tolerant to label variations across model versions ("license_plate",
|
|
// "License Plate", "LP", "plate", etc.).
|
|
static bool ClassNameLooksLikeLicensePlate(const std::string& className) {
|
|
std::string lower;
|
|
lower.reserve(className.size());
|
|
for (char c : className) lower.push_back((char)std::tolower((unsigned char)c));
|
|
if (lower.find("plate") != std::string::npos) return true;
|
|
if (lower.find("license") != std::string::npos) return true;
|
|
if (lower == "lp" || lower == "lpn" || lower == "anpr") return true;
|
|
return false;
|
|
}
|
|
|
|
// Helmet worker: pulls frames from the SAME FilePlayer, runs the custom
|
|
// detector via RunInferenceComplete_CPP (same code path LabVIEW takes), then
|
|
// — for any detections that look like license plates — runs a single batched
|
|
// ALPR call on those crops via ANSALPR::RunInferencesBatch (the C++ method
|
|
// that ANSALPR_RunInferencesBatch_LV wraps in production). This is the
|
|
// "OD-then-batched-ALPR" pipeline ANSVIS uses on top of the standalone
|
|
// full-frame ALPR worker, so both paths exercise alprHandle concurrently.
|
|
//
|
|
// Note on `_LV` vs C++:
|
|
// ANSALPR_RunInferencesBatch_LV serialises results into an LStrHandle
|
|
// (LabVIEW string), which requires DSSetHandleSize from the LabVIEW
|
|
// runtime — not available in a console test. The _LV wrapper is only
|
|
// ~30 lines of LStrHandle plumbing around `engine->RunInferencesBatch`,
|
|
// so calling that method directly here exercises the identical inference
|
|
// code path without dragging in lvrt.dll.
|
|
static void HelmetParallelWorker(ANSCENTER::ANSFILEPLAYER* fpClient,
|
|
ANSCENTER::ANSODBase* odHandle,
|
|
ANSCENTER::ANSALPR* alprHandle,
|
|
ParallelTaskState& state) {
|
|
const char* tag = "[Helmet]";
|
|
printf("%s Worker thread started\n", tag);
|
|
ANS_DBG("ALPRParallel", "Helmet worker started: fpClient=%p odHandle=%p alprHandle=%p",
|
|
(void*)fpClient, (void*)odHandle, (void*)alprHandle);
|
|
|
|
int width = 0, height = 0;
|
|
int64_t pts = 0;
|
|
int emptyFrames = 0;
|
|
const std::string cameraId = "ParallelHelmetCam";
|
|
std::deque<std::chrono::steady_clock::time_point> fpsTimestamps;
|
|
long long batchAlprCalls = 0;
|
|
long long batchAlprPlatesFound = 0;
|
|
|
|
while (g_parallelRunning.load()) {
|
|
auto grabStart = std::chrono::steady_clock::now();
|
|
cv::Mat* framePtr = nullptr;
|
|
GetFilePlayerCVImage(&fpClient, width, height, pts, &framePtr);
|
|
auto grabEnd = std::chrono::steady_clock::now();
|
|
double grabMs = std::chrono::duration<double, std::milli>(grabEnd - grabStart).count();
|
|
|
|
if (!framePtr || framePtr->empty()) {
|
|
emptyFrames++;
|
|
if (emptyFrames > 500) {
|
|
printf("%s Too many empty frames (%d), stopping\n", tag, emptyFrames);
|
|
ANS_DBG("ALPRParallel", "Helmet worker stopping: too many empty frames=%d", emptyFrames);
|
|
break;
|
|
}
|
|
if (framePtr) ANSCV_ReleaseImage_S(&framePtr);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
emptyFrames = 0;
|
|
|
|
cv::Mat* clonedImage = nullptr;
|
|
int cloneResult = ANSCV_CloneImage_S(&framePtr, &clonedImage);
|
|
if (cloneResult != 1 || !clonedImage) {
|
|
printf("%s CloneImage failed (result=%d)\n", tag, cloneResult);
|
|
ANS_DBG("ALPRParallel", "ANSCV_CloneImage_S failed: result=%d", cloneResult);
|
|
ANSCV_ReleaseImage_S(&framePtr);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
continue;
|
|
}
|
|
ANSCV_ReleaseImage_S(&framePtr);
|
|
|
|
// --- Stage 1: helmet OD ---
|
|
auto infStart = std::chrono::steady_clock::now();
|
|
std::vector<ANSCENTER::Object> detections;
|
|
int infResult = 0;
|
|
{
|
|
GpuCallScope gpu("helmetOD", &g_helmetOdLatency);
|
|
infResult = RunInferenceComplete_CPP(&odHandle, &clonedImage,
|
|
cameraId.c_str(), "", detections);
|
|
}
|
|
g_helmetOdCalls.fetch_add(1, std::memory_order_relaxed);
|
|
auto infEnd = std::chrono::steady_clock::now();
|
|
double infMs = std::chrono::duration<double, std::milli>(infEnd - infStart).count();
|
|
|
|
// --- Stage 2: filter LP-like detections, run batched ALPR on them ---
|
|
std::vector<cv::Rect> lpBoxes;
|
|
if (infResult > 0) {
|
|
lpBoxes.reserve(detections.size());
|
|
for (const auto& obj : detections) {
|
|
if (ClassNameLooksLikeLicensePlate(obj.className)) {
|
|
if (obj.box.width > 0 && obj.box.height > 0) {
|
|
lpBoxes.push_back(obj.box);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<ANSCENTER::Object> plateResults;
|
|
double batchAlprMs = 0.0;
|
|
if (!lpBoxes.empty() && alprHandle && clonedImage && !clonedImage->empty()) {
|
|
ANS_DBG("ALPRParallel", "Batch ALPR call: lpBoxes=%zu", lpBoxes.size());
|
|
auto t0 = std::chrono::steady_clock::now();
|
|
try {
|
|
GpuCallScope gpu("batchALPR", &g_batchAlprLatency);
|
|
plateResults = alprHandle->RunInferencesBatch(
|
|
*clonedImage, lpBoxes, cameraId);
|
|
} catch (const std::exception& e) {
|
|
ANS_DBG("ALPRParallel", "RunInferencesBatch threw: %s", e.what());
|
|
} catch (...) {
|
|
ANS_DBG("ALPRParallel", "RunInferencesBatch threw unknown exception");
|
|
}
|
|
g_batchAlprCalls.fetch_add(1, std::memory_order_relaxed);
|
|
auto t1 = std::chrono::steady_clock::now();
|
|
batchAlprMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
batchAlprCalls += 1;
|
|
batchAlprPlatesFound += static_cast<long long>(plateResults.size());
|
|
ANS_DBG("ALPRParallel",
|
|
"Batch ALPR DONE: %zu LP boxes -> %zu plates, %.1f ms",
|
|
lpBoxes.size(), plateResults.size(), batchAlprMs);
|
|
}
|
|
|
|
// --- Draw helmet boxes (yellow) + plate boxes (green w/ OCR text) ---
|
|
// Important: when the helmet OD detects a license-plate-class object,
|
|
// its `className` is the literal string "license plate" — NOT a
|
|
// recognized plate number. We deliberately SKIP the OD label for
|
|
// LP-class detections; the batch-ALPR loop below redraws those boxes
|
|
// with the OCR'd plate string when ANSALPR succeeds. If ANSALPR
|
|
// fails to read a plate, the green box still flags where the helmet
|
|
// OD found one, but no misleading "license plate" text is drawn.
|
|
cv::Mat display;
|
|
if (clonedImage && !clonedImage->empty()) display = clonedImage->clone();
|
|
|
|
std::string lastDet; // last NON-LP helmet class (helmet, no_helmet, motorcyclist...)
|
|
std::string lastPlateText; // last successfully OCR'd plate string
|
|
int detCount = 0;
|
|
if (infResult > 0) {
|
|
for (const auto& obj : detections) {
|
|
++detCount;
|
|
if (ClassNameLooksLikeLicensePlate(obj.className)) {
|
|
// Defer to the batch-ALPR loop — it owns LP visualisation.
|
|
continue;
|
|
}
|
|
if (!display.empty()) {
|
|
cv::rectangle(display, obj.box, cv::Scalar(0, 255, 255), 2);
|
|
std::string label = obj.className + " " +
|
|
std::to_string((int)(obj.confidence * 100)) + "%";
|
|
cv::putText(display, label,
|
|
cv::Point(obj.box.x, obj.box.y - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.6,
|
|
cv::Scalar(0, 255, 255), 2);
|
|
}
|
|
lastDet = obj.className;
|
|
}
|
|
}
|
|
for (const auto& p : plateResults) {
|
|
if (display.empty()) break;
|
|
cv::rectangle(display, p.box, cv::Scalar(0, 255, 0), 2);
|
|
const std::string plateText = DecodeUnicodeEscapes(p.className);
|
|
if (!plateText.empty()) {
|
|
#ifdef WIN32
|
|
int ty = p.box.y - 5 - (int)(1.0 * 30);
|
|
if (ty < 0) ty = p.box.y + 3;
|
|
putTextUnicode(display, plateText, cv::Point(p.box.x, ty),
|
|
1.0, cv::Scalar(0, 0, 255), 2);
|
|
#else
|
|
cv::putText(display, plateText,
|
|
cv::Point(p.box.x, std::max(0, p.box.y - 5)),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8,
|
|
cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
|
|
#endif
|
|
lastPlateText = plateText;
|
|
}
|
|
}
|
|
// Status-bar text: prefer the OCR'd plate over a non-LP helmet class.
|
|
// Both are kept so the cell stays informative when the OD detected
|
|
// (e.g.) a helmet but no plate text was recognised this frame.
|
|
std::string statusDet;
|
|
if (!lastPlateText.empty()) statusDet = "PLATE: " + lastPlateText;
|
|
else if (!lastDet.empty()) statusDet = lastDet;
|
|
// Overlay "ALPR-from-OD: <calls> calls, <plates> plates" badge
|
|
if (!display.empty() && batchAlprCalls > 0) {
|
|
char badge[160];
|
|
snprintf(badge, sizeof(badge),
|
|
"ALPR-from-OD: %lld calls, %lld plates (last: %.1f ms)",
|
|
batchAlprCalls, batchAlprPlatesFound, batchAlprMs);
|
|
cv::putText(display, badge, cv::Point(8, 50),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.55,
|
|
cv::Scalar(0, 255, 0), 2);
|
|
}
|
|
|
|
ANSCV_ReleaseImage_S(&clonedImage);
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
fpsTimestamps.push_back(now);
|
|
while (!fpsTimestamps.empty() &&
|
|
std::chrono::duration<double>(now - fpsTimestamps.front()).count() > 2.0) {
|
|
fpsTimestamps.pop_front();
|
|
}
|
|
double fps = fpsTimestamps.size() / 2.0;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(state.mtx);
|
|
if (!display.empty()) state.displayFrame = display;
|
|
state.fps = fps;
|
|
state.inferenceMs = infMs + batchAlprMs; // total cost per OD frame
|
|
state.grabMs = grabMs;
|
|
state.frameCount += 1;
|
|
state.detectionCount += detCount + (int)plateResults.size();
|
|
if (!statusDet.empty()) state.lastDetection = statusDet;
|
|
}
|
|
|
|
if ((state.frameCount % 200) == 0) {
|
|
printf("%s %d frames | %.1f FPS | OD: %.0f ms | Batch ALPR: %.0f ms (%lld calls, %lld plates) | Det: %d | last:%s\n",
|
|
tag, state.frameCount, fps, infMs, batchAlprMs,
|
|
batchAlprCalls, batchAlprPlatesFound, state.detectionCount,
|
|
statusDet.empty() ? "-" : statusDet.c_str());
|
|
}
|
|
}
|
|
|
|
printf("%s Worker thread finished (%d frames, %lld batch-ALPR calls -> %lld plates)\n",
|
|
tag, state.frameCount, batchAlprCalls, batchAlprPlatesFound);
|
|
ANS_DBG("ALPRParallel",
|
|
"Helmet worker finished: frames=%d detections=%d batchCalls=%lld batchPlates=%lld",
|
|
state.frameCount, state.detectionCount, batchAlprCalls, batchAlprPlatesFound);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int ALPR_Helmet_Parallel_VideoTest() {
|
|
printf("\n");
|
|
printf("============================================================\n");
|
|
printf(" ALPR + Helmet Parallel Video Test (ANSVIS simulation)\n");
|
|
printf(" 1 FilePlayer feeds 2 AI tasks running concurrently:\n");
|
|
printf(" A) ANSALPR_OCR full-frame (engineType=2, country=VIETNAM)\n");
|
|
printf(" B) Custom helmet detector (modelType=16)\n");
|
|
printf(" └─ on LP-class detections, batched ALPR via\n");
|
|
printf(" ANSALPR::RunInferencesBatch (= the C++ method\n");
|
|
printf(" ANSALPR_RunInferencesBatch_LV calls internally)\n");
|
|
printf(" Press ESC to stop\n");
|
|
printf("============================================================\n\n");
|
|
ANS_DBG("ALPRParallel", "ALPR_Helmet_Parallel_VideoTest START");
|
|
|
|
// ===== CONFIG (edit to match your environment) =====================
|
|
const std::string videoFilePath =
|
|
"C:\\ProgramData\\ANSCENTER\\Shared\\HM1.mp4";
|
|
|
|
// ALPR (ANSALPR_OCR)
|
|
const std::string alprModelPath =
|
|
"C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_GenericALPR_v2.0.zip";
|
|
const int alprEngineType = 2; // 2 = ANSALPR_OCR
|
|
const int alprCountry = 0; // 0 = VIETNAM
|
|
const double alprDetectionThresh = 0.3;
|
|
const double alprOcrThresh = 0.5;
|
|
const double alprColourThresh = 0.0;
|
|
|
|
// Helmet (custom detector — same model the OD stress test uses)
|
|
const std::string helmetModelPath =
|
|
"C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\Models\\B-IN_ANS_Helmet_v2.0_102728911.zip";
|
|
const int helmetModelType = 16; // 16 = CustomDetector
|
|
const int helmetDetectorType = 1; // 1 = Detection
|
|
const float helmetScoreThresh = 0.5f;
|
|
const float helmetConfThresh = 0.5f;
|
|
const float helmetNmsThresh = 0.45f;
|
|
// ===================================================================
|
|
|
|
g_parallelRunning.store(true);
|
|
|
|
ANSCENTER::ANSOPENCV::InitCameraNetwork();
|
|
|
|
// --- Optimize helmet model (warmup engine cache, same as OD stress test) ---
|
|
std::cout << "Optimizing helmet model, please wait..." << std::endl;
|
|
std::string optimizedFolder;
|
|
OptimizeModelStr(helmetModelPath.c_str(), "",
|
|
helmetModelType, helmetDetectorType, /*fp16=*/1,
|
|
optimizedFolder);
|
|
std::cout << "Optimized helmet model folder: " << optimizedFolder << std::endl;
|
|
ANS_DBG("ALPRParallel", "Helmet model optimize done: folder=%s",
|
|
optimizedFolder.c_str());
|
|
|
|
// --- Create FilePlayer (single source feeds both tasks) ---
|
|
ANSCENTER::ANSFILEPLAYER* fpClient = nullptr;
|
|
{
|
|
printf("[Stream] Creating FilePlayer: %s\n", videoFilePath.c_str());
|
|
int result = CreateANSFilePlayerHandle(&fpClient, "", videoFilePath.c_str());
|
|
if (result != 1 || !fpClient) {
|
|
printf("[Stream] FAILED to create FilePlayer (result=%d)\n", result);
|
|
ANS_DBG("ALPRParallel", "CreateANSFilePlayerHandle FAIL result=%d", result);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -1;
|
|
}
|
|
SetFilePlayerDisplayResolution(&fpClient, 1920, 1080);
|
|
printf("[Stream] FilePlayer created (display: 1920x1080)\n");
|
|
}
|
|
|
|
// --- Create ALPR handle ---
|
|
ANSCENTER::ANSALPR* alprHandle = nullptr;
|
|
{
|
|
printf("[ALPR ] Creating ANSALPR handle (engineType=%d)...\n", alprEngineType);
|
|
int created = CreateANSALPRHandle(&alprHandle, "",
|
|
alprModelPath.c_str(), "",
|
|
alprEngineType, alprDetectionThresh, alprOcrThresh, alprColourThresh);
|
|
if (!created || !alprHandle) {
|
|
printf("[ALPR ] FAILED to create ANSALPR handle\n");
|
|
ANS_DBG("ALPRParallel", "CreateANSALPRHandle FAIL");
|
|
StopFilePlayer(&fpClient);
|
|
ReleaseANSFilePlayerHandle(&fpClient);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -2;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&alprHandle, 1);
|
|
ANSALPR_SetCountry(&alprHandle, alprCountry);
|
|
auto loadStart = std::chrono::steady_clock::now();
|
|
int loadResult = LoadANSALPREngineHandle(&alprHandle);
|
|
auto loadEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(loadEnd - loadStart).count();
|
|
if (!loadResult) {
|
|
printf("[ALPR ] FAILED to load ANSALPR engine\n");
|
|
ANS_DBG("ALPRParallel", "LoadANSALPREngineHandle FAIL");
|
|
ReleaseANSALPRHandle(&alprHandle);
|
|
StopFilePlayer(&fpClient);
|
|
ReleaseANSFilePlayerHandle(&fpClient);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -3;
|
|
}
|
|
printf("[ALPR ] Engine loaded in %.0f ms (country=%d)\n", loadMs, alprCountry);
|
|
ANS_DBG("ALPRParallel", "ALPR engine loaded: %.0f ms country=%d", loadMs, alprCountry);
|
|
}
|
|
|
|
// --- Create helmet OD handle (same API as VideoDetectorEngine / LabVIEW) ---
|
|
ANSCENTER::ANSODBase* odHandle = nullptr;
|
|
{
|
|
printf("[Helmet] Creating OD handle (modelType=%d)...\n", helmetModelType);
|
|
auto loadStart = std::chrono::steady_clock::now();
|
|
std::string labelMap = CreateANSODHandle(
|
|
&odHandle, "",
|
|
helmetModelPath.c_str(), "",
|
|
helmetScoreThresh, helmetConfThresh, helmetNmsThresh,
|
|
/*autoDetectEngine=*/1, helmetModelType, helmetDetectorType,
|
|
/*loadEngineOnCreation=*/1);
|
|
auto loadEnd = std::chrono::steady_clock::now();
|
|
double loadMs = std::chrono::duration<double, std::milli>(loadEnd - loadStart).count();
|
|
if (!odHandle) {
|
|
printf("[Helmet] FAILED to create OD handle\n");
|
|
ANS_DBG("ALPRParallel", "CreateANSODHandle FAIL");
|
|
ReleaseANSALPRHandle(&alprHandle);
|
|
StopFilePlayer(&fpClient);
|
|
ReleaseANSFilePlayerHandle(&fpClient);
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return -4;
|
|
}
|
|
// Print the FULL label map — needed so the operator can confirm the
|
|
// helmet model exposes a license-plate class for the OD-then-batched-
|
|
// ALPR pipeline (HelmetParallelWorker filters by class name).
|
|
printf("[Helmet] Model loaded in %.0f ms\n", loadMs);
|
|
printf("[Helmet] Full label map: %s\n",
|
|
labelMap.empty() ? "(none)" : labelMap.c_str());
|
|
ANS_DBG("ALPRParallel", "Helmet OD handle created: %.0f ms labelLen=%zu labels=%s",
|
|
loadMs, labelMap.size(),
|
|
labelMap.empty() ? "(none)" : labelMap.c_str());
|
|
}
|
|
|
|
// --- Start playback ---
|
|
StartFilePlayer(&fpClient);
|
|
printf("[Stream] Playback started\n");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
// --- Launch worker threads (both pull from the same FilePlayer) ---
|
|
ParallelTaskState alprState;
|
|
ParallelTaskState helmetState;
|
|
{
|
|
std::lock_guard<std::mutex> lkA(alprState.mtx);
|
|
alprState.engineLoaded = true;
|
|
alprState.statusMsg = "Running";
|
|
std::lock_guard<std::mutex> lkH(helmetState.mtx);
|
|
helmetState.engineLoaded = true;
|
|
helmetState.statusMsg = "Running";
|
|
}
|
|
|
|
// Helmet worker shares alprHandle with the standalone ALPR worker — both
|
|
// call into ANSALPR concurrently (full-frame in ALPRParallelWorker;
|
|
// batched on OD-supplied LP boxes in HelmetParallelWorker). ANSALPR is
|
|
// refcounted internally (see ALPRHandleGuard / AcquireALPRHandle in
|
|
// ANSLPR/dllmain.cpp) so concurrent use is safe.
|
|
std::thread alprWorker(ALPRParallelWorker, fpClient, alprHandle, std::ref(alprState));
|
|
std::thread helmetWorker(HelmetParallelWorker, fpClient, odHandle, alprHandle,
|
|
std::ref(helmetState));
|
|
// Watchdog thread: scans in-flight GPU calls every 500 ms and screams
|
|
// about anything alive >2 s (the Windows TDR threshold). Captures the
|
|
// exact precursor to the AMD 0xE0434352 device-loss.
|
|
std::thread watchdog(GpuWatchdogThread);
|
|
|
|
// --- Display loop: 2 cells side-by-side ---
|
|
const int cellW = 720, cellH = 480;
|
|
const char* windowName = "ALPR + Helmet Parallel (ANSVIS sim)";
|
|
cv::namedWindow(windowName, cv::WINDOW_NORMAL);
|
|
cv::resizeWindow(windowName, cellW * 2, cellH + 40);
|
|
|
|
auto testStart = std::chrono::steady_clock::now();
|
|
|
|
auto renderCell = [cellW, cellH](ParallelTaskState& s, const char* title,
|
|
cv::Scalar accent) -> cv::Mat {
|
|
cv::Mat cell;
|
|
double fps = 0, infMs = 0;
|
|
int fCount = 0, dCount = 0;
|
|
std::string statusMsg, lastDet;
|
|
bool engineLoaded = false;
|
|
{
|
|
std::lock_guard<std::mutex> lk(s.mtx);
|
|
if (!s.displayFrame.empty()) {
|
|
cv::resize(s.displayFrame, cell, cv::Size(cellW, cellH));
|
|
}
|
|
fps = s.fps;
|
|
infMs = s.inferenceMs;
|
|
fCount = s.frameCount;
|
|
dCount = s.detectionCount;
|
|
statusMsg = s.statusMsg;
|
|
lastDet = s.lastDetection;
|
|
engineLoaded = s.engineLoaded;
|
|
}
|
|
if (cell.empty()) {
|
|
cell = cv::Mat(cellH, cellW, CV_8UC3, cv::Scalar(40, 40, 40));
|
|
cv::putText(cell, std::string(title) + ": " + statusMsg,
|
|
cv::Point(20, cellH / 2),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.8,
|
|
cv::Scalar(100, 100, 255), 2);
|
|
}
|
|
cv::rectangle(cell, cv::Rect(0, 0, cellW, 25),
|
|
cv::Scalar(0, 0, 0), cv::FILLED);
|
|
cv::putText(cell, title, cv::Point(8, 18),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.55, accent, 2);
|
|
cv::rectangle(cell, cv::Rect(0, cellH - 45, cellW, 45),
|
|
cv::Scalar(0, 0, 0), cv::FILLED);
|
|
char bar1[256], bar2[256];
|
|
snprintf(bar1, sizeof(bar1),
|
|
"%.1f FPS | inf:%.0fms | Frames:%d | Det:%d",
|
|
fps, infMs, fCount, dCount);
|
|
snprintf(bar2, sizeof(bar2), "last:%s",
|
|
lastDet.empty() ? "-" : lastDet.c_str());
|
|
cv::Scalar barColor = engineLoaded ? cv::Scalar(0, 255, 0)
|
|
: cv::Scalar(0, 100, 255);
|
|
cv::putText(cell, bar1, cv::Point(5, cellH - 25),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5, barColor, 1);
|
|
cv::putText(cell, bar2, cv::Point(5, cellH - 5),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5,
|
|
cv::Scalar(0, 200, 255), 1);
|
|
return cell;
|
|
};
|
|
|
|
while (g_parallelRunning.load()) {
|
|
// Periodic AMD/DML diagnostics dump — flushes latency histograms
|
|
// and inflight depth to ANS_DBG + stdout every ~5 s.
|
|
g_statsSnap.Maybe(std::chrono::steady_clock::now());
|
|
|
|
cv::Mat canvas(cellH + 40, cellW * 2, CV_8UC3, cv::Scalar(30, 30, 30));
|
|
|
|
cv::Mat alprCell = renderCell(alprState, "ALPR full-frame (ANSALPR_OCR)",
|
|
cv::Scalar(0, 200, 255));
|
|
cv::Mat helmetCell = renderCell(helmetState, "Helmet OD + batched ALPR-on-OD",
|
|
cv::Scalar(0, 255, 255));
|
|
alprCell .copyTo(canvas(cv::Rect(0, 0, cellW, cellH)));
|
|
helmetCell.copyTo(canvas(cv::Rect(cellW, 0, cellW, cellH)));
|
|
|
|
double elapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
double totalFps = alprState.fps + helmetState.fps;
|
|
char bottomBar[320];
|
|
snprintf(bottomBar, sizeof(bottomBar),
|
|
"Elapsed: %.0fs | Combined: %.1f FPS | depth=%d peak=%d slow=%lld stuck=%lld | ESC to stop",
|
|
elapsed, totalFps,
|
|
g_dmlInflight.load(), g_dmlPeakDepth.load(),
|
|
g_slowCalls.load(), g_stuckEvents.load());
|
|
cv::putText(canvas, bottomBar, cv::Point(10, cellH + 25),
|
|
cv::FONT_HERSHEY_SIMPLEX, 0.5,
|
|
cv::Scalar(200, 200, 200), 1);
|
|
|
|
cv::imshow(windowName, canvas);
|
|
int key = cv::waitKey(30);
|
|
if (key == 27) {
|
|
printf("\nESC pressed — stopping...\n");
|
|
ANS_DBG("ALPRParallel", "ESC pressed, requesting stop");
|
|
g_parallelRunning.store(false);
|
|
}
|
|
}
|
|
|
|
// --- Wait for workers ---
|
|
printf("Waiting for worker threads...\n");
|
|
if (alprWorker.joinable()) alprWorker.join();
|
|
if (helmetWorker.joinable()) helmetWorker.join();
|
|
if (watchdog.joinable()) watchdog.join();
|
|
|
|
double totalElapsed = std::chrono::duration<double>(
|
|
std::chrono::steady_clock::now() - testStart).count();
|
|
printf("\n============================================================\n");
|
|
printf(" PARALLEL TEST SUMMARY (runtime: %.0fs)\n", totalElapsed);
|
|
printf("============================================================\n");
|
|
printf(" ALPR : %d frames | %d detections | %.1f FPS | Inf: %.0fms\n",
|
|
alprState.frameCount, alprState.detectionCount,
|
|
alprState.fps, alprState.inferenceMs);
|
|
printf(" Helmet: %d frames | %d detections | %.1f FPS | Inf: %.0fms\n",
|
|
helmetState.frameCount, helmetState.detectionCount,
|
|
helmetState.fps, helmetState.inferenceMs);
|
|
printf(" Combined throughput: %.1f FPS\n",
|
|
alprState.fps + helmetState.fps);
|
|
printf("------------------------------------------------------------\n");
|
|
printf(" AMD/DML diagnostics (compare with Intel/NVIDIA run)\n");
|
|
printf(" Peak inflight depth : %d (NVIDIA usually >=2; AMD usually 1)\n",
|
|
g_dmlPeakDepth.load());
|
|
printf(" SLOW(>1s) calls total : %lld\n", g_slowCalls.load());
|
|
printf(" Watchdog STUCK events : %lld (each = >2s in flight, TDR precursor)\n",
|
|
g_stuckEvents.load());
|
|
{
|
|
size_t n; double mn, mx, mean, p99;
|
|
g_alprFullLatency.Snapshot(n, mn, mx, mean, p99);
|
|
printf(" alprFull latency : n=%zu min=%.0f mean=%.0f p99=%.0f max=%.0f ms\n",
|
|
n, mn, mean, p99, mx);
|
|
g_helmetOdLatency.Snapshot(n, mn, mx, mean, p99);
|
|
printf(" helmetOD latency : n=%zu min=%.0f mean=%.0f p99=%.0f max=%.0f ms\n",
|
|
n, mn, mean, p99, mx);
|
|
g_batchAlprLatency.Snapshot(n, mn, mx, mean, p99);
|
|
printf(" batchALPR latency : n=%zu min=%.0f mean=%.0f p99=%.0f max=%.0f ms\n",
|
|
n, mn, mean, p99, mx);
|
|
}
|
|
printf("============================================================\n");
|
|
ANS_DBG("ALPRParallel",
|
|
"TEST END runtime=%.0fs peakDepth=%d slow=%lld stuck=%lld "
|
|
"ALPR(frames=%d det=%d) HELMET(frames=%d det=%d)",
|
|
totalElapsed, g_dmlPeakDepth.load(),
|
|
g_slowCalls.load(), g_stuckEvents.load(),
|
|
alprState.frameCount, alprState.detectionCount,
|
|
helmetState.frameCount, helmetState.detectionCount);
|
|
|
|
// --- Release ---
|
|
if (odHandle) ReleaseANSODHandle(&odHandle);
|
|
if (alprHandle) ReleaseANSALPRHandle(&alprHandle);
|
|
if (fpClient) {
|
|
StopFilePlayer(&fpClient);
|
|
ReleaseANSFilePlayerHandle(&fpClient);
|
|
}
|
|
|
|
cv::destroyAllWindows();
|
|
ANSCENTER::ANSOPENCV::DeinitCameraNetwork();
|
|
return 0;
|
|
}
|
|
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
// ALPR_OCR_FolderInferences_Test — folder-driven counterpart of
|
|
// ALPR_OCR_VideoTest. Walks the same image folder ANSLPR_OD_FolderInferences_Test
|
|
// uses but routes inference through the ANSALPR_OCR engine (engineType=2)
|
|
// and the same generic-ALPR model that ALPR_OCR_VideoTest loads. Country
|
|
// is set to VIETNAM so the Vietnam-only post-processing
|
|
// (alnum filter + uppercase + 11-pattern format validation) is exercised.
|
|
//
|
|
// Output format mirrors ANSLPR_OD_FolderInferences_Test exactly: one block
|
|
// per image with raw JSON, parsed detections, and a tail summary with
|
|
// total detections + average inference time. Compare side-by-side with
|
|
// the OD result on the same folder to A/B-test OCR vs OD on Vietnam input.
|
|
// ──────────────────────────────────────────────────────────────────────
|
|
int ALPR_OCR_FolderInferences_Test() {
|
|
std::cout << "=== ALPR_OCR_FolderInferences_Test: ANSALPR_OCR engine on folder ===" << std::endl;
|
|
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 = "";
|
|
|
|
// Same model the video test uses (ANSALPR_OCR generic Paddle-based model).
|
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_GenericALPR_v2.0.zip";
|
|
|
|
// Same folder ANSLPR_OD_FolderInferences_Test scans.
|
|
std::string folderPath = "E:\\Programs\\DemoAssets\\ImageSeries\\ALPR character M";
|
|
|
|
int engineType = 2; // 2 = ANSALPR_OCR (Paddle-based OCR pipeline)
|
|
double detectionThreshold = 0.3;
|
|
double ocrThreshold = 0.5;
|
|
double colourThreshold = 0.0;
|
|
|
|
// Step 1: Create handle.
|
|
int createResult = CreateANSALPRHandle(&infHandle, licenseKey.c_str(),
|
|
modelZipFile.c_str(), "", engineType,
|
|
detectionThreshold, ocrThreshold, colourThreshold);
|
|
std::cout << "CreateANSALPRHandle result: " << createResult << std::endl;
|
|
if (!createResult || !infHandle) {
|
|
std::cerr << "Failed to create ANSALPR_OCR handle" << std::endl;
|
|
return -1;
|
|
}
|
|
ANSALPR_SetALPRCheckerEnabled(&infHandle, 1);
|
|
|
|
// Step 2: Country = VIETNAM (0). Activates Vietnam-only post-processing
|
|
// (alnum filter + uppercase + format validation against the 11 standard
|
|
// Vietnam patterns). Country codes:
|
|
// 0 = VIETNAM, 1 = CHINA, 2 = AUSTRALIA, 3 = USA, 4 = INDONESIA, 5 = JAPAN.
|
|
ANSALPR_SetCountry(&infHandle, 0);
|
|
std::cout << "Country set to VIETNAM" << std::endl;
|
|
|
|
// Step 3: Load engine.
|
|
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 (!loadEngine) {
|
|
std::cerr << "Failed to load ANSALPR_OCR engine" << std::endl;
|
|
ReleaseANSALPRHandle(&infHandle);
|
|
return -1;
|
|
}
|
|
|
|
// Step 4: Enumerate images.
|
|
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::recursive_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 (recursive): " << folderPath << std::endl;
|
|
std::cout << "Found " << imageFiles.size() << " image(s)." << std::endl;
|
|
|
|
// Step 5: Run inference per image.
|
|
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;
|
|
}
|
|
|
|
// Step 6: Summary.
|
|
std::cout << "\n=== ALPR_OCR_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);
|
|
std::cout << "=== ALPR_OCR_FolderInferences_Test complete ===" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
#ifdef WIN32
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
SetConsoleCP(CP_UTF8);
|
|
#endif
|
|
//ANSLPR_OD_INDOInferences_FileTest();
|
|
//ANSLPR_OD_Inferences_FileTest();
|
|
// Disabled — has a hardcoded NVIDIA RTX 4070 TensorRT-optimised zip path
|
|
// (ServerOptimised\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip) that
|
|
// doesn't exist on AMD/Intel machines. Re-enable after fixing the path
|
|
// at line 573 if you need the OD folder test.
|
|
//ANSLPR_OD_FolderInferences_Test();
|
|
ANSLPR_OD_VideoTest();
|
|
//ANSLPR_BigSize_VideoTest();
|
|
//ANSLPR_CPU_VideoTest();
|
|
//for (int i = 0; i < 100; i++) {
|
|
// ANSLPR_CPU_Inferences_FileTest();
|
|
//}
|
|
//ANSLPR_SingleTask_Test();
|
|
//ANSLPR_CPU_StressTest();
|
|
// ANSLPR_MultiGPU_StressTest();
|
|
//ANSLPR_MultiGPU_StressTest_SimulatedCam();
|
|
// ANSLPR_MultiGPU_StressTest_FilePlayer();
|
|
//ANSLPR_OD_CPU_VideoTest();
|
|
//ALPR_OCR_Test();
|
|
//ALPR_OCR_VideoTest();
|
|
//ALPR_OCR_FolderInferences_Test();
|
|
//ALPR_Helmet_Parallel_VideoTest(); // ANSVIS sim: ALPR + Helmet on same FilePlayer
|
|
return 0;
|
|
|
|
}
|
|
|
|
|