zoukankan      html  css  js  c++  java
  • 识别验证码之百度通用识别接口

    在识别验证码的时候,可以调用百度的通用文字识别接口。

    步骤

    Step1 获取access_token的值。

    ① 登陆 https://ai.baidu.com/ ,找到通用文字识别,点击立即使用。

    ② 点击创建应用后,会得到API KeySecret Key

    ③ 将API Key 和Secret Key写入如下代码中,得到 access_token 的值。

    1 # encoding:utf-8
    2 import requests 
    3 
    4 # client_id 为官网获取的AK, client_secret 为官网获取的SK
    5 host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
    6 response = requests.get(host)
    7 if response:
    8     print(response.json())

    Step2 调用通用文字识别接口

      将得到的 access_token 值下载到本地的验证码图片名写入如下代码中,就可以获取识别结果。

     1 # encoding:utf-8
     2 
     3 import requests
     4 import base64
     5 
     6 '''
     7 通用文字识别
     8 '''
     9 
    10 request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    11 # 二进制方式打开图片文件
    12 f = open('[本地文件]', 'rb')
    13 img = base64.b64encode(f.read())
    14 
    15 params = {"image":img}
    16 access_token = '[调用鉴权接口获取的token]'
    17 request_url = request_url + "?access_token=" + access_token
    18 headers = {'content-type': 'application/x-www-form-urlencoded'}
    19 response = requests.post(request_url, data=params, headers=headers)
    20 if response:
    21     print (response.json())

    完整代码如下

     1 def identify_Verification_code(API_Key,Secret_Key,Verification_code):
     2     #获取Access Token官方代码,必须要有API Key和Secret Key两个参数才能获取。
     3     import requests
     4 
     5     host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+ API_Key + '&client_secret=' + Secret_Key
     6     response = requests.get(host)
     7     access_token = response.json()['access_token']
     8 
     9     #通用文字识别官方代码
    10     import base64
    11     request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    12     # 二进制方式打开图片文件,Verification_code是要识别的验证码的名字
    13     f = open(Verification_code, 'rb')
    14     img = base64.b64encode(f.read())
    15 
    16     params = {"image":img}
    17     access_token = access_token
    18     request_url = request_url + "?access_token=" + access_token
    19     headers = {'content-type': 'application/x-www-form-urlencoded'}
    20     response = requests.post(request_url, data=params, headers=headers)
    21     shibie_result = response.json()['words_result'][0]['words']
    22     print(shibie_result)
    23 
    24 if __name__ == '__main__':
    25     API_Key = '***'
    26     Secret_Key = '***'
    27     Verification_code = '验证码.png'
    28     identify_Verification_code(API_Key,Secret_Key,Verification_code)
  • 相关阅读:
    debian 中安装GIT
    多核处理器 利用多线程 加速 编译内核 速度
    ubuntu下安装中文输入法(乱码等问题)
    ubuntu 10.04源 更新源列表
    php empty,isset,is_null比较(差异与异同) Leone
    Win 7 各版本的含义 Leone
    Atitit DbServiceV4qb9 数据库查询类库v4 新特性
    Atitit 图像处理之仿油画效果 Oilpaint油画滤镜 水彩画 漫画滤镜 v2
    Atitit 多继承实现解决方案 java c#
    Atitit 基于图片图像 与文档混合文件夹的分类
  • 原文地址:https://www.cnblogs.com/weifeng1998/p/13227726.html
Copyright © 2011-2022 走看看