Fix mutex lock issues

This commit is contained in:
2026-04-13 19:48:32 +10:00
parent 56a8f09adf
commit 844d7396b2
30 changed files with 445 additions and 575 deletions

View File

@@ -55,6 +55,7 @@ namespace ANSCENTER
}
bool ANSYOLOV12RTOD::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
bool result = ANSODBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
@@ -147,6 +148,7 @@ namespace ANSCENTER
}
bool ANSYOLOV12RTOD::LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName,std::string className, const std::string& modelFolder, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
bool result = ANSODBase::LoadModelFromFolder(licenseKey, modelConfig,modelName, className,modelFolder, labelMap);
if (!result) return false;
@@ -248,6 +250,7 @@ namespace ANSCENTER
}
bool ANSYOLOV12RTOD::Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
const bool engineAlreadyLoaded = _modelLoadValid && _isInitialized && m_trtEngine != nullptr;
_modelLoadValid = false;
@@ -343,35 +346,11 @@ namespace ANSCENTER
return RunInference(inputImgBGR, "TensorRT12Cam");
}
std::vector<Object> ANSYOLOV12RTOD::RunInference(const cv::Mat& inputImgBGR, const std::string& camera_id) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// Validate model, license, and initialization
if (!_modelLoadValid) {
this->_logger.LogFatal("ANSYOLOV12RTOD::RunInference",
"Cannot load the TensorRT model. Please check if it is exist", __FILE__, __LINE__);
if (!PreInferenceCheck("ANSYOLOV12RTOD::RunInference")) return {};
if (inputImgBGR.empty() || inputImgBGR.cols < 10 || inputImgBGR.rows < 10)
return {};
}
if (!_licenseValid) {
this->_logger.LogFatal("ANSYOLOV12RTOD::RunInference",
"Runtime license is not valid or expired. Please contact ANSCENTER", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
this->_logger.LogFatal("ANSYOLOV12RTOD::RunInference",
"Model is not initialized", __FILE__, __LINE__);
return {};
}
// Validate input
if (inputImgBGR.empty() || inputImgBGR.cols < 10 || inputImgBGR.rows < 10) {
return {};
}
try {
return DetectObjects(inputImgBGR, camera_id);
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSYOLOV12RTOD::RunInference", e.what(), __FILE__, __LINE__);
@@ -919,22 +898,7 @@ namespace ANSCENTER
std::vector<std::vector<Object>> ANSYOLOV12RTOD::RunInferencesBatch(
const std::vector<cv::Mat>& inputs, const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
_logger.LogError("ANSYOLOV12RTOD::RunInferencesBatch", "Model not loaded", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("ANSYOLOV12RTOD::RunInferencesBatch", "Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("ANSYOLOV12RTOD::RunInferencesBatch", "Engine not initialized", __FILE__, __LINE__);
return {};
}
if (inputs.empty()) return {};
}
if (!PreInferenceCheck("ANSYOLOV12RTOD::RunInferencesBatch")) return {};
try {
return DetectObjectsBatch(inputs, camera_id);
}