如何用Python将复杂的JSON文件拆分成多个文件

rggaifut  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(202)

我目前正在分割Json档案。
JSON文件的结构如下:

{
    "id": 2131424,
    "file": "video_2131424_1938263.mp4",
    "metadata": {
        "width": 3840,
        "height": 2160,
        "duration": 312.83,
        "fps": 30,
        "frames": 9385,
        "created": "Sun Jan 17 17:48:52 2021"
    },
    "frames": [
        {
            "number": 207,
            "image": "frame_207.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 730,
                        "y": 130,
                        "width": 62,
                        "height": 152
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 815,
                        "y": 81,
                        "width": 106,
                        "height": 197
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },
        {
            "number": 221,
            "image": "frame_221.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 730,
                        "y": 130,
                        "width": 64,
                        "height": 160
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 819,
                        "y": 82,
                        "width": 106,
                        "height": 200
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },
        {
            "number": 236,
            "image": "frame_236.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 731,
                        "y": 135,
                        "width": 74,
                        "height": 160
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 821,
                        "y": 83,
                        "width": 106,
                        "height": 206
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },

我必须从每个标签中提取[x,y,width,height]。我尝试了如下代码:

file = json.load(open('annotation_2131424.json'))
file['frames'][i]['annotations'][j]['label']['x']

但是我不能拆分JSON。我试过这样做,但是我不能运行...

lhcgjxsq

lhcgjxsq1#

我希望我没理解错你的问题,要从每个label中得到xywidthheightdct是你的问题字典):

out = [
    [
        [
            a["label"]["x"],
            a["label"]["y"],
            a["label"]["width"],
            a["label"]["height"],
        ]
        for a in frame["annotations"]
    ]
    for frame in dct["frames"]
]

print(out)

印刷品:

[
    [[730, 130, 62, 152], [815, 81, 106, 197]],
    [[730, 130, 64, 160], [819, 82, 106, 200]],
    [[731, 135, 74, 160], [821, 83, 106, 206]],
]

相关问题