2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "machine_learning_exception.h"
18 #include "face_detection_adapter.h"
19 #include "mv_object_detection_config.h"
22 using namespace MediaVision::Common;
23 using namespace mediavision::machine_learning;
24 using namespace mediavision::machine_learning::exception;
28 namespace machine_learning
30 template<typename T, typename V> FaceDetectionAdapter<T, V>::FaceDetectionAdapter() : _source()
32 _config = make_shared<MachineLearningConfig>();
33 _config->parseConfigFile(_config_file_name);
35 ObjectDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
39 template<typename T, typename V> FaceDetectionAdapter<T, V>::~FaceDetectionAdapter()
41 _object_detection->preDestroy();
44 template<typename T, typename V> void FaceDetectionAdapter<T, V>::create(ObjectDetectionTaskType task_type)
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)
54 if (task_type == ObjectDetectionTaskType::FD_MOBILENET_V1_SSD)
55 _object_detection = make_unique<MobilenetV1Ssd>(task_type, _config);
59 template<typename T, typename V>
60 ObjectDetectionTaskType FaceDetectionAdapter<T, V>::convertToTaskType(string model_name)
62 if (model_name.empty())
63 throw InvalidParameter("model name is empty.");
65 transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);
67 if (model_name == "FD_MOBILENET_V1_SSD")
68 return ObjectDetectionTaskType::FD_MOBILENET_V1_SSD;
71 throw InvalidParameter("Invalid face detection model name.");
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)
79 _config->setUserModel(model_file, meta_file, label_file);
81 ObjectDetectionTaskType model_type = convertToTaskType(model_name);
83 } catch (const BaseException &e) {
84 LOGW("A given model name is invalid so default task type will be used.");
87 if (!model_file && !meta_file) {
88 LOGW("Given model info is invalid so default model info will be used instead.");
93 template<typename T, typename V>
94 void FaceDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
96 _object_detection->setEngineInfo(string(engine_type), string(device_type));
99 template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
101 _object_detection->configure();
104 template<typename T, typename V> void FaceDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
106 _object_detection->getNumberOfEngines(number_of_engines);
109 template<typename T, typename V>
110 void FaceDetectionAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
112 _object_detection->getEngineType(engine_index, engine_type);
115 template<typename T, typename V>
116 void FaceDetectionAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
118 _object_detection->getNumberOfDevices(engine_type, number_of_devices);
121 template<typename T, typename V>
122 void FaceDetectionAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
124 _object_detection->getDeviceType(engine_type, device_index, device_type);
127 template<typename T, typename V> void FaceDetectionAdapter<T, V>::prepare()
129 _object_detection->prepare();
132 template<typename T, typename V> void FaceDetectionAdapter<T, V>::setInput(T &t)
137 template<typename T, typename V> void FaceDetectionAdapter<T, V>::perform()
139 _object_detection->perform(_source.inference_src);
142 template<typename T, typename V> void FaceDetectionAdapter<T, V>::performAsync(T &t)
144 _object_detection->performAsync(t);
147 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutput()
149 return _object_detection->getOutput();
152 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutputCache()
154 return _object_detection->getOutputCache();
157 template class FaceDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;