在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract软件来识别验证码(数字加字母)。
我们在网上浏览网页或注册账号时,会经常遇到验证码(CAPTCHA),如下图:


本文将具体介绍如何利用Python的图像处理模块pillow和OCR模块pytesseract来识别上述验证码(数字加字母)。
我们识别上述验证码的算法过程如下:
- 将原图像进行灰度处理,转化为灰度图像;
- 获取图片中像素点数量最多的像素(此为图片背景),将该像素作为阈值进行二值化处理,将灰度图像转化为黑白图像(用来提高识别的准确率);
- 去掉黑白图像中的噪声,噪声定义为:以该点为中心的九宫格的黑点的数量小于等于4;
- 利用pytesseract模块识别,去掉识别结果中的特殊字符,获得识别结果。
我们的图片如下(共66张图片):

完整的Python代码如下:
import os
import pytesseract
from PIL import Image
from collections import defaultdict
pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)/Tesseract-OCR/tesseract.exe'
def get_threshold(image):
pixel_dict = defaultdict(int)
rows, cols = image.size
for i in range(rows):
for j in range(cols):
pixel = image.getpixel((i, j))
pixel_dict[pixel] += 1
count_max = max(pixel_dict.values())
pixel_dict_reverse = {v:k for k,v in pixel_dict.items()}
threshold = pixel_dict_reverse[count_max]
return threshold
def get_bin_table(threshold):
table = []
for i in range(256):
rate = 0.1
if threshold*(1-rate)<= i <= threshold*(1+rate):
table.append(1)
else:
table.append(0)
return table
def cut_noise(image):
rows, cols = image.size
change_pos = []
for i in range(1, rows-1):
for j in range(1, cols-1):
pixel_set = []
for m in range(i-1, i+2):
for n in range(j-1, j+2):
if image.getpixel((m, n)) != 1:
pixel_set.append(image.getpixel((m, n)))
if len(pixel_set) <= 4:
change_pos.append((i,j))
for pos in change_pos:
image.putpixel(pos, 1)
return image
def OCR_lmj(img_path):
image = Image.open(img_path)
imgry = image.convert('L')
max_pixel = get_threshold(imgry)
table = get_bin_table(threshold=max_pixel)
out = imgry.point(table, '1')
out = cut_noise(out)
text = pytesseract.image_to_string(out)
exclude_char_list = ' .:\\|\'\"?![],()~@#$%^&*_+-={};<>/¥'
text = ''.join([x for x in text if x not in exclude_char_list])
return text
def main():
dir = 'E://figures'
correct_count = 0
total_count = 0
for file in os.listdir(dir):
if file.endswith('.png') or file.endswith('.jpg'):
image_path = '%s/%s'%(dir,file)
answer = file.split('.')[0]
recognizition = OCR_lmj(image_path)
print((answer, recognizition))
if recognizition == answer:
correct_count += 1
total_count += 1
print('Total count: %d, correct: %d.'%(total_count, correct_count))
'''
# 单张图片识别
image_path = 'E://figures/code (1).jpg'
OCR_lmj(image_path)
'''
main()
运行结果如下:
('101659', '101659')
('111073', '111073')
('114510', '114510')
('118235', '118235')
('124677', '124677')
('147291', '147291')
('169147', '169147')
('185302', '185302')
('23YB', '23YB')
('262051', '262051')
('2HED', '2MED')
('315386', '315386')
('3D7K', '3D7K')
('3DYH', '3DYH')
('3QG8', '30G8')
('3XNR', 'EXNR')
('44G5', '44G5')
('470259', '470259')
('515413', '515413')
('522351', '522351')
('539824', '539824')
('5CVL', 'SCVL')
('642689', '642689')
('671991', '671991')
('672838', '672838')
('6F5Y', '6F5Y')
('6USB', 'GUSB')
('703167', '703167')
('765120', '765120')
('779931', '779931')
('8UEF', '8SUEF')
('905857', '905857')
('9H4H', '9H4H')
('9SK1', 'OSK1')
('BDP4', 'BDP4')
('DXV3', 'DXV3')
('E78Y', 'E78Y')
('EAHR', 'EAHR')
('F585', 'Fss§')
('FBV8', 'FBV8')
('FJKK', 'FJKK')
('GXKQ', 'GXKQ')
('H7Y9', 'H7Y9')
('J4LJ', 'J4LJ')
('J8YH', 'J8YH')
('JCDL', 'JCDL')
('JTX2', 'JTX2')
('JYLH', 'JYLH')
('KFYA', 'KFYA')
('L3VZ', 'L3VZ')
('LCGV', 'LCGV')
('LKEK', 'LKEK')
('N3FJ', 'N3FJ')
('PJZN', 'PJZN')
('PNDQ', 'PNDQ')
('Q7HP', 'Q7HP')
('QSHU', 'QSHU')
('R1RN', 'RLRN')
('RPNX', 'RPNX')
('TUKG', 'TUKG')
('U9G3', 'U9G3')
('UZAH', 'UZAH')
('V6P9', 'very')
('Y18D', '18D')
('Y237', 'Y237')
('ZZT5', '2215')
Total count: 66, correct: 54.
我们可以看到图片识别的正确率为80%以上,其中数字类图片的识别正确率为100%.
我们可以在图片识别方面的算法再加改进,以提高图片识别的正确率。当然,以上算法并不是对所有验证码都适用,不同的验证码需要用不同的图片处理算法。
注意:本人现已开通两个微信公众号: 因为Python(微信号为:python_math)以及轻松学会Python爬虫(微信号为:easy_web_scrape), 欢迎大家关注哦~~