summaryrefslogtreecommitdiff
path: root/mv_machine_learning/object_detection/src/face_detection_adapter.cpp
blob: 1e1473da492231916ee17a3a970a03427ef6449e (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
/**
 * 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 "face_detection_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> FaceDetectionAdapter<T, V>::FaceDetectionAdapter() : _source()
{
	// In default, FD 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().
	_object_detection = make_unique<MobilenetV1Ssd>(ObjectDetectionTaskType::FD_MOBILENET_V1_SSD);
}

template<typename T, typename V> FaceDetectionAdapter<T, V>::~FaceDetectionAdapter()
{
	_object_detection->preDestroy();
}

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

	_object_detection.reset();

	if (task_type == ObjectDetectionTaskType::FD_MOBILENET_V1_SSD)
		_object_detection = make_unique<MobilenetV1Ssd>(task_type);
	// TODO.
}

template<typename T, typename V>
void FaceDetectionAdapter<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);

		ObjectDetectionTaskType task_type = ObjectDetectionTaskType::OBJECT_DETECTION_TASK_NONE;

		if (model_name_str == string("FD_MOBILENET_V1_SSD"))
			task_type = ObjectDetectionTaskType::FD_MOBILENET_V1_SSD;
		// TODO.
		else
			throw InvalidParameter("Invalid face 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() && _label_file.empty())
		return;

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

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

template<typename T, typename V> void FaceDetectionAdapter<T, V>::configure()
{
	_object_detection->configure("face_detection.json");
}

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

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

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

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

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

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

template<typename T, typename V> void FaceDetectionAdapter<T, V>::perform()
{
	_object_detection->perform(_source.inference_src);
}

template<typename T, typename V> void FaceDetectionAdapter<T, V>::performAsync(T &t)
{
	_object_detection->performAsync(t);
}

template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutput()
{
	return _object_detection->getOutput();
}

template<typename T, typename V> V &FaceDetectionAdapter<T, V>::getOutputCache()
{
	return _object_detection->getOutputCache();
}

template class FaceDetectionAdapter<ObjectDetectionInput, ObjectDetectionResult>;
}
}