CSV文件格式
image_name,x1,y1,x2,y2,class
db1.jpg,6,1550,145,1704,object
db1.jpg,151,1545,288,1704,object
db1.jpg,291,1552,472,1704,object
db1.jpg,733,1600,863,1704,object
db1.jpg,881,1592,1015,1704,object
db10.jpg,865,592,1043,848,object
db10.jpg,1823,1228,1958,1442,object
db10.jpg,1147,1237,1323,1446,object
db10.jpg,1708,583,1840,832,object
转xml
# .csv-->.xml
# ! /usr/bin/python
# -*- coding:UTF-8 -*-
import os, sys
import glob
from PIL import Image
import csv
import os
import numpy as np
import random
import requests
# VEDAI 图像存储位置
src_img_dir = "/home/train/dataset-expand/park_voc/VOC2007/JPEGImages"
# VEDAI 图像的 ground truth 的 txt 文件存放位置
src_txt_dir = "/home/train/dataset-expand/label_expand"
src_xml_dir = "/home/train/dataset-expand/park_voc/VOC2007/Annotations"
img_Lists = glob.glob(src_img_dir + '/*.jpg')
# read csv 1 without width and height
file_path = "WebMarket_coco_gt_object.csv"
with open(file_path) as csvfile:
csv_reader = csv.reader(csvfile) # 使用csv.reader读取csvfile中的文件
birth_header = next(csv_reader) # 读取第一行每一列的标题
count = 0
img_pre = ''
flag = 0
for row in csv_reader: # 将csv 文件中的数据保存到birth_data中
#print(row)
#first
if flag == 0:
img_pre = row[0].split('.')[0]
flag = 1
xml_file = open(('./WebMarket_coco_gt_object/' + img_pre + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img_pre) + '.jpg' + '</filename>\n')
img = row[0].split('.')[0]
# new file
if img != img_pre:
# close file
xml_file.write('</annotation>')
xml_file.close()
# new file
xml_file = open(('./WebMarket_coco_gt_object/' + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img) + '.jpg' + '</filename>\n')
#print(img)
'''
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[5]) + '</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')