python脚本制作dlib数据集

来源:互联网 发布:聊城气象数据 编辑:程序博客网 时间:2024/06/09 19:13

前提:你已经得到了预选框范围,来自动制作dlib数据集(xml),从而省去imglab花框过程
预选框文件内容格式(图片名,左上角x,左上角y,右下角x,右下角y),如:left_88.png 294 170 115 190
python脚本:

root_path="'/home/ubuntu/sotfware/dlib-18.16-new/examples/tiaotong/"k=open('a.xml','w')k.write("<?xml version='1.0' encoding='ISO-8859-1'?>")k.write("\n")k.write("<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>")k.write("\n")k.write("<dataset>")k.write("\n")k.write("<name>imglab dataset</name>")k.write("\n")k.write("<comment>Created by imglab tool.</comment>")k.write("\n")k.write("<images>")k.write("\n")f=open('11.txt','r')c=[]c.append('0')for line in open('11.txt'):    #line=f.readline()    filename=line.split(' ')[0]    left_top_x=line.split(' ')[2]    left_top_y=line.split(' ')[1]    width=line.split(' ')[3]    height=line.split(' ')[4].strip()    #print height    if c[-1]==filename:        k.write("    <box top='{0}' left='{1}' width='{2}' height='{3}'/>".format(left_top_x,left_top_y,width,height))        k.write("\n")    else:        if len(c)>1:            k.write("  </image>")            k.write("\n")        k.write("  <image file={0}{1}'>".format(root_path,filename))        k.write("\n")        k.write("    <box top='{0}' left='{1}' width='{2}' height='{3}'/>".format(left_top_x,left_top_y,width,height))        k.write("\n")    c.append(filename)f.close()k.write("  </image>")k.write("\n")k.write("</images>")k.write("\n")k.write("</dataset>")k.close()

路径自行修改,执行该脚本,即可得到a.xml

原创粉丝点击