【Python3-API】通用文字識別示例程式碼
阿新 • • 發佈:2022-04-28
- AccessToken獲取可以參考:http://ai.baidu.com/forum/topic/show/497663(Python3-urllib3示例)
- Python安裝
-----------------------------------------------------下面開始程式碼-----------------------------------------------------
- Python3-API示例程式碼(通用文字識別)
''' Created on 2018-1-25 通用文字識別-Python3 -API示例程式碼 @author: 小帥丶 ''' import urllib3,base64 from urllib.parse import urlencode access_token='自己應用資訊獲取的access_token' http=urllib3.PoolManager() url='https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token='+access_token f = open('F:/demo.jpg','rb') #引數image:影象base64編碼 img = base64.b64encode(f.read()) params={'image':img} #對base64資料進行urlencode處理 params=urlencode(params) request=http.request('POST', url, body=params, headers={'Content-Type':'application/x-www-form-urlencoded'}) #對返回的byte位元組進行處理。Python3輸出位串,而不是可讀的字串,需要進行轉換 result = str(request.data,'utf-8') print(result)
- 返回的識別結果內容
{ "log_id": 6101980364103585000, "words_result_num": 1, "words_result": [ { "location": { "width": 146, "top": 7, "height": 17, "left": 11 }, "words": "小帥開發者服務開發者" } ] }
- 原圖檔案
-----------------------------------------------------程式碼結束-----------------------------------------------------
確實發現Python簡單。而且寫的程式碼也很少。程式碼僅供參考。