Ocr call Baidu's API, python simple version
1. Registration
Baidu cloud registered account https://cloud.baidu.com/?from=console
management application https://console.bce.baidu.com/ai/#/ai/ocr/overview/index create a
Create an application after entering the link, because the point is to go from character recognition, it is selected by default ocr relevant content, fill out the form to confirm.
With these three things, AppID, API Key, Secret Key, we can call interface in the code.
2. Call API
Official Guide: https://ai.baidu.com/docs#/OCR-Python-SDK/top
installation SDK Python: PIP install baidu-AIP
CV2 need to install: pip install opencv_python
If you only need to predict the text box and the text area , the following code.
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)
Oblique angle can be detected pretty good