summaryrefslogtreecommitdiff
path: root/mv_machine_learning/landmark_detection/src/pose_landmark_adapter.cpp
blob: 5fb6c3132e0fd5d8e9a89964435fdb1239a5802f (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
/**
 * 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 "machine_learning_exception.h"
#include "pose_landmark_adapter.h"

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

namespace mediavision
{
namespace machine_learning
{
template<typename T, typename V> PoseLandmarkAdapter<T, V>::PoseLandmarkAdapter() : _source()
{
	// In default, Mobilenet v1 ssd model will be used.
	// If other model is set by user then strategy pattern will be used
	// to create its corresponding concrete class by calling create().
	_landmark_detection = make_unique<PldCpm>(LandmarkDetectionTaskType::PLD_CPM);
}

template<typename T, typename V> PoseLandmarkAdapter<T, V>::~PoseLandmarkAdapter()
{}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::create(LandmarkDetectionTaskType task_type)
{
	// If default task type is same as a given one then skip.
	if (_landmark_detection->getTaskType() == task_type)
		return;

	_landmark_detection.reset();

	if (task_type == LandmarkDetectionTaskType::PLD_CPM)
		_landmark_detection = make_unique<PldCpm>(task_type);
	// TODO.
}

template<typename T, typename V>
void PoseLandmarkAdapter<T, V>::setModelInfo(const char *model_file, const char *meta_file, const char *label_file,
											 const char *model_name)
{
	string model_name_str(model_name);

	if (!model_name_str.empty()) {
		transform(model_name_str.begin(), model_name_str.end(), model_name_str.begin(), ::toupper);

		LandmarkDetectionTaskType task_type = LandmarkDetectionTaskType::LANDMARK_DETECTION_TASK_NONE;

		if (model_name_str == string("PLD_CPM"))
			task_type = LandmarkDetectionTaskType::PLD_CPM;
		// TODO.
		else
			throw InvalidParameter("Invalid landmark detection model name.");

		create(task_type);
	}

	_model_file = string(model_file);
	_meta_file = string(meta_file);
	_label_file = string(label_file);

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

	_landmark_detection->setUserModel(_model_file, _meta_file, _label_file);
}

template<typename T, typename V>
void PoseLandmarkAdapter<T, V>::setEngineInfo(const char *engine_type, const char *device_type)
{
	_landmark_detection->setEngineInfo(string(engine_type), string(device_type));
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::configure()
{
	_landmark_detection->parseMetaFile("pose_landmark.json");
	_landmark_detection->configure();
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::getNumberOfEngines(unsigned int *number_of_engines)
{
	_landmark_detection->getNumberOfEngines(number_of_engines);
}

template<typename T, typename V>
void PoseLandmarkAdapter<T, V>::getEngineType(unsigned int engine_index, char **engine_type)
{
	_landmark_detection->getEngineType(engine_index, engine_type);
}

template<typename T, typename V>
void PoseLandmarkAdapter<T, V>::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
{
	_landmark_detection->getNumberOfDevices(engine_type, number_of_devices);
}

template<typename T, typename V>
void PoseLandmarkAdapter<T, V>::getDeviceType(const char *engine_type, unsigned int device_index, char **device_type)
{
	_landmark_detection->getDeviceType(engine_type, device_index, device_type);
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::prepare()
{
	_landmark_detection->prepare();
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::setInput(T &t)
{
	_source = t;
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::perform()
{
	shared_ptr<MetaInfo> metaInfo = _landmark_detection->getInputMetaInfo();
	if (metaInfo->dataType == MV_INFERENCE_DATA_UINT8)
		_landmark_detection->perform<unsigned char>(_source.inference_src, metaInfo);
	else if (metaInfo->dataType == MV_INFERENCE_DATA_FLOAT32)
		_landmark_detection->perform<float>(_source.inference_src, metaInfo);
	else
		throw InvalidOperation("Invalid model data type.");
}

template<typename T, typename V> void PoseLandmarkAdapter<T, V>::performAsync(T &t)
{
	throw InvalidOperation("Not support yet.");
}

template<typename T, typename V> V &PoseLandmarkAdapter<T, V>::getOutput()
{
	return _landmark_detection->result();
}

template class PoseLandmarkAdapter<LandmarkDetectionInput, LandmarkDetectionResult>;
}
}