qinlangsky

使用python调用百度ocr的API

注册账号

进入以下链接注册百度账号或云账号

点击跳转注册

创建应用

点击创建应用

创建应用

得到如上AppID 、API Key、Secret Key三个信息后,我们就可以在代码里调用接口了

安装Python SDK

sudo pip3 install baidu-aip              

调用API识别本地图片

from aip import AipOcr

"""定义常量"""
APP_ID = \'19854954\'
API_KEY = \'tloxML8vTIeuGsHuWZESGdYF\'
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(\'本地图片位置绝对路径\')

"""调用通用文字识别接口, 识别本地图像"""
result = client.basicGeneral(image)
print(result)
# 打印每行文字 
for item in res[\'words_result\']:
    print(item[\'words\'])

# 将每行文字拼接成一个整体
string_text = ""
for item in result[\'words_result\']:
    string_text += item[\'words\']
print(\'string_text:\', string_text)

常用接口说明

通用文字识别 client.basicGeneral(image)

通用文字识别(含位置信息版)client.general(image)

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

通用文字识别(高精度含位置版)client.accurate(image)

通用文字识别(含生僻字版)client.enhancedGeneral(image)

网络图片文字识别 client.webImage(image)          

实例化时的可选参数

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

调用API识别url上的图片

from aip import AipOcr

"""定义常量"""
APP_ID = \'19854954\'
API_KEY = \'tloxML8vTIeuGsHuWZESGdYF\'
SECRET_KEY = \'*******\'

"""初始化对象"""
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

""" 带参数调用通用文字识别, 图片参数为远程url图片 """
url = "http://xxxxxxxx"
# 如果有可选参数
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

reusult = client.basicGeneralUrl(url, options)
print(result)
# 打印每行文字 
for item in res[\'words_result\']:
    print(item[\'words\'])

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-01-14
  • 2021-11-23
  • 2021-04-02
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2022-02-07
相关资源
相似解决方案