I want to extract the information I want from several json files and save it as a txt file.

Asked 2 years ago, Updated 2 years ago, 36 views

{
    "info": {
        "description": "V0F_HY_4668_20210105_090409_E_CH0_Busan_Sun_Industrialroads_Sunrise_36019_BBOX JSON file",
        "url": "",
        "version": "1.0",
        "year": 2021,
        "contributor": "Konkuk_university",
        "date_created": "2021/05/12"
    },
    "images": {
        "file_name": "V0F_HY_4668_20210105_090409_E_CH0_Busan_Sun_Industrialroads_Sunrise_36019.png",
        "height": 720,
        "width": 1280,
        "id": 1
    },
    "annotations": [
        {
            "segmentation": [],
            "polyline": [],
            "image_id": 1,
            "bbox": [
                675.8196721311476,
                454.09836065573774,
                263.52459016393436,
                61.47540983606558
            ],
            "category_id": 10,
            "area": 16200.282182209081,
            "is_crowd": 0,
            "id": 1
        },
        {
            "segmentation": [],
            "polyline": [],
            "image_id": 1,
            "bbox": [
                634.5,
                456.5,
                96,
                18
            ],
            "category_id": 8,
            "area": 1728,
            "is_crowd": 0,
            "id": 2
        }
    ],
    "categories": [
        {
            "id": 1,
            "name": "Animals(Dolls)"
        },
        {
            "id": 2,
            "name": "Person"
        },
        {
            "id": 3,
            "name": "Garbage bag & sacks"
        },
        {
            "id": 4,
            "name": "Construction signs & Parking prohibited board"
        },
        {
            "id": 5,
            "name": "Traffic cone"
        },
        {
            "id": 6,
            "name": "Box"
        },
        {
            "id": 7,
            "name": "Stones on road"
        },
        {
            "id": 8,
            "name": "Pothole on road"
        },
        {
            "id": 9,
            "name": "Filled pothole"
        },
        {
            "id": 10,
            "name": "Manhole"
        }
    ]
}





import json

def convert(size, box): #box: coco format xmin, ymin, w, h
    dw = 1/size[0]
    dh = 1/size[1]
    w = box[2]
    h = box[3]
    x = box[0]+ w/2
    y = box[1]+ h/2
    x = round(x*dw,6)
    w = round(w*dw,6)
    y = round(y*dh,6)
    h = round(h*dh,6)
    if w <0 or h < 0:
        return False
    return (x,y,w,h)

with open('V0F_HY_0001_20210108_145405_E_CH0_Busan_Sun_Frontback_Day_21068_BBOX.json') as f:
    data=json.load(f)

size=[640,512]

for i in data['annotations']:
    file_number=i['image_id']+8863
    file_number=f'2.txt'
    b=i['bbox']

    bb=convert(size, b)

    if bb==False:
        continue

    label_file=open(file_number,'w')

    label_number=i['category_id']

    line=f'{label_number} {bb[0]} {bb[1]} {bb[2]} {bb[3]}\n'

    label_file.write(line)
    label_file.close()

    progress=i['id']
    print(f'{progress}/11696')

print('finish')

There are several of these json files, but in each file, only the category_id and bbox parts of the annotations part I want to save it as a text file.


10  675.8196721311476 
454.09836065573774  263.52459016393436  61.47540983606558 8  634.5  456.5  96  18

After googling, I know how to extract it from one json file, but I extracted it from several json files I don't know how to save each txt file.

json python

2022-09-20 10:24

1 Answers

# Extract
a = data['annotations'][0]['image_id']
b = data['annotations'][0]['bbox']

# Writing
label_file.write(str(a))
label_file.write('\n')
label_file.write(','.join(b))


2022-09-20 10:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.