4c72a4a6c910a0ddddfcab83e4228672fc65d0f4
[platform/core/api/mediavision.git] / mv_machine_learning / object_detection / src / face_detection_adapter.cpp
1 /**
2  * Copyright (c) 2023 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 "face_detection_adapter.h"
19 #include "mv_object_detection_config.h"
20
21 using namespace std;
22 using namespace MediaVision::Common;
23 using namespace mediavision::machine_learning;
24 using namespace mediavision::machine_learning::exception;
25
26 namespace mediavision
27 {
28 namespace machine_learning
29 {
30 template<typename T, typename V> FaceDetectionAdapter<T, V>::FaceDetectionAdapter() : _source()
31 {
32         _config = make_shared<MachineLearningConfig>();
33         _config->parseConfigFile(_config_file_name);
34
35         ObjectDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
36         create(model_type);
37 }
38
39 template<typename T, typename V> FaceDetectionAdapter<T, V>::~FaceDetectionAdapter()
40 {
41         _object_detection->preDestroy();
42 }
43
44 template<typename T, typename V> void FaceDetectionAdapter<T, V>::create(ObjectDetectionTaskType task_type)
45 {
46         // If a concrete class object created already exists, reset the object
47         // so that other concrete class object can be created again according to a given task_type.
48         if (_object_detection) {
49                 // If default task type is same as a given one then skip.
50                 if (_object_detection->getTaskType() == task_type)
51                         return;
52         }
53
54         if (task_type == ObjectDetectionTaskType::FD_MOBILENET_V1_SSD)
55                 _object_detection = make_unique<MobilenetV1Ssd>(task_type, _config);
56         // TODO.
57 }
58
59 template<typename T, typename V>
60 ObjectDetectionTaskType FaceDetectionAdapter<T, V>::convertToTaskType(string model_name)
61 {
62         if (model_name.empty())
63                 throw InvalidParameter("model name is empty.");
64
65         transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);
66
67         if (model_name == "FD_MOBILENET_V1_SSD")
68                 return ObjectDetectionTaskType::FD_MOBILENET_V1_SSD;
69         // TODO.
70
71         throw InvalidParameter("Invalid face detection model name.");
72 }
73
74 template<typename T, typename V>
75 void FaceDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
76                                                                                           const char *model_name)
77 {
78         try {
79                 _config->setUserModel(model_file, meta_file, label_file);
80
81                 ObjectDetectionTaskType model_type = convertToTaskType(model_name);
82                 create(model_type);
83         } catch (const BaseException &e) {
84                 LOGW("A given model name is invalid so default task type will be used.");
85         }
86
87         if (!model_file && !meta_file) {
88                 LOGW("Given model info is invalid so default model info will be used instead.");
89                 return;
90         }
91 }
92
93 template<typename T, typename V>
94 void FaceDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
95 {
96         _object_detection->setEngineInfo(string(engine_type), string(device_type));
97 }
98
99 template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
100 {
101         _object_detection->configure();
102 }
103
104 template<typename T, typename V> void FaceDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
105 {
106         _object_detection->getNumberOfEngines(number_of_engines);
107 }
108
109 template<typename T, typename V>
110 void FaceDetectionAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
111 {
112         _object_detection->getEngineType(engine_index, engine_type);
113 }
114
115 template<typename T, typename V>
116 void FaceDetectionAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
117 {
118         _object_detection->getNumberOfDevices(engine_type, number_of_devices);
119 }
120
121 template<typename T, typename V>
122 void FaceDetectionAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
123 {
124         _object_detection->getDeviceType(engine_type, device_index, device_type);
125 }
126
127 template<typename T, typename V> void FaceDetectionAdapter<T, V>::prepare()
128 {
129         _object_detection->prepare();
130 }
131
132 template<typename T, typename V> void FaceDetectionAdapter<T, V>::setInput(T &t)
133 {
134         _source = t;
135 }
136
137 template<typename T, typename V> void FaceDetectionAdapter<T, V>::perform()
138 {
139         _object_detection->perform(_source.inference_src);
140 }
141
142 template<typename T, typename V> void FaceDetectionAdapter<T, V>::performAsync(T &t)
143 {
144         _object_detection->performAsync(t);
145 }
146
147 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutput()
148 {
149         return _object_detection->getOutput();
150 }
151
152 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutputCache()
153 {
154         return _object_detection->getOutputCache();
155 }
156
157 template class FaceDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;
158 }
159 }