将所有手指图片统一尺寸
import os
from PIL import Image
import sys
sys.path.append('/home/xm/桌面/finger_detection')
from utils import parse_voc_xml,mkdir_if_not_exist
class_names = ['finger']
os.environ['data_root'] = data_root = '/home/xm/桌面/finger_detection/data'
os.environ['im2rec']= "python /home/xm/桌面/finger_detection/im2rec.py"
resize = (512,512)
os.environ['resize'] = resize_str = str(resize[0]) + '_' + str(resize[1])
mkdir_if_not_exist(data_root+'/rev')
for imgName in os.listdir(data_root+'/img'):
imgPath = data_root + '/img/' + imgName
img = Image.open(imgPath)
img = img.resize(resize,Image.BILINEAR)
#print(data_root+'/'+'img%d_%d'%(resize[0],resize[1]))
mkdir_if_not_exist(data_root+'/'+'img%d_%d'%(resize[0],resize[1]))
img.save(data_root + '/'+'img%d_%d'%(resize[0],resize[1]) + '/' + imgName[:-4]+'png')
取其中80%作为训练集,20%作为测试集
os.system('$im2rec --list --train-ratio 0.8 ${data_root}/rev/img_$resize ${data_root}/img%d_%d'%(resize[0],resize[1]))
根据索引文件读取标签信息,生成检测图片训练的数据标签
new_lst_content = ''
with open(data_root + '/rev/img_%s_train.lst'%(resize_str)) as f:
contents = f.read().split('\n')
for content in contents:
if content == '':
break
content = content.split('\t')
idx = content[0]
#print('idx',idx)
file = content[-1][:-3] + 'xml'
#print('file',file)
# analyse .xml files
bndboxs, names, filename = parse_voc_xml(data_root + '/labels/' + file)
data = idx + '\t2\t5\t'
for bndbox, name in zip(bndboxs,names):
data += '%d\t%f\t%f\t%f\t%f\t'%(1,bndbox[0],bndbox[1],bndbox[2],bndbox[3])
# data += filename + '\t'
#print(data)
new_lst_content += data + filename + '.png' + '\n'
#print('*******************************',file[:-3]+'png')
#print('**********************',new_lst_content)
with open(data_root+'/rev/img_%s_train.lst'%(resize_str),'w') as f:
f.write(new_lst_content)
根据索引文件读取标签信息,生成检测图片验证的数据标签
new_lst_content1 = ''
with open(data_root + '/rev/img_%s_val.lst'%(resize_str)) as f:
contents = f.read().split('\n')
for content in contents:
if content == '':
break
content = content.split('\t')
idx = content[0]
file = content[-1][:-3] + 'xml'
print('file',file)
# analyse .xml files
bndboxs, names, filename = parse_voc_xml(data_root + '/labels/' + file)
data = idx + '\t2\t5\t'
for bndbox, name in zip(bndboxs,names):
data += '%d\t%f\t%f\t%f\t%f\t'%(1,bndbox[0],bndbox[1],bndbox[2],bndbox[3])
# data += filename + '\t'
#print(data)
new_lst_content1 += data + filename + '.png' + '\n'
#print('**********************',new_lst_content1)
with open(data_root+'/rev/img_%s_val.lst'%(resize_str),'w') as f:
f.write(new_lst_content1)
接着生成mxnet检测所需要的.rec文件类型
os.system('$im2rec --num-thread 10 --pass-through --pack-label $data_root/rev/img_$resize $data_root/img$resize --encoding=.png --quality 100')