3bdd82ac2ef29b467290045061d78b68ebcb5985
[platform/core/api/mediavision.git] / mv_machine_learning / object_detection / src / object_detection_adapter.cpp
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "machine_learning_exception.h"
18 #include "object_detection_adapter.h"
19 #include "object_detection_external.h"
20 #include "mv_object_detection_config.h"
21
22 using namespace std;
23 using namespace MediaVision::Common;
24 using namespace mediavision::machine_learning;
25 using namespace mediavision::machine_learning::exception;
26
27 namespace mediavision
28 {
29 namespace machine_learning
30 {
31 template<typename T, typename V> ObjectDetectionAdapter<T, V>::ObjectDetectionAdapter() : _source()
32 {
33         _config = make_shared<MachineLearningConfig>();
34         _config->parseConfigFile(_config_file_name);
35
36         ObjectDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
37         create(model_type);
38 }
39
40 template<typename T, typename V> ObjectDetectionAdapter<T, V>::~ObjectDetectionAdapter()
41 {
42         _object_detection->preDestroy();
43 }
44
45 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::create(ObjectDetectionTaskType task_type)
46 {
47         // If a concrete class object created already exists, reset the object
48         // so that other concrete class object can be created again according to a given task_type.
49         if (_object_detection) {
50                 // If default task type is same as a given one then skip.
51                 if (_object_detection->getTaskType() == task_type)
52                         return;
53         }
54
55         if (task_type == ObjectDetectionTaskType::MOBILENET_V1_SSD)
56                 _object_detection = make_unique<MobilenetV1Ssd>(task_type, _config);
57         else if (task_type == ObjectDetectionTaskType::MOBILENET_V2_SSD)
58                 _object_detection = make_unique<MobilenetV2Ssd>(task_type, _config);
59         else if (task_type == ObjectDetectionTaskType::OD_PLUGIN || task_type == ObjectDetectionTaskType::FD_PLUGIN)
60                 _object_detection = make_unique<ObjectDetectionExternal>(task_type);
61         // TODO.
62 }
63
64 template<typename T, typename V>
65 ObjectDetectionTaskType ObjectDetectionAdapter<T, V>::convertToTaskType(string model_name)
66 {
67         if (model_name.empty())
68                 throw InvalidParameter("model name is empty.");
69
70         transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);
71
72         if (model_name == "OD_PLUGIN")
73                 return ObjectDetectionTaskType::OD_PLUGIN;
74         else if (model_name == "FD_PLUGIN")
75                 return ObjectDetectionTaskType::FD_PLUGIN;
76         else if (model_name == "MOBILENET_V1_SSD")
77                 return ObjectDetectionTaskType::MOBILENET_V1_SSD;
78         else if (model_name == "MOBILENET_V2_SSD")
79                 return ObjectDetectionTaskType::MOBILENET_V2_SSD;
80         // TODO.
81
82         throw InvalidParameter("Invalid object detection model name.");
83 }
84
85 template<typename T, typename V>
86 void ObjectDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
87                                                                                                 const char *model_name)
88 {
89         try {
90                 _config->setUserModel(model_file, meta_file, label_file);
91
92                 ObjectDetectionTaskType model_type = convertToTaskType(model_name);
93                 create(model_type);
94         } catch (const BaseException &e) {
95                 LOGW("A given model name is invalid so default task type will be used.");
96         }
97
98         if (!model_file && !meta_file) {
99                 LOGW("Given model info is invalid so default model info will be used instead.");
100                 return;
101         }
102 }
103
104 template<typename T, typename V>
105 void ObjectDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
106 {
107         _object_detection->setEngineInfo(string(engine_type), string(device_type));
108 }
109
110 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::configure()
111 {
112         _object_detection->configure();
113 }
114
115 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
116 {
117         _object_detection->getNumberOfEngines(number_of_engines);
118 }
119
120 template<typename T, typename V>
121 void ObjectDetectionAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
122 {
123         _object_detection->getEngineType(engine_index, engine_type);
124 }
125
126 template<typename T, typename V>
127 void ObjectDetectionAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
128 {
129         _object_detection->getNumberOfDevices(engine_type, number_of_devices);
130 }
131
132 template<typename T, typename V>
133 void ObjectDetectionAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
134 {
135         _object_detection->getDeviceType(engine_type, device_index, device_type);
136 }
137
138 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::prepare()
139 {
140         _object_detection->prepare();
141 }
142
143 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::setInput(T &t)
144 {
145         _source = t;
146 }
147
148 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::perform()
149 {
150         _object_detection->perform(_source.inference_src);
151 }
152
153 template<typename T, typename V> V &ObjectDetectionAdapter<T, V>::getOutput()
154 {
155         return _object_detection->getOutput();
156 }
157
158 template<typename T, typename V> V &ObjectDetectionAdapter<T, V>::getOutputCache()
159 {
160         return _object_detection->getOutputCache();
161 }
162
163 template<typename T, typename V> void ObjectDetectionAdapter<T, V>::performAsync(T &t)
164 {
165         _object_detection->performAsync(t);
166 }
167
168 template class ObjectDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;
169 }
170 }