码迷,mamicode.com
首页 > 其他好文 > 详细

把txt格式数据制作成xml数据

时间:2017-09-14 20:05:44      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:width   XML   分享   ase   specified   com   open   tab   truncate   

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数据

标签:width   XML   分享   ase   specified   com   open   tab   truncate   

原文地址:http://www.cnblogs.com/ymjyqsx/p/7522217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!