python生成VOC2007的xml代码

来源:互联网 发布:php post 源代码 编辑:程序博客网 时间:2024/05/29 17:08

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

后来索性自己用python写了一个,代码粗糙,欢迎大家提意见

# !/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'xiaobanderui'from xml.dom import minidomimport numpy as npimport cv2imgpath = u'e:/VOC2007/JPEGImages/' # 图片存放路径txtpath = u'e:/data.txt' # 保存标注信息的文件,每一行的内容为[filename objectname xmin ymin xmax ymax]xml_path = u'e:/VOC2007/Annotations/'lastname = u'begin'foldername = u'VOC2007'num = 0 # 总样本数valid_num = 0 # 有效样本数cv2.namedWindow('img', cv2.WINDOW_NORMAL)with open(txtpath, 'r') as fr:    for item in fr.readlines():        num += 1        print 'processing %d' % num        item = item.strip().decode('gbk')        str_item = item.split(' ')        img =cv2.imread((imgpath + str_item[0]).encode('gbk'), cv2.IMREAD_UNCHANGED)        ht. wd. cn = img.shape        xmin = int(float(str_item[2]))        xmax = int(float(str_item[3]))        ymin = int(float(str_item[4]))        ymax = int(float(str_item[5]))        cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)        cv2.imshow('img', img)        cv2.waitKey()             # 过滤掉标注信息错误的样本        # 框的位置越界        if xmin >= xmax or ymin >= ymax or xmin < 0 or ymin < 0 or xmax < 0 or ymax < 0 or xmin >= wd or xmax >= wd or ymin >= ht or ymax >= ht        continue        # 框的位置不对,比如翻转时    oldx1 =xmin    oldx2 = xmax    newxmin = wd - oldx2 - 1    newxmax = wd - oldx1 - 1    if newxmin >= newxmax        continue     valid_num += 1    if str_item[0] == last_name:        object_node = doc.createElem('object')        rootNode = doc.appendChild(object_node)           node = doc.createElem('name')        node_text = doc.createTextNode(str_item[1])        node.appendChild(node_text)        object_ode = doc.appendChild(node)       '''        其他类似添加,不再赘述        '''    else:        if vars().has_key('rootNode'):            fw_xml = open(xml_path + file_full_name[0] + '.xml', 'w')            fw_xml.write(doc.toprettyxml(encoding='utf-8'))            fw_xml.close()        node = doc.createElem('name')        node_text = doc.createTextNode(str_item[1])        node.appendChild(node_text)        object_ode = doc.appendChild(node)    fw_xml = open(xml_path + file_full_name[0] + '.xml', 'w')    fw_xml.write(doc.toprettyxml(encoding='utf-8'))    fw_xml.close()








1 1