(日常搬砖)数据集标注格式转换:txt转xml(VOC格式)

提前声明一下,本文转换的txt格式的标注为openimg的txt标注,txt里面的内容如下:
Rifle 205.599744 405.500823 1023.2002560000001 644.642271
第一个为类别名。

而darknet使用的txt标注内容如下:
1 0.56171875 0.49953703703703706 0.10885416666666667 0.20833333333333334

darknet中的类别名改为0,1等数字代替。
二者的目标框的标注形式不一样,不可混用。
本文的代码只可对openimg的txt文件进行转换!!

转换代码来自:OIDv4_to_VOC,如果有用,麻烦给原作者点颗星。
废话不多说,正文开始。

  1. 准备数据文件夹,以Handgun_Rifle为例,文件夹形式如下:

└───Handgun_Rifle

|0fdea8a716155a8e.jpg
|2fe4f21e409f0a56.jpg
|...
└───Labels
        |0fdea8a716155a8e.txt
        |2fe4f21e409f0a56.txt
        |...

其中,Apple为主文件夹名,其中包含图片文件和labels文件夹,labels文件夹中为.txt格式的标注文件。
2. 代码如下:


from xml.etree.ElementTree import Element, SubElement, Comment
import xml.etree.cElementTree as ET
#from ElementTree_pretty import prettify
import cv2
import os
from pathlib import Path
from shutil import move
import argparse

parser = argparse.ArgumentParser(description = 'Convert OIDV4 dataset to VOC XML format')
parser.add_argument('--sourcepath',type = str, default = 'dataset/', help ='Path of class to convert')
parser.add_argument('--dest_path',type=str, required=True, default='Annotation/',help='Path of Dest XML files')
args = parser.parse_args()

ids = []
for file in os.listdir(args.sourcepath): #Save all images in a list
    filename = os.fsdecode(file)
    if filename.endswith('.jpg'):
        ids.append(filename[:-4])

for fname in ids: 
    myfile = os.path.join(args.dest_path,fname +'.xml')
    myfile = Path(myfile)
    if not myfile.exists(): #if file is not existing 
        txtfile = os.path.join(args.sourcepath, 'Label', fname + '.txt') #Read annotation of each image from txt file
        f = open(txtfile,"r")
        imgfile = os.path.join(args.sourcepath, fname +'.jpg')
        img = cv2.imread(imgfile, cv2.IMREAD_UNCHANGED) #Read image to get image width and height
        top = Element('annotation')
        child = SubElement(top,'folder')
        child.text = 'open_images_volume'

        child_filename = SubElement(top,'filename')
        child_filename.text = fname +'.jpg'

        child_path = SubElement(top,'path')
        child_path.text = '/mnt/open_images_volume/' + fname +'.jpg'

        child_source = SubElement(top,'source')
        child_database = SubElement(child_source, 'database')
        child_database.text = 'Unknown'

        child_size = SubElement(top,'size')
        child_width = SubElement(child_size,'width')
        child_width.text = str(img.shape[1])

        child_height = SubElement(child_size,'height')
        child_height.text = str(img.shape[0])

        child_depth = SubElement(child_size,'depth')
        if len(img.shape) == 3: 
            child_depth.text = str(img.shape[2])
        else:
            child_depth.text = '3'
        child_seg = SubElement(top, 'segmented')
        child_seg.text = '0'
        for x in f:     #Iterate for each object in a image. 
            x = list(x.split())
            child_obj = SubElement(top, 'object')

            child_name = SubElement(child_obj, 'name')
            child_name.text = x[0] #name

            child_pose = SubElement(child_obj, 'pose')
            child_pose.text = 'Unspecified'

            child_trun = SubElement(child_obj, 'truncated')
            child_trun.text = '0'

            child_diff = SubElement(child_obj, 'difficult')
            child_diff.text = '0'

            child_bndbox = SubElement(child_obj, 'bndbox')

            child_xmin = SubElement(child_bndbox, 'xmin')
            child_xmin.text = str(int(float(x[1]))) #xmin

            child_ymin = SubElement(child_bndbox, 'ymin')
            child_ymin.text = str(int(float(x[2]))) #ymin

            child_xmax = SubElement(child_bndbox, 'xmax')
            child_xmax.text = str(int(float(x[3]))) #xmax

            child_ymax = SubElement(child_bndbox, 'ymax')
            child_ymax.text = str(int(float(x[4]))) #ymax

        tree = ET.ElementTree(top)
        save = fname+'.xml'
        tree.write(save)
        move(fname+'.xml', myfile)

将以上代码另存为文件python3 OIDv4_to_VOC.py
3. 终端进入到python3 OIDv4_to_VOC.py所在的文件夹,运行如下指令:

 python3 OIDv4_to_VOC.py --sourcepath /OID/Dataset/train/Handgun_Rifle --dest_path /forSSD_txt2xml/VOC2007/Annotations

其中,/OID/Dataset/train/Handgun_Rifle为openimg下载的数据集文件夹,/forSSD_txt2xml/VOC2007/Annotations为生成的xml文件的保存路径。

你可能感兴趣的:(日常搬砖,xml,深度学习,目标检测)