In this article, we share the example of SVM handwritten digit recognition function of the specific code for your reference, the details are as follows
1、SVM handwritten number recognition
Identify the steps:
(1) Preparation of sample images.
(2) Image size standardization: standardize all image sizes to 8*8 size.
(3) Read the unknown sample image, extract image features, and generate an image feature set.
(4) The unknown test sample image feature set is fed into the SVM for testing and the results of the test are output.
Identification Code:
#!/usr/bin/env python import numpy as np import mlpy import cv2 print 'loading ...' def getnumc(fn): '''Return numeric features''' fnimg = (fn) #Read the image img=(fnimg,(8,8)) # Resize the image to 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 (nowtz) alltz+=xtz return alltz #Read sample numbers x=[] y=[] for numi in xrange(1,10): for numij in xrange(1,5): fn='nums/'+str(numi)+'-'+str(numij)+'.png' (getnumc(fn)) (numi) x=(x) y=(y) svm = (svm_type='c_svc', kernel_type='poly',gamma=10) (x, y) print u"Training Sample Testing:" print (x) print u"Unknown Image Test:" for iii in xrange (1,10): testfn= 'nums/test/'+str(iii)+'-' testx=[] (getnumc(testfn)) print print testfn+":", print (testx)
Sample:
Results:
This is the entire content of this article.