1. 程式人生 > >把txt格式數據制作成xml數據

把txt格式數據制作成xml數據

div pan extra for pre max () images eight

txt格式數據:

技術分享

代碼:

s1="""    <object>
        <name>{0}</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>{1}</xmin>
            <ymin>{2}</ymin>
            <xmax>{3}</xmax>
            <ymax>{4}</ymax>
        </bndbox>
    </object>
""" s2="""<annotation> <folder>VOC2007</folder> <filename>{0}</filename> <source> <database>My Database</database> <annotation>VOC2007</annotation> <image>flickr</image> <flickrid>NULL</flickrid> </source> <owner> <flickrid>NULL</flickrid> <name>J</name> </owner> <size> <width>960</width> <height>600</height> <depth>3</depth> </size> <segmented>0</segmented> <object> <name>{1}</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>{2}</xmin> <ymin>{3}</ymin> <xmax>{4}</xmax> <ymax>{5}</ymax> </bndbox> </object>{6} </annotation>
""" with open(/home/xingyuzhou/Desktop/test_GT.txt,r) as textlist: for text in textlist: text = text.strip().split() image_name = text[0] image_num = int(text[1]) ob2 = ‘‘ x1 = car x3 = text[2:6] if image_num > 1: # extra annotation
for i in range(2,image_num+1): y = text[4*(i-1)+2:4*i+2] ob2+=\n + s1.format(x1,y[0],y[1],y[2],y[3]) savename = image_name.split(.)[0] + .xml file = open(/home/xingyuzhou/data/%s%savename,w) # with open(‘/home/bnrc/data/%s‘%savename,‘a‘) as file: ob1=s2.format(image_name, x1, x3[0],x3[1],x3[2],x3[3], ob2) file.write(ob1) file.close()

把txt格式數據制作成xml數據