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

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

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

相关推荐
飞哥数智坊9 小时前
我的“龙虾”罢工了!正好对比下GLM、MiniMax、Kimi 3家谁更香
人工智能
风象南10 小时前
很多人说,AI 让技术平权了,小白也能乱杀老师傅 ?
人工智能·后端
董董灿是个攻城狮11 小时前
大模型连载1:了解 Token
人工智能
RoyLin14 小时前
沉睡三十年的标准:HTTP 402、生成式 UI 与智能体原生软件的时代
人工智能
needn16 小时前
TRAE为什么要发布SOLO版本?
人工智能·ai编程
毅航16 小时前
自然语言处理发展史:从规则、统计到深度学习
人工智能·后端
前端付豪16 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
ursazoo16 小时前
写了一份 7000字指南,让 AI 帮我消化每天的信息流
人工智能·开源·github
_志哥_20 小时前
Superpowers 技术指南:让 AI 编程助手拥有超能力
人工智能·ai编程·测试
YongGit21 小时前
OpenClaw 本地 AI 助手完全指南:飞书接入 + 远程部署实战
人工智能