鱼C论坛

 找回密码
 立即注册
查看: 4664|回复: 21

[技术交流] python小练习(088):【番外篇】不用机器学习的方法进行数字校验码的自动识别

[复制链接]
发表于 2017-4-7 11:47:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jerryxjr1220 于 2017-4-7 14:33 编辑

前面几章都是介绍利用keras搭建的神经网络进行机器的自动学习。

有鱼油反应,机器学习太复杂了,那么有没有办法不用神经网络进行这样的数字图片的自动识别呢?

答案是有的,我们可以利用kNN(k邻近算法)直接比较数字图片的像素点来进行判别。

先讲原理,比如,我有一张数字图片“2”,那么无论这张图片左偏一点或者右转一点,凡是“2”的图片上的像素点与所有其他“2”的图片的像素点的平方差应该是最小的。当然,这个假设是分类的图片足够简单,太复杂的分类kNN算不了。

我们以python小练习(087)的图片为例,我用kNN算法进行了100组数据的测试,结果如下:
Test  0 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  1 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  2 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  3 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  4 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  5 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  6 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  7 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  8 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  9 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  10 : Predict X: 9 , Actural is: 0 , Result is: False
--------------------------------------------------
Test  11 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  12 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  13 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  14 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  15 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  16 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  17 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  18 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  19 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  20 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  21 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  22 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  23 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  24 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  25 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  26 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  27 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  28 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  29 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  30 : Predict X: 9 , Actural is: 0 , Result is: False
--------------------------------------------------
Test  31 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  32 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  33 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  34 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  35 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  36 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  37 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  38 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  39 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  40 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  41 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  42 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  43 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  44 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  45 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  46 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  47 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  48 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  49 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  50 : Predict X: 9 , Actural is: 0 , Result is: False
--------------------------------------------------
Test  51 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  52 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  53 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  54 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  55 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  56 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  57 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  58 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  59 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  60 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  61 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  62 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  63 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  64 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  65 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  66 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  67 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  68 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  69 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  70 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  71 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  72 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  73 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  74 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  75 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  76 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  77 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  78 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  79 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  80 : Predict X: 9 , Actural is: 0 , Result is: False
--------------------------------------------------
Test  81 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  82 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  83 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  84 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  85 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  86 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  87 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  88 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  89 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
Test  90 : Predict X: 0 , Actural is: 0 , Result is: True
--------------------------------------------------
Test  91 : Predict X: 1 , Actural is: 1 , Result is: True
--------------------------------------------------
Test  92 : Predict X: 2 , Actural is: 2 , Result is: True
--------------------------------------------------
Test  93 : Predict X: 3 , Actural is: 3 , Result is: True
--------------------------------------------------
Test  94 : Predict X: 4 , Actural is: 4 , Result is: True
--------------------------------------------------
Test  95 : Predict X: 5 , Actural is: 5 , Result is: True
--------------------------------------------------
Test  96 : Predict X: 6 , Actural is: 6 , Result is: True
--------------------------------------------------
Test  97 : Predict X: 7 , Actural is: 7 , Result is: True
--------------------------------------------------
Test  98 : Predict X: 8 , Actural is: 8 , Result is: True
--------------------------------------------------
Test  99 : Predict X: 9 , Actural is: 9 , Result is: True
--------------------------------------------------
可以看到,100组数据中有4组错误,其他96组都正确,正确率达到96%。
而且错误的都是把“9”和“0”搞错了(确实“9”和“0”如果加上旋转的话,是会比较像的)。

源代码及注解如下:(源代码中,除了numpy和PIL,没有用掉任何第三方库,也不需要使用神经网络)
游客,如果您要查看本帖隐藏内容请回复

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-7 14:58:28 | 显示全部楼层
厉害啊,学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-10 19:48:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-21 23:28:40 | 显示全部楼层
厉害厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-26 10:49:08 | 显示全部楼层
u 2 nb la
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-31 21:54:42 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-17 10:35:31 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-18 12:43:44 | 显示全部楼层
学习!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-3 20:43:49 | 显示全部楼层
66666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 00:57:05 | 显示全部楼层
观摩
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-29 15:51:02 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-18 22:31:59 | 显示全部楼层
特意过来学习python, 同时学习神经网络, 最近要用到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-10 11:16:48 | 显示全部楼层
go
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 16:37:58 | 显示全部楼层
回复
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 16:54:44 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 19:47:21 From FishC Mobile | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-19 09:33:38 | 显示全部楼层
谢谢分享,支持
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 10:22:20 | 显示全部楼层
xx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-8 17:20:02 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-14 19:08:02 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 10:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表