python识别手写文字_Python3实现简单可学习的手写体识别(实例讲解)

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主函数模块运行程序。

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

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

(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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现文字的准确识别,可以使用深度学习模型,例如卷积神经网络(CNN)。以下是使用TensorFlow和Keras构建的简单CNN模型的代码示例: ```python import tensorflow as tf from tensorflow import keras # 加载数字数据集MNIST (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # 将像素值缩放到0到1之间 x_train = x_train / 255.0 x_test = x_test / 255.0 # 添加一个颜色通道维度 x_train = x_train[..., tf.newaxis] x_test = x_test[..., tf.newaxis] # 构建CNN模型 model = keras.Sequential([ keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)), keras.layers.MaxPooling2D(), keras.layers.Flatten(), keras.layers.Dense(10) ]) # 编译模型 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # 训练模型 model.fit(x_train, y_train, epochs=5) # 测试模型 test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2) print('\nTest accuracy:', test_acc) ``` 在此示例中,我们使用MNIST数据集进行训练和测试。首先,我们将像素值缩放到0到1之间,并将数据集中的每个图像添加一个颜色通道维度。然后,我们构建一个具有卷积层、池化层和全连接层的CNN模型。最后,我们编译模型,并在训练数据上拟合5个时期。最终,我们评估模型在测试数据上的性能,并打印出测试准确性。 请注意,这只是一个简单的示例,实际上要实现更准确的文字识别可能需要更复杂的CNN模型和更大的数据集。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值