Split BoxInfo implementation and add missing header
authorKwanghoon Son <k.son@samsung.com>
Mon, 29 Aug 2022 02:43:48 +0000 (22:43 -0400)
committerInki Dae <inki.dae@samsung.com>
Fri, 2 Sep 2022 08:09:25 +0000 (17:09 +0900)
Change-Id: Idcf2a0d06986cb8b7b23ec94af582a6fa4f37991
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_machine_learning/inference/include/BoxInfo.h
mv_machine_learning/inference/include/DecodeInfo.h
mv_machine_learning/inference/include/DimInfo.h
mv_machine_learning/inference/include/Utils.h
mv_machine_learning/inference/src/BoxInfo.cpp [new file with mode: 0644]

index d384ecd..783342a 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2021 - 2022 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.
@@ -36,253 +36,117 @@ namespace inference
 {
 namespace box
 {
-       struct Label
-       {
-               std::string name;
-               DimInfo dimInfo;
-       };
-
-       struct Number
-       {
-               std::string name;
-               DimInfo dimInfo;
-       };
-
-       class BoxInfo
-       {
-       private:
-               std::string name;
-               DimInfo dimInfo;
-               inference_box_type_e type; // 0:L-T-R-B, 1: Cx-Cy-W-H
-               std::vector<int> order; // Order based on box type
-               inference_box_coordinate_type_e coordinate; // 0: ratio, 1: pixel
-               inference_box_decoding_type_e decodingType; // 0: bypass , 1:ssd with anchor
-               DecodeInfo decodingInfo;
-               Label label;
-               Number number;
-
-               std::map<std::string, inference_box_type_e> supportedBoxTypes;
-               std::map<std::string, inference_box_coordinate_type_e> supportedBoxCoordinateTypes;
-               std::map<std::string, inference_box_decoding_type_e> supportedBoxDecodingTypes;
-
-       public:
-               BoxInfo() :
-                               name(),
-                               dimInfo(),
-                               type(INFERENCE_BOX_TYPE_ORIGIN_LEFTTOP),
-                               order(),
-                               coordinate(INFERENCE_BOX_COORDINATE_TYPE_RATIO),
-                               decodingType(INFERENCE_BOX_DECODING_TYPE_BYPASS),
-                               decodingInfo()
-
-               {
-                       supportedBoxTypes.insert({"ORIGIN_LEFTTOP", INFERENCE_BOX_TYPE_ORIGIN_LEFTTOP});
-                       supportedBoxTypes.insert({"ORIGIN_CENTER", INFERENCE_BOX_TYPE_ORIGIN_CENTER});
-
-                       supportedBoxCoordinateTypes.insert({"RATIO", INFERENCE_BOX_COORDINATE_TYPE_RATIO});
-                       supportedBoxCoordinateTypes.insert({"PIXEL", INFERENCE_BOX_COORDINATE_TYPE_PIXEL});
-
-                       supportedBoxDecodingTypes.insert({"BYPASS", INFERENCE_BOX_DECODING_TYPE_BYPASS});
-                       supportedBoxDecodingTypes.insert({"SSD_ANCHOR", INFERENCE_BOX_DECODING_TYPE_SSD_ANCHOR});
-                       supportedBoxDecodingTypes.insert({"YOLO_ANCHOR", INFERENCE_BOX_DECODING_TYPE_YOLO_ANCHOR});
-               }
-
-               ~BoxInfo() = default;
-
-               std::string GetName() { return name; }
-               DimInfo GetDimInfo() { return dimInfo; }
-               inference_box_type_e GetType() { return type; }
-               inference_box_decoding_type_e GetDecodingType() { return decodingType; }
-               std::vector<int> GetOrder() { return order; }
-               int GetCoordinate() { return coordinate; }
-               DecodeInfo& GetDecodeInfo() {return decodingInfo; }
-               std::string GetLabelName() { return label.name; }
-               std::string GetNumberName() { return number.name; }
-               DimInfo GetNumberDimInfo() { return number.dimInfo; }
-
-               int ParseBox(JsonObject *root)
-               {
-                       LOGI("ENTER");
-
-                       JsonArray * rootArray = json_object_get_array_member(root, "box");
-                       unsigned int elements = json_array_get_length(rootArray);
-
-                       for (unsigned int elem_idx = 0; elem_idx < elements; ++elem_idx) {
-                               JsonNode *pNode = json_array_get_element(rootArray, elem_idx);
-                               JsonObject *pObject = json_node_get_object(pNode);
-
-                               name = json_object_get_string_member(pObject,"name");
-                               LOGI("layer: %s", name.c_str());
-
-                               JsonArray * array = json_object_get_array_member(pObject, "index");
-                               unsigned int elements2 = json_array_get_length(array);
-
-                               LOGI("range dim: size[%u]", elements2);
-
-                               for (unsigned int elem2_idx = 0; elem2_idx < elements2; ++elem2_idx)
-                                       if (static_cast<int>(json_array_get_int_element(array, elem2_idx)) == 1)
-                                               dimInfo.SetValidIndex(elem2_idx);
-
-                               try {
-                                       type = GetSupportedType(pObject, "box_type", supportedBoxTypes);
-                                       coordinate = GetSupportedType(pObject, "box_coordinate", supportedBoxCoordinateTypes);
-                                       decodingType = GetSupportedType(pObject, "decoding_type", supportedBoxDecodingTypes);
-                               } catch (const std::exception& e) {
-                                       LOGE("Invalid %s", e.what());
-                                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
-                               }
-
-                               array = json_object_get_array_member(pObject, "box_order");
-                               elements2 = json_array_get_length(array);
-                               LOGI("box order should have 4 elements and it has [%u]", elements2);
-
-                               for (unsigned int elem2_idx = 0; elem2_idx < elements2; ++elem2_idx) {
-                                       auto val = static_cast<int>(json_array_get_int_element(array, elem2_idx));
-                                       order.push_back(val);
-                                       LOGI("%d", val);
-                               }
-                       }
-
-                       LOGI("LEAVE");
-                       return MEDIA_VISION_ERROR_NONE;
-               }
-
-               int ParseLabel(JsonObject *root)
-               {
-                       LOGI("ENTER");
-
-                       if (!json_object_has_member(root, "label")) {
-                               LOGE("No box outputmetadata");
-                               LOGI("LEAVE");
-                               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-                       }
-
-                       JsonArray * rootArray = json_object_get_array_member(root, "label");
-                       unsigned int elements = json_array_get_length(rootArray);
-
-                       // TODO: handling error
-                       for (unsigned int elem = 0; elem < elements; ++elem) {
-                               JsonNode *pNode = json_array_get_element(rootArray, elem);
-                               JsonObject *pObject = json_node_get_object(pNode);
-
-                               label.name = json_object_get_string_member(pObject,"name");
-                               LOGI("layer: %s", label.name.c_str());
-
-                               JsonArray * array = json_object_get_array_member(pObject, "index");
-                               unsigned int elements2 = json_array_get_length(array);
-                               LOGI("range dim: size[%u]", elements2);
-                               for (unsigned int elem2 = 0; elem2 < elements2; ++elem2) {
-                                       if (static_cast<int>(json_array_get_int_element(array, elem2)) == 1)
-                                               label.dimInfo.SetValidIndex(elem2);
-                               }
-                       }
-
-                       LOGI("LEAVE");
-                       return MEDIA_VISION_ERROR_NONE;
-               }
-
-               int ParseNumber(JsonObject *root)
-               {
-                       LOGI("ENTER");
-
-                       if (!json_object_has_member(root, "number")) {
-                               LOGE("No number outputmetadata");
-                               LOGI("LEAVE");
-                               return MEDIA_VISION_ERROR_INVALID_OPERATION;
-                       }
-
-                       // box
-                       JsonArray * rootArray = json_object_get_array_member(root, "number");
-                       unsigned int elements = json_array_get_length(rootArray);
-
-                       // TODO: handling error
-                       for (unsigned int elem = 0; elem < elements; ++elem) {
-                               JsonNode *pNode = json_array_get_element(rootArray, elem);
-                               JsonObject *pObject = json_node_get_object(pNode);
-
-                               number.name = json_object_get_string_member(pObject,"name");
-
-                               LOGI("layer: %s", number.name.c_str());
-
-                               JsonArray * array = json_object_get_array_member(pObject, "index");
-                               unsigned int elements2 = json_array_get_length(array);
-
-                               LOGI("range dim: size[%u]", elements2);
-
-                               for (unsigned int elem2 = 0; elem2 < elements2; ++elem2)
-                                       if (static_cast<int>(json_array_get_int_element(array, elem2)) == 1)
-                                               number.dimInfo.SetValidIndex(elem2);
-                       }
-
-                       LOGI("LEAVE");
-                       return MEDIA_VISION_ERROR_NONE;
-               }
-
-               int ParseDecodeInfo(JsonObject *root)
-               {
-                       LOGI("ENTER");
-
-                       // box
-                       JsonArray * rootArray = json_object_get_array_member(root, "box");
-                       unsigned int elements = json_array_get_length(rootArray);
-
-                       // TODO: handling error
-                       for (unsigned int elem = 0; elem < elements; ++elem) {
-                               JsonNode *pNode = json_array_get_element(rootArray, elem);
-                               JsonObject *pObject = json_node_get_object(pNode);
-
-                               if (!json_object_has_member(pObject, "decoding_info")) {
-                                       LOGE("decoding_info is mandatory. Invalid metadata");
-                                       LOGI("LEAVE");
-
-                                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
-                               }
+struct Label
+{
+       std::string name;
+       DimInfo dimInfo;
+};
 
-                               int ret = MEDIA_VISION_ERROR_NONE;
-                               JsonObject *cObject = json_object_get_object_member(pObject, "decoding_info");
-                               if (json_object_has_member(cObject, "anchor")) {
-                                       ret = GetDecodeInfo().ParseAnchorParam(cObject);
-                                       if (ret != MEDIA_VISION_ERROR_NONE) {
-                                               LOGE("Fail to ParseAnchorParam[%d]", ret);
-                                               return ret;
-                                       }
-                               } else if (json_object_has_member(cObject, "cell")) {
-                                       ret = GetDecodeInfo().ParseCellParam(cObject);
-                                       if (ret != MEDIA_VISION_ERROR_NONE) {
-                                               LOGE("Fail to ParseCellParam[%d]", ret);
-                                               return ret;
-                                       }
-                               } else {
+struct Number
+{
+       std::string name;
+       DimInfo dimInfo;
+};
 
-                                       LOGE("anchor is mandatory. Invalid metadata");
-                                       LOGI("LEAVE");
+class BoxInfo
+{
+private:
+       std::string name;
+       DimInfo dimInfo;
+       inference_box_type_e type; // 0:L-T-R-B, 1: Cx-Cy-W-H
+       std::vector<int> order; // Order based on box type
+       inference_box_coordinate_type_e coordinate; // 0: ratio, 1: pixel
+       inference_box_decoding_type_e decodingType; // 0: bypass , 1:ssd with anchor
+       DecodeInfo decodingInfo;
+       Label label;
+       Number number;
+
+       std::map<std::string, inference_box_type_e> supportedBoxTypes;
+       std::map<std::string, inference_box_coordinate_type_e>
+                       supportedBoxCoordinateTypes;
+       std::map<std::string, inference_box_decoding_type_e>
+                       supportedBoxDecodingTypes;
+
+public:
+       BoxInfo()
+                       : name()
+                       , dimInfo()
+                       , type(INFERENCE_BOX_TYPE_ORIGIN_LEFTTOP)
+                       , order()
+                       , coordinate(INFERENCE_BOX_COORDINATE_TYPE_RATIO)
+                       , decodingType(INFERENCE_BOX_DECODING_TYPE_BYPASS)
+                       , decodingInfo()
 
-                                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
-                               }
+       {
+               supportedBoxTypes.insert(
+                               { "ORIGIN_LEFTTOP", INFERENCE_BOX_TYPE_ORIGIN_LEFTTOP });
+               supportedBoxTypes.insert(
+                               { "ORIGIN_CENTER", INFERENCE_BOX_TYPE_ORIGIN_CENTER });
+
+               supportedBoxCoordinateTypes.insert(
+                               { "RATIO", INFERENCE_BOX_COORDINATE_TYPE_RATIO });
+               supportedBoxCoordinateTypes.insert(
+                               { "PIXEL", INFERENCE_BOX_COORDINATE_TYPE_PIXEL });
+
+               supportedBoxDecodingTypes.insert(
+                               { "BYPASS", INFERENCE_BOX_DECODING_TYPE_BYPASS });
+               supportedBoxDecodingTypes.insert(
+                               { "SSD_ANCHOR", INFERENCE_BOX_DECODING_TYPE_SSD_ANCHOR });
+               supportedBoxDecodingTypes.insert(
+                               { "YOLO_ANCHOR", INFERENCE_BOX_DECODING_TYPE_YOLO_ANCHOR });
+       }
+
+       ~BoxInfo() = default;
+
+       std::string GetName()
+       {
+               return name;
+       }
+       DimInfo GetDimInfo()
+       {
+               return dimInfo;
+       }
+       inference_box_type_e GetType()
+       {
+               return type;
+       }
+       inference_box_decoding_type_e GetDecodingType()
+       {
+               return decodingType;
+       }
+       std::vector<int> GetOrder()
+       {
+               return order;
+       }
+       int GetCoordinate()
+       {
+               return coordinate;
+       }
+       DecodeInfo &GetDecodeInfo()
+       {
+               return decodingInfo;
+       }
+       std::string GetLabelName()
+       {
+               return label.name;
+       }
+       std::string GetNumberName()
+       {
+               return number.name;
+       }
+       DimInfo GetNumberDimInfo()
+       {
+               return number.dimInfo;
+       }
 
-                               ret = GetDecodeInfo().ParseNms(cObject);
-                               if (ret != MEDIA_VISION_ERROR_NONE) {
-                                       LOGE("Fail to ParseNms[%d]", ret);
-                                       return ret;
-                               }
+       int ParseBox(JsonObject *root);
 
-                               ret = GetDecodeInfo().ParseRotate(cObject);
-                               if (ret != MEDIA_VISION_ERROR_NONE) {
-                                       LOGE("Fail to ParseRotate[%d]", ret);
-                                       return ret;
-                               }
+       int ParseLabel(JsonObject *root);
 
-                               ret = GetDecodeInfo().ParseRoiOption(cObject);
-                               if (ret != MEDIA_VISION_ERROR_NONE) {
-                                       LOGE("Fail to ParseRoiOption[%d]", ret);
-                                       return ret;
-                               }
-                       }
+       int ParseNumber(JsonObject *root);
 
-                       LOGI("LEAVE");
-                       return MEDIA_VISION_ERROR_NONE;
-               }
-       };
+       int ParseDecodeInfo(JsonObject *root);
+};
 } /* box */
 } /* Inference */
 } /* MediaVision */
