使用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'])


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM