基于深度学习神经网络的验证码识别系统

第一步:建立验证码数据库

目前演示的是四位验证码,里面所包含的字符类别有62种

第二步:搭建模型

本文利用一个简单的cnn模型,进行端到端识别:

python 复制代码
class CNN(nn.Module):
    def __init__(self, num_class=62, num_char=4):
        super(CNN, self).__init__()
        self.num_class = num_class
        self.num_char = num_char
        self.conv = nn.Sequential(
                #batch*3*120*40
                nn.Conv2d(3, 16, 3, padding=(1, 1)),
                nn.MaxPool2d(2, 2),
                nn.BatchNorm2d(16),
                nn.ReLU(),
                #batch*16*60*20
                nn.Conv2d(16, 64, 3, padding=(1, 1)),
                nn.MaxPool2d(2, 2),
                nn.BatchNorm2d(64),
                nn.ReLU(),
                #batch*64*30*10
                nn.Conv2d(64, 512, 3, padding=(1, 1)),
                nn.MaxPool2d(2, 2),
                nn.BatchNorm2d(512),
                nn.ReLU(),
                #batch*512*15*5
                nn.Conv2d(512, 512, 3, padding=(1, 1)),
                nn.MaxPool2d(2, 2),
                nn.BatchNorm2d(512),
                nn.ReLU(),
                #batch*512*7*2
                )
        self.fc = nn.Linear(512*7*2, self.num_class*self.num_char)
        
    def forward(self, x):
        x = self.conv(x)
        x = x.view(-1, 512*7*2)
        x = self.fc(x)
        return x

第三步:进行识别

读入一张验证码图片,然后通过模型进行推理,直接预测出结果来:

python 复制代码
import torch
import torch.nn as nn
from Testmodel import CNN
from datasets import CaptchaData
from torchvision.transforms import Compose, ToTensor
import pandas as pd
import os
from PIL import Image
import matplotlib.pyplot as plt

model_path = './checkpoints/model.pth'
imgpath = '13.jpg'

source = [str(i) for i in range(0, 10)]
source += [chr(i) for i in range(97, 97+26)]
source += [chr(i) for  i in range(65,65+26)]
alphabet = ''.join(source)

def run():
        img = Image.open(imgpath)
        img = img.resize((120,40))
        trans = ToTensor()
        img_tensor = trans(img)
        cnn = CNN()
        # if torch.cuda.is_available():
        #     cnn = cnn.cuda()
        #     cnn.eval()
        #     cnn.load_state_dict(torch.load(model_path))
        # else:
        cnn.eval()
        model = torch.load(model_path, map_location='cpu')
        cnn.load_state_dict(model)
        img_tensor = img_tensor.view(1, 3, 40, 120)
        output = cnn(img_tensor)
        output = output.view(-1, 62)
        output = nn.functional.softmax(output, dim=1)
        output = torch.argmax(output, dim=1)
        output = output.view(-1, 4)[0]
        label = ''.join([alphabet[i] for i in output.cpu().numpy()])
        print("label:",label)



if __name__=="__main__":
    run()

第四步:运行结果

第六步:整个工程的内容

提供整套代码,包括训练和测试代码,也提供训练测试数据

项目完整文件下载请见演示与介绍视频的简介处给出:➷➷➷

https://www.bilibili.com/video/BV16eUeYtEac/

相关推荐
那个村的李富贵5 小时前
光影魔术师:CANN加速实时图像风格迁移,让每张照片秒变大师画作
人工智能·aigc·cann
腾讯云开发者7 小时前
“痛点”到“通点”!一份让 AI 真正落地产生真金白银的实战指南
人工智能
CareyWYR7 小时前
每周AI论文速递(260202-260206)
人工智能
hopsky7 小时前
大模型生成PPT的技术原理
人工智能
禁默8 小时前
打通 AI 与信号处理的“任督二脉”:Ascend SIP Boost 加速库深度实战
人工智能·信号处理·cann
心疼你的一切8 小时前
昇腾CANN实战落地:从智慧城市到AIGC,解锁五大行业AI应用的算力密码
数据仓库·人工智能·深度学习·aigc·智慧城市·cann
AI绘画哇哒哒8 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
数据分析能量站9 小时前
Clawdbot(现名Moltbot)-现状分析
人工智能
那个村的李富贵9 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
二十雨辰9 小时前
[python]-AI大模型
开发语言·人工智能·python