目标检测-数据集转换: csv转voc(xml)格式
csv标签样式voc(xml)格式转换代码# .csv-->.xml# ! /usr/bin/python# -*- coding:UTF-8 -*-import globfrom PIL import Imageimport csv#xml保存的位置save_xml_dir = "Annotations_1/"#src_img_dir = "Intestinal Organoid Data
·
csv标签样式
voc(xml)格式
转换代码
# .csv-->.xml
# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import csv
#xml保存的位置 需要创建该文件夹
save_xml_dir = "Annotations/"
#src_img_dir = "Intestinal Organoid Dataset/train"
#img_Lists = glob.glob(src_img_dir + '/*.jpg')
# read csv
file_path = "test_labels.csv"
width = 450
height = 450
with open(file_path) as csvfile:
#读取csv数据
csv_reader = csv.reader(csvfile)
#去掉第一行(第一行是列名)
csv_header = next(csv_reader)
#因为csv数据中有许多行其实是同一个照片,因此需要pre_img
pre_img = '' # 存储前一张图像的名称
for row in csv_reader:
#C:/Users/Timothy/Desktop/keras-retinanet/images/test/Subset_1_450x450_001.jpg
#只要文件名Subset_1_450x450_001
img = row[0].split("/")[-1].split(".")[0]
#遇到的是一张新图片
if img != pre_img:
#非第一张图片,在上一个xml中写下</annotation>
if pre_img != '':
xml_file1 = open((save_xml_dir + pre_img + '.xml'), 'a')
xml_file1.write('</annotation>')
xml_file1.close()
#新建xml文件
xml_file = open((save_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('<source>\n')
xml_file.write('<database> orgaquant </database>\n')
xml_file.write('<annotation> organoid </annotation>\n')
xml_file.write('</source>\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(' <object>\n')
xml_file.write('<name>'+str(row[-1])+'</name>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(row[1]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(row[2]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(row[3]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(row[4]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.close()
pre_img = img
else:
#同一张图片,只需要追加写入object
xml_file = open((save_xml_dir + pre_img + '.xml'), 'a')
xml_file.write(' <object>\n')
xml_file.write('<name>'+str(row[-1])+'</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')
xml_file.write(' <xmin>' + str(row[1]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(row[2]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(row[3]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(row[4]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.close()
pre_img = img
#csv表格最后一个xml需要写入</annotation>
xml_file1 = open((save_xml_dir + pre_img + '.xml'), 'a')
xml_file1.write('</annotation>')
xml_file1.close()
print("done!")
更多推荐
已为社区贡献2条内容
所有评论(0)