from xml.dom.minidom import Document
def save_to_xml(save_path, im_width, im_height, im_depth, objects_axis, label_name):
object_num = len(objects_axis)
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder_name = doc.createTextNode('VOC2007')
folder.appendChild(folder_name)
annotation.appendChild(folder)
filename = doc.createElement('filename')
filename_name = doc.createTextNode('000024.jpg')
filename.appendChild(filename_name)
annotation.appendChild(filename)
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
database.appendChild(doc.createTextNode('The VOC2007 Database'))
source.appendChild(database)
annotation_s = doc.createElement('annotation')
annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007'))
source.appendChild(annotation_s)
image = doc.createElement('image')
image.appendChild(doc.createTextNode('flickr'))
source.appendChild(image)
flickrid = doc.createElement('flickrid')
flickrid.appendChild(doc.createTextNode('322409915'))
source.appendChild(flickrid)
owner = doc.createElement('owner')
annotation.appendChild(owner)
flickrid_o = doc.createElement('flickrid')
flickrid_o.appendChild(doc.createTextNode('knautia'))
owner.appendChild(flickrid_o)
name_o = doc.createElement('name')
name_o.appendChild(doc.createTextNode('yang'))
owner.appendChild(name_o)
size = doc.createElement('size')
annotation.appendChild(size)
##需要修改的就是这部分,宽高
width = doc.createElement('width')
width.appendChild(doc.createTextNode(str(im_width)))
height = doc.createElement('height')
height.appendChild(doc.createTextNode(str(im_height)))
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode(str(im_depth)))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode('0'))
annotation.appendChild(segmented)
##需要添加目标
for i in range(object_num):
objects = doc.createElement('object')
annotation.appendChild(objects)
object_name = doc.createElement('name')
object_name.appendChild(doc.createTextNode(label_name[int(objects_axis[i][4])]))
objects.appendChild(object_name)
pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode('Unspecified'))
objects.appendChild(pose)
truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode('1'))
objects.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode('0'))
objects.appendChild(difficult)
bndbox = doc.createElement('bndbox')
objects.appendChild(bndbox)
xmin = doc.createElement('xmin')
xmin.appendChild(doc.createTextNode((objects_axis[i][0])))
bndbox.appendChild(xmin)
ymin = doc.createElement('ymin')
ymin.appendChild(doc.createTextNode((objects_axis[i][1])))
bndbox.appendChild(ymin)
xmax = doc.createElement('xmax')
xmax.appendChild(doc.createTextNode((objects_axis[i][2])))
bndbox.appendChild(xmax)
ymax = doc.createElement('ymax')
ymax.appendChild(doc.createTextNode((objects_axis[i][3])))
bndbox.appendChild(ymax)
f = open(save_path,'w')
f.write(doc.toprettyxml(indent = ''))
f.close()
import cv2 as cv
from save_xml import save_to_xml
train_save_prefix = '/home/yang/Public/hand_dataset/hand_dataset/train_xml'
test_save_prefix = '/home/yang/Public/hand_dataset/hand_dataset/test_xml'
train_path_prefix = '/home/yang/Public/hand_dataset/hand_dataset/training_dataset/training_data/images'
test_path_prefix = '/home/yang/Public/hand_dataset/hand_dataset/test_dataset/test_data/images'
train_file = '/home/yang/Public/hand_dataset/hand_dataset/train_list_mark_xy.txt'
test_file = '/home/yang/Public/hand_dataset/hand_dataset/test_list_mark_xy.txt'
train_image_xml = '/home/yang/Public/hand_dataset/hand_dataset/train_anno.txt'
test_image_xml = '/home/yang/Public/hand_dataset/hand_dataset/test_anno.txt'
label_name = {0:'hand'}
fid_train = open(train_file)
fid_test = open(test_file)
fid_train_image_xml = open(train_image_xml,'w')
fid_test_image_xml = open(test_image_xml,'w')
lines = fid_train.readlines()
objects_axis = []
save_path = []
im_width = 0
im_height = 0
im_depth = 0
for line in lines:
line = line.strip()
if line.find('.') != -1:
print 'train '+line
if len(objects_axis) != 0:
save_to_xml(save_path, im_width, im_height, im_depth, objects_axis, label_name)
image_path = train_path_prefix + '/' + line
save_path = train_save_prefix+'/'+line.split('.')[0]+'.xml'
image = cv.imread(image_path)
fid_train_image_xml.writelines(image_path + ' ' + save_path + '\n')
(im_height, im_width, im_depth) = image.shape
objects_axis = []
else:
objects_axis.append(line.split(' '))
lines = fid_test.readlines()
objects_axis = []
save_path = []
im_width = 0
im_height = 0
im_depth = 0
for line in lines:
line = line.strip()
if line.find('.') != -1:
print 'test '+line
if len(objects_axis) != 0:
save_to_xml(save_path, im_width, im_height, im_depth, objects_axis, label_name)
image_path = test_path_prefix + '/' + line
save_path = test_save_prefix + '/' + line.split('.')[0] + '.xml'
fid_test_image_xml.writelines(image_path+' '+save_path+'\n')
image = cv.imread(image_path)
(im_height, im_width, im_depth) = image.shape
objects_axis = []
else:
objects_axis.append(line.split(' '))
fid_train.close()
fid_test.close()
fid_train_image_xml.close()
fid_test_image_xml.close()