dataset/YOLO_format.py
def xml_to_txt(xml_file, txt_file, categories):
    """Function to convert pascal xml annotation to yolo txt format"""
    tree = ET.parse(xml_file)
    root = tree.getroot()
    size = get_and_check(root, "size", 1)
    width = int(get_and_check(size, "width", 1).text)
    height = int(get_and_check(size, "height", 1).text)
    #convert to yolo bbox
    with open(txt_file, 'a+') as f:
        for obj in get(root, "object"):
            category = get_and_check(obj, "name", 1).text
            category_id = categories[category]
            bndbox = get_and_check(obj, "bndbox", 1)
            xmin = int(get_and_check(bndbox, "xmin", 1).text) 
            ymin = int(get_and_check(bndbox, "ymin", 1).text) 
            xmax = int(get_and_check(bndbox, "xmax", 1).text)
            ymax = int(get_and_check(bndbox, "ymax", 1).text)
            #adjust to yolo: <class> <x_center> <y_center> <box_width> <box_height> 
            x_center = ((xmin+xmax)/2)/width   #relative to image (between 0-1)
            y_center = ((ymin+ymax)/2)/height
            bbox_w = (xmax-xmin)/width
            bbox_h = (ymax-ymin)/height
            f.write("{} {} {} {} {}\n".format(category_id, x_center, y_center, bbox_w, bbox_h))
Function to convert pascal xml annotation to yolo txt format
Function to convert pascal xml annotation to yolo txt format
Function to convert pascal xml annotation to yolo txt format
def xml_to_txt(xml_file, txt_file, categories):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    size = get_and_check(root, "size", 1)
    width = int(get_and_check(size, "width", 1).text)
    height = int(get_and_check(size, "height", 1).text)
    with open(txt_file, 'a+') as f:
        for obj in get(root, "object"):
            category = get_and_check(obj, "name", 1).text
            category_id = categories[category]
            bndbox = get_and_check(obj, "bndbox", 1)
            xmin = int(get_and_check(bndbox, "xmin", 1).text) 
            ymin = int(get_and_check(bndbox, "ymin", 1).text) 
            xmax = int(get_and_check(bndbox, "xmax", 1).text)
            ymax = int(get_and_check(bndbox, "ymax", 1).text)
            x_center = ((xmin+xmax)/2)/width   
            y_center = ((ymin+ymax)/2)/height
            bbox_w = (xmax-xmin)/width
            bbox_h = (ymax-ymin)/height
            f.write("{} {} {} {} {}\n".format(category_id, x_center, y_center, bbox_w, bbox_h))
def xml_to_txt ( xml_file , txt_file , categories ) : tree = ET . parse ( xml_file ) root = tree . getroot ( ) size = get_and_check ( root , "size" , 1 ) width = int ( get_and_check ( size , "width" , 1 ) . text ) height = int ( get_and_check ( size , "height" , 1 ) . text ) with open ( txt_file , 'a+' ) as f : for obj in get ( root , "object" ) : category = get_and_check ( obj , "name" , 1 ) . text category_id = categories [ category ] bndbox = get_and_check ( obj , "bndbox" , 1 ) xmin = int ( get_and_check ( bndbox , "xmin" , 1 ) . text ) ymin = int ( get_and_check ( bndbox , "ymin" , 1 ) . text ) xmax = int ( get_and_check ( bndbox , "xmax" , 1 ) . text ) ymax = int ( get_and_check ( bndbox , "ymax" , 1 ) . text ) x_center = ( ( xmin + xmax ) / 2 ) / width y_center = ( ( ymin + ymax ) / 2 ) / height bbox_w = ( xmax - xmin ) / width bbox_h = ( ymax - ymin ) / height f . write ( "{} {} {} {} {}\n" . format ( category_id , x_center , y_center , bbox_w , bbox_h ) )
Function to convert pascal xml annotation to yolo txt format
Function to convert pascal xml annotation to yolo txt format
"""Function to convert pascal xml annotation to yolo txt format""" #convert to yolo bbox #adjust to yolo: <class> <x_center> <y_center> <box_width> <box_height>  #relative to image (between 0-1)
xml_file
txt_file
categories