python调用百度通用文字识别接口进行验证码识别

版权声明:非诚勿扰 https://blog.csdn.net/kaikai0803/article/details/84630039

官方文档入口

https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E7.AE.80.E4.BB.8B

安装

pip3 install baidu-aip

新建AipOcr

AipOcr是OCR的Python SDK客户端,为使用OCR的开发人员提供了一系列的交互方法。

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': 17140**********97, 'direction': 0, 'words_result_num': 1, 'words_result': [{'words': '+?=7', 'probability': {'variance': 0.003346, 'average': 0.931648, 'min': 0.865472}}], 'language': -1}
<class 'dict'>

猜你喜欢

转载自blog.csdn.net/kaikai0803/article/details/84630039