将yolo格式数据集转换为VOC格式

将yolo格式数据集转换为VOC格式

由于需要切分图象,将yolo转换为voc格式好处理一些

背景

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码

import xml.dom.minidom
import glob
from PIL import Image
from math import ceil
import shutil
import os

yolo_file = r'E://黄花标注//glass//train//split//final9.14//yesann'  # yolo格式下的存放txt标注文件的文件夹
turn_xml_file = r'E://黄花标注//glass//train//split//final9.14//xml'  # 转换后储存xml的文件夹地址
img_file = r'E://黄花标注//glass//train//split//final9.14//yes'  # 存放图片的文件夹

labels = ['defect', 'mh']
src_img_dir = img_file
src_txt_dir = yolo_file
src_xml_dir = turn_xml_file  # 转换后储存xml的文件夹地址

img_Lists = glob.glob(src_img_dir + '/*.bmp')
img_basenames = []
for item in img_Lists:
    img_basenames.append(os.path.basename(item))  # os.path.basename返回path最后的文件名

img_names = []
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)  # os.path.splitext(“文件路径”)    分离文件名与扩展名
    img_names.append(temp1)

total_num = len(img_names)  # 统计当前总共要转换的图片标注数量
count = 0  # 技术变量
for img in img_names:  # 这里的img是不加后缀的图片名称,如:'GF3_SAY_FSI_002732_E122.3_N29.9_20170215_L1A_HH_L10002188179__1__4320___10368'
    count += 1
    if count % 1000 == 0:
        print("当前转换进度{}/{}".format(count, total_num))
    im = Image.open((src_img_dir + '/' + img + '.bmp'))
    width, height = im.size

    # 打开yolo格式下的txt文件
    gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()
    if gt:
        # 将主干部分写入xml文件中
        xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
        xml_file.write('<annotation>\n')
        xml_file.write('    <folder>VOC2007</folder>\n')
        xml_file.write('    <filename>' + str(img) + '.bmp' + '</filename>\n')
        xml_file.write('    <size>\n')
        xml_file.write('        <width>' + str(width) + '</width>\n')
        xml_file.write('        <height>' + str(height) + '</height>\n')
        xml_file.write('        <depth>3</depth>\n')
        xml_file.write('    </size>\n')

        # write the region of image on xml file
        for img_each_label in gt:
            spt = img_each_label.split(' ')  # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
            xml_file.write('    <object>\n')
            xml_file.write('        <name>' + str(labels[int(spt[0])]) + '</name>\n')
            xml_file.write('        <pose>Unspecified</pose>\n')
            xml_file.write('        <truncated>0</truncated>\n')
            xml_file.write('        <difficult>0</difficult>\n')
            xml_file.write('        <bndbox>\n')

            center_x = round(float(spt[1].strip()) * width)
            center_y = round(float(spt[2].strip()) * height)
            bbox_width = round(float(spt[3].strip()) * width)
            bbox_height = round(float(spt[4].strip()) * height)
            xmin = str(int(center_x - bbox_width / 2))
            ymin = str(int(center_y - bbox_height / 2))
            xmax = str(int(center_x + bbox_width / 2))
            ymax = str(int(center_y + bbox_height / 2))

            xml_file.write('            <xmin>' + xmin + '</xmin>\n')
            xml_file.write('            <ymin>' + ymin + '</ymin>\n')
            xml_file.write('            <xmax>' + xmax + '</xmax>\n')
            xml_file.write('            <ymax>' + ymax + '</ymax>\n')
            xml_file.write('        </bndbox>\n')
            xml_file.write('    </object>\n')

        xml_file.write('</annotation>')
    else:
        # 将主干部分写入xml文件中
        xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
        xml_file.write('<annotation>\n')
        xml_file.write('    <folder>VOC2007</folder>\n')
        xml_file.write('    <filename>' + str(img) + '.bmp' + '</filename>\n')
        xml_file.write('    <size>\n')
        xml_file.write('        <width>' + str(width) + '</width>\n')
        xml_file.write('        <height>' + str(height) + '</height>\n')
        xml_file.write('        <depth>3</depth>\n')
        xml_file.write('    </size>\n')
        xml_file.write('</annotation>')

参考文章

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值