summaryrefslogtreecommitdiff
path: root/mv_machine_learning/landmark_detection/src/FacialLandmarkAdapter.cpp
blob: 53ffe7970df047fe1758e164539d105c4a807d80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MvMlException.h"
#include "FacialLandmarkAdapter.h"
#include "mv_landmark_detection_config.h"

using namespace std;
using namespace MediaVision::Common;
using namespace mediavision::machine_learning;
using namespace mediavision::machine_learning::exception;

namespace mediavision
{
namespace machine_learning
{
FacialLandmarkAdapter::FacialLandmarkAdapter()
{
	_config = make_shared<Config>();
	_config->parseConfigFile(_config_file_name);

	create(_config->getDefaultModelName());
}

FacialLandmarkAdapter::~FacialLandmarkAdapter()
{
	_landmark_detection->preDestroy();
}

template<typename U> void FacialLandmarkAdapter::create(LandmarkDetectionTaskType task_type)
{
	switch (task_type) {
	case LandmarkDetectionTaskType::FLD_TWEAK_CNN:
		_landmark_detection = make_unique<FldTweakCnn<U> >(task_type, _config);
		break;
	default:
		throw InvalidOperation("Invalid landmark detection task type.");
	}
}

void FacialLandmarkAdapter::create(const string &model_name)
{
	auto task_type = convertToTaskType(model_name.empty() ? _config->getDefaultModelName() : model_name);

	_config->loadMetaFile(make_unique<LandmarkDetectionParser>(static_cast<int>(task_type)));
	mv_inference_data_type_e dataType = _config->getInputMetaMap().begin()->second->dataType;

	switch (dataType) {
	case MV_INFERENCE_DATA_UINT8:
		create<unsigned char>(task_type);
		break;
	case MV_INFERENCE_DATA_FLOAT32:
		create<float>(task_type);
		break;
	default:
		throw InvalidOperation("Invalid landmark detection data type.");
	}
}

LandmarkDetectionTaskType FacialLandmarkAdapter::convertToTaskType(string model_name)
{
	if (model_name.empty())
		throw InvalidParameter("model name is empty.");

	transform(model_name.begin(), model_name.end(), model_name.begin(), ::toupper);

	if (model_name == "FLD_TWEAK_CNN")
		return LandmarkDetectionTaskType::FLD_TWEAK_CNN;
	// TODO.

	throw InvalidParameter("Invalid facial detection model name.");
}

void FacialLandmarkAdapter::setModelInfo(const string &model_file, const string &meta_file, const string &label_file,
										 const string &model_name)
{
	try {
		_config->setUserModel(model_file, meta_file, label_file);
		create(model_name);
	} catch (const BaseException &e) {
		LOGW("A given model name is invalid so default task type will be used.");
	}

	if (model_file.empty() && meta_file.empty()) {
		LOGW("Given model info is invalid so default model info will be used instead.");
		return;
	}
}

void FacialLandmarkAdapter::setEngineInfo(const string &engine_type, const string &device_type)
{
	_landmark_detection->setEngineInfo(string(engine_type), string(device_type));
}

void FacialLandmarkAdapter::configure()
{
	_landmark_detection->configure();
}

unsigned int FacialLandmarkAdapter::getNumberOfEngines()
{
	return _landmark_detection->getNumberOfEngines();
}

const string &FacialLandmarkAdapter::getEngineType(unsigned int engine_index)
{
	return _landmark_detection->getEngineType(engine_index);
}

unsigned int FacialLandmarkAdapter::getNumberOfDevices(const string &engine_type)
{
	return _landmark_detection->getNumberOfDevices(engine_type);
}

const string &FacialLandmarkAdapter::getDeviceType(const string &engine_type, unsigned int device_index)
{
	return _landmark_detection->getDeviceType(engine_type, device_index);
}

void FacialLandmarkAdapter::prepare()
{
	_landmark_detection->prepare();
}

void FacialLandmarkAdapter::perform(InputBaseType &input)
{
	_landmark_detection->perform(input.inference_src);
}

void FacialLandmarkAdapter::performAsync(InputBaseType &input)
{
	_landmark_detection->performAsync(static_cast<LandmarkDetectionInput &>(input));
}

OutputBaseType &FacialLandmarkAdapter::getOutput()
{
	return _landmark_detection->getOutput();
}

OutputBaseType &FacialLandmarkAdapter::getOutputCache()
{
	return _landmark_detection->getOutputCache();
}

}
}