使用说明:
由于voc2007中包含有20类的图像数据,但我只拿来训练person类,所以利用darknet自带的脚本生成的train2.txt无法满足要求(直接使用会导致对应的图片中并没有此类,但参与了网络的训练),此代码用于清除掉train2中那些并不包含person类的图片地址。
代码如下:
#coding=utf-8
import osimport os.path
filePath ='/home/dlc/yolo/darknetyolov25/vocdata/VOCdevkit/VOC2007/labels'
list_save=[]
#遍历文件夹
#三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for parent ,dirnames , filenames in os.walk(filePath):
#输出文件夹信息
for dirname in dirnames:
print 'parent is :'+parent
print 'dirname is '+ dirname
#输出文件信息
for filename in filenames :
print 'parent is :'+parent
print 'filename is :' + filename
#输出文件路径信息
currentPath = os.path.join(parent,filename)
print 'the fulll name of the file is :'+ currentPath
filesize = os.path.getsize(currentPath)
print 'the file size is : %.3f bytes' %(filesize)
#删除大于50m的文件
if filesize <=0:
#os.remove(currentPath)
list_save.append(filename);
print list_save
list_num=[]
for list1 in list_save:
temp=list1[0:len(list1)-4]
temp2='.jpg'
temp3=temp+temp2
list_num.append(temp3)
print list_num
f2=open('/home/dlc/yolo/darknetyolov25/vocdata/VOCdevkit/VOC2007/train2.txt')
list_file = open('/home/dlc/yolo/darknetyolov25/vocdata/VOCdevkit/VOC2007/train.txt','wb')
for line in f2:
exist_flag=0;
for list_a in list_num:
if line.find(list_a)!=-1: #当这一行里面找到了相等的
exist_flag=1 #标志位置1
continue #不需要再找
if exist_flag==0:
list_file.write(line)
f2.close()
list_file.close()