index d28444a..dba3647 100644 (file)
@@ -22,6 +22,7 @@
 #include <map>
 #include <memory>
 
+#include <OutputMetadataTypes.h>
 #include <mv_inference_type.h>
 #include <opencv2/core.hpp>
 #include "Utils.h"
index d061122..64e0f05 100644 (file)
@@ -18,6 +18,7 @@
 #define __DIM_INFO_H__
 
 #include <vector>
+#include <dlog.h>
 
 namespace mediavision
 {
index c8a37cd..d5db2b5 100644 (file)
@@ -23,6 +23,7 @@
 #include <memory>
 
 #include <json-glib/json-glib.h>
+#include <dlog.h>
 
 namespace mediavision
 {
diff --git a/mv_machine_learning/inference/src/BoxInfo.cpp b/mv_machine_learning/inference/src/BoxInfo.cpp
new file mode 100644 (file)
index 0000000..1585691
--- /dev/null
@@ -0,0 +1,207 @@
+
+/**
+ * Copyright (c) 2022 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 <BoxInfo.h>
+
+using namespace mediavision::inference::box;
+
+int BoxInfo::ParseBox(JsonObject *root)
+{
+       LOGI("ENTER");
+
+       JsonArray *rootArray = json_object_get_array_member(root, "box");
+       unsigned int elements = json_array_get_length(rootArray);
+
+       for (unsigned int elem_idx = 0; elem_idx < elements; ++elem_idx) {
+               JsonNode *pNode = json_array_get_element(rootArray, elem_idx);
+               JsonObject *pObject = json_node_get_object(pNode);
+
+               name = json_object_get_string_member(pObject, "name");
+               LOGI("layer: %s", name.c_str());
+
+               JsonArray *array = json_object_get_array_member(pObject, "index");
+               unsigned int elements2 = json_array_get_length(array);
+
+               LOGI("range dim: size[%u]", elements2);
+
+               for (unsigned int elem2_idx = 0; elem2_idx < elements2; ++elem2_idx)
+                       if (static_cast<int>(
+                                               json_array_get_int_element(array, elem2_idx)) == 1)
+                               dimInfo.SetValidIndex(elem2_idx);
+
+               try {
+                       type = GetSupportedType(pObject, "box_type", supportedBoxTypes);
+                       coordinate = GetSupportedType(pObject, "box_coordinate",
+                                                                                 supportedBoxCoordinateTypes);
+                       decodingType = GetSupportedType(pObject, "decoding_type",
+                                                                                       supportedBoxDecodingTypes);
+               } catch (const std::exception &e) {
+                       LOGE("Invalid %s", e.what());
+                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               }
+
+               array = json_object_get_array_member(pObject, "box_order");
+               elements2 = json_array_get_length(array);
+               LOGI("box order should have 4 elements and it has [%u]", elements2);
+
+               for (unsigned int elem2_idx = 0; elem2_idx < elements2; ++elem2_idx) {
+                       auto val = static_cast<int>(
+                                       json_array_get_int_element(array, elem2_idx));
+                       order.push_back(val);
+                       LOGI("%d", val);
+               }
+       }
+
+       LOGI("LEAVE");
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int BoxInfo::ParseLabel(JsonObject *root)
+{
+       LOGI("ENTER");
+
+       if (!json_object_has_member(root, "label")) {
+               LOGE("No box outputmetadata");
+               LOGI("LEAVE");
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+       }
+
+       JsonArray *rootArray = json_object_get_array_member(root, "label");
+       unsigned int elements = json_array_get_length(rootArray);
+
+       // TODO: handling error
+       for (unsigned int elem = 0; elem < elements; ++elem) {
+               JsonNode *pNode = json_array_get_element(rootArray, elem);
+               JsonObject *pObject = json_node_get_object(pNode);
+
+               label.name = json_object_get_string_member(pObject, "name");
+               LOGI("layer: %s", label.name.c_str());
+
+               JsonArray *array = json_object_get_array_member(pObject, "index");
+               unsigned int elements2 = json_array_get_length(array);
+               LOGI("range dim: size[%u]", elements2);
+               for (unsigned int elem2 = 0; elem2 < elements2; ++elem2) {
+                       if (static_cast<int>(json_array_get_int_element(array, elem2)) == 1)
+                               label.dimInfo.SetValidIndex(elem2);
+               }
+       }
+
+       LOGI("LEAVE");
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int BoxInfo::ParseNumber(JsonObject *root)
+{
+       LOGI("ENTER");
+
+       if (!json_object_has_member(root, "number")) {
+               LOGE("No number outputmetadata");
+               LOGI("LEAVE");
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+       }
+
+       // box
+       JsonArray *rootArray = json_object_get_array_member(root, "number");
+       unsigned int elements = json_array_get_length(rootArray);
+
+       // TODO: handling error
+       for (unsigned int elem = 0; elem < elements; ++elem) {
+               JsonNode *pNode = json_array_get_element(rootArray, elem);
+               JsonObject *pObject = json_node_get_object(pNode);
+
+               number.name = json_object_get_string_member(pObject, "name");
+
+               LOGI("layer: %s", number.name.c_str());
+
+               JsonArray *array = json_object_get_array_member(pObject, "index");
+               unsigned int elements2 = json_array_get_length(array);
+
+               LOGI("range dim: size[%u]", elements2);
+
+               for (unsigned int elem2 = 0; elem2 < elements2; ++elem2)
+                       if (static_cast<int>(json_array_get_int_element(array, elem2)) == 1)
+                               number.dimInfo.SetValidIndex(elem2);
+       }
+
+       LOGI("LEAVE");
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int BoxInfo::ParseDecodeInfo(JsonObject *root)
+{
+       LOGI("ENTER");
+
+       // box
+       JsonArray *rootArray = json_object_get_array_member(root, "box");
+       unsigned int elements = json_array_get_length(rootArray);
+
+       // TODO: handling error
+       for (unsigned int elem = 0; elem < elements; ++elem) {
+               JsonNode *pNode = json_array_get_element(rootArray, elem);
+               JsonObject *pObject = json_node_get_object(pNode);
+
+               if (!json_object_has_member(pObject, "decoding_info")) {
+                       LOGE("decoding_info is mandatory. Invalid metadata");
+                       LOGI("LEAVE");
+
+                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               }
+
+               int ret = MEDIA_VISION_ERROR_NONE;
+               JsonObject *cObject =
+                               json_object_get_object_member(pObject, "decoding_info");
+               if (json_object_has_member(cObject, "anchor")) {
+                       ret = GetDecodeInfo().ParseAnchorParam(cObject);
+                       if (ret != MEDIA_VISION_ERROR_NONE) {
+                               LOGE("Fail to ParseAnchorParam[%d]", ret);
+                               return ret;
+                       }
+               } else if (json_object_has_member(cObject, "cell")) {
+                       ret = GetDecodeInfo().ParseCellParam(cObject);
+                       if (ret != MEDIA_VISION_ERROR_NONE) {
+                               LOGE("Fail to ParseCellParam[%d]", ret);
+                               return ret;
+                       }
+               } else {
+                       LOGE("anchor is mandatory. Invalid metadata");
+                       LOGI("LEAVE");
+
+                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
+               }
+
+               ret = GetDecodeInfo().ParseNms(cObject);
+               if (ret != MEDIA_VISION_ERROR_NONE) {
+                       LOGE("Fail to ParseNms[%d]", ret);
+                       return ret;
+               }
+
+               ret = GetDecodeInfo().ParseRotate(cObject);
+               if (ret != MEDIA_VISION_ERROR_NONE) {
+                       LOGE("Fail to ParseRotate[%d]", ret);
+                       return ret;
+               }
+
+               ret = GetDecodeInfo().ParseRoiOption(cObject);
+               if (ret != MEDIA_VISION_ERROR_NONE) {
+                       LOGE("Fail to ParseRoiOption[%d]", ret);
+                       return ret;
+               }
+       }
+
+       LOGI("LEAVE");
+       return MEDIA_VISION_ERROR_NONE;
+}
\ No newline at end of file