手写字体识别用python实现_Python3实现简单可学习的手写体识别

0.目录

1.前言

1.前言

版本:Python3.6.1 + PyQt5 + SQL Server 2012

以前一直觉得,机器学习、手写体识别这种程序都是很高大上很难的,直到偶然看到了这个视频,听了老师讲的思路后,瞬间觉得原来这个并不是那么的难,原来我还是有可能做到的。

于是我开始顺着思路打算用Python、PyQt、SQLServer做一个出来,看看能不能行。然而中间遇到了太多的问题,数据库方面的问题有十几个,PyQt方面的问题有接近一百个,还有数十个Python基础语法的问题。但好在,通过不断的Google,终于凑出了这么一个成品来。

最终还是把都凑在一个函数里的代码重构了一下,改写成了4个模块:

main.py、Learning.py、LearningDB.py、LearningUI.py

其中LearningDB实现python与数据库的交互,LearningUI实现界面的交互,Learning继承LearningUI类添加上了与LearningDB数据库类的交互,最后通过main主函数模块运行程序。

其中涉及数据库的知识可参考之前的文章:Python3操作SQL Server数据库,涉及PyQt的知识可参考:Python3使用PyQt5制作简单的画板/手写板

手写体识别的主要思路是将手写的字,用一个列表记录其所经过的点,划分为一个九宫格,然后数每个格子中点的数目,将数目转化为所占总点数的百分比。然后两两保存的九维数,求他们之间的距离,距离越近代表越接近。

2.通过pymssql与数据库的交互

因为使用程序之前先需要建表,建表我就直接使用SQL语句执行了:

create database PyLearningDB

drop table table0

create table table0

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table1

create table table1

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table2

create table table2

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table3

create table table3

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table4

create table table4

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table5

create table table5

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table6

create table table6

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table7

create table table7

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table8

create table table8

(dim0 int not null,

dim1 int not null,

dim2 int not null,

dim3 int not null,

dim4 int not null,

dim5 int not null,

dim6 int not null,

dim7 int not null,

dim8 int not null)

drop table table9

create table table9

(di

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是基于Python实现LeNet5的MNIST字体识别的步骤: 1. 数据准备:下载MNIST数据集,包括训练集和测试集。可以使用Python库`tensorflow_datasets`来下载和加载MNIST数据集。 ```python import tensorflow_datasets as tfds # 加载MNIST数据集 mnist_dataset = tfds.load(name="mnist", split=tfds.Split.TRAIN) mnist_test_dataset = tfds.load(name="mnist", split=tfds.Split.TEST) ``` 2. 数据预处理:对数据进行标准化处理,并将训练集和测试集转换为合适的格式。 ```python def preprocess_data(data): # 将数据转换为浮点数类型 data['image'] = tf.cast(data['image'], tf.float32) # 标准化处理 data['image'] = tf.divide(data['image'], 255.0) # 调整形状 data['image'] = tf.reshape(data['image'], [-1, 28, 28, 1]) # 将标签转换为One-hot编码 data['label'] = tf.one_hot(data['label'], depth=10) return data # 对训练集和测试集进行预处理 mnist_dataset = mnist_dataset.map(preprocess_data) mnist_test_dataset = mnist_test_dataset.map(preprocess_data) # 将训练集转换为可迭代的数据集 batch_size = 32 mnist_dataset = mnist_dataset.batch(batch_size) # 将测试集转换为可迭代的数据集 mnist_test_dataset = mnist_test_dataset.batch(batch_size) ``` 3. 构建LeNet5模型:使用TensorFlow构建LeNet5模型,包括卷积层、池化层和全连接层。 ```python from tensorflow.keras import layers, models # 构建LeNet5模型 model = models.Sequential([ layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dense(units=120, activation='relu'), layers.Dense(units=84, activation='relu'), layers.Dense(units=10, activation='softmax') ]) ``` 4. 编译模型:定义损失函数、优化器和评估指标。 ```python # 定义损失函数、优化器和评估指标 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) ``` 5. 训练模型:使用训练集训练模型,并在测试集上进行评估。 ```python epochs = 10 history = model.fit(mnist_dataset, epochs=epochs, validation_data=mnist_test_dataset) ``` 6. 可视化训练过程:使用Matplotlib库可视化训练过程。 ```python import matplotlib.pyplot as plt # 可视化训练过程 plt.plot(history.history['accuracy'], label='training accuracy') plt.plot(history.history['val_accuracy'], label='validation accuracy') plt.title('Training and Validation Accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend() plt.show() ``` 7. 预测结果:使用训练好的模型对新的数字进行预测。 ```python import numpy as np # 加载新的数字图片 new_image = plt.imread('new_image.png') # 将图片转换为灰度图像 new_image = np.dot(new_image[...,:3], [0.299, 0.587, 0.114]) # 调整形状 new_image = np.reshape(new_image, (1, 28, 28, 1)) # 标准化处理 new_image = new_image / 255.0 # 对新的数字进行预测 prediction = model.predict(new_image) # 打印预测结果 print(np.argmax(prediction)) ``` 以上就是基于Python实现LeNet5的MNIST字体识别的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值