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"
21 using namespace MediaVision::Common;
22 using namespace mediavision::machine_learning;
23 using namespace mediavision::machine_learning::exception;
27 namespace machine_learning
29 template<typename T, typename V> FaceDetectionAdapter<T, V>::FaceDetectionAdapter() : _source()
31 // In default, FD Mobilenet v1 ssd model will be used.
32 // If other model is set by user then strategy pattern will be used
33 // to create its corresponding concerte class by calling create().
34 _object_detection = make_unique<MobilenetV1Ssd>(ObjectDetectionTaskType::FD_MOBILENET_V1_SSD);
37 template<typename T, typename V> FaceDetectionAdapter<T, V>::~FaceDetectionAdapter()
39 _object_detection->preDestroy();
42 template<typename T, typename V> void FaceDetectionAdapter<T, V>::create(int type)
44 ObjectDetectionTaskType task_type = static_cast<ObjectDetectionTaskType>(type);
46 // If default task type is same as a given one then skip.
47 if (_object_detection->getTaskType() == task_type)
50 _object_detection.reset();
52 if (task_type == ObjectDetectionTaskType::FD_MOBILENET_V1_SSD)
53 _object_detection = make_unique<MobilenetV1Ssd>(task_type);
57 template<typename T, typename V>
58 void FaceDetectionAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
59 const char *model_name)
61 string model_name_str(model_name);
63 if (!model_name_str.empty()) {
64 transform(model_name_str.begin(), model_name_str.end(), model_name_str.begin(), ::toupper);
68 if (model_name_str == string("FD_MOBILENET_V1_SSD"))
69 model_type = static_cast<int>(ObjectDetectionTaskType::FD_MOBILENET_V1_SSD);
72 throw InvalidParameter("Invalid face detection model name.");
74 create(static_cast<int>(model_type));
77 _model_file = string(model_file);
78 _meta_file = string(meta_file);
79 _label_file = string(label_file);
81 if (_model_file.empty() && _meta_file.empty() && _label_file.empty())
84 _object_detection->setUserModel(_model_file, _meta_file, _label_file);
87 template<typename T, typename V>
88 void FaceDetectionAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
90 _object_detection->setEngineInfo(string(engine_type), string(device_type));
93 template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
95 _object_detection->parseMetaFile("face_detection.json");
96 _object_detection->configure();
99 template<typename T, typename V> void FaceDetectionAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
101 _object_detection->getNumberOfEngines(number_of_engines);
104 template<typename T, typename V>
105 void FaceDetectionAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
107 _object_detection->getEngineType(engine_index, engine_type);
110 template<typename T, typename V>
111 void FaceDetectionAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
113 _object_detection->getNumberOfDevices(engine_type, number_of_devices);
116 template<typename T, typename V>
117 void FaceDetectionAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
119 _object_detection->getDeviceType(engine_type, device_index, device_type);
122 template<typename T, typename V> void FaceDetectionAdapter<T, V>::prepare()
124 _object_detection->prepare();
127 template<typename T, typename V> void FaceDetectionAdapter<T, V>::setInput(T &t)
132 template<typename T, typename V> void FaceDetectionAdapter<T, V>::perform()
134 shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
135 if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
136 _object_detection->perform<unsigned char>(_source.inference_src, metaInfo);
137 else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
138 _object_detection->perform<float>(_source.inference_src, metaInfo);
140 throw InvalidOperation("Invalid model data type.");
143 template<typename T, typename V> void FaceDetectionAdapter<T, V>::performAsync(T &t)
145 shared_ptr<MetaInfo> metaInfo = _object_detection->getInputMetaInfo();
147 if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8) {
148 _object_detection->performAsync<unsigned char>(t, metaInfo);
149 } else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32) {
150 _object_detection->performAsync<float>(t, metaInfo);
153 throw InvalidOperation("Invalid model data type.");
157 template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutput()
159 return _object_detection->getOutput<V>();
162 template class FaceDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;