前面讲到了INRIA数据集转换为VOC2007数据集格式:点击打开链接,本文和前面有点类似,只是做了进一步的精简,有点画蛇添足的感觉。不过还是希望能够帮到大家!因为比较简单,下面直接上代码。
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 4 15:28:10 2018
@author: kuangyongjian
func:generator box label
"""
import os
import re
#原始训练集标签存储信息的目录,这里可以根据需要改成自己的目录
lists = os.listdir("F:/za/Model/pedestrain/INRIAPerson/Train/annotations")
#如果为非.txt文件,则跳过
for l in lists:
if str(".txt") not in l:
continue
#保存文件的路径
newfilename = 'label/' + l
#读取文件的路径,根据需要改成自己的目录
l = "F:/za/Model/pedestrain/INRIAPerson/Train/annotations/" + l
f = open(l,"r")
#写入文件的具体路径名称
ff = open(newfilename,'w')
#按行遍历文件寻找关键信息
fr = f.readlines()
for line in fr:
if(str(line).__contains__('Objects')):
nums = re.findall('\d+',line) #每幅图像包含了多少个object
break
#将每个object对应的box信息写入文件
for index in range(1,int(nums[0]) + 1):
for line in fr:
if str(line).__contains__("Bounding box for object " + str(index)):
coordinate = re.findall('\d+', line)
print('coordinate = ',1)
count = 0
for c in coordinate:
if(count == 0):
count += 1
continue
c = c + ' '
ff.write(c)
count += 1
f.close()
ff.close()
若有不当之处,请指教,谢谢!