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

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

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

相关推荐
AI_yangxi18 分钟前
短视频矩阵系统选哪家
大数据·人工智能·矩阵
Shockang6 小时前
AI Slop 治理实战
人工智能
寒霜雨刃7 小时前
【原创】海光Z100 DCU适配vLLM实录(二):W4A16大M Prefill结构重写、GDN融合与长上下文HIP Attention
深度学习·神经网络·amd·rocm·ai infra·海光z100·gfx906
Mr数据杨7 小时前
医学影像分类实战复盘 从 Kaggle 竞赛到可落地建模流程
人工智能·数据分析·kaggle竞赛
AI情绪识别开源8 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
ZGIAI8 小时前
ZGI 迭代节点:批量资料的逐项处理
人工智能·架构
ZGIAI9 小时前
ZGI 知识检索:让业务回答有据可查
人工智能·架构
Asize9 小时前
框架的说明书是写给 AI 看的:我用 Next.js 搭了个博客
人工智能·代码规范·next.js
2601_955662469 小时前
AI 配音工具 7 款实测:短视频、影视解说、小说推文音质横向对比
人工智能·音视频·语音识别·视频