1xscscasv

import os
import shutil

# 定义输入和输出文件夹路径
input_folder = 'obj'
output_folder = 'VOC_dataset'

# 创建输出文件夹
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 遍历每个图像和文本文件
for i in range(1, 901):
    # 构建文件名
    image_file = os.path.join(input_folder, f'img{i}.jpg')
    text_file = os.path.join(input_folder, f'img{i}.txt')

    # 定义输出文件路径
    output_image = os.path.join(output_folder, f'img{i}.jpg')
    output_annotation = os.path.join(output_folder, f'img{i}.xml')

    # 复制图像文件到输出文件夹
    shutil.copyfile(image_file, output_image)

    # 读取文本文件内容
    with open(text_file, 'r') as file:
        content = file.read().strip()
    
    # 创建VOC格式的XML注释文件
    annotation = f'''
    <annotation>
        <filename>img{i}.jpg</filename>
        <object>
            <name>{content}</name>
            <bndbox>
                <xmin>1</xmin>
                <ymin>1</ymin>
                <xmax>100</xmax>
                <ymax>100</ymax>
            </bndbox>
        </object>
    </annotation>
    '''

    # 保存XML注释文件
    with open(output_annotation, 'w') as file:
        file.write(annotation)

print("转换完成!")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值