Move responsibility of cascade init to ObjDetectParameters

This commit is contained in:
WarmUpTill
2025-05-23 21:27:02 +02:00
committed by WarmUpTill
parent 0e5f56b562
commit 32d29875ed
4 changed files with 65 additions and 45 deletions

View File

@@ -48,6 +48,31 @@ bool PatternMatchParameters::Load(obs_data_t *obj)
return true;
}
static std::shared_ptr<cv::CascadeClassifier>
initObjectCascade(std::string &path)
{
auto cascade = std::make_shared<cv::CascadeClassifier>();
try {
cascade->load(path);
} catch (...) {
blog(LOG_WARNING, "failed to load model data \"%s\"",
path.c_str());
}
return cascade;
}
bool ObjDetectParameters::LoadModelData()
{
const auto path = QString::fromStdString(modelPath);
if (!QFileInfo(path).exists(path)) {
cascade.reset();
return false;
}
cascade = initObjectCascade(modelPath);
return !cascade->empty();
}
bool ObjDetectParameters::Save(obs_data_t *obj) const
{
auto data = obs_data_create();
@@ -107,9 +132,29 @@ bool ObjDetectParameters::Load(obs_data_t *obj)
minSize.Load(data, "minSize");
maxSize.Load(data, "maxSize");
obs_data_release(data);
return true;
}
bool ObjDetectParameters::SetModelPath(const std::string &path)
{
modelPath = path;
return LoadModelData();
}
std::shared_ptr<cv::CascadeClassifier> ObjDetectParameters::GetModel()
{
if (cascade && !cascade->empty()) {
return cascade;
}
if (!LoadModelData()) {
return {};
}
return cascade;
}
bool AreaParameters::Save(obs_data_t *obj) const
{
auto data = obs_data_create();