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("转换完成!")
1xscscasv
最新推荐文章于 2024-09-25 20:26:56 发布