1. 爬取验证码图片
from urllib import request
def download_pics(pic_name):
url = 'http://wsbs.zjhz.hrss.gov.cn/captcha.svl'
res = request.urlopen(url)
get_img = res.read()
with open('/Users/luacheng/project/python/image/vcode/%s.jpg' % (pic_name), 'wb') as f:
f.write(get_img)
if __name__ == '__main__':
for i in range(100):
pic_name = i
download_pics(pic_name)
2. 二值化
接下来要做的工作就是二值化验证码,所谓二值化,就是将每一个像素点用0或1来表示,图像的每个像素点都有rgb三个值,我们首先转化成灰度图,这样每个像素点就只有一个灰度值了。接下来根据自己设定的阈值来确定每个像素点是该为0还是为1。
我的思路是首先将图像转化为array处理,当然完全可以直接图像处理。
from PIL import Image
import numpy
def binarization(im): # 二值化
imgry = im.convert('L')
imgry = numpy.array(imgry) # 将图像转化为数组
height, width = imgry.shape
f = open('1.txt', 'w')
for i in range(height):
for j in range(width):
gray = imgry[i, j]
if gray <= 220: # 阈值设为220
imgry[i, j] = 0
else:
imgry[i, j] = 1
f.write(str(imgry[i,j])) #输出到txt查看
f.write('
')
'''
plt.figure('')
plt.imshow(imgry, cmap='gray')
plt.axis('off')
plt.show()
'''
return imgry
if __name__ == '__main__':
img = Image.open('/Users/luacheng/project/python/image/vcode/1.jpg')
binarization(img)
在二值化处理之后,处理结果如下所示:
3 图片分割
接下来要做的就是将这四个字符分割开来形成训练集,这个操作并不难。因为这些验证码的位置都是差不多的,如果验证码字符位置比较乱的话就会比较麻烦
1 def cutImg(img): #图像切割
2 s = 12
3 w = 40
4 h = 81
5 t = 0
6 cut_img = []
7 for i in range(4):
8 pic = img.crop((s + w * i, t, s + w * (i + 1), h))
9 cut_img.append(pic)
10 return cut_img
4 图片分类
这个步骤的目的就是人为的给训练集打上标签。 将相同的数字放在同一个文件夹下面
5 训练模型
训练模型很简单,因为直接就是使用libsvm库,我们只需要按照数据格式生成一些特征值即可
1 import os
2 from PIL import *
3 from PIL import Image
4 import numpy as np
5 from libsvm.python.svmutil import *
6 from libsvm.python.svm import *
7
8
9 address = 'D:python验证码-sort\'
10 f = open('train.txt', 'w')
11
12 def get_feature(dir, file):
13 f.write(dir)
14 im = Image.open(address + dir +'\' + file)
15 imarr = np.array(im)
16 height, width = imarr.shape
17 for i in range(height):
18 for j in range(width):
19 gray = imarr[i,j]
20 if gray <= 150:
21 imarr[i, j] = 0
22 else:
23 imarr[i, j] = 255
24 im = Image.fromarray(imarr)
25 count = 0
26 width, height = im.size
27 for i in range(height):
28 c = 0
29 for j in range(width):
30 if im.getpixel((j, i)) == 0: c += 1
31 f.write(' %d:%d'%(count, c))
32 count += 1
33 for i in range(width):
34 c = 0
35 for j in range(height):
36 if im.getpixel((i, j)) == 0: c += 1
37 f.write(' %d:%d'%(count, c))
38 count += 1
39 f.write('
')
40
41 def train_svm_model():
42 y, x = svm_read_problem('train.txt')
43 model = svm_train(y, x)
44 svm_save_model('model_file', model)
45
46 if __name__ == '__main__':
47 dirs = os.listdir(address)
48 for dir in dirs:
49 files = os.listdir(address + dir)
50 for file in files:
51 get_feature(dir, file)
52 train_svm_model()
6 测试模型
用测试数据对模型进行测试
1 from libsvm.python.svmutil import *
2 from libsvm.python.svm import *
3 import image_slove
4
5 if __name__ == '__main__':
6 model = svm_load_model('model_file')
7 yt, xt = svm_read_problem('test.txt')
8 p_label, p_acc, p_val = svm_predict(yt, xt, model)