편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2022.05.20

    여러개의 json파일에서 각각 원하는 정보를 추출해 txt파일로 저장하고 싶습니다.


    json

    {
        "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"
            }
        ]
    }
    
    
    
    

    python

    
    
    import json
    
    def convert(size, box): #box: coco형식 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')
    

    이런 json파일이 여러개 존재하는데 각 파일에서 annotations 부분의 category_id와 bbox부분만 텍스트 파일 형태로 저장하고 싶습니다.

    
    10  675.8196721311476 
    454.09836065573774  263.52459016393436  61.47540983606558 8  634.5  456.5  96  18
    
    

    구글링을 해보니 하나의 json파일에서 추출하는 법은 알겠는데 여러개의 json파일에서 추출해서 각각의 txt파일로 저장하는 방법을 모르겠습니다.

  • 프로필 munseona님의 편집
    날짜2022.05.20

    여러개의 json파일에서 각각 원하는 정보를 추출해 txt파일로 저장하고 싶습니다.


    json

    {
        "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"
            }
        ]
    }
    
    
    
    

    python

    
    
    import json
    
    def convert(size, box): #box: coco형식 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')
    

    이런 json파일이 여러개 존재하는데 각 파일에서 annotations 부분의 category_id와 bbox부분만 텍스트 파일 형태로 저장하고 싶습니다. ㅜㅜ

    
    10  675.8196721311476 
    454.09836065573774  263.52459016393436  61.47540983606558 8  634.5  456.5  96  18
    
    

    구글링을 해보니 하나의 json파일에서 추출하는 법은 알겠는데 여러개의 json파일에서 추출해서 각각의 txt파일로 저장하는 방법을 모르겠습니다.ㅜㅜ