#coding:utf-8
import urllib2
from PIL import Image
from PIL import ImageFilter
import os,sys
def getImage():
'''网上获取图片'''
global time,pixels
url = ''
fname = 'validatecode.bmp'
open(fname,'wb').write(urllib2.urlopen(url).read())
img = Image.open(fname).convert('L')
return img
def getMatrix(i):
'''把模板文件读入数组'''
global cache,time
if i in cache.keys():
return cache[i]
arr = [[0 for col in range(6)] for row in range(10)]
f = open('%d.txt'%i)
for y in range(10):
l = f.readline()
for x in range(6):
arr[y][x] = int(l[x])
cache[i] = arr
return arr
def recognize(img,i):
'''返回img和数字i的识别率rate'''
rate = 0
arr = img.load()
for x in range(6):
for y in range(10):
if arr[x,y]<128 and getMatrix(i)[y][x]==0:
rate += 1
elif arr[x,y]>128 and getMatrix(i)[y][x]==1:
rate += 1
return rate
os.chdir('./')
cache = {}
img = getImage()
left = 7
up = 5
right = 13
down = 15
for i in range(4):
#把图片切割成仅容下数字的四个图片
im = img.crop((left,up,right,down))
#下面8行是识别过程
rec_n = -1
rec_rate = -1
for n in range(10):
t = recognize(im,n)
if t > rec_rate:
rec_n = n
rec_rate = t
sys.stdout.write('%d'%rec_n)
#下面注释行是学习过程
#im.save('%d.bmp'%i)
#txt = open('%d.try.txt'%i,'w')
#arr = im.load()
#for y in range(10):
#for x in range(6):
#if arr[x,y] > 128:
#txt.write('1')
#else:
#txt.write('0')
#txt.write(os.linesep)
#txt.close()
left += 13
right += 13
print ' '
raw_input('Press enter key to continue...')
|
首先把学习过程解注释掉,并运行学习过程,这样会在同目录下生成1.bmp:1.try.txt 2.bmp:2.try.txt 3.bmp:3.try.txt 4.bmp:4.try.txt四对文件,*.bmp的模板文件是*.try.txt,将*.try.txt改成*.bmp的字样数字(比如*.bmp的字样是数字5,就把*.try.txt改成5.txt)。重复上面的过程,知道0.txt 1.txt .... 9.txt都有了,这样模板就识别完毕。
然后运行识别过程就可以了。
阅读(2381) | 评论(0) | 转发(0) |