云从科技数据集处理,转为PASCAL VOC格式

import sys
import numpy
from tqdm import tqdm
from xml.etree import ElementTree as etree
from xml.etree.ElementTree import Element, SubElement, ElementTree
import shutil

核心代码

for line in tqdm(lines):
    
    
#     if len(line.strip(' ').split(' ')[0])<20:
#         imgnam = line.strip(' ').split(' ')[0][6::]
#         imgname = imgnam.replace('/','_')
#     else:
#         imgname = line.strip(' ').split(' ')[0][24::]
    print(line)
    imgname = line.strip(' ').split(' ')[0][16::]        ##########################3
#     print(line.strip(' ').split(' ')[0])
#     print(line)
#     print(imgnam)
#     imgname = r+imgnam                                    ##########################################3
#     print(imgname)
    
    head_cout = int(line.strip(' ').split(' ')[1])
#     print(head_cout)
    b = []
    c = int(len(line.strip(' ').split(' ')))
    for i in range(c-2):
        b.append(line.strip(' ').split(' ')[i+2])
      
    temp = []
    cout = 0
    for m in b:
        if cout%5 !=0:
            temp.append(m)
        cout = cout + 1
    tem = numpy.array(temp).reshape(-1,4)
    
#     print(tem)
    #定义xml
    root = Element('annotation')
    folder = SubElement(root,'folder')
    folder.text = "yuncong2018"
    filename = SubElement(root,'filename')
    filename.text = imgname
    
    source = SubElement(root,'source')
    database = SubElement(source, 'database')
#     annatation = SubElement(source, 'annatation')
    image = SubElement(source, 'image')
    
    size = SubElement(root, 'size')
    width = SubElement(size, 'width')
    
    height = SubElement(size, 'height')
    
    depth = SubElement(size, 'depth')
    depth.text = "3"
    
#     if len(line.strip(' ').split(' ')[0])<20:
#         width.text = "720"   
#         height.text = "576"
#     else:
#         width.text = "1280"
#         height.text = "720"

    width.text = "238"   ########################################
    height.text = "158"   ########################################
    
    segmented = SubElement(root,'segmented')
    
    segmented.text = "1"
    
    for heads in range(head_cout):
        object = SubElement(root,'object')
        name = SubElement(object, 'name')
        pose= SubElement(object,'pose')
        truncated = SubElement(object, 'truncated')
        difficult = SubElement(object, 'difficult')
        
        bndbox = SubElement(object, 'bndbox')
        xmin = SubElement(bndbox,'xmin')
        ymin = SubElement(bndbox,'ymin')
        xmax = SubElement(bndbox,'xmax')
        ymax = SubElement(bndbox,'ymax')
        
        name.text = 'head'
        pose.text = 'Unspecified'
        truncated.text = '1'
        difficult.text = '0'
        
#         print(heads)
#         print(int(tem[heads][0])+int(tem[heads][2]))
#         print(tem[heads][0].strip())
#         print(tem[heads][1].strip())
#         print(tem[heads][2].strip())
#         print(tem[heads][3].strip())
       
#         print('######')
        xmin.text = str(tem[heads][0])
        ymin.text = str(tem[heads][1])
        xmax.text = str(int(tem[heads][0])+int(tem[heads][2]))
        ymax.text = str(int(tem[heads][1])+int(tem[heads][3]))
    tree = ElementTree(root)
    try:
        tree.write(save_path+ imgname[:-4]+ '.xml',encoding = 'utf-8')
        x= x+1
#         print(x)
    except IOError:
        print('error')

猜你喜欢

转载自blog.csdn.net/weixin_43310928/article/details/83032617