数据集格式转换:欧洲水下YOLO2VOC

欧洲水下数据集YOLO格式转VOC格式

from xml.dom.minidom import Document
import os
import os.path
from PIL import Image

#ann_path = "yolo/"  # yolov3标注.txt文件夹
ann_path = r"C:/Users/Lenovo/Desktop/only_shrimp/txts/"  # yolov3标注.txt文件夹
#img_path = "images/"  # 图片文件夹
img_path = r"C:/Users/Lenovo/Desktop/only_shrimp/pictures_all/"  # 图片文件夹
xml_path = r"C:/Users/Lenovo/Desktop/only_shrimp/xmls/"  # .xml文件存放地址
class_name = r'C:\Users\Lenovo\Desktop\only_shrimp\classes_name.txt'
Class_Name = []

with open(class_name, 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.strip().split()
        Class_Name.append(line[0])

if not os.path.exists(xml_path):
    os.mkdir(xml_path)


def writeXml(tmp, imgname, w, h, objbud, wxml):
    doc = Document()
    # owner
    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)
    # owner
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder_txt = doc.createTextNode("VOC2005")
    folder.appendChild(folder_txt)

    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename_txt = doc.createTextNode(imgname)
    filename.appendChild(filename_txt)
    # ones#
    source = doc.createElement('source')
    annotation.appendChild(source)

    database = doc.createElement('database')
    source.appendChild(database)
    database_txt = doc.createTextNode("The VOC2007 Database")#2005个人改成了2007
    database.appendChild(database_txt)

    annotation_new = doc.createElement('annotation')
    source.appendChild(annotation_new)
    annotation_new_txt = doc.createTextNode("PASCAL VOC2007")#2005个人改成了2007
    annotation_new.appendChild(annotation_new_txt)

    image = doc.createElement('image')
    source.appendChild(image)
    image_txt = doc.createTextNode("flickr")
    image.appendChild(image_txt)
    # onee#
    # twos#
    size = doc.createElement('size')
    annotation.appendChild(size)

    width = doc.createElement('width')
    size.appendChild(width)
    width_txt = doc.createTextNode(str(w))
    width.appendChild(width_txt)

    height = doc.createElement('height')
    size.appendChild(height)
    height_txt = doc.createTextNode(str(h))
    height.appendChild(height_txt)

    depth = doc.createElement('depth')
    size.appendChild(depth)
    depth_txt = doc.createTextNode("3")
    depth.appendChild(depth_txt)
    # twoe#
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented_txt = doc.createTextNode("0")
    segmented.appendChild(segmented_txt)

    for i in range(0, int(len(objbud))):
        objbuds = objbud[i].split(' ')
        #print(objbuds)
        # threes#
        object_new = doc.createElement("object")
        annotation.appendChild(object_new)

        name = doc.createElement('name')
        object_new.appendChild(name)
        name_txt = doc.createTextNode(Class_Name[int(float(objbuds[0]))])
        name.appendChild(name_txt)

        pose = doc.createElement('pose')
        object_new.appendChild(pose)
        pose_txt = doc.createTextNode("Unspecified")
        pose.appendChild(pose_txt)

        truncated = doc.createElement('truncated')
        object_new.appendChild(truncated)
        truncated_txt = doc.createTextNode("0")
        truncated.appendChild(truncated_txt)

        difficult = doc.createElement('difficult')
        object_new.appendChild(difficult)
        difficult_txt = doc.createTextNode("0")
        difficult.appendChild(difficult_txt)
        # threes-1#
        bndbox = doc.createElement('bndbox')
        object_new.appendChild(bndbox)

        xmin = doc.createElement('xmin')
        bndbox.appendChild(xmin)
        #xmin_txt = doc.createTextNode(str(int((float(objbuds[1]) * w - float(objbuds[3]) * w /
        xmin_txt = doc.createTextNode(str(int(float(objbuds[1])-float(objbuds[3])/2)))

        xmin.appendChild(xmin_txt)

        ymin = doc.createElement('ymin')
        bndbox.appendChild(ymin)
        #ymin_txt = doc.createTextNode(str(int(float(objbuds[2]) * h - float(objbuds[4]) * h / 2.0)))
        ymin_txt = doc.createTextNode(str(int(float(objbuds[2])-float(objbuds[4])/2)))
        ymin.appendChild(ymin_txt)

        xmax = doc.createElement('xmax')
        bndbox.appendChild(xmax)
        #xmax_txt = doc.createTextNode(str(int(float(objbuds[1]) * w + float(objbuds[3]) * w / 2.0)))
        xmax_txt = doc.createTextNode(str(int(float(objbuds[1])+float(objbuds[3])/2)))
        xmax.appendChild(xmax_txt)

        ymax = doc.createElement('ymax')
        bndbox.appendChild(ymax)
        #ymax_txt = doc.createTextNode(str(int(float(objbuds[2]) * h + float(objbuds[4]) * h / 2)))
        ymax_txt = doc.createTextNode(str(int(float(objbuds[2])+float(objbuds[4])/2)))
        ymax.appendChild(ymax_txt)
        # threee-1#
        # threee#

    tempfile = tmp + "test.xml"
    with open(tempfile, "w") as f:
        f.write(doc.toprettyxml(indent='\t'))

    rewrite = open(tempfile, "r")
    lines = rewrite.read().split('\n')
    newlines = lines[1:len(lines) - 1]

    fw = open(wxml, "w")
    for i in range(0, len(newlines)):
        fw.write(newlines[i] + '\n')

    fw.close()
    rewrite.close()
    os.remove(tempfile)
    return


