OCRは、BaiduのAPI、Pythonのシンプルなバージョンを呼び出します
1.登録
Baiduのクラウドはアカウント登録https://cloud.baidu.com/?from=consoleの
管理アプリケーションをhttps://console.bce.baidu.com/ai/#/ai/ocr/overview/index作成
リンクを入力した後、アプリケーションを作成し、ポイントは文字認識から行くことですので、それはデフォルトのOCR関連するコンテンツによって選択され、確認するためにフォームに記入してください。
これら三つのこと、AppIDを、APIキー、秘密鍵で、我々は、コード内のインターフェイスを呼び出すことができます。
2.コールAPI
公式ガイド:https://ai.baidu.com/docs#/OCR-Python-SDK/top
インストールSDKのPython:PIPは、Baiduの-AIPインストール
CV2をインストールする必要があります。インストールopencv_python PIP
あなただけのテキストボックスとテキスト領域を予測する必要がある場合、次のコード。
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)
斜めの角度はかなり良い検出することができます