1.将图片命名为六位的形式:000001:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon May 21 17:19:35 2018
@author: zbin
"""
import os
class BatchRename():
'''''
批量重命名文件夹中的图片文件
'''
def __init__(self):
#我的图片文件夹路径horse
self.path = '/home/zbin/deeplearning_API/Detection_API/research/object_detection/voc/VOCdevkit/VOC2007/JPEGImages'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 1
n = 6
for item in filelist:
if item.endswith('.jpg'):
n = 6 - len(str(i))
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), str(0)*n + str(i) + '.jpg')
try:
os.rename(src, dst)
print 'converting %s to %s ...' % (src, dst)
i = i + 1
except:
continue
print 'total %d to rename & converted %d jpgs' % (total_num, i)
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
2.使用labelimg工具将图片生成.xml文件保存在Annocation文件夹中:
(1)打开labelimg使用的命令:到labelImg-master中修改类别,在原作者的github下载源码压缩包,解压可得到名为 labelImg-master的文件夹,进入该文件夹,进入当前目录的命令行窗口,依次输入下面语句即可打开软件。
pyrcc4 -o resources.py resources.qrc
python labelImg.py
3.将.xml分成train.txt val.txt trainval.txt test.txt 四个文件:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri May 18 22:48:38 2018
@author: zbin
"""
import os
import random
trainval_percent = 0.66
train_percent = 0.5
xmlfilepath = '/home/zbin/Make_image/VOC2007/Annotations'
textsavepath = '/home/zbin/Make_image/VOC2007/Annotations/Main'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num*trainval_percent)
tr = int(tv*train_percent)
trainval = random.sample(list,tv)
train = random.sample(trainval,tr)
ftrainval = open('/home/zbin/Make_image/VOC2007/ImageSets/Main/trainval.txt','w')
ftest = open('/home/zbin/Make_image/VOC2007/ImageSets/Main/test.txt','w')
ftrain = open('/home/zbin/Make_image/VOC2007/ImageSets/Main/train.txt','w')
fval = open('/home/zbin/Make_image/VOC2007/ImageSets/Main/val.txt','w')
for i in list:
name = total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
注意!!!!!: 在使用create_pascal_tf_record.py文件时,注意查看.xml生成的文件中floder的值若不是VOC2007需要手动修改代码。另外在保存.txt文件时不要手贱多按enter否则会在生成list时出现空的.xml文件报错!!!!!
接下来就可以进行训练了!