mv_machine_learning: drop importLabel wrapper
authorInki Dae <inki.dae@samsung.com>
Wed, 18 Oct 2023 11:11:50 +0000 (20:11 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 25 Oct 2023 01:54:03 +0000 (10:54 +0900)
[Issue type] : code cleanup

Drop importLabel member function of FaceRecognition class. This wrapper
function isn't needed anymore so we can call the importLabel function of
LabelManager class instead.

And also change return type to void including removeLabel().
It's enough with throwing an exception in error case, and return NO_DATA
if label file doesn't exist when we called recognitionFace() - it means
that we tried to recognize a given input but didn't find a proper label.

Change-Id: Ib49b72261a1b8be029d58f8088507e8bece0c674
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/face_recognition/include/face_recognition.h
mv_machine_learning/face_recognition/src/face_recognition.cpp
mv_machine_learning/training/include/label_manager.h
mv_machine_learning/training/src/label_manager.cpp

index c24410e..f8a493e 100644 (file)
@@ -83,8 +83,6 @@ private:
        FaceRecognitionConfig _config;
        FaceRecognitionResult _result;
 
-       // FYI. This function should be called every time a new face is registered.
-       void importLabel();
        void checkFeatureVectorFile(std::string fv_file_name, std::string new_fv_file_name);
        void storeDataSet(std::unique_ptr<DataSetManager> &data_set, unsigned int label_cnt);
        void checkResult();
index 9ca5e15..f1a1989 100644 (file)
@@ -121,19 +121,6 @@ int FaceRecognition::initialize()
        return MEDIA_VISION_ERROR_NONE;
 }
 
-void FaceRecognition::importLabel()
-{
-       try {
-               // Update label manager from a given label file.
-               int cnt = _label_manager->importLabel();
-
-               LOGD("%d labels have been imported", cnt);
-       } catch (const BaseException &e) {
-               LOGE("%s", e.what());
-               throw e;
-       }
-}
-
 int FaceRecognition::registerNewFace(std::vector<float> &input_vec, string label_name)
 {
        if (_status < WorkingStatus::INITIALIZED) {
@@ -148,7 +135,7 @@ int FaceRecognition::registerNewFace(std::vector<float> &input_vec, string label
                        _label_manager->addLabelToFile(label_name);
 
                // Import label data from a label file.
-               importLabel();
+               _label_manager->importLabel();
 
                // Get label index and count.
                unsigned int label_idx = _label_manager->getLabelIndex(label_name);
@@ -230,7 +217,7 @@ int FaceRecognition::recognizeFace(std::vector<float> &input_vec)
 
        try {
                // Import label data from a label file.
-               importLabel();
+               _label_manager->importLabel();
 
                if (_label_manager->isEmpty()) {
                        _result.is_valid = false;
@@ -241,7 +228,7 @@ int FaceRecognition::recognizeFace(std::vector<float> &input_vec)
 
                if (!FaceRecogUtil::isFileExist(_config.internal_model_file_path)) {
                        LOGE("Internal model file(%s) doesn't exist.", _config.internal_model_file_path.c_str());
-                       return MEDIA_VISION_ERROR_INVALID_PATH;
+                       return MEDIA_VISION_ERROR_NO_DATA;
                }
 
                TrainingEngineBackendInfo engine_info = _training_model->getTrainingEngineInfo();
@@ -337,7 +324,7 @@ int FaceRecognition::deleteLabel(string label_name)
                }
 
                // Import label data from a label file.
-               importLabel();
+               _label_manager->importLabel();
 
                unsigned int target_label_idx = _label_manager->getLabelIndex(label_name);
                auto label_cnt_ori = _label_manager->getMaxLabel();
index f5a23b3..325c1ab 100644 (file)
@@ -42,10 +42,10 @@ public:
        float getDecisionWeight();
        unsigned int getLabelIndex(const std::string &given_label);
        bool isExist(const std::string given_label);
-       unsigned int removeLabel(const std::string given_label);
+       void removeLabel(const std::string given_label);
        void getLabelString(std::string &label, const int idx);
        void addLabelToFile(std::string given_label);
-       size_t importLabel(void);
+       void importLabel(void);
        const std::vector<std::string> &getLabels(void);
        size_t getMaxLabel();
        void removeFile();
index a90b0fe..0a3e72d 100644 (file)
@@ -55,7 +55,7 @@ unsigned int LabelManager::getLabelIndex(const string &given_label)
        return distance(_labels.begin(), iter);
 }
 
-unsigned int LabelManager::removeLabel(const string given_label)
+void LabelManager::removeLabel(const string given_label)
 {
        ifstream readFile { _label_file };
 
@@ -81,8 +81,6 @@ unsigned int LabelManager::removeLabel(const string given_label)
        if (total_length == writeFile.tellp()) {
                if (::remove(new_label_file.c_str()) == -1)
                        throw InvalidOperation("Fail to remove label file.");
-
-               return _labels.size();
        }
 
        if (::remove(_label_file.c_str()) == -1)
@@ -91,7 +89,8 @@ unsigned int LabelManager::removeLabel(const string given_label)
        if (::rename(new_label_file.c_str(), _label_file.c_str()) == -1)
                throw InvalidOperation("Fail to rename new labal file to original one.");
 
-       return importLabel(); // Update _labels because label file is changed.
+       // Update _labels because label file is changed.
+       importLabel();
 }
 
 void LabelManager::getLabelString(string &label, const int idx)
@@ -116,13 +115,11 @@ void LabelManager::addLabelToFile(string given_label)
        writeFile.close();
 }
 
-size_t LabelManager::importLabel(void)
+void LabelManager::importLabel(void)
 {
-       // label count is 0 if label file doesn't exist.
-       if (!FaceRecogUtil::isFileExist(_label_file)) {
-               _labels.clear();
-               return 0;
-       }
+       // just return if label file doesn't exist.
+       if (!FaceRecogUtil::isFileExist(_label_file))
+               return;
 
        ifstream readFile;
 
@@ -145,7 +142,7 @@ size_t LabelManager::importLabel(void)
 
        readFile.close();
 
-       return _labels.size();
+       LOGD("%zu labels have been imported", _labels.size());
 }
 
 const std::vector<std::string> &LabelManager::getLabels(void)
@@ -178,4 +175,4 @@ void LabelManager::removeFile()
 bool LabelManager::isEmpty()
 {
        return _labels.empty();
-}
\ No newline at end of file
+}