diff --git a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp index 5d5810f..0d3c4b2 100644 --- a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp +++ b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "ANSFR.h" #include "fastdeploy/vision.h" #include "ANSFilePlayer.h" @@ -2066,8 +2067,145 @@ int ModelSpeedBenchmark() return 0; } +// =========================================================================== +// Output-format parity: the v1.4 pack (adaface + ONNX attributes) must emit +// the same JSON schema as the v1.1 pack (arcface + OpenVINO attributes). +// Runs the identical enroll + inference flow through both packs and compares +// the result-object keys, the attribute keys (v1.4 may only ADD "AgeYears"), +// and the value vocabularies (Gender / Age buckets / Emotion labels). +// =========================================================================== +static std::string ParityRunOnePack(const std::string& label, + const std::string& modelZip, const std::string& dbFolder) +{ + const std::string detectorZip = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip"; + const std::string imagePath = "C:\\Programs\\DemoAssets\\TestImages\\FacialRecognition\\Obama+Oprah1.jpg"; + + std::error_code ec; + std::filesystem::remove_all(dbFolder, ec); + const std::string dbFile = dbFolder + "\\parity.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 << "[PARITY " << label << "] CreateANSRFHandle FAILED\n"; return ""; } + if (LoadANSRFEngine(&h) != 1) { std::cout << "[PARITY " << label << "] LoadANSRFEngine FAILED\n"; ReleaseANSRFHandle(&h); return ""; } + std::cout << "[PARITY " << label << "] " << h->GetActiveModelInfo() << std::endl; + + cv::Mat frame = cv::imread(imagePath, cv::IMREAD_COLOR); + int userId = InsertUser(&h, "0001", "ParityUser"); + unsigned int bufLen = 0; + unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen); + InsertFaceBinary(&h, userId, bytes, frame.cols, frame.rows); + delete bytes; + Reload(&h); + + std::string res; + // Several frames so attributes are computed and smoothed like production + for (int i = 0; i < 6; ++i) { + unsigned int len = 0; + unsigned char* b = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len); + res = RunANSRFInferenceBinary(&h, b, frame.cols, frame.rows); + delete b; + } + ReleaseANSRFHandle(&h); + return res; +} + +int TestOutputFormatParity() +{ + int failures = 0; + std::cout << "\n===== TestOutputFormatParity =====" << std::endl; + + std::string oldJson = ParityRunOnePack("v1.1", + "C:\\Projects\\SharedCore\\ANS_FaceRecognizer_v1.1.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v11_parity"); + std::string newJson = ParityRunOnePack("v1.4", + "C:\\Programs\\Models\\ANS_FaceRecognizer_v1.4.zip", + "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_parity"); + + if (!V14Check(!oldJson.empty() && !newJson.empty(), "both packs produced results", failures)) + return failures; + std::cout << "v1.1 JSON: " << oldJson << std::endl; + std::cout << "v1.4 JSON: " << newJson << std::endl; + + auto parse = [](const std::string& json) { + boost::property_tree::ptree pt; + std::stringstream ss; ss << json; + boost::property_tree::read_json(ss, pt); + return pt; + }; + auto keysOf = [](const boost::property_tree::ptree& node) { + std::set keys; + for (const auto& kv : node) keys.insert(kv.first); + return keys; + }; + + try { + auto oldPt = parse(oldJson); + auto newPt = parse(newJson); + + const auto& oldFirst = oldPt.get_child("results").begin()->second; + const auto& newFirst = newPt.get_child("results").begin()->second; + + // 1. Result-object fields must be identical + V14Check(keysOf(oldFirst) == keysOf(newFirst), + "result-object field names identical", failures); + + // 2. Attribute keys must be identical — AgeYears comes from the + // shared serializer, so both backends emit the exact same key set. + auto attrKeys = [&](const boost::property_tree::ptree& r) { + auto ept = parse(r.get("extra_info")); + return keysOf(ept.get_child("attributes").begin()->second); + }; + V14Check(attrKeys(oldFirst) == attrKeys(newFirst), + "attribute keys identical", failures); + + // 3. Value vocabularies must match on every face of both packs + auto checkVocab = [&](const boost::property_tree::ptree& pt, const char* label) { + for (const auto& child : pt.get_child("results")) { + auto ept = parse(child.second.get("extra_info")); + for (const auto& attr : ept.get_child("attributes")) { + const auto gender = GetOptionalValue(attr.second, "Gender", ""); + const auto age = GetOptionalValue(attr.second, "Age", ""); + const auto emotion = GetOptionalValue(attr.second, "Emotion", ""); + V14Check(gender == "Male" || gender == "Female", + std::string(label) + " Gender in {Male, Female}: " + gender, failures); + V14Check(age == "Kids" || age == "Young Adults" || age == "Adults" || age == "Elders", + std::string(label) + " Age in bucket set: " + age, failures); + const bool emotionOk = + (emotion.rfind("Happy", 0) == 0 || emotion.rfind("Neutral", 0) == 0 || + emotion.rfind("Attentive", 0) == 0) && emotion.find("%)") != std::string::npos; + V14Check(emotionOk, + std::string(label) + " Emotion label/format: " + emotion, failures); + } + } + }; + checkVocab(oldPt, "v1.1"); + checkVocab(newPt, "v1.4"); + + // 4. Both packs must recognize the enrolled user (same matching semantics) + V14Check(oldJson.find("ParityUser") != std::string::npos, "v1.1 recognizes enrolled user", failures); + V14Check(newJson.find("ParityUser") != std::string::npos, "v1.4 recognizes enrolled user", failures); + } + catch (const std::exception& e) { + std::cout << "[FAIL] parity comparison threw: " << e.what() << std::endl; + ++failures; + } + + std::cout << "===== TestOutputFormatParity: " + << (failures == 0 ? "ALL PASSED" : (std::to_string(failures) + " FAILURE(S)")) + << " =====" << std::endl; + return failures; +} + int main(int argc, char** argv) { + // Output-format parity between v1.1 and v1.4 packs: + // ANSFR-UnitTest.exe --parity + if (argc >= 2 && std::string(argv[1]) == "--parity") { + return TestOutputFormatParity(); + } + // 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") {