Python基于百度AI實現(xiàn)OCR文字識別
百度AI功能還是很強大的,百度AI開放平臺真的是測試接口的天堂,免費接口很多,當然有量的限制,但個人使用是完全夠用的,什么人臉識別、MQTT服務(wù)器、語音識別等等,應(yīng)有盡有。
看看OCR識別免費的量
快速安裝:執(zhí)行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’)''' 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 '''client.basicGeneral(image);''' 如果有可選參數(shù) '''options = {}options['language_type'] = 'CHN_ENG'options['detect_direction'] = 'true'options['detect_language'] = 'true'options['probability'] = 'true'''' 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 '''client.basicGeneral(image, options)url = 'http//www.x.com/sample.jpg'''' 調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片 '''client.basicGeneralUrl(url);''' 如果有可選參數(shù) '''options = {}options['language_type'] = 'CHN_ENG'options['detect_direction'] = 'true'options['detect_language'] = 'true'options['probability'] = 'true'''' 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片 '''client.basicGeneralUrl(url, options)
通用文字識別 請求參數(shù)詳情
通用文字識別 返回數(shù)據(jù)參數(shù)詳情
通用文字識別
from aip import AipOcr#更換為自己的注冊信息APP_ID = ’---’API_KEY = ’---’SECRET_KEY = ’---’client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#創(chuàng)建連接fp=open('tu2.png','rb').read()#打開并讀取文件內(nèi)容res=client.basicGeneral(fp)#普通#print(res)#將所有的文字都合并到一起strx=''for tex in res['words_result']:#遍歷結(jié)果 strx+=tex['words']#每一行print(strx)#輸出內(nèi)容
最終代碼
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’)# 調(diào)用通用文字識別, 圖片為本地圖片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);
網(wǎng)絡(luò)圖片文字識別client.webImage(image);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)2. JavaWeb Servlet中url-pattern的使用3. ASP.NET MVC遍歷驗證ModelState的錯誤信息4. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式5. HTML5 Canvas繪制圖形從入門到精通6. 使用EF Code First搭建簡易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移7. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲8. .Net Core和RabbitMQ限制循環(huán)消費的方法9. ASP中if語句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明
