Python基于百度AI實現OCR文字識別
百度AI功能還是很強大的,百度AI開放平臺真的是測試接口的天堂,免費接口很多,當然有量的限制,但個人使用是完全夠用的,什么人臉識別、MQTT服務器、語音識別等等,應有盡有。
看看OCR識別免費的量
快速安裝:執行pip install baidu-aip即可
新建一個AipOcr:
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 = 'http//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)
通用文字識別 請求參數詳情
通用文字識別 返回數據參數詳情
通用文字識別
from aip import AipOcr#更換為自己的注冊信息APP_ID = ’---’API_KEY = ’---’SECRET_KEY = ’---’client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#創建連接fp=open('tu2.png','rb').read()#打開并讀取文件內容res=client.basicGeneral(fp)#普通#print(res)#將所有的文字都合并到一起strx=''for tex in res['words_result']:#遍歷結果 strx+=tex['words']#每一行print(strx)#輸出內容
最終代碼
from aip import AipOcr # 定義常量APP_ID = ’14544448’API_KEY = ’yRZGUXAlCd0c9vQj1kAjBEfY’SECRET_KEY = ’sc0DKGy7wZ9MeWFGZnbscbRyoDB2IQlj’ # 初始化AipFace對象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(’binary_best.jpg’)# 調用通用文字識別, 圖片為本地圖片res=client.general(image)print(res) for item in res[’words_result’]: print(item[’words’])
例:
from aip import AipOcrimport reAPP_ID=’17010327’API_KEY=’X2MWCU1LG1PX5H6GAXgdlWD7’SECRET_KEY=’vz6GZ6TkhSFvY3quqcuC3EG8oEW3kThB’client=AipOcr(APP_ID,API_KEY,SECRET_KEY)i=open(r’C:UsersAdministratorDesktopexample.png’,’rb’)image = i.read()result=client.basicGeneral(image)#將所有的文字都合并到一起for item in result[’words_result’]: print(item[’words’])
通用文字識別client.basicGeneral(image)
通用文字識別(高精度版)client.basicAccurate(image);
通用文字識別(含位置信息版)client.general(image);
通用文字識別(含位置高精度版)client.accurate(image);
通用文字識別(含生僻字版)client.enhancedGeneral(image);
網絡圖片文字識別client.webImage(image);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: