百度车牌识别API-Python版
支持Python版本:2.7.+ ,3.+
安装使用Python SDK有如下方式:
如果已安装pip,执行pip install baidu-aip即可。
如果已安装setuptools,执行python setup.py install即可。
打开https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/overview/index创建应用
获得
APP_ID
API_KEY
SECRET_KEY
# encoding:utf-8 from aip import AipOcr import cv2 import numpy from PIL import Image, ImageDraw, ImageFont """ 你的 APPID AK SK """ APP_ID = '111' API_KEY = 'aaa' SECRET_KEY = 'bbb' 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') """ 调用车牌识别 """ result = client.licensePlate(image); print(result); print(result["words_result"]["number"]); vl = result["words_result"]["vertexes_location"] carNumber = result["words_result"]["number"] img = cv2.imread('example.jpg') ## 画方框 cv2.rectangle(img, (vl[0]["x"], vl[0]["y"]), (vl[2]["x"], vl[2]["y"]), (0,0,255), 2) def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, numpy.ndarray)): # 判断是否OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 创建一个可以在给定图像上绘图的对象 draw = ImageDraw.Draw(img) # 字体的格式 fontStyle = ImageFont.truetype( "font/simsun.ttc", textSize, encoding="utf-8") # 绘制文本 draw.text((left, top), text, textColor, font=fontStyle) # 转换回OpenCV格式 return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR) img = cv2ImgAddText(cv2.imread('example.jpg'), carNumber, 0, 0, (255,255,255), 30) cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows()