制作手指检测数据集

将所有手指图片统一尺寸

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')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值