yolo 클래스에 맞도록 데이터 형식 바꾸기

youngseojeon·2022년 1월 31일
0

yolo는 클래스로 label xmin, ymin, w, h 이 순서대로 써주어야 하기 때문에 이렇게 바꾸어주어야 한다,,
이 코드는 json에 있는 특정 정보를 잘 뜯어와서 잘 오리고 붙이는 방법인데,, file을 열고, 다양한 정보들을 추출해 온 후, 오리고 붙이는 과정을 적어놓았다.!

for i in range(1,1001):
    num = str(i).zfill(4)
    with open('./Training/[라벨]돼지_bbox/livestock_pig_bbox_00' + num + '.json', 'r') as f:
        json_data = json.load(f)
        label_info = json_data['label_info']

        image_name = label_info['image']['file_name'] # 이미지 파일명
        image_width = label_info['image']['width'] # 이미지 넓이
        image_height = label_info['image']['height'] # 이미지 높이
        annotations =  label_info['annotations']
        image_name = image_name.replace(".jpg", "")
        file = open("./labels/"+image_name + ".txt", "w")
        for annot in annotations:
            x1, y1, x2, y2 = annot['bbox'] # 객체 1개의 bbox 위치 좌표
            x1 = x1/image_width
            y1 = y1/image_height
            x2 = (x2-x1)/image_width
            y2 = (y2-y1)/image_height
            file.write(f"0 {x1:f} {y1:f} {x2:f} {y2:f}\n")
        file.close()
profile
youngseo-computerblog.tistory.com 을 사용하고 있습니다.

0개의 댓글