Python + selenium + Tesseract 识别验证码

Tesseract就是开源引擎里的执牛耳项目。起源于惠普实验室并在1984-1994年间大力发展的Tesseract,曾一度可与商业OCR软件比肩。2005年,HP在GoogleCode上将Tesseract开源,2006年开始由谷歌赞助,并由Ray Smith负责该项目。

Tesseract支持的语言非常多,每种语言有相应的语言包可以下载。但是从中文的情况来看,貌似效果不是太好,识别率几乎不可商用。相关论文中提到中文的准确率在95%以上,不知道实际的测试样本是怎么样的。英文准确率还不错,可以直接拿来用。这也是为什么这篇文章只能用于简单的中英文验证码。

# -*- coding: utf-8 -*-
from selenium import webdriver
from PIL import Image, ImageDraw
import time
import pytesseract
import re


#目标网站
PostUrl = "http://vgumoney.club"
#Firfox驱动要指定excutable_path
driver=webdriver.Firefox(executable_path='C:\\Users\\user\\geckodriver.exe')
driver.get(PostUrl)

elem_user = driver.find_element_by_name('username')
elem_user.send_keys('xxx') 
elem_psw = driver.find_element_by_name('password')
elem_psw.send_keys('xxx')

#点击登录
click_first = driver.find_element_by_name('button')
click_first.click()

time.sleep(1)
#登陆后选择
click_second = driver.find_element_by_xpath("//*[@id='main']/h2[2]/div/input")
click_second.click()
code_list = []
while True:
    now_handle = driver.current_window_handle
    all_handle = driver.window_handles
    try:
        for handle in all_handle:
            if handle != now_handle:
                alert = driver.switch_to_alert() #识别弹窗处理
                time.sleep(1)
                alert.accept()
            else:
                imgelement = driver.find_element_by_id('cimg1')
                location = imgelement.location
                size = imgelement.size
                time_id = str(time.time())[-7:-3]
                savepath = r'image\codingpy_' + time_id + '.png'
                driver.save_screenshot(savepath)
                im = Image.open(savepath)
                left = location['x']
                top = location['y']
                right = left + 4*size['width']
                bottom = location['y'] + size['height']
                im = im.crop((left, top, right, bottom)) #截取验证码图片区域
                im.save(savepath)

                def getPixel(image, x, y, G, N):
                    L = image.getpixel((x, y))
                    if L > G:
                        L = True
                    else:
                        L = False
                    nearDots = 0
                    if L == (image.getpixel((x - 1, y - 1)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x - 1, y)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x - 1, y + 1)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x, y - 1)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x, y + 1)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x + 1, y - 1)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x + 1, y)) > G):
                        nearDots += 1
                    if L == (image.getpixel((x + 1, y + 1)) > G):
                        nearDots += 1
                    if nearDots < N:
                        return image.getpixel((x, y - 1))
                    else:
                        return None

                def clearNoise(image, G, N, Z):
                    draw = ImageDraw.Draw(image)
                    for i in range(0, Z):
                        for x in range(1, image.size[0] - 1):
                            for y in range(1, image.size[1] - 1):
                                color = getPixel(image, x, y, G, N)
                                if color != None:
                                    draw.point((x, y), color)


                img = Image.open(savepath)
                img = img.convert('L') #图像灰度化
                clearNoise(img, 127.5, 2, 4) #降噪
                standard = 117
                pixel = img.load()
                #二值化
                for x in range(img.width):
                    for y in range(img.height):
                        if pixel[x, y] > standard:
                            pixel[x, y] = 255
                        else:
                            pixel[x, y] = 0
                img.save(savepath)
                
                testdata_dir_config = '--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'
                text_code = pytesseract.image_to_string(Image.open(savepath), lang='eng', config=testdata_dir_config)
                text_Code = re.sub("\W", "", text_code)
                #识别长度不为4的,做textCode替换为空
                if len(text_Code) != 4:
                    text_Code = ''
                else:
                    text_Code = text_Code.replace("A", '1') #有一定几率将1识别为A
                capcha = driver.find_element_by_xpath("//*[@id='main']/form/div[2]/input")
                capcha.send_keys(text_Code)
                click_foruth = driver.find_element_by_xpath("//*[@id='main']/form/div[3]/input")
                click_foruth.click()
    except BaseException as e:
        driver.refresh()







你可能感兴趣的:(Python + selenium + Tesseract 识别验证码)