Yolo标准数据集格式转Voc数据集

Yolo数据集格式

在这里插入图片描述
在这里插入图片描述
yolo格式详解:
1代表类别,后面小数依次是目标框x中心点坐标归一化处理,y中心点坐标归一化处理,目标框宽和高进行归一化处理(这里的归一化是按照图片的宽高进行计算的)

转换代码

方法一

import os
import glob
from PIL import Image

voc_annotations = 'D:/yolo_test_review/annotations/'
yolo_txt = 'D:/yolo_test_review/yolov3/labels/'
img_path = 'D:/yolo_test_review/yolov3/images/'
labels = ['A', 'B', 'C']  # label for datasets
# 图像存储位置
src_img_dir = img_path  # 添加你的路径
# 图像的txt文件存放位置


src_txt_dir = yolo_txt
src_xml_dir = voc_annotations

img_Lists = glob.glob(src_img_dir + '/*.jpg')

img_basenames = []
for item in img_Lists:
    img_basenames.append(os.path.basename(item))

img_names = []
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)
    img_names.append(temp1)

for img in img_names:
    im = Image.open((src_img_dir + '/' + img + '.jpg'))
    width, height = im.size

    # 打开txt文件
    gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()
    print(gt)
    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) + '.jpg' + '</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(',')。
            print(f'spt:{spt}')
            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>')


方法二

import os
from PIL import Image
import glob

yolo_img = 'D:/shuichi_test/yolo/img/'
yolo_txt = 'D:/shuichi_test/yolo/txt/'
voc_xml = 'D:/shuichi_test/voc/annotations/'

# 目标类别
labels = ['HLB', 'health', 'ill']
# 匹配文件路径下的所有jpg文件,并返回列表
img_glob = glob.glob(yolo_img + '*.jpg')

img_base_names = []

for img in img_glob:
    # os.path.basename:取文件的后缀名
    img_base_names.append(os.path.basename(img))

img_pre_name = []

for img in img_base_names:
    # os.path.splitext:将文件按照后缀切分为两块
    temp1, temp2 = os.path.splitext(img)
    img_pre_name.append(temp1)
    print(f'imgpre:{img_pre_name}')
for img in img_pre_name:
    with open(voc_xml + img + '.xml', 'w') as xml_files:
        image = Image.open(yolo_img + img + '.jpg')
        img_w, img_h = image.size
        xml_files.write('<annotation>\n')
        xml_files.write('   <folder>folder</folder>\n')
        xml_files.write(f'   <filename>{img}.jpg</filename>\n')
        xml_files.write('   <source>\n')
        xml_files.write('   <database>Unknown</database>\n')
        xml_files.write('   </source>\n')
        xml_files.write('   <size>\n')
        xml_files.write(f'     <width>{img_w}</width>\n')
        xml_files.write(f'     <height>{img_h}</height>\n')
        xml_files.write(f'     <depth>3</depth>\n')
        xml_files.write('   </size>\n')
        xml_files.write('   <segmented>0</segmented>\n')
        with open(yolo_txt + img + '.txt', 'r') as f:
            # 以列表形式返回每一行
            lines = f.read().splitlines()
            for each_line in lines:
                line = each_line.split(' ')
                xml_files.write('   <object>\n')
                xml_files.write(f'      <name>{labels[int(line[0])]}</name>\n')
                xml_files.write('      <pose>Unspecified</pose>\n')
                xml_files.write('      <truncated>0</truncated>\n')
                xml_files.write('      <difficult>0</difficult>\n')
                xml_files.write('      <bndbox>\n')
                center_x = round(float(line[1]) * img_w)
                center_y = round(float(line[2]) * img_h)
                bbox_w = round(float(line[3]) * img_w)
                bbox_h = round(float(line[4]) * img_h)
                xmin = str(int(center_x - bbox_w / 2))
                ymin = str(int(center_y - bbox_h / 2))
                xmax = str(int(center_x + bbox_w / 2))
                ymax = str(int(center_y + bbox_h / 2))
                xml_files.write(f'         <xmin>{xmin}</xmin>\n')
                xml_files.write(f'         <ymin>{ymin}</ymin>\n')
                xml_files.write(f'         <xmax>{xmax}</xmax>\n')
                xml_files.write(f'         <ymax>{ymax}</ymax>\n')
                xml_files.write('      </bndbox>\n')
                xml_files.write('   </object>\n')
        xml_files.write('</annotation>')

生成的XML文件

在这里插入图片描述
在这里插入图片描述

版权声明:本文为CSDN博主「阿吉的CV之路」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35140742/article/details/118157598

阿吉的CV之路

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