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

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

目前演示的是四位验证码,里面所包含的字符类别有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/

相关推荐
2301_81852778几秒前
瑜伽服面料科技——AI加速创新材料研发
人工智能
键盘侠伍十七2 分钟前
Gandalf Lakera AI Prompt Injection 靶场深度教程:从 Level 1 到 Level 8 全面攻防解析
人工智能·prompt·ai安全
调试优选官2 分钟前
2026年上海GEO优化公司全景透视:技术路线、选型逻辑与实施路径
人工智能·技术分享·geo·上海
li-xun3 分钟前
2026年6月9日博客精选
人工智能·每日阅读
黑马师兄6 分钟前
RAG混合检索深度解析:让AI真正找到你要的内容
java·人工智能·ai·agent·rag·ai-native
哈伦20196 分钟前
第十二章 深度学习基础 案例:MLP实现银行单据手写数字识别
人工智能·深度学习·图像识别
右耳朵猫AI10 分钟前
GitHub周趋势2026W22 | AI编程工具、知识图谱、自托管、AI代理、代码智能
人工智能·github·ai编程
lqqjuly21 分钟前
MLA — 多头潜在注意力深度解析
深度学习·神经网络·算法
Black蜡笔小新23 分钟前
企业AI算力工作站DLTM深度学习推理工作站零代码私有化重塑企业AI落地新模式
人工智能·深度学习
2601_9594801535 分钟前
Moneta Markets亿汇:“比特币反弹走势仍脆弱”
人工智能