0

0

PaddleOCR:基于 MNIST 数据集的手写多数字识别

P粉084495128

P粉084495128

发布时间:2025-07-21 11:33:15

|

850人浏览过

|

来源于php中文网

原创

本文介绍利用MNIST数据集构建多数字识别模型的过程。先通过预处理MNIST数据,拼接生成含多个数字的训练集和测试集;接着安装PaddleOCR及依赖,下载预训练模型;然后训练模型并导出;最后采样测试图片,用导出的模型进行识别测试。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

paddleocr:基于 mnist 数据集的手写多数字识别 - php中文网

VWO
VWO

一个A/B测试工具

下载

引入

  • 传统的基于 MNIST 数据集的手写数字识别模型只能识别单个数字
  • 但实际使用环境中,多数字识别才是更加常见的情况
  • 本次就使用 MNIST 数据集,通过拼接数据的方式,实现多数字识别模型

构建数据集

  • 拼接采样数据集
In [ ]
%cd ~
!mkdir dataset 
!mkdir dataset/train
!mkdir dataset/testimport cv2import randomimport numpy as npfrom tqdm import tqdmfrom paddle.vision.datasets import MNIST# 加载数据集mnist_train = MNIST(mode='train', backend='cv2')
mnist_test = MNIST(mode='test', backend='cv2')# 数据集预处理datas_train = {}for i in range(len(mnist_train)):
    sample = mnist_train[i]
    x, y = sample[0], sample[1]

    _sum = np.sum(x, axis=0)
    _where = np.where(_sum > 0)
    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_train:
        datas_train[str(y[0])].append(x)    else:
        datas_train[str(y[0])] = [x]

datas_test = {}for i in range(len(mnist_test)):
    sample = mnist_test[i]
    x, y = sample[0], sample[1]

    _sum = np.sum(x, axis=0)
    _where = np.where(_sum > 0)
    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_test:
        datas_test[str(y[0])].append(x)    else:
        datas_test[str(y[0])] = [x]# 图片拼接采样datas_train_list = []for num in tqdm(range(0, 999)):    for _ in range(1000):
        imgs = [255 - np.zeros((28, np.random.randint(10)))]        for word in str(num):
            index = np.random.randint(0, len(datas_train[word]))
            imgs.append(datas_train[word][index])
            imgs.append(255 - np.zeros((28, np.random.randint(10))))
        img = np.concatenate(imgs, 1)
        cv2.imwrite('dataset/train/%03d_%04d.jpg' % (num, _), img)
        datas_train_list.append('train/%03d_%04d.jpg\t%d\n' % (num, _, num))

datas_test_list = []for num in tqdm(range(0, 999)):    for _ in range(50):
        imgs = [255 - np.zeros((28, np.random.randint(10)))]        for word in str(num):
            index = np.random.randint(0, len(datas_test[word]))
            imgs.append(datas_test[word][index])
            imgs.append(255 - np.zeros((28, np.random.randint(10))))
        img = np.concatenate(imgs, 1)
        cv2.imwrite('dataset/test/%03d_%04d.jpg' % (num, _), img)
        datas_test_list.append('test/%03d_%04d.jpg\t%d\n' % (num, _, num))# 数据列表生成with open('dataset/train.txt', 'w') as f:    for line in datas_train_list:
        f.write(line)with open('dataset/test.txt', 'w') as f:    for line in datas_test_list:
        f.write(line)
   

数据样例展示

PaddleOCR:基于 MNIST 数据集的手写多数字识别 - php中文网        

安装 PaddleOCR

In [ ]
!git clone https://gitee.com/PaddlePaddle/PaddleOCR -b release/2.1 --depth 1
   

安装依赖环境

In [ ]
!pip install imgaug pyclipper lmdb Levenshtein
   

下载预训练模型

In [ ]
%cd ~/PaddleOCR

!wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_pre.tar
!cd pretrain_models && tar -xf ch_ppocr_mobile_v2.0_rec_pre.tar && rm -rf ch_ppocr_mobile_v2.0_rec_pre.tar
   

模型训练

In [8]
%cd ~/PaddleOCR

!python tools/train.py -c ../multi_mnist.yml
   

模型导出

In [34]
%cd ~/PaddleOCR

!python3 tools/export_model.py \
    -c ../multi_mnist.yml -o Global.pretrained_model=../output/multi_mnist/best_accuracy \
    Global.load_static_weights=False \
    Global.save_inference_dir=../inference/multi_mnist
   

采样测试图片

In [45]
%cd ~/PaddleOCR
!mkdir ~/test_imgsimport cv2import randomimport numpy as npfrom tqdm import tqdmfrom paddle.vision.datasets import MNIST# 加载数据集mnist_test = MNIST(mode='test', backend='cv2')# 数据集预处理datas_test = {}for i in range(len(mnist_test)):
    sample = mnist_test[i]
    x, y = sample[0], sample[1]

    _sum = np.sum(x, axis=0)
    _where = np.where(_sum > 0)
    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_test:
        datas_test[str(y[0])].append(x)    else:
        datas_test[str(y[0])] = [x]# 图片拼接采样for num in range(0, 1000):
    imgs = [255 - np.zeros((28, np.random.randint(10)))]    for word in str(num):
        index = np.random.randint(0, len(datas_test[word]))
        imgs.append(datas_test[word][index])
        imgs.append(255 - np.zeros((28, np.random.randint(10))))
    img = np.concatenate(imgs, 1)
    cv2.imwrite('../test_imgs/%03d.jpg' % num , img)
   

模型测试

In [46]
%cd ~/PaddleOCR

!python tools/infer/predict_rec.py \
    --image_dir="../test_imgs" \
    --rec_model_dir="../inference/multi_mnist/" \
    --rec_image_shape="3, 28, 64" \
    --rec_char_type="ch" \
    --rec_char_dict_path="../label_list.txt"
   

相关专题

更多
Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

11

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

4

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

16

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

2

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

6

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

25

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

7

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 11.1万人学习

Django 教程
Django 教程

共28课时 | 3.3万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号