ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用python调用百度ocr的API

2020-08-12 17:01:26  阅读:278  来源: 互联网

标签:识别 python image client API KEY ocr options


目录

注册账号

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

点击跳转注册

创建应用

点击创建应用

创建应用

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

标签:识别,python,image,client,API,KEY,ocr,options
来源: https://www.cnblogs.com/qinlangsky/p/13491581.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有