summaryrefslogtreecommitdiff
path: root/mv_machine_learning/object_detection/src/object_detection_external.cpp
blob: 08ef64707cf6b9d40b24020bb28381f810e3a4d9 (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
/**
 * 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 <dlfcn.h>
#include <sys/stat.h>

#include "mv_private.h"

#include "machine_learning_exception.h"
#include "object_detection_external.h"

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

namespace mediavision
{
namespace machine_learning
{
typedef IObjectDetection *create_t(ObjectDetectionTaskType task_type);
typedef void destroy_t(IObjectDetection *);

ObjectDetectionExternal::ObjectDetectionExternal(ObjectDetectionTaskType task_type)
		: _plugin_handle(), _object_detection_plugin()
{
	// TODO. plugin module name will be given by configuration file later.
	const char *module_name = "libobject-detection-plugin.so";

	// Load external object detection library.
	LOGI("lib: %s", module_name);
	_plugin_handle = dlopen(module_name, RTLD_NOW);

	if (!_plugin_handle)
		throw InvalidOperation("Fail to open plugin library.");

	create_t *createPlugin = (create_t *) dlsym(_plugin_handle, "createPlugin");
	if (createPlugin == NULL) {
		dlclose(_plugin_handle);
		createPlugin = NULL;
		throw InvalidOperation("Fail to get symbol from plugin library.");
	}

	_object_detection_plugin = createPlugin(task_type);
	if (_object_detection_plugin == NULL) {
		dlclose(_plugin_handle);
		_plugin_handle = NULL;
		throw InvalidOperation("Fail to create plugin module.");
	}
}

ObjectDetectionExternal::~ObjectDetectionExternal()
{
	if (_plugin_handle) {
		destroy_t *destroyPlugin = (destroy_t *) dlsym(_plugin_handle, "destroyPlugin");

		destroyPlugin(_object_detection_plugin);
		dlclose(_plugin_handle);
		_object_detection_plugin = nullptr;
		_plugin_handle = nullptr;
	}
}

void ObjectDetectionExternal::preDestroy()
{
	_object_detection_plugin->preDestroy();
}

ObjectDetectionTaskType ObjectDetectionExternal::getTaskType()
{
	return _object_detection_plugin->getTaskType();
}

void ObjectDetectionExternal::setUserModel(std::string model_file, std::string meta_file, std::string label_file)
{
	_object_detection_plugin->setUserModel(model_file, meta_file, label_file);
}

void ObjectDetectionExternal::setEngineInfo(std::string engine_type, std::string device_type)
{
	_object_detection_plugin->setEngineInfo(engine_type, device_type);
}

void ObjectDetectionExternal::getNumberOfEngines(unsigned int *number_of_engines)
{
	_object_detection_plugin->getNumberOfEngines(number_of_engines);
}

void ObjectDetectionExternal::getEngineType(unsigned int engine_index, char **engine_type)
{
	_object_detection_plugin->getEngineType(engine_index, engine_type);
}

void ObjectDetectionExternal::getNumberOfDevices(const char *engine_type, unsigned int *number_of_devices)
{
	_object_detection_plugin->getNumberOfDevices(engine_type, number_of_devices);
}

void ObjectDetectionExternal::getDeviceType(const char *engine_type, const unsigned int device_index,
											char **device_type)
{
	_object_detection_plugin->getDeviceType(engine_type, device_index, device_type);
}

void ObjectDetectionExternal::configure(std::string configFile)
{
	_object_detection_plugin->configure(configFile);
}

void ObjectDetectionExternal::prepare()
{
	_object_detection_plugin->prepare();
}

void ObjectDetectionExternal::perform(mv_source_h &mv_src)
{
	_object_detection_plugin->perform(mv_src);
}

void ObjectDetectionExternal::performAsync(ObjectDetectionInput &input)
{
	_object_detection_plugin->performAsync(input);
}

ObjectDetectionResult &ObjectDetectionExternal::getOutput()
{
	_current_result = _object_detection_plugin->getOutput();

	return _current_result;
}

ObjectDetectionResult &ObjectDetectionExternal::getOutputCache()
{
	return _current_result;
}

}
}