Fix box array to object in meta json
authorKwanghoon Son <k.son@samsung.com>
Mon, 29 Aug 2022 06:00:14 +0000 (02:00 -0400)
committerInki Dae <inki.dae@samsung.com>
Fri, 2 Sep 2022 08:09:25 +0000 (17:09 +0900)
outputmetadata box object has array, but usage only accept 1 array
element. This change handle object instead array.
`before`
"box" : [
    {
        "name" : "some name",
        ...
    }
],
`after`
"box" :
    {
        "name" : "some name",
        ...
    }

Change-Id: If77d781a5f4bbf6beb7078e34edd5309f29a471e
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_machine_learning/inference/src/BoxInfo.cpp

index 1585691..2c44757 100644 (file)
@@ -23,47 +23,45 @@ int BoxInfo::ParseBox(JsonObject *root)
 {
        LOGI("ENTER");
 
-       JsonArray *rootArray = json_object_get_array_member(root, "box");
-       unsigned int elements = json_array_get_length(rootArray);
+       if (!json_object_has_member(root, "box")) {
+               LOGE("member box not exists");
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       }
 
-       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);
+       JsonObject *pObject = json_object_get_object_member(root, "box");
 
-               name = json_object_get_string_member(pObject, "name");
-               LOGI("layer: %s", name.c_str());
+       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);
+       JsonArray *array = json_object_get_array_member(pObject, "index");
+       unsigned int elements2 = json_array_get_length(array);
 
-               LOGI("range dim: size[%u]", elements2);
+       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;
-               }
+       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);
+       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);
-               }
+       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");
@@ -145,61 +143,58 @@ 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(root, "box")) {
+               LOGE("member box not exists");
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       }
 
-               if (!json_object_has_member(pObject, "decoding_info")) {
-                       LOGE("decoding_info is mandatory. Invalid metadata");
-                       LOGI("LEAVE");
+       JsonObject *pObject = json_object_get_object_member(root, "box");
 
-                       return MEDIA_VISION_ERROR_INVALID_OPERATION;
-               }
+       if (!json_object_has_member(pObject, "decoding_info")) {
+               LOGE("decoding_info is mandatory. Invalid metadata");
+               LOGI("LEAVE");
 
-               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;
-               }
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+       }
 
-               ret = GetDecodeInfo().ParseNms(cObject);
+       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 ParseNms[%d]", ret);
+                       LOGE("Fail to ParseAnchorParam[%d]", ret);
                        return ret;
                }
-
-               ret = GetDecodeInfo().ParseRotate(cObject);
+       } else if (json_object_has_member(cObject, "cell")) {
+               ret = GetDecodeInfo().ParseCellParam(cObject);
                if (ret != MEDIA_VISION_ERROR_NONE) {
-                       LOGE("Fail to ParseRotate[%d]", ret);
+                       LOGE("Fail to ParseCellParam[%d]", ret);
                        return ret;
                }
+       } else {
+               LOGE("anchor is mandatory. Invalid metadata");
+               LOGI("LEAVE");
 
-               ret = GetDecodeInfo().ParseRoiOption(cObject);
-               if (ret != MEDIA_VISION_ERROR_NONE) {
-                       LOGE("Fail to ParseRoiOption[%d]", ret);
-                       return ret;
-               }
+               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");