写一段txt转换成voc数据集的python代码
时间: 2023-11-18 18:03:21 浏览: 112
以下是一个简单的示例代码,用于将txt文件转换为voc数据集格式:
```python
import os
import xml.etree.ElementTree as ET
# 设置类别列表
classes = ['class1', 'class2', 'class3']
# 设置输入文件和输出目录
input_txt = 'input.txt'
output_dir = 'output'
# 创建输出目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 读取输入文件
with open(input_txt, 'r') as f:
lines = f.readlines()
# 遍历每一行并创建对应的XML文件
for i, line in enumerate(lines):
# 解析行数据
data = line.strip().split()
# 获取图像路径和尺寸
img_path = data[0]
img_width = int(data[1])
img_height = int(data[2])
# 创建XML文件
root = ET.Element('annotation')
# 添加图像信息
folder = ET.SubElement(root, 'folder')
folder.text = output_dir
filename = ET.SubElement(root, 'filename')
filename.text = os.path.basename(img_path)
size = ET.SubElement(root, 'size')
width = ET.SubElement(size, 'width')
width.text = str(img_width)
height = ET.SubElement(size, 'height')
height.text = str(img_height)
depth = ET.SubElement(size, 'depth')
depth.text = '3'
# 添加目标信息
for j in range(3, len(data), 5):
# 获取目标框坐标和类别
x_min = int(data[j])
y_min = int(data[j+1])
x_max = int(data[j+2])
y_max = int(data[j+3])
class_idx = int(data[j+4])
# 创建目标框
obj = ET.SubElement(root, 'object')
name = ET.SubElement(obj, 'name')
name.text = classes[class_idx]
bndbox = ET.SubElement(obj, 'bndbox')
xmin = ET.SubElement(bndbox, 'xmin')
xmin.text = str(x_min)
ymin = ET.SubElement(bndbox, 'ymin')
ymin.text = str(y_min)
xmax = ET.SubElement(bndbox, 'xmax')
xmax.text = str(x_max)
ymax = ET.SubElement(bndbox, 'ymax')
ymax.text = str(y_max)
# 保存XML文件
xml_path = os.path.join(output_dir, os.path.splitext(os.path.basename(img_path))[0] + '.xml')
tree = ET.ElementTree(root)
tree.write(xml_path)
```
其中,输入文件`input.txt`的格式应为每行一条记录,用空格隔开,包括图像路径、图像宽度、图像高度以及每个目标框的坐标和类别索引。例如:
```
/path/to/image1.jpg 640 480 10 20 100 200 0 30 50 200 300 1
/path/to/image2.jpg 800 600 50 100 200 400 1 200 300 400 500 2
```
输出目录`output`中将生成与输入文件中每个图像对应的XML文件,如`image1.xml`和`image2.xml`。
阅读全文