python 百度ocr安装_使用python调用百度ocr的API

注册账号

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

创建应用

Createapp.png

得到如上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'])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值