From 775d4d6b16c2eeb10c6ae6fd035ea3241be5e441 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Wed, 26 Aug 2026 11:20:43 +1000 Subject: [PATCH] ANSFR-UnitTest: classroom attribute-overlay demo + v1.1 vs v1.4 benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ClassroomAttributesVideoTest (default main): plays classroom.mp4 through the v1.4 pipeline and overlays user name, Gender/Age, and Emotion parsed from each result's extra_info JSON; saves an annotated frame every 30 frames to C:\Programs\DemoAssets for headless review. - --bench old|new|both: per-pack speed benchmark (100 measured frames after 10 warmup, full pipeline with attributes on) run one pack per process. RTX 3070 laptop results: v1.1 arcface+OpenVINO 17.8 ms avg (56.2 fps) vs v1.4 adaface+ONNX/TRT 18.2 ms avg (55.1 fps) at 3.1 faces/frame — the new models carry no throughput cost. Co-Authored-By: Claude Fable 5 --- tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp | 256 +++++++++++++++++++++++- 1 file changed, 255 insertions(+), 1 deletion(-) diff --git a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp index 0db29df..b178654 100644 --- a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp +++ b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp @@ -1825,8 +1825,261 @@ int TestModelSwitchV14() return failures; } +// =========================================================================== +// Classroom video demo — v1.4 pack with face attributes drawn as overlays. +// Each face gets its box, user name, and the Gender / Age / Emotion parsed +// from the result's extra_info JSON. ESC quits; an annotated frame is saved +// every 30 frames to C:\Programs\DemoAssets\ so there is a reviewable +// artifact even in headless runs. +// =========================================================================== +int ClassroomAttributesVideoTest() +{ + const std::string modelZip = "C:\\Programs\\Models\\ANS_FaceRecognizer_v1.4.zip"; + const std::string detectorZip = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; + const std::string dbFile = "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_test\\ANSFR_v14_test.db"; + const std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; + const int maxFrames = 600; + + std::cout << "\n===== ClassroomAttributesVideoTest =====" << std::endl; + + ANSCENTER::ANSFacialRecognition* h = nullptr; + // age/gender + emotions + head pose ON, liveness/antispoof OFF for speed + int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(), + 0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0); + std::cout << "CreateANSRFHandle = " << cr << std::endl; + if (cr <= 0 || !h) return 1; + + int le = LoadANSRFEngine(&h); + std::cout << "LoadANSRFEngine = " << le << std::endl; + std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl; + if (le != 1) { ReleaseANSRFHandle(&h); return 1; } + + cv::VideoCapture cap(videoPath); + if (!cap.isOpened()) { + std::cout << "ERROR: cannot open video " << videoPath << std::endl; + ReleaseANSRFHandle(&h); + return 1; + } + + const std::string win = "ANSFR v1.4 - Face Attributes (ESC to quit)"; + cv::namedWindow(win, cv::WINDOW_NORMAL); + cv::resizeWindow(win, 1280, 720); + + int frameIdx = 0, saved = 0; + cv::Mat frame; + while (cap.read(frame)) { + if (frame.empty()) break; + if (++frameIdx > maxFrames) break; + + unsigned int bufLen = 0; + unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen); + std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows); + delete bytes; + + try { + boost::property_tree::ptree pt; + std::stringstream ss; ss << res; + boost::property_tree::read_json(ss, pt); + if (pt.count("results")) { + BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) { + const boost::property_tree::ptree& r = child.second; + const auto name = GetData(r, "user_name"); + const int x = static_cast(GetData(r, "x")); + const int y = static_cast(GetData(r, "y")); + const int w = static_cast(GetData(r, "width")); + const int ht = static_cast(GetData(r, "height")); + + // extra_info is itself a JSON string: + // {"attributes":[{"Gender":..,"Age":..,"Emotion":..,"Pose(r,p,y)":..}]} + std::string gender, age, emotion; + const auto extra = GetData(r, "extra_info"); + if (!extra.empty()) { + try { + boost::property_tree::ptree ept; + std::stringstream ess; ess << extra; + boost::property_tree::read_json(ess, ept); + if (ept.count("attributes")) { + for (const auto& attr : ept.get_child("attributes")) { + gender = GetOptionalValue(attr.second, "Gender", ""); + age = GetOptionalValue(attr.second, "Age", ""); + emotion = GetOptionalValue(attr.second, "Emotion", ""); + break; + } + } + } + catch (...) {} + } + + const bool known = (name != "Unknown"); + const cv::Scalar boxColor = known ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 200, 255); + cv::rectangle(frame, cv::Rect(x, y, w, ht), boxColor, 2); + + std::vector lines; + lines.push_back(name); + if (!gender.empty() || !age.empty()) + lines.push_back(gender + " / " + age); + if (!emotion.empty()) + lines.push_back(emotion); + + int ty = y - 8 - 18 * static_cast(lines.size() - 1); + for (const auto& line : lines) { + cv::putText(frame, line, cv::Point(x, ty), + cv::FONT_HERSHEY_SIMPLEX, 0.55, cv::Scalar(0, 0, 0), 3, cv::LINE_AA); + cv::putText(frame, line, cv::Point(x, ty), + cv::FONT_HERSHEY_SIMPLEX, 0.55, boxColor, 1, cv::LINE_AA); + ty += 18; + } + } + } + } + catch (...) {} + + cv::putText(frame, cv::format("Frame %d", frameIdx), cv::Point(25, 45), + cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 255, 255), 2, cv::LINE_AA); + + if (frameIdx % 30 == 0) { + std::string fp = cv::format("C:\\Programs\\DemoAssets\\ANSFR_v14_attr_frame_%04d.jpg", frameIdx); + cv::imwrite(fp, frame); + ++saved; + } + + cv::imshow(win, frame); + if (cv::waitKey(1) == 27) break; // ESC + } + + std::cout << "Played " << frameIdx << " frames, saved " << saved + << " annotated frames to C:\\Programs\\DemoAssets\\" << std::endl; + cap.release(); + cv::destroyAllWindows(); + ReleaseANSRFHandle(&h); + return 0; +} + +// =========================================================================== +// Speed benchmark — v1.1 (arcface + OpenVINO attributes) vs +// v1.4 (adaface + ONNX/TensorRT attributes) on the same classroom video. +// Full pipeline per frame (detect + recognize + attributes), attributes ON. +// First run of a pack pays its engine builds; those are excluded from the +// timing (LoadEngine reported separately, plus warmup frames). +// Run one pack per process with: ANSFR-UnitTest.exe --bench old|new +// =========================================================================== +static void RunSinglePackBenchmark(const std::string& label, + const std::string& modelZip, const std::string& dbFolder, + int warmupFrames = 10, int measuredFrames = 100) +{ + const std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; + const std::string detectorZip = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; + + std::cout << "\n===== BENCH [" << label << "] =====" << std::endl; + std::cout << "model pack: " << modelZip << std::endl; + + std::error_code ec; + std::filesystem::remove_all(dbFolder, ec); + const std::string dbFile = dbFolder + "\\bench.db"; + + ANSCENTER::ANSFacialRecognition* h = nullptr; + int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(), + 0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0); + if (cr <= 0 || !h) { + std::cout << "[BENCH " << label << "] CreateANSRFHandle FAILED: " << cr << std::endl; + return; + } + + auto loadStart = std::chrono::steady_clock::now(); + int le = LoadANSRFEngine(&h); + auto loadMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - loadStart).count(); + std::cout << "LoadANSRFEngine = " << le << " (" << loadMs << " ms)" << std::endl; + std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl; + if (le != 1) { ReleaseANSRFHandle(&h); return; } + + cv::VideoCapture cap(videoPath); + if (!cap.isOpened()) { + std::cout << "ERROR: cannot open video " << videoPath << std::endl; + ReleaseANSRFHandle(&h); + return; + } + + double totalMs = 0.0, minMs = 1e9, maxMs = 0.0; + long long totalFaces = 0; + int measured = 0, frameIdx = 0; + cv::Mat frame; + while (cap.read(frame) && measured < measuredFrames) { + if (frame.empty()) break; + ++frameIdx; + + unsigned int bufLen = 0; + unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen); + + auto t0 = std::chrono::steady_clock::now(); + std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows); + double ms = std::chrono::duration( + std::chrono::steady_clock::now() - t0).count(); + delete bytes; + + if (frameIdx <= warmupFrames) continue; // engine warmup / TRT autotuning + + ++measured; + totalMs += ms; + minMs = (std::min)(minMs, ms); + maxMs = (std::max)(maxMs, ms); + + // rough face count for context + size_t pos = 0; + while ((pos = res.find("\"user_name\"", pos)) != std::string::npos) { + ++totalFaces; pos += 11; + } + } + cap.release(); + + if (measured > 0) { + const double avg = totalMs / measured; + std::cout << std::fixed << std::setprecision(1); + std::cout << "[BENCH " << label << "] frames=" << measured + << " avg=" << avg << " ms" + << " min=" << minMs << " ms" + << " max=" << maxMs << " ms" + << " fps=" << (1000.0 / avg) + << " avg faces/frame=" << (double)totalFaces / measured + << std::endl; + } + else { + std::cout << "[BENCH " << label << "] no frames measured" << std::endl; + } + + ReleaseANSRFHandle(&h); +} + +int ModelSpeedBenchmark() +{ + RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs", + "C:\\Projects\\SharedCore\\ANS_FaceRecognizer_v1.1.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench"); + RunSinglePackBenchmark("v1.4 adaface + ONNX attrs", + "C:\\Programs\\Models\\ANS_FaceRecognizer_v1.4.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench"); + return 0; +} + int main(int argc, char** argv) { + // Single-pack benchmark mode (one pack per process so the packs cannot + // influence each other): ANSFR-UnitTest.exe --bench old|new|both + if (argc >= 3 && std::string(argv[1]) == "--bench") { + const std::string which = argv[2]; + if (which == "old") + RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs", + "C:\\Projects\\SharedCore\\ANS_FaceRecognizer_v1.1.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench"); + else if (which == "new") + RunSinglePackBenchmark("v1.4 adaface + ONNX attrs", + "C:\\Programs\\Models\\ANS_FaceRecognizer_v1.4.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench"); + else + ModelSpeedBenchmark(); + return 0; + } + // ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ───────────── // Usage: ANSFR-UnitTest.exe --gpu 0 → only GPU0 visible (Test A, display GPU) // ANSFR-UnitTest.exe --gpu 1 → only GPU1 visible (Test B, headless GPU) @@ -1847,7 +2100,8 @@ int main(int argc, char** argv) //FaceDetectorTest(); //TestFaceRecognition(); //TestCompleteFR1(); - TestModelSwitchV14(); + //TestModelSwitchV14(); + ClassroomAttributesVideoTest(); //ANSVISTest(); //ANSVISImageTest(); //ANSVISTest();