百度雲註冊帳號 https://cloud.baidu.com/?from=console
管理應用 https://console.bce.baidu.com/ai/#/ai/ocr/overview/index 建立一個
python
官方指南:https://ai.baidu.com/docs#/OCR-Python-SDK/top
安裝使用Python SDK: pip install baidu-aip
cv2 須要安裝:pip install opencv_python
若是隻須要預測文字以及框出文字區域,執行如下代碼便可。spa
import cv2
from aip import AipOcr
""" 你的 APPID AK SK 圖2的內容"""
APP_ID = '14318340'
API_KEY = 'DUvK5jEkNmCIEz4cXH8VvIVC'
SECRET_KEY = '*******'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
fname = 'picture/test4.jpg'
""" 讀取圖片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content(fname)
""" 調用通用文字識別, 圖片參數爲本地圖片 """
results = client.general(image)["words_result"] # 還可使用身份證駕駛證模板,直接獲得字典對應所需字段
img = cv2.imread(fname)
for result in results:
text = result["words"]
location = result["location"]
print(text)
# 畫矩形框
cv2.rectangle(img, (location["left"],location["top"]), (location["left"]+location["width"],location["top"]+location["height"]), (0,255,0), 2)
cv2.imwrite(fname[:-4]+"_result.jpg", img)
斜必定角度也能檢測出來 還不錯code