zoukankan      html  css  js  c++  java
  • 把txt格式数据制作成xml数据

    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+='
    ' + 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()
  • 相关阅读:
    python错误信息 object is not subscriptable 的原因
    python join函数
    string的部分总结
    第k个非立方数(忘记哪里的题了)
    pandas 学习
    数学建模中 时间序列典型分解模型 matlab实现
    matlab三维画图学习 三次插值
    原型
    JavaScript
    js数组去重(多种写法)
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/7522217.html
Copyright © 2011-2022 走看看