mv_machine_learning: do not use fixed array
authorInki Dae <inki.dae@samsung.com>
Tue, 20 Sep 2022 09:01:39 +0000 (18:01 +0900)
committerInki Dae <inki.dae@samsung.com>
Mon, 7 Nov 2022 03:12:39 +0000 (12:12 +0900)
[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 <inki.dae@samsung.com>
mv_machine_learning/object_detection/include/mv_object_detection_3d_config.h
mv_machine_learning/object_detection/include/object_detection.h
mv_machine_learning/object_detection/include/object_detection_type.h
mv_machine_learning/object_detection/include/objectron.h
mv_machine_learning/object_detection/meta/object_detection_3d.json
mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp
mv_machine_learning/object_detection/src/object_detection.cpp
mv_machine_learning/object_detection/src/objectron.cpp

index 572201696acb0f66f7a61ff024c082de5eb97d6a..91e340c6086a0a3a68d3b98ea248c6baed7de817 100644 (file)
@@ -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
  *
  * @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__ */
index b1e2a8dc1d68819bc65ae0d5b98c34de352f7c9f..bbdbe7c76a3a4da7d462d1d08619c04fe224a4af 100644 (file)
@@ -35,10 +35,10 @@ class ObjectDetection
 protected:
        std::unique_ptr<mediavision::inference::Inference> _inference;
        std::unique_ptr<MediaVision::Common::EngineConfig> _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();
index 1131c9e3ac14327ba72a7f183526cf69023fd6ba..669438740aa99067e8ddc61ebb20055c9599c130 100644 (file)
@@ -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,
index 4087fd6ea50eb4ffc220942790e8e704d61bd26b..3761e0d0c7a3791436e52a076871d329b38c0506 100644 (file)
@@ -30,7 +30,9 @@ namespace machine_learning
 class Objectron : public ObjectDetection
 {
 private:
-       object_detection_3d_result_s _result;
+       std::unique_ptr<object_detection_3d_result_s> _result;
+       int _maxPoints;
+       int _maxEdges;
 
 public:
        Objectron();
index 25636fe5a69c1cf5bb59b5a36e7c7a339b9e4468..b261f385dc14e607dadd08636d5c3fc94544e5ba 100644 (file)
@@ -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
         }
index 16bfe164f82d57ea4e61b419cbe61538e9c4ea75..38ae68174b8a7df01712424e46355e0feeab201d 100644 (file)
@@ -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();
index 641dd551bd3fbc77b9d2c5045017d9cbab2d8054..a4e0626b3e8045446eac8f843d9d247e671a4578 100644 (file)
@@ -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();
index df229f0b375ef7f8bcc03c5c16e9ca16272c5905..fa67444c56ff224fc1ce50e6c3221754f6f6cc31 100644 (file)
@@ -32,9 +32,10 @@ namespace mediavision
 namespace machine_learning
 {
 
-Objectron::Objectron() : _result()
+Objectron::Objectron()
 {
        _inference = make_unique<Inference>();
+       _result = make_unique<object_detection_3d_result_s>();
 }
 
 Objectron::~Objectron()
@@ -49,35 +50,68 @@ static bool IsJsonFile(const string &fileName)
 void Objectron::parseMetaFile()
 {
        _config = make_unique<EngineConfig>(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<float>(_inference->getSourceHeight()) / static_cast<float>(_inference->getInputHeight());
 
        for (unsigned int idx = 0; idx < output_size; idx += 2) {
-               _result.x[result_idx] = static_cast<int>(keypoints[idx] * x_scale);
-               _result.y[result_idx++] = static_cast<int>(keypoints[idx + 1] * y_scale);
+               _result->x_points[result_idx] = static_cast<int>(keypoints[idx] * x_scale);
+               _result->y_points[result_idx++] = static_cast<int>(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<float *>(tensor_buffer->buffer);
-       _result.probability = static_cast<unsigned int>(prob[0] * 100);
+       _result->probability = static_cast<unsigned int>(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();
 }
 
 }