summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInki Dae <inki.dae@samsung.com>2022-08-12 14:29:00 +0900
committerInki Dae <inki.dae@samsung.com>2022-08-12 14:44:47 +0900
commit920fa14b73cadbe4ecdb73ab2b3ac6eb8757d5b7 (patch)
treedd64a247203db9e0e52af9bbd2ced282f6279425
parent2f52a96298347cbb1ed52eed542ed0fbb0b64a49 (diff)
downloadmediavision-920fa14b73cadbe4ecdb73ab2b3ac6eb8757d5b7.tar.gz
mediavision-920fa14b73cadbe4ecdb73ab2b3ac6eb8757d5b7.tar.bz2
mediavision-920fa14b73cadbe4ecdb73ab2b3ac6eb8757d5b7.zip
mv_machine_learning: fix coverity issues
[Version] : 0.23.14 [Issue type] : bug fix Fixed coverity issues. Change-Id: I2e376d517d99722366a05f8ab584858808ec2194 Signed-off-by: Inki Dae <inki.dae@samsung.com>
-rw-r--r--mv_machine_learning/face_recognition/src/face_recognition.cpp127
-rw-r--r--mv_machine_learning/face_recognition/src/simple_shot.cpp33
-rw-r--r--mv_machine_learning/inference/src/mv_inference_open.cpp2
-rw-r--r--mv_machine_learning/training/src/label_manager.cpp13
-rw-r--r--packaging/capi-media-vision.spec2
5 files changed, 118 insertions, 59 deletions
diff --git a/mv_machine_learning/face_recognition/src/face_recognition.cpp b/mv_machine_learning/face_recognition/src/face_recognition.cpp
index 66473cc2..8a6505c7 100644
--- a/mv_machine_learning/face_recognition/src/face_recognition.cpp
+++ b/mv_machine_learning/face_recognition/src/face_recognition.cpp
@@ -62,14 +62,20 @@ void FaceRecognition::CheckFeatureVectorFile(unique_ptr<FeatureVectorManager>& o
// Change new feature vector file to existing one in case that current process is terminated just after removing existing feature vector file but
// new feature vector file isn't changed to existing one yet.
if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName()) && !FaceRecogUtil::IsFileExist(old_fvm->GetFileName())) {
- rename(new_fvm->GetFileName().c_str(), old_fvm->GetFileName().c_str());
+ int ret = ::rename(new_fvm->GetFileName().c_str(), old_fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to rename new feature vector file to original one.");
+
return;
}
// Make sure to remove a temp file in case that current process is terminated just after generating new feature vector file
// which is not correct file but existing one isn't removed. In this cae, existing file is used again.
- if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName()))
- remove(new_fvm->GetFileName().c_str());
+ if (FaceRecogUtil::IsFileExist(new_fvm->GetFileName())) {
+ int ret = ::remove(new_fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove new feature vector file.");
+ }
}
unique_ptr<DataSetManager> FaceRecognition::CreateDSM(const mv_inference_backend_type_e backend_type)
@@ -78,7 +84,7 @@ unique_ptr<DataSetManager> FaceRecognition::CreateDSM(const mv_inference_backend
case MV_INFERENCE_BACKEND_NNTRAINER:
return make_unique<NNTrainerDSM>();
default:
- throw InvalidParameter("Invalid training engine backend type.");
+ break;
}
throw InvalidParameter("Invalid training engine backend type.");
@@ -90,7 +96,7 @@ unique_ptr<FeatureVectorManager> FaceRecognition::CreateFVM(const mv_inference_b
case MV_INFERENCE_BACKEND_NNTRAINER:
return make_unique<NNTrainerFVM>(file_name);
default:
- throw InvalidParameter("Invalid training engine backend type.");
+ break;
}
throw InvalidParameter("Invalid training engine backend type.");
@@ -99,65 +105,85 @@ unique_ptr<FeatureVectorManager> FaceRecognition::CreateFVM(const mv_inference_b
void FaceRecognition::UpdateDataSet(unique_ptr<DataSetManager>& data_set, vector<float>& feature_vec, const int label_idx, const int label_cnt)
{
size_t data_set_cnt = 0;
- auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
- auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new");
- // Make sure feature vector file.
- CheckFeatureVectorFile(fvm, fvm_new);
+ try {
+ auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
+ auto fvm_new = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path + ".new");
- data_set = CreateDSM(_config.training_engine_backend_type);
+ // Make sure feature vector file.
+ CheckFeatureVectorFile(fvm, fvm_new);
- // 1. If data set file exists then load the file to DataSetManager object first
- // and then write them to the data set file with updated label value, and then
- // write a new dataset to the data set file.
- // Otherwise, it writes only new data set to the data set file.
- if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) {
- data_set->LoadDataSet(fvm->GetFileName());
+ data_set = CreateDSM(_config.training_engine_backend_type);
- vector<vector<float>> feature_vectors = data_set->GetData();
- vector<unsigned int> label_idx_vectors = data_set->GetLabelIdx();
+ // 1. If data set file exists then load the file to DataSetManager object first
+ // and then write them to the data set file with updated label value, and then
+ // write a new dataset to the data set file.
+ // Otherwise, it writes only new data set to the data set file.
+ if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) {
+ data_set->LoadDataSet(fvm->GetFileName());
- // 1) Write existing feature vectors and its one-hot encoding table considered
- // for new label count to the data set file.
- for (unsigned int idx = 0; idx < feature_vectors.size(); ++idx)
- fvm_new->WriteFeatureVec(feature_vectors[idx], label_cnt, label_idx_vectors[idx]);
+ vector<vector<float>> feature_vectors = data_set->GetData();
+ vector<unsigned int> label_idx_vectors = data_set->GetLabelIdx();
+
+ // 1) Write existing feature vectors and its one-hot encoding table considered
+ // for new label count to the data set file.
+ for (unsigned int idx = 0; idx < feature_vectors.size(); ++idx)
+ fvm_new->WriteFeatureVec(feature_vectors[idx], label_cnt, label_idx_vectors[idx]);
- data_set_cnt += feature_vectors.size();
+ data_set_cnt += feature_vectors.size();
- // 2) If same feature vector isn't duplicated then write the feature vector to data set file.
- if (!data_set->IsFeatureVectorDuplicated(feature_vec)) {
+ // 2) If same feature vector isn't duplicated then write the feature vector to data set file.
+ if (!data_set->IsFeatureVectorDuplicated(feature_vec)) {
+ fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx);
+ LOGD("Added a new feature vector to data set file.");
+ data_set_cnt++;
+ }
+ } else {
+ // 1) Write only a new data set to the data st file.
fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx);
LOGD("Added a new feature vector to data set file.");
data_set_cnt++;
}
- } else {
- // 1) Write only a new data set to the data st file.
- fvm_new->WriteFeatureVec(feature_vec, label_cnt, label_idx);
- LOGD("Added a new feature vector to data set file.");
- data_set_cnt++;
- }
- // 2. Write feature vector header.
- fvm_new->WriteHeader(feature_vec.size(), label_cnt, data_set_cnt);
+ // 2. Write feature vector header.
+ fvm_new->WriteHeader(feature_vec.size(), label_cnt, data_set_cnt);
+
+ int ret = 0;
+
+ // 3. Change new data file to existing one.
+ if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) {
+ ret = ::remove(fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove feature vector file.");
+ }
- // 3. Change new data file to existing one.
- remove(fvm->GetFileName().c_str());
- rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str());
+ ret = ::rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to rename new feature vector file to original one.");
- data_set->Clear();
- data_set->LoadDataSet(fvm->GetFileName());
+ data_set->Clear();
+ data_set->LoadDataSet(fvm->GetFileName());
+ } catch (const BaseException& e) {
+ LOGE("%s", e.what());
+ throw e;
+ }
}
void FaceRecognition::UpdateDataSet(unique_ptr<DataSetManager>& data_set)
{
- data_set = CreateDSM(_config.training_engine_backend_type);
+ try {
+ data_set = CreateDSM(_config.training_engine_backend_type);
- auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
+ auto fvm = CreateFVM(_config.training_engine_backend_type, _config.feature_vector_file_path);
- if (FaceRecogUtil::IsFileExist(fvm->GetFileName()) == false)
- throw InvalidOperation("Feature vector file not found.");
+ if (FaceRecogUtil::IsFileExist(fvm->GetFileName()) == false)
+ throw InvalidOperation("Feature vector file not found.");
- data_set->LoadDataSet(fvm->GetFileName());
+ data_set->LoadDataSet(fvm->GetFileName());
+ } catch (const BaseException& e) {
+ LOGE("%s", e.what());
+ throw e;
+ }
}
void FaceRecognition::SetConfig(FaceRecognitionConfig& config)
@@ -568,9 +594,18 @@ int FaceRecognition::DeleteLabel(string label_name)
fvm_new->WriteHeader(feature_vectors[0].size(), label_cnt, data_set_cnt);
- // Change new data file to existing one.
- remove(fvm->GetFileName().c_str());
- rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str());
+ int ret = 0;
+
+ if (FaceRecogUtil::IsFileExist(fvm->GetFileName())) {
+ // Change new data file to existing one.
+ ret = ::remove(fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove feature vector file.");
+ }
+
+ ret = ::rename(fvm_new->GetFileName().c_str(), fvm->GetFileName().c_str());
+ if (ret)
+ throw InvalidOperation("Fail to rename new feature vector file to original one.");
if (data_set_cnt == 0) {
_training_model->RemoveModel();
diff --git a/mv_machine_learning/face_recognition/src/simple_shot.cpp b/mv_machine_learning/face_recognition/src/simple_shot.cpp
index 8d31e5a8..c3a5ff89 100644
--- a/mv_machine_learning/face_recognition/src/simple_shot.cpp
+++ b/mv_machine_learning/face_recognition/src/simple_shot.cpp
@@ -27,6 +27,7 @@
#include "simple_shot.h"
#include "data_set_manager.h"
#include "feature_vector_manager.h"
+#include "file_util.h"
using namespace std;
using namespace TrainingEngineInterface::Common;
@@ -119,14 +120,22 @@ void SimpleShot::ConfigureModel(int num_of_class)
void SimpleShot::SaveModel(const string file_path)
{
- int ret = TRAINING_ENGINE_ERROR_NONE;
-
string bin_file_path = file_path.substr(0, file_path.find('.')) + ".bin";
+ int ret = 0;
// NNStreamer returns an error if internal model file(ini and bin files) exists before generating it.
// So remove existing files.
- ::remove(bin_file_path.c_str());
- ::remove(file_path.c_str());
+ if (FaceRecogUtil::IsFileExist(bin_file_path)) {
+ ret = ::remove(bin_file_path.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove internal model file.");
+ }
+
+ if (FaceRecogUtil::IsFileExist(file_path)) {
+ ret = ::remove(file_path.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove internal model file.");
+ }
ret = _training->SaveModel(_model.get(), file_path);
if (ret != TRAINING_ENGINE_ERROR_NONE)
@@ -135,11 +144,19 @@ void SimpleShot::SaveModel(const string file_path)
void SimpleShot::RemoveModel(const string file_path)
{
- int ret = TRAINING_ENGINE_ERROR_NONE;
-
string bin_file_path = file_path.substr(0, file_path.find('.')) + ".bin";
+ int ret = 0;
// Remove existing files forcely.
- ::remove(bin_file_path.c_str());
- ::remove(file_path.c_str());
+ if (FaceRecogUtil::IsFileExist(bin_file_path)) {
+ ret = ::remove(bin_file_path.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove internal model file.");
+ }
+
+ if (FaceRecogUtil::IsFileExist(file_path)) {
+ ret = ::remove(file_path.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove internal model file.");
+ }
} \ No newline at end of file
diff --git a/mv_machine_learning/inference/src/mv_inference_open.cpp b/mv_machine_learning/inference/src/mv_inference_open.cpp
index a9e30e35..da48ac83 100644
--- a/mv_machine_learning/inference/src/mv_inference_open.cpp
+++ b/mv_machine_learning/inference/src/mv_inference_open.cpp
@@ -216,7 +216,7 @@ static int configure_model_open(Inference *pInfer, mv_engine_config_h engine_con
if (std::string(modelMetaFilePath).empty()) {
LOGW("Skip ParseMetadata and run without Metadata");
- goto release_model_meta_file_path;
+ goto release_model_user_file_path;
}
if (!IsJsonFile(std::string(modelMetaFilePath))) {
diff --git a/mv_machine_learning/training/src/label_manager.cpp b/mv_machine_learning/training/src/label_manager.cpp
index 11f1881a..6a4deb4e 100644
--- a/mv_machine_learning/training/src/label_manager.cpp
+++ b/mv_machine_learning/training/src/label_manager.cpp
@@ -132,10 +132,17 @@ unsigned int LabelManager::RemoveLabel(const string given_label)
if (label_index == 0) {
// Just keep original version in case of no copy so drop the new label file.
- remove(new_label_file.c_str());
+ int ret = ::remove(new_label_file.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove label file.");
} else {
- remove(_label_file.c_str());
- rename(new_label_file.c_str(), _label_file.c_str());
+ int ret = ::remove(_label_file.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to remove label file.");
+
+ ret = ::rename(new_label_file.c_str(), _label_file.c_str());
+ if (ret)
+ throw InvalidOperation("Fail to rename new labal file to original one.");
}
return label_index;
diff --git a/packaging/capi-media-vision.spec b/packaging/capi-media-vision.spec
index 871bd153..7121aa7a 100644
--- a/packaging/capi-media-vision.spec
+++ b/packaging/capi-media-vision.spec
@@ -1,6 +1,6 @@
Name: capi-media-vision
Summary: Media Vision library for Tizen Native API
-Version: 0.23.13
+Version: 0.23.14
Release: 0
Group: Multimedia/Framework
License: Apache-2.0 and BSD-3-Clause