深度学习——基于Tensorflow的CAPTCHA注册码识别实验
本次人工智能实验3的内容就是验证码识别技术,因为实验时间有限,本人又是生生得拿自己电脑CPU来跑,所以训练数据集选择比较小,只选择了0-9的数字,4位为一组,如果条件允许,你?️可用的云平台、GPU之类的可以考虑加入大写字母、小写字母
提前声明一下,本代码是将三色图转换成灰度图处理,再进行识别,节省了时间,在正确率达到80%的时候就break掉啦,有更高要求或者需求不一样的童鞋可以自己加以更改。
下面是代码部分:生成验证码图片 文件名:gen_captcha.py
"""
function:识别captcha验证码技术
生成验证码函数
"""
from captcha.image import ImageCaptcha # pip install captcha_recognize
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
# 验证码中的字符, 就不用汉字了
number = ['0','1','2','3','4','5','6','7','8','9']
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','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','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
# 验证码一般都无视大小写;验证码长度4个字符 这里为节省演示时间 将alphabet ALPHABET注释掉
# def random_captcha_text(char_set=number+alphabet+ALPHABET, captcha_size=4):
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()
captcha_text = random_captcha_text()
captcha_text = ''.join(captcha_text) # 用字符['m', 'i', 'R', 'd']转化为miRd
captcha = image.generate(captcha_text) # 将captcha_text转化为图片
# image.write(captcha_text, captcha_text + '.jpg') # 写到文件
captcha_image = Image.open(captcha)
# captcha_image.show()