本部分是生成简单的4位数字验证码,及将验证码生成TFRecord.
1.生成数字验证码
#验证码生成
from captcha.image import ImageCaptcha #pip install captcha
import numpy as np
from PIL import Image
import random
import sys
number = ['0','1','2','3','4','5','6','7','8','9']
#alphabet = ['a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
def random_captcha_text(char_set=number,captcha_size=4):
#验证码列表
captcha_text=[]
for i in range(captcha_size):
#随机选择
c = random.choice(char_set)
#加入验证码列表
captcha_text.append(c)
return captcha_text
#生成字符对应的验证码
def gen_captcha_text_and_image():
image = ImageCaptcha() #默认高度60,宽度160
captcha_text = random_captcha_text()
captcha_string = ''.join(captcha_text)
#生成验证码
captcha = image.generate(captcha_string)
image.write(captcha_string,'captcha/images/'+ captcha_string + '.jpg') #写到文件
#数量少于10000,因为重名
num = 10000
if __name__=='__main__':
for i in range(num):
gen_captcha_text_and_image()
sys.stdout.write('\r>> Creating image %d/%d ' %(i+1,num))
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
print('生成完毕')
实际生成了6386张不重复的验证码:
2.将图片转化为TFRecord
import tensorflow as tf
import os
import random
import math
import sys
from PIL import Image
import numpy as np
# 验证集数量
_NUM_TEST = 500
#s随机种子
_RANDOM_SEED = 0
#数据集路径
DATASET_DIR = "D:/Jupyter_path/TFstudy2019/captcha/images/"
#tfrecord文件存放路径
TFRECORD_DIR = "D:/Jupyter_path/TFstudy2019/captcha"
#判断tfrecord文件是否存在
def _dataset_exists(dataset_dir):
for split_name in ["train","test"]:
output_filename = os.path.join(dataset_dir,split_name+'.tfrecords')
if not tf.gfile.Exists(output_filename):
return False
return True
#获取所有文件及分类
def _get_filenames_and_classes(dataset_dir):
photo_filenames = []
#循环每个分类的文件夹
for filename in os.listdir(dataset_dir):
#
path = os.path.join(dataset_dir,filename) #此filename是图片名字
#把图片加入图片列表
photo_filenames.append(path)
return photo_filenames #photo_filenames中保存的是图片的绝对路径
def int64_feature(values):
if not isinstance(values,(tuple,list)):
values = [values]
return tf.train.Feature(int64_list=tf.train.Int64List(value=values))
def bytes_feature(values):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))
tf.train.Feature
def image_to_tfexample(image_data,label0,label1,label2,label3):
#abatract base class for protocol messages
return tf.train.Example(features=tf.train.Features(feature={
'image':bytes_feature(image_data),
'label0':int64_feature(label0),
'label1':int64_feature(label1),
'label2':int64_feature(label2),
'label3':int64_feature(label3),
}))
#把数据转换为TFRecord格式
def _convert_dataset(split_name,filenames,dataset_dir):
assert split_name in ['train','test']
with tf.Session() as sess:
#定义tfrecords文件的路径+名字
output_filename = os.path.join(TFRECORD_DIR,split_name+'.tfrecords')
with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
for i,filename in enumerate(filenames):
try: #将损坏的图片跳过
sys.stdout.write('\r>> Converting image %d%d' % (i+1,len(filenames)))
sys.stdout.flush()
#读取图片
image_data = Image.open(filename)
#根据模型的结构resize
image_data = image_data.resize((224,224))
#灰度化
image_data = np.array(image_data.convert('L'))
#将图片转化为bytes
image_data = image_data.tobytes()
#获取label
#print('\n filename:'+filename+'\n')
labels = filename.split('/')[-1][0:4]
#print('\n labels:'+labels+'\n')
num_labels = []
for j in range(4):
num_labels.append(int(labels[j]))
#生成protocol数据模型
example = image_to_tfexample(image_data,num_labels[0],num_labels[1],num_labels[2],num_labels[3])
tfrecord_writer.write(example.SerializeToString())
except IOError as e:
print("could not read:",filenames[i])
print("Error:",e)
print("skip it\n")
sys.stdout.write('\n')
sys.stdout.flush()
if __name__ == '__main__':
#判断tfrecord文件是否存在
if _dataset_exists(DATASET_DIR):
print('tfrecord文件已存在')
else:
#获得所有图片及分类
photo_filenames = _get_filenames_and_classes(DATASET_DIR)
#把数据切分成训练集和数据集
random.seed(_RANDOM_SEED)
random.shuffle(photo_filenames)
training_filenames = photo_filenames[_NUM_TEST:]
testing_filenames = photo_filenames[:_NUM_TEST]
#数据转化
_convert_dataset('train',training_filenames,DATASET_DIR)
_convert_dataset('test',testing_filenames,DATASET_DIR)
print('生成tfrecord文件')
程序运行完毕后生成了训练集和测试集的tfrecord: