python实现基于SVM手写数字识别功能
作者:阳光下的Smiles 时间:2021-10-03 12:33:41
本文实例为大家分享了SVM手写数字识别功能的具体代码,供大家参考,具体内容如下
1、SVM手写数字识别
识别步骤:
(1)样本图像的准备。
(2)图像尺寸标准化:将图像大小都标准化为8*8大小。
(3)读取未知样本图像,提取图像特征,生成图像特征组。
(4)将未知测试样本图像特征组送入SVM进行测试,将测试的结果输出。
识别代码:
#!/usr/bin/env python
import numpy as np
import mlpy
import cv2
print 'loading ...'
def getnumc(fn):
'''返回数字特征'''
fnimg = cv2.imread(fn) #读取图像
img=cv2.resize(fnimg,(8,8)) #将图像大小调整为8*8
alltz=[]
for now_h in xrange(0,8):
xtz=[]
for now_w in xrange(0,8):
b = img[now_h,now_w,0]
g = img[now_h,now_w,1]
r = img[now_h,now_w,2]
btz=255-b
gtz=255-g
rtz=255-r
if btz>0 or gtz>0 or rtz>0:
nowtz=1
else:
nowtz=0
xtz.append(nowtz)
alltz+=xtz
return alltz
#读取样本数字
x=[]
y=[]
for numi in xrange(1,10):
for numij in xrange(1,5):
fn='nums/'+str(numi)+'-'+str(numij)+'.png'
x.append(getnumc(fn))
y.append(numi)
x=np.array(x)
y=np.array(y)
svm = mlpy.LibSvm(svm_type='c_svc', kernel_type='poly',gamma=10)
svm.learn(x, y)
print u"训练样本测试:"
print svm.pred(x)
print u"未知图像测试:"
for iii in xrange (1,10):
testfn= 'nums/test/'+str(iii)+'-test.png'
testx=[]
testx.append(getnumc(testfn))
print testfn+":",
print svm.pred(testx)
样本:
结果:
来源:http://blog.csdn.net/liyuqian199695/article/details/54236092
标签:python,SVM,数字识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Idea开发工具之SpringBoot整合JSP的过程
2023-06-14 19:50:23
![](https://img.aspxhome.com/file/2023/2/69272_0s.png)
Python中Tkinter组件Menu的具体使用
2023-09-11 12:46:26
![](https://img.aspxhome.com/file/2023/7/94147_0s.png)
运行python脚本更改Windows背景
2022-06-11 05:36:54
![](https://img.aspxhome.com/file/2023/3/91923_0s.png)
python3字符串输出常见面试题总结
2021-01-13 08:39:23
python中实现迭代器(iterator)的方法示例
2021-07-12 19:32:41
![](https://img.aspxhome.com/file/2023/9/64639_0s.png)
13个最常用的Python深度学习库介绍
2023-08-04 03:08:09
JavaScript版的DateAdd、DateDiff、IsDate函数
2008-01-30 15:35:00
python实现邮件自动发送
2023-06-10 16:24:44
基于python(urlparse)模板的使用方法总结
2022-10-08 19:56:50
python基础知识之try...except...的详细用法实例
2022-12-26 10:21:27
Gradio机器学习模型快速部署工具应用分享前篇
2023-07-23 12:10:45
![](https://img.aspxhome.com/file/2023/5/62845_0s.jpg)
Django 自定义分页器的实现代码
2023-06-20 15:21:03
![](https://img.aspxhome.com/file/2023/3/118533_0s.gif)
python 与服务器的共享文件夹交互方法
2021-02-10 14:29:02
python 正则式使用心得
2021-09-17 14:39:49
五种Python转义表示法
2021-12-22 03:24:01
![](https://img.aspxhome.com/file/2023/3/128593_0s.jpg)
selenium+python实现自动登录脚本
2021-09-30 01:36:19
苹果Macbook Pro13 M1芯片安装Pillow的方法步骤
2021-01-11 18:23:46
python实现通讯录管理系统
2022-01-31 08:25:31
Python爬虫之爬取二手房信息
2021-08-11 19:40:50
![](https://img.aspxhome.com/file/2023/9/68869_0s.jpg)
利用golang的字符串解决leetcode翻转字符串里的单词
2023-07-17 16:36:21
![](https://img.aspxhome.com/file/2023/8/99048_0s.jpg)