Fix model optimisation

This commit is contained in:
2026-04-09 08:09:02 +10:00
parent 34854d87f4
commit eeb205779a
10 changed files with 688 additions and 160 deletions

View File

@@ -30,6 +30,7 @@ namespace ANSCENTER {
_isInitialized = false; // Reset initialization flag
}
std::string onnxModel = CreateFilePath(_modelFolder, "scrfd.onnx");
_scrfdModelPath = onnxModel;
this->_face_detector = std::make_unique<SCRFD>(onnxModel);
_isInitialized = true;
_movementObjects.clear();
@@ -80,6 +81,7 @@ namespace ANSCENTER {
_face_detector.reset(); // Releases previously allocated memory for face detection
_isInitialized = false; // Reset initialization flag
}
_scrfdModelPath = modelFullName;
this->_face_detector = std::make_unique<SCRFD>(modelFullName);
_isInitialized = true;
return _isInitialized;
@@ -98,6 +100,30 @@ namespace ANSCENTER {
return result;
}
std::vector<Object> ANSOVFD::RunInference(const cv::Mat& input, const std::string& camera_id, bool useDynamicImage, bool validateFace, bool facelivenessCheck) {
// ── DML device-lost recovery (outside mutex) ──────────────
if (_dmlDeviceLost && _face_detector) {
// The DML session is broken — recreate on CPU
try {
auto cpuDetector = std::make_unique<ANSCENTER::SCRFD>(
_scrfdModelPath, ANSCENTER::EngineType::CPU);
{
std::lock_guard<std::mutex> guard(_mtx);
_face_detector = std::move(cpuDetector);
}
_logger.LogInfo("ANSOVFD::RunInference",
"CPU fallback session created successfully",
__FILE__, __LINE__);
} catch (const std::exception& re) {
_logger.LogFatal("ANSOVFD::RunInference",
std::string("CPU fallback exception: ") + re.what(),
__FILE__, __LINE__);
std::lock_guard<std::mutex> guard(_mtx);
_face_detector.reset();
_isInitialized = false;
return {};
}
_dmlDeviceLost = false; // Recovery complete
}
if (facelivenessCheck) {
std::vector<Object> rawFaceResults = Inference(input, camera_id, useDynamicImage, validateFace);
std::vector<Object> facesWithLivenessResults = ValidateLivenessFaces(input, rawFaceResults, camera_id);
@@ -108,6 +134,29 @@ namespace ANSCENTER {
}
}
std::vector<Object> ANSOVFD::RunInference(const cv::Mat& input, bool useDynamicImage, bool validateFace, bool facelivenessCheck) {
// ── DML device-lost recovery (outside mutex) ──────────────
if (_dmlDeviceLost && _face_detector) {
try {
auto cpuDetector = std::make_unique<ANSCENTER::SCRFD>(
_scrfdModelPath, ANSCENTER::EngineType::CPU);
{
std::lock_guard<std::mutex> guard(_mtx);
_face_detector = std::move(cpuDetector);
}
_logger.LogInfo("ANSOVFD::RunInference",
"CPU fallback session created successfully",
__FILE__, __LINE__);
} catch (const std::exception& re) {
_logger.LogFatal("ANSOVFD::RunInference",
std::string("CPU fallback exception: ") + re.what(),
__FILE__, __LINE__);
std::lock_guard<std::mutex> guard(_mtx);
_face_detector.reset();
_isInitialized = false;
return {};
}
_dmlDeviceLost = false;
}
if (facelivenessCheck) {
std::vector<Object> rawFaceResults = Inference(input, "CustomCam", useDynamicImage, validateFace);
std::vector<Object> facesWithLivenessResults = ValidateLivenessFaces(input, rawFaceResults, "CustomCam");
@@ -331,8 +380,21 @@ namespace ANSCENTER {
}
catch (const std::exception& e) {
const std::string msg = e.what();
// DML device-removal detection (see ANSONNXYOLO.cpp for details)
if (msg.find("887A0005") != std::string::npos) {
if (!_dmlDeviceLost) {
_dmlDeviceLost = true;
_logger.LogFatal("ANSOVFD::RunInference",
"DirectML GPU device lost (887A0005) — will attempt CPU fallback on next call",
__FILE__, __LINE__);
}
return {};
}
_logger.LogFatal("ANSOVFD::RunInference",
"Exception: " + std::string(e.what()),
"Exception: " + msg,
__FILE__, __LINE__);
return {};
}