0x00 前言
本篇博文的代码主要修改自以前的一篇文章: Tensorflow应用之简单验证码识别
0x01 test_check_code.py
这个python 文件主要是实现了顺序读取训练集里面的内容并将其封装成一个函数供神经网络测试使用。
其中的 root_dir 参数要根据具体的训练文件的位置进行修改。
(代码如下)
import numpy as np
from PIL import Image
import os
root_dir = "d:\\jwxt\\test"
img_list = []
def gen_list():
for parent, dirnames, filenames in os.walk(root_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for filename in filenames: # 输出文件信息
img_list.append(filename.replace(".jpg", ""))
# print("parent is:" + parent)
# print("filename is:" + filename)
# print("the full name of the file is:" + os.path.join(parent, filename)) # 输出文件路径信息
return img_list
img_list = gen_list()
def get_test_captcha_text_and_image(i=None):
img = img_list[i]
captcha_image = Image.open(root_dir + "\\" + img + ".jpg")
# captcha_image = captcha_image.resize((160, 60))
captcha_image = np.array(captcha_image)
return img, captcha_image
def get_test_sets_length():
return len(img_list)
0x02 相关的代码说明
get_test_captcha_text_and_image 函数包含一个参数(i)
表示读取这个文件夹的第i张文件