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 "facial_landmark_adapter.h"
19 #include "mv_landmark_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> FacialLandmarkAdapter<T, V>::FacialLandmarkAdapter() : _source()
32 _config = make_shared<LandmarkDetectionConfig>();
33 _config->parseConfigFile(_config_file_name);
35 LandmarkDetectionTaskType model_type = convertToTaskType(_config->getDefaultModelName());
39 template<typename T, typename V> FacialLandmarkAdapter<T, V>::~FacialLandmarkAdapter()
41 _landmark_detection->preDestroy();
44 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::create(LandmarkDetectionTaskType task_type)
46 if (_landmark_detection) {
47 // If current task type is same as a given one then skip.
48 if (_landmark_detection->getTaskType() == task_type)
52 // if model name is changed by user then reallocate the parser and reload the meta file corresponding to the model name.
53 _config->loadMetaFile(task_type);
55 if (task_type == LandmarkDetectionTaskType::FLD_TWEAK_CNN)
56 _landmark_detection = make_unique<FldTweakCnn>(task_type, _config);
59 template<typename T, typename V>
60 LandmarkDetectionTaskType FacialLandmarkAdapter<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 == "FLD_TWEAK_CNN")
68 return LandmarkDetectionTaskType::FLD_TWEAK_CNN;
71 throw InvalidParameter("Invalid facial detection model name.");
74 template<typename T, typename V>
75 void FacialLandmarkAdapter<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 LandmarkDetectionTaskType 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 FacialLandmarkAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
96 _landmark_detection->setEngineInfo(string(engine_type), string(device_type));
99 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::configure()
101 _landmark_detection->configure();
104 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
106 _landmark_detection->getNumberOfEngines(number_of_engines);
109 template<typename T, typename V>
110 void FacialLandmarkAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
112 _landmark_detection->getEngineType(engine_index, engine_type);
115 template<typename T, typename V>
116 void FacialLandmarkAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
118 _landmark_detection->getNumberOfDevices(engine_type, number_of_devices);
121 template<typename T, typename V>
122 void FacialLandmarkAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
124 _landmark_detection->getDeviceType(engine_type, device_index, device_type);
127 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::prepare()
129 _landmark_detection->prepare();
132 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::setInput(T &t)
137 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::perform()
139 _landmark_detection->perform(_source.inference_src);
142 template<typename T, typename V> void FacialLandmarkAdapter<T, V>::performAsync(T &t)
144 _landmark_detection->performAsync(t);
147 template<typename T, typename V> V &FacialLandmarkAdapter<T, V>::getOutput()
149 return _landmark_detection->getOutput();
152 template<typename T, typename V> V &FacialLandmarkAdapter<T, V>::getOutputCache()
154 throw InvalidOperation("Not support yet.");
157 template class FacialLandmarkAdapter<LandmarkDetectionInput, LandmarkDetectionResult>;