百度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 AipOcr import re APP_ID='17010327' API_KEY='X2MWCU1LG1PX5H6GAXgdlWD7' SECRET_KEY='vz6GZ6TkhSFvY3quqcuC3EG8oEW3kThB' client=AipOcr(APP_ID,API_KEY,SECRET_KEY) i=open(r'C:\Users\Administrator\Desktop\example.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);
Python SDK文檔 http://ai.baidu.com/docs#/OCR-Python-SDK/53932383
OCR文字識別筆記總結 https://www.cnblogs.com/wj-1314/p/9580387.html