百度智能云-文字识别SDK-识别验证码Demo

百度智能云-文字识别-识别验证码


一、注册账号

https://login.bce.baidu.com/reg.html?tpl=bceplat&from=portal

二、个人认证(好像可以不认证)

https://console.bce.baidu.com/qualify/#/qualify/result
只需要初级认证就行,不需要刷脸认证

三、文字识别

https://console.bce.baidu.com/ai/?_=1597892791979#/ai/ocr/overview/index

在这里插入图片描述
点击上图右下角的领取免费资源,把文字识别的0元领取了,其他的随意

然后点击左上角的创建应用,创建一下应用

创建完之后,在应用列表里,记住以下三个量
在这里插入图片描述

四、下载Python SDK

文档
支持Python版本:2.7.+ ,3.+

安装使用Python SDK有如下方式:

-如果已安装pip,执行pip install baidu-aip即可。
-如果已安装setuptools,执行python setup.py install即可。

我是在cmd里pip install baidu-aip

五、新建AipOcr

from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

六、接口说明

文档

通用文字识别

用户向服务请求识别某张图中的所有文字。

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')

""" 调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image);

""" 如果有可选参数 """
options = {
    
    }
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image, options)

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

""" 调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url);

""" 如果有可选参数 """
options = {
    
    }
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url, options)

通用文字识别 返回示例


{
    
    
"log_id": 2471272194,
"words_result_num": 2,
"words_result":
    [
        {
    
    "words": " TSINGTAO"},
        {
    
    "words": "青島睥酒"}
    ]
}

通用文字识别(高精度版)

用户向服务请求识别某张图中的所有文字,相对于通用文字识别该产品精度更高,但是识别耗时会稍长。

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')

""" 调用通用文字识别(高精度版) """
client.basicAccurate(image);

""" 如果有可选参数 """
options = {
    
    }
options["detect_direction"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别(高精度版) """
client.basicAccurate(image, options)

通用文字识别(高精度版) 返回示例

参考通用文字识别返回示例

七、识别验证码Demo

通用文字识别

from aip import AipOcr

def yzm_ocr(url):
    APP_ID = ''  # 在百度官网的应用列表中查看APP_ID
    API_KEY = ''  # 在百度官网的应用列表中查看API_KEY
    SECRET_KEY = ''  # 在百度官网的应用列表中查看SECRET_KEY
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    text = client.basicGeneralUrl(url);
    print(text)
    if text['words_result_num'] == 1:
        return text['words_result'][0]['words'].strip()
    else:
        return ''
        
if __name__ == '__main__':
    url = "https://img-blog.csdnimg.cn/b8f56f748cf84f7aafbb8e38b0ef267a.png"   
    yzm = ''
    while len(yzm) != 4:
        yzm = yzm_ocr(url)
        print(yzm)
        yzm = yzm.replace(' ','')#去除空格
        print(yzm)
    print('成功')

在这里插入图片描述
在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/tfnmdmx/article/details/119035700