dlib标签制作

dlib的标签的数据格式如下:

dlib标签制作_第1张图片

大多用于目标检测的标签的数据格式:(图片名  left_top_y  left_top_x  width  height),如果用于dlib进行模型训练,那么就需要进行数据格式转换,下面就是标签的转换程序:

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 10 17:52:11 2018

@author: Administrator
"""
import numpy as np

def convert(file_txt,file_xml,root_path):
    xml = open(file_xml,'w')
    xml.write("")
    xml.write("\n")
    xml.write("")
    xml.write("\n")
    xml.write("")
    xml.write("\n")
    xml.write("dlib face detect train")
    xml.write("\n")
    xml.write("")
    xml.write("\n")
    xml.write("")
    xml.write("\n")
    
    f = open(file_txt,"r")
    temp = []
    temp.append('0')
    
    for line in f.readlines():
        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()
        
        #同一张图片有多个目标
        if temp[-1] == filename:
            xml.write("    ".format(left_top_x,left_top_y,width,height))
            xml.write("\n")
        else: #读入一张新的图片
            if len(temp) > 1:
                xml.write("  ")
                xml.write("\n")
            
            xml.write("  ".format(root_path,filename))
            xml.write("\n")
            xml.write("    ".format(left_top_x,left_top_y,width,height))
            xml.write("\n")
        temp.append(filename)
        
    f.close()
    xml.write("  ")
    xml.write("\n")
    xml.write("")
    xml.write("\n")
    xml.write("")
    xml.close()


if __name__ == '__main__':
    convert('train.txt','train.xml','kk/')

代码下载:https://download.csdn.net/download/yongjiankuang/10657881

有不当之处,望指教!

你可能感兴趣的:(图像处理基础算法,python)