mv_machine_learning: code clean to setModelInfo function
authorInki Dae <inki.dae@samsung.com>
Mon, 7 Aug 2023 22:45:58 +0000 (07:45 +0900)
committerKwanghoon Son <k.son@samsung.com>
Mon, 4 Sep 2023 04:57:02 +0000 (13:57 +0900)
[Issue type] : code cleanup

Clean up setModelInfo function by doing function extraction.

setModelInfo function converted a given model name to its corresponding
task type internally so it made the function to be complicated.
This patch extracts the conversion portion from setModelInfo function as
another function and makes the function to be called instead.

Change-Id: I93243ffba543f9c6e9a1f0d7690d8f48a1bd14e1
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/object_detection/include/object_detection_adapter.h
mv_machine_learning/object_detection/src/object_detection_adapter.cpp

index 7aa6ff4..45aea8b 100644 (file)
@@ -39,6 +39,7 @@ private:
        std::string _label_file;
 
        void create(ObjectDetectionTaskType task_type);
+       ObjectDetectionTaskType convertToTaskType(std::string model_name);
 
 public:
        ObjectDetectionAdapter();
index 57993b3..c6bb566 100644 (file)
@@ -58,38 +58,36 @@ template<typename T, typename V> void ObjectDetectionAdapter<T, V>::create(Objec
 }
 
 template<typename T, typename V>
-void ObjectDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
-                                                                                               const char *model_name)
+ObjectDetectionTaskType ObjectDetectionAdapter<T, V>::convertToTaskType(string model_name)
 {
-       string model_name_str(model_name);
-
-       if (!model_name_str.empty()) {
-               transform(model_name_str.begin(), model_name_str.end(), model_name_str.begin(), ::toupper);
+       if (model_name.empty())
+               throw InvalidParameter("model name is empty.");
 
-               ObjectDetectionTaskType task_type = ObjectDetectionTaskType::OBJECT_DETECTION_TASK_NONE;
+       transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);
 
-               if (model_name_str == "OD_PLUGIN" || model_name_str == "FD_PLUGIN") {
-                       if (model_name_str == "OD_PLUGIN")
-                               task_type = ObjectDetectionTaskType::OD_PLUGIN;
-                       if (model_name_str == "FD_PLUGIN")
-                               task_type = ObjectDetectionTaskType::FD_PLUGIN;
+       ObjectDetectionTaskType task_type = ObjectDetectionTaskType::OBJECT_DETECTION_TASK_NONE;
 
-                       // In case of using plugin module, model information will be managed by the plugin module.
-                       // Therefore, create plugin instance now.
-                       create(task_type);
-                       return;
-               }
+       if (model_name == "OD_PLUGIN")
+               return ObjectDetectionTaskType::OD_PLUGIN;
+       else if (model_name == "FD_PLUGIN")
+               return ObjectDetectionTaskType::FD_PLUGIN;
+       else if (model_name == string("MOBILENET_V1_SSD"))
+               return ObjectDetectionTaskType::MOBILENET_V1_SSD;
+       else if (model_name == string("MOBILENET_V2_SSD"))
+               return ObjectDetectionTaskType::MOBILENET_V2_SSD;
+       // TODO.
 
-               if (model_name_str == string("MOBILENET_V1_SSD")) {
-                       task_type = ObjectDetectionTaskType::MOBILENET_V1_SSD;
-               } else if (model_name_str == string("MOBILENET_V2_SSD")) {
-                       task_type = ObjectDetectionTaskType::MOBILENET_V2_SSD;
-                       // TODO.
-               } else {
-                       throw InvalidParameter("Invalid object detection model name.");
-               }
+       throw InvalidParameter("Invalid object detection model name.");
+}
 
-               create(task_type);
+template<typename T, typename V>
+void ObjectDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
+                                                                                               const char *model_name)
+{
+       try {
+               create(convertToTaskType(string(model_name)));
+       } catch (const BaseException &e) {
+               LOGW("A given model name is invalid so default task type will be used.");
        }
 
        _model_file = string(model_file);