From 9c1f864d713e11f85aa7f43451016a3f1b683af1 Mon Sep 17 00:00:00 2001 From: Inki Dae Date: Tue, 20 Sep 2022 18:01:39 +0900 Subject: [PATCH] mv_machine_learning: do not use fixed array [Issue type] : code cleanup Did code cleanup to the use of result structure, object_detection_3d_result_s by doing, - Get the maximum number of poinsts and edges from specific json file. - Clean up json key string. Change-Id: I23200c80c8d019c7aa02a608d3b92556af1e1605 Signed-off-by: Inki Dae --- .../include/mv_object_detection_3d_config.h | 14 +++-- .../object_detection/include/object_detection.h | 8 +-- .../include/object_detection_type.h | 27 ++++++-- .../object_detection/include/objectron.h | 4 +- .../object_detection/meta/object_detection_3d.json | 22 +++++-- .../src/mv_object_detection_3d_open.cpp | 4 +- .../object_detection/src/object_detection.cpp | 6 +- .../object_detection/src/objectron.cpp | 71 ++++++++++++++++------ 8 files changed, 113 insertions(+), 43 deletions(-) diff --git a/mv_machine_learning/object_detection/include/mv_object_detection_3d_config.h b/mv_machine_learning/object_detection/include/mv_object_detection_3d_config.h index 5722016..91e340c 100644 --- a/mv_machine_learning/object_detection/include/mv_object_detection_3d_config.h +++ b/mv_machine_learning/object_detection/include/mv_object_detection_3d_config.h @@ -23,7 +23,7 @@ * * @since_tizen 7.0 */ -#define MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH "MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH" +#define MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH "MODEL_FILE_PATH" /** * @brief Defines #MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH to set inference @@ -34,7 +34,7 @@ * * @since_tizen 7.0 */ -#define MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH "MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH" +#define MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH "META_FILE_PATH" /** * @brief Defines #MV_OBJECT_DETECT_3D_BACKEND_TYPE @@ -42,7 +42,7 @@ * * @since_tizen 7.0 */ -#define MV_OBJECT_DETECTION_3D_BACKEND_TYPE "MV_OBJECT_DETECTION_3D_BACKEND_TYPE" +#define MV_OBJECT_DETECTION_3D_BACKEND_TYPE "BACKEND_TYPE" /** * @brief Defines #MV_OBJECT_DETECT_3D_TARGET_DEVICE_TYPE @@ -50,8 +50,12 @@ * * @since_tizen 7.0 */ -#define MV_OBJECT_DETECTION_3D_TARGET_DEVICE_TYPE "MV_OBJECT_DETECTION_3D_TARGET_DEVICE_TYPE" +#define MV_OBJECT_DETECTION_3D_TARGET_DEVICE_TYPE "TARGET_DEVICE_TYPE" -#define OBJECT_DETECTION_3D_META_FILE_NAME "object_detection_3d.json" +#define MV_OBJECT_DETECTION_3D_MAX_NUM_OF_POINTS "MAX_NUM_OF_POINTS" + +#define MV_OBJECT_DETECTION_3D_MAX_NUM_OF_EDGES "MAX_NUM_OF_EDGES" + +#define MV_OBJECT_DETECTION_3D_META_FILE_NAME "object_detection_3d.json" #endif /* __MEDIA_VISION_INFERENCE_OPEN_H__ */ diff --git a/mv_machine_learning/object_detection/include/object_detection.h b/mv_machine_learning/object_detection/include/object_detection.h index b1e2a8d..bbdbe7c 100644 --- a/mv_machine_learning/object_detection/include/object_detection.h +++ b/mv_machine_learning/object_detection/include/object_detection.h @@ -35,10 +35,10 @@ class ObjectDetection protected: std::unique_ptr _inference; std::unique_ptr _config; - std::string _model_file_path; - std::string _model_meta_file_path; - int _backend_type; - int _target_device_type; + std::string _modelFilePath; + std::string _modelMetaFilePath; + int _backendType; + int _targetDeviceType; public: ObjectDetection(); diff --git a/mv_machine_learning/object_detection/include/object_detection_type.h b/mv_machine_learning/object_detection/include/object_detection_type.h index 1131c9e..6694387 100644 --- a/mv_machine_learning/object_detection/include/object_detection_type.h +++ b/mv_machine_learning/object_detection/include/object_detection_type.h @@ -29,18 +29,35 @@ typedef struct { mv_source_h inference_src; } object_detection_input_s; +typedef struct { + unsigned int start; + unsigned int end; +} edge_index_s; + /** * @brief The object detection result structure. * @details Contains object detection 3d result. */ -typedef struct { +struct object_detection_3d_result_s { unsigned int probability; unsigned int number_of_points; - unsigned int x[9]; - unsigned int y[9]; + unsigned int *x_points; + unsigned int *y_points; unsigned int number_of_edges; - unsigned int edge_indexes[12][2]; -} object_detection_3d_result_s; + edge_index_s *edge_indexes; + + ~object_detection_3d_result_s() + { + if (x_points) + delete []x_points; + + if (y_points) + delete []y_points; + + if (edge_indexes) + delete []edge_indexes; + } +}; typedef enum { OBJECT_DETECTION_TASK_NONE = 0, diff --git a/mv_machine_learning/object_detection/include/objectron.h b/mv_machine_learning/object_detection/include/objectron.h index 4087fd6..3761e0d 100644 --- a/mv_machine_learning/object_detection/include/objectron.h +++ b/mv_machine_learning/object_detection/include/objectron.h @@ -30,7 +30,9 @@ namespace machine_learning class Objectron : public ObjectDetection { private: - object_detection_3d_result_s _result; + std::unique_ptr _result; + int _maxPoints; + int _maxEdges; public: Objectron(); diff --git a/mv_machine_learning/object_detection/meta/object_detection_3d.json b/mv_machine_learning/object_detection/meta/object_detection_3d.json index 25636fe..b261f38 100644 --- a/mv_machine_learning/object_detection/meta/object_detection_3d.json +++ b/mv_machine_learning/object_detection/meta/object_detection_3d.json @@ -2,27 +2,37 @@ "attributes": [ { - "name" : "MV_OBJECT_DETECTION_3D_MODEL_OBJECT_NAME", + "name" : "OBJECT_NAME", "type" : "string", "value" : "cup" }, - { - "name" : "MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH", + { + "name" : "MAX_NUM_OF_POINTS", + "type" : "integer", + "value" : 9 + }, + { + "name" : "MAX_NUM_OF_EDGES", + "type" : "integer", + "value" : 12 + }, + { + "name" : "MODEL_FILE_PATH", "type" : "string", "value" : "/home/owner/media/res/object_detection_3d/object_detection_3d_cup.tflite" }, { - "name" : "MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH", + "name" : "META_FILE_PATH", "type" : "string", "value" : "/home/owner/media/res/object_detection_3d/object_detection_3d_cup.json" }, { - "name" : "MV_OBJECT_DETECTION_3D_BACKEND_TYPE", + "name" : "BACKEND_TYPE", "type" : "integer", "value" : 1 }, { - "name" : "MV_OBJECT_DETECTION_3D_TARGET_DEVICE_TYPE", + "name" : "TARGET_DEVICE_TYPE", "type" : "integer", "value" : 1 } diff --git a/mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp b/mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp index 16bfe16..38ae681 100644 --- a/mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp +++ b/mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp @@ -231,8 +231,8 @@ int mv_object_detection_3d_get_points_open(mv_object_detection_3d_h handle, unsi auto result = task->getOutput(); - *out_x = result->x; - *out_y = result->y; + *out_x = result->x_points; + *out_y = result->y_points; } catch (const BaseException &e) { LOGE("%s", e.what()); return e.getError(); diff --git a/mv_machine_learning/object_detection/src/object_detection.cpp b/mv_machine_learning/object_detection/src/object_detection.cpp index 641dd55..a4e0626 100644 --- a/mv_machine_learning/object_detection/src/object_detection.cpp +++ b/mv_machine_learning/object_detection/src/object_detection.cpp @@ -30,13 +30,13 @@ namespace mediavision namespace machine_learning { -ObjectDetection::ObjectDetection() : _backend_type(), _target_device_type() +ObjectDetection::ObjectDetection() : _backendType(), _targetDeviceType() { } void ObjectDetection::configure() { - int ret = _inference->Bind(_backend_type, _target_device_type); + int ret = _inference->Bind(_backendType, _targetDeviceType); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to bind a backend engine."); } @@ -51,7 +51,7 @@ void ObjectDetection::prepare() if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to configure output tensor info from meta file."); - _inference->ConfigureModelFiles("", _model_file_path, ""); + _inference->ConfigureModelFiles("", _modelFilePath, ""); // Request to load model files to a backend engine. ret = _inference->Load(); diff --git a/mv_machine_learning/object_detection/src/objectron.cpp b/mv_machine_learning/object_detection/src/objectron.cpp index df229f0..fa67444 100644 --- a/mv_machine_learning/object_detection/src/objectron.cpp +++ b/mv_machine_learning/object_detection/src/objectron.cpp @@ -32,9 +32,10 @@ namespace mediavision namespace machine_learning { -Objectron::Objectron() : _result() +Objectron::Objectron() { _inference = make_unique(); + _result = make_unique(); } Objectron::~Objectron() @@ -49,35 +50,68 @@ static bool IsJsonFile(const string &fileName) void Objectron::parseMetaFile() { _config = make_unique(string(MV_CONFIG_PATH) + - string(OBJECT_DETECTION_3D_META_FILE_NAME)); + string(MV_OBJECT_DETECTION_3D_META_FILE_NAME)); int ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_3D_BACKEND_TYPE), - &_backend_type); + &_backendType); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to get backend engine type."); ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_3D_TARGET_DEVICE_TYPE), - &_target_device_type); + &_targetDeviceType); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to get target device type."); - ret = _config->getStringAttribute(MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH, &_model_file_path); + ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_3D_MAX_NUM_OF_POINTS), + &_maxPoints); + if (ret != MEDIA_VISION_ERROR_NONE) + throw InvalidOperation("Fail to get maximum number of points."); + + ret = _config->getIntegerAttribute(string(MV_OBJECT_DETECTION_3D_MAX_NUM_OF_EDGES), + &_maxEdges); + if (ret != MEDIA_VISION_ERROR_NONE) + throw InvalidOperation("Fail to get maximum number of edges."); + + ret = _config->getStringAttribute(MV_OBJECT_DETECTION_3D_MODEL_FILE_PATH, &_modelFilePath); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to get model file path"); - ret = _config->getStringAttribute(MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH, &_model_meta_file_path); + ret = _config->getStringAttribute(MV_OBJECT_DETECTION_3D_MODEL_META_FILE_PATH, &_modelMetaFilePath); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to get model meta file path"); - if (_model_meta_file_path.empty()) + if (_modelMetaFilePath.empty()) throw InvalidOperation("Model meta file doesn't exist."); - if (!IsJsonFile(_model_meta_file_path)) + if (!IsJsonFile(_modelMetaFilePath)) throw InvalidOperation("Model meta file should be json"); - ret = _inference->ParseMetadata(_model_meta_file_path); + ret = _inference->ParseMetadata(_modelMetaFilePath); if (ret != MEDIA_VISION_ERROR_NONE) throw InvalidOperation("Fail to ParseMetadata"); + + if (!_result->x_points) { + _result->x_points = new (nothrow) unsigned int[_maxPoints]; + if (!_result->x_points) + throw InvalidOperation("Fail to allocate x edges"); + } + + if (!_result->y_points) { + _result->y_points = new (nothrow) unsigned int[_maxPoints]; + if (!_result->y_points) { + delete []_result->x_points; + throw InvalidOperation("Fail to allocate y edges"); + } + } + + if (!_result->edge_indexes) { + _result->edge_indexes = new (nothrow) edge_index_s[_maxEdges]; + if (!_result->edge_indexes) { + delete []_result->y_points; + delete []_result->x_points; + throw InvalidOperation("Fail to allocate edge indexes"); + } + } } object_detection_3d_result_s* Objectron::getResult() @@ -108,11 +142,11 @@ object_detection_3d_result_s* Objectron::getResult() float y_scale = static_cast(_inference->getSourceHeight()) / static_cast(_inference->getInputHeight()); for (unsigned int idx = 0; idx < output_size; idx += 2) { - _result.x[result_idx] = static_cast(keypoints[idx] * x_scale); - _result.y[result_idx++] = static_cast(keypoints[idx + 1] * y_scale); + _result->x_points[result_idx] = static_cast(keypoints[idx] * x_scale); + _result->y_points[result_idx++] = static_cast(keypoints[idx + 1] * y_scale); } - _result.number_of_points = output_size / 2; + _result->number_of_points = output_size / 2; string& identity_layer = output_layer_names[0]; @@ -121,19 +155,22 @@ object_detection_3d_result_s* Objectron::getResult() throw InvalidOperation("Fail to get tensor buffer."); auto *prob = reinterpret_cast(tensor_buffer->buffer); - _result.probability = static_cast(prob[0] * 100); + _result->probability = static_cast(prob[0] * 100); - _result.number_of_edges = 12; + _result->number_of_edges = _maxEdges; - unsigned int edges[12][2] = { + unsigned int edges[_maxEdges][2] = { {2, 3}, {4, 5}, {6, 7}, {8, 9}, {2, 4}, {3, 5}, {6, 8}, {7, 9}, {2, 6}, {3, 7}, {4, 8}, {5, 9} }; - ::copy(&edges[0][0], &edges[0][0] + 12 * 2, &_result.edge_indexes[0][0]); + for (auto idx = 0; idx < _maxEdges; ++idx) { + _result->edge_indexes[idx].start = edges[idx][0]; + _result->edge_indexes[idx].end = edges[idx][1]; + } - return &_result; + return _result.get(); } } -- 2.7.4