본문 바로가기
반치용/문제해결(trouble shooting)

convert voc to yolo (xml to yolo, xml to txt)

by  반  2020. 5. 13.

https://gist.github.com/M-Younus/ceaf66e11a9c0f555b66a75d5b557465

이 코드를 사용하였음.

 

영상 인공지능을 다루다 보면 레이블링 포맷이 다른 경우가 생김.

특히 yolo v4가 나온 지 얼마 안 되서 실험해보려고 하니 xml을 yolo labeling 형태로 바꿔야 해서 이것저것 시도하다가 위 코드가 제일 나아보여서 사용하였고, 내 상황에서 생기는 에러나 오작동 부분만 추가 가공 하였음.

수정사항 :

png, jpg 둘 다 처리하게 바꿈

인코딩 관련하여 문제가 생기는 상황(cp 949 파일명 못불러들이는거) 처리함

 

사용법 :

같은 폴더 ( ./ 라고 칭하겠음 ) 안에 폴더 만들고 이미지 파일과 xml 파일을 넣음

ex) 

 ./voc2yolo_converter.ipynb

 ./annotation/1.jpg
 ./annotation/1.xml

 코드 내에 classes를 본인이 설정한 classes 명(오브젝트 명칭)으로 변경

 ex)

 clasees = ['polyp','adenoma']
-> 리스트 형태 가능

 

 작동!!

voc2yolo_converter.py
0.00MB
voc2yolo_converter.ipynb
0.01MB

import glob
import os
import pickle
import xml.etree.ElementTree as ET
from os import listdir, getcwd
from os.path import join

dirs = ['annotation']
classes = ['polyp']


def getImagesInDir(dir_path):
    image_list = []
    for filename in glob.glob(dir_path + '/*.png'):
        image_list.append(filename)
    for filename in glob.glob(dir_path + '/*.jpg'):
        image_list.append(filename)

    return image_list


def convert(size, box):
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)



def convert_annotation(dir_path, output_path, image_path):
    basename = os.path.basename(image_path)
    basename_no_ext = os.path.splitext(basename)[0]

    in_file = open(dir_path + '/' + basename_no_ext + '.xml' ,encoding='UTF8')
    out_file = open(output_path + basename_no_ext + '.txt', 'w' ,encoding='UTF8')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult)==1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')



cwd = getcwd()




for dir_path in dirs:
    full_dir_path = cwd + '/' + dir_path
    output_path = full_dir_path +'/yolo/'
    

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    image_paths = getImagesInDir(full_dir_path)
    print(full_dir_path)
    list_file = open(full_dir_path + '.txt', 'w')

    for image_path in image_paths:
        list_file.write(image_path + '\n')
        convert_annotation(full_dir_path, output_path, image_path)
        
    list_file.close()

    print("Finished processing: " + dir_path)




댓글