python 数据转 voc xml

原创 2017年01月03日 20:09:35
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()
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

python生成VOC2007的xml代码

之前在网上下了一份matlab版本的代码,在遇到中文路径写入时,使用gVim打开出现了乱码,弄了一下午不知道怎么回事,后来发现好像是gVim环境配置没有写支持'utf-8'导致,纠结了一下午。。。 后...

python 处理pascal voc数据 读取xml文件

Pascal VOC数据的annotation是xml文件,要利用xml文件里的标注信息裁剪出数据~~

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

VOC2007数据集制作——createXML

#-*- coding:utf-8 -*- ''' 该代码是将数据转为VOC2007,该代码实现了XML生成和图片位置更改以及文件重命名 ''' import xml.dom import x...
  • yjl9122
  • yjl9122
  • 2017年02月24日 15:17
  • 869

Python生成PASCAL VOC格式的xml标注文件

转自点击打开链接 Python生成PASCAL VOC格式的xml标注文件 PASCAL VOC数据集的标注文件是xml格式的。对于py-faster-rcnn,通常以下示例的字段是合适的: ...

Faster RCNN 训练自己的数据集(Matlab,python版本)及制作VOC2007格式数据集

一、 faster rcnn环境搭建  下载源码 https://github.com/rbgirshick/py-faster-rcnn Python版本  https://github.com/S...

PASCAL Annotation转换为VOC xml格式数据

本代码可以把PASCAL Annoation Verson 1.00的 txt标记文件 转换为VOC格式的xml文件,以便数据训练。 前提:得到 PASCAL Annotation Version...

把faster-rcnn检测出来的结果保存成txt,再转成xml

利用faster-rcnn检测图片,先把结果保存成txt,就像下面这样 利用下面这段代码就可以做到,把这段代码保存成XX.py,再运行。代码里需要改的地方都注释了,不知道怎么上传源码的,将就着用...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

使用Python将TXT文本内容读取后生成指定XML格式的文件

任务说明: 解析服务器内存和CPU信息。格式是无后缀的文本格式。将其转换为统一XML格式。源文件名:zmonitor.2017-04-06-14_28 172.17.8.64 saptmqas...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:python 数据转 voc xml
举报原因:
原因补充:

(最多只允许输入30个字)