python 数据转 voc xml

4 篇文章 0 订阅
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
# fold path of save 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'
# fold path of image
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'
# path of txt
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'
# path of save image path and xml path
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 index and it's name
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')

### save_to_xml(save_path, im_width, im_height, im_depth, objects_axis, label_name):

# train set
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(' '))
        #print line.split(' ')
# test set
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()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值