for files in os.walk(ann_path):
    temp = "temp/"
    if not os.path.exists(temp):
        os.mkdir(temp)
    for file in files[2]:
        print(file + "-->start!")
        img_name = os.path.splitext(file)[0] + '.png'
        fileimgpath = img_path + img_name
        im = Image.open(fileimgpath)
        width = int(im.size[0])
        height = int(im.size[1])

        filelabel = open(ann_path + file, "r")
        lines = filelabel.read().split('\n')
        obj = lines[:len(lines) - 1]
        #         print(obj)

        filename = xml_path + os.path.splitext(file)[0] + '.xml'
        writeXml(temp, img_name, width, height, obj, filename)
    os.rmdir(temp)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将YOLO格式数据集转换为VOC格式数据集,需要按照以下步骤进行操作: 1. 根据YOLO格式数据集的标注文件,将每个图像中的目标对象的位置、类别和置信度信息提取出来,存储在一个文本文件中。每行包含一个目标对象的信息,格式如下: ``` <class> <x_center> <y_center> <width> <height> ``` 其中,`<class>`表示目标对象所属的类别,`<x_center>`和`<y_center>`表示目标对象中心点在图像中的坐标,`<width>`和`<height>`表示目标对象的宽度和高度,所有这些值都是相对于图像大小的比例。 2. 将每个图像的文件名和对应的标注文件名存储在一个XML文件中,格式如下: ``` <annotation> <folder>image_folder</folder> <filename>image_name.jpg</filename> <size> <width>image_width</width> <height>image_height</height> <depth>3</depth> </size> <object> <name>object_class</name> <bndbox> <xmin>xmin_value</xmin> <ymin>ymin_value</ymin> <xmax>xmax_value</xmax> <ymax>ymax_value</ymax> </bndbox> </object> ... </annotation> ``` 其中,`<folder>`表示图像文件所在的文件夹,`<filename>`表示图像文件名,`<width>`和`<height>`表示图像的宽度和高度,`<object>`表示一个目标对象,`<name>`表示目标对象所属的类别,`<bndbox>`表示目标对象的边界框,`<xmin>`、`<ymin>`、`<xmax>`和`<ymax>`分别表示边界框左上角和右下角的坐标。 3. 将所有XML文件和对应的图像文件存储在一个文件夹中,这样就得到了一个VOC格式的数据集。 需要注意的是,YOLO格式数据集和VOC格式数据集的标注信息格式不同,因此需要进行格式转换。此外,VOC格式数据集还需要包含图像文件本身,因此需要将YOLO格式数据集中的图像文件也复制到VOC格式数据集中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值