利用百度ai接口识别验证码

#coding=utf8

from selenium import webdriver

import pickle

import time

from PIL import Image

from PIL import ImageEnhance

from aip import AipOcr

def baidu_image_to_word(image_path):

    """ 你的 APPID AK SK """

    APP_ID = '14364432'

    API_KEY = 'jgopMYaecGeGgaBr2EYWKNDZ'

    SECRET_KEY = 'TnpKrHyyc3IgrGw2L5ZzKRiY9F2seCSk'

    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    with open(image_path, 'rb') as fp:

        image = fp.read()


    """ 调用通用文字识别, 图片参数为本地图片 """

    client.basicGeneral(image);

    """ 如果有可选参数 """

    options = {}

    options["language_type"] = "ENG"

    options["detect_direction"] = "true"#是否检测图像朝向,默认不检测,即:false

    ##options["detect_language"] = "true"#是否检测语言,默认不检测

    options["probability"] = "true"#是否返回识别结果中每一行的置信度

    """ 带参数调用通用文字识别, 图片参数为本地图片 """


    res = client.basicAccurate(image, options)#通用文字识别(高精度版),普通版是client.basicGeneral(image, options);

    ##""" 调用通用文字识别, 图片参数为远程url图片 """

    ##url = "https//www.x.com/sample.jpg"

    ##client.basicGeneralUrl(url,options);

    try:

        guess = res['words_result'][0]['words']

        probability = res['words_result'][0]['probability']['average']

    except:

        print("识别失败,将置信度归为0,文字为空")

        guess = '';probability=0;

    return guess,probability,res

browser = webdriver.Chrome()

browser.get('http://icrm.baidu.com/crm-portal/index.action')

browser.maximize_window()

time.sleep(2)

image = browser.find_element_by_xpath("//*[@id='imgCodeId']")

location = image.location# 获取验证码x,y轴坐标

size = image.size # 获取验证码的长宽

x_begin = int(location['x'])+5;

y_begin = int(location['y']);

my_with = size['width'] - 10

rangle = (x_begin,

          y_begin,

          x_begin+my_with,

          y_begin+size['height']) # 写成我们需要截取的位置坐标

probability = 0;count = 0;count_max = 20;words_num = 0

##图片文字识别的置信度大于0.9的时候才说明识别得比较准确,否则刷新验证码重新识别

while (probability<0.9 or words_num!=4) and count

    if count>0:

        browser.find_element_by_xpath("//*[@id='imgCodeId']").click()

        time.sleep(2)


    count += 1

    browser.save_screenshot('All.png') # 截取当前网页,该网页有我们需要的验证码

    save_image = Image.open("All.png") # 打开截图

    result = save_image.crop(rangle)

    result.save('result.jpg')

    ##增强图形识别率的处理

    i2=Image.open(r"result.jpg")

    imgry = i2.convert('L')  #图像加强,二值化,PIL中有九种不同模式。分别为1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。L为灰度图像

    sharpness =ImageEnhance.Contrast(imgry)#对比度增强

    i3 = sharpness.enhance(3.0)  #3.0为图像的饱和度

    i3.save("result.png")

    ##连接api获取返回结果

    guess,probability,res = baidu_image_to_word('result.png') #连接百度api

    words_num = len(guess)

    print('第%d次猜测验证码,猜测结果为%s,猜测验证码个数为%d,置信度平均值为%f'%(count,guess,words_num,probability))

    if ' ' in guess:

        if len(guess.replace(' ',''))==4:

            guess = guess.replace(' ','')

            words_num = len(guess)

        else:

            probability = 0

你可能感兴趣的:(利用百度ai接口识别验证码)