阅读 170

百度车牌识别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()



文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