summaryrefslogtreecommitdiff
path: root/mv_machine_learning/image_segmentation/src/selfie_segmentation_adapter.cpp
blob: d43b75b654716e1b4bc5dbccb03dd721162421bb (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
160
161
162
163
164
165
166
167
168
169
170
/**
 * 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 "selfie_segmentation_adapter.h"
#include "image_segmentation_external.h"
#include "mv_image_segmentation_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
{
ImageSegmentationAdapter::ImageSegmentationAdapter()
{
	_config = make_shared<MachineLearningConfig>();

	// If the model type needs external plugin then bypass to load the meta file and just create the external plugin.
	// In this case, external plugin will use its own meta file approach regardless of Mediavision's one.
	_config->parsePluginConfigFile(_plugin_config_file_name);
	if (!_config->isPluginUsed())
		_config->parseConfigFile(_config_file_name);

	create(_config->getDefaultModelName());
}

ImageSegmentationAdapter::~ImageSegmentationAdapter()
{
	_selfie_segmentation->preDestroy();
}

template<typename U> void ImageSegmentationAdapter::create(ImageSegmentationTaskType task_type)
{
	// TODO. add switch-case statement here for Mediavision own task types.
}

void ImageSegmentationAdapter::create(std::string model_name)
{
	if (model_name.empty())
		model_name = _config->getDefaultModelName();

	auto task_type = convertToTaskType(model_name);

	if (_config->isPluginUsed()) {
		const auto &plugin_name = _config->getPluginFileName();

		_selfie_segmentation = make_unique<ImageSegmentationExternal>(task_type, plugin_name.c_str());
		return;
	}

	_config->loadMetaFile(make_unique<ImageSegmentationParser>(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 image segmentation data type.");
	}
}

ImageSegmentationTaskType ImageSegmentationAdapter::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 == "SELFIE_SEGMENTATION")
		return ImageSegmentationTaskType::SELFIE_SEGMENTATION;

	throw InvalidParameter("Invalid selfie segmentation model name.");
}

void ImageSegmentationAdapter::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;
	}

	_selfie_segmentation->setUserModel(model_file, meta_file, label_file);
}

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

void ImageSegmentationAdapter::configure()
{
	_selfie_segmentation->configure();
}

unsigned int ImageSegmentationAdapter::getNumberOfEngines()
{
	return _selfie_segmentation->getNumberOfEngines();
}

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

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

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

void ImageSegmentationAdapter::prepare()
{
	_selfie_segmentation->prepare();
}

void ImageSegmentationAdapter::perform(InputBaseType &input)
{
	_selfie_segmentation->perform(input.inference_src);
}

OutputBaseType &ImageSegmentationAdapter::getOutput()
{
	return _selfie_segmentation->getOutput();
}

OutputBaseType &ImageSegmentationAdapter::getOutputCache()
{
	return _selfie_segmentation->getOutputCache();
}

void ImageSegmentationAdapter::performAsync(InputBaseType &input)
{
	_selfie_segmentation->performAsync(static_cast<ImageSegmentationInput &>(input));
}

}
}