最近想研究学习OCR识别的知识,当然就想起来先拿百度AI来试个手体验一下啦~???
不过好像百度的SDK升级了,记得去年八月份的时候用这个的时候要先修改原SDK中的部分代码(主要是图片编码处理的),这次直接就可以用了,话不多说直接贴上代码:
#-*-coding:utf-8-*-
from aip import AipOcr
import cv2 as cv
import numpy as np
#from PIL import Image
""" 你的 APPID AK SK """
APP_ID = '160**570'
API_KEY = 'wuN**pO25TGHFaUF8l2MI42I'
SECRET_KEY = 'H3G2K9N**TDnjG1zpwFgGo7iK3xvcNl'
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.licensePlate(image);
""" 如果有可选参数 """
options = {}
options["multi_detect"] = "true"
""" 带参数调用车牌识别 """
fb = client.licensePlate(image, options)
print("return log:",fb)
result = fb['words_result']
#print(result)
#print(type(result))
#color = result[0]['color']
#number = result[0]['number']
location = result[0]['vertexes_location']
#print(color)
#print(number)
#print(type(number))
#print(location)
img = cv.imread("./example.jpg")
#cv.putText(img, number1, (np.int(location[0]['x']), np.int(location[0]['y'])-20), cv.FONT_HERSHEY_TRIPLEX, 0.8, (0, 255,0), 2)
cv.rectangle(img, (np.int(location[0]['x']), np.int(location[0]['y'])), (np.int(location[2]['x']), np.int(location[2]['y'])), (0, 255, 0),2,cv.LINE_8,0)
cv.imshow("result",img)
cv.waitKey(0)
cv.destroyAllWindows()
代码基本照抄的SDK参考文档里的,仅仅加入了opencv来可视化结果的代码。由于options[“multi_detect”] = “true”,所以是可以检测包含多个车牌的图片的,这里我只是使用了只有一个车牌的图片。
返回的log如下:
return log: {'log_id': 5299849045275730993,
'words_result': [{'color': 'blue',
'number': '苏JAY888',
'probability': [1.0, 1.0, 0.9999914169311523,
0.9999885559082031,
0.9993658065795898, 0.9993288516998291,
0.9992438554763794],
'vertexes_location': [
{'y': 184, 'x': 88},
{'y': 181, 'x': 303},
{'y': 235, 'x': 304},
{'y': 239, 'x': 90}
]}]}
其中color就是车牌颜色,number就是车牌号,prob就是六个字符的置信度,v_l就是车牌四个定点的位置(左上,右上,右下,左下)。