python对接百度云车牌识别

注册百度智能云,选择产品服务。

https://console.bce.baidu.com/

每天赠送200次,做开发测试足够了。

在应用列表复制 AppID , API Key ,Secret Key 备用。

SDK下载地址

https://ai.baidu.com/sdk#ocr

下载SDK文件,解压,复制aip文件夹到项目的根目录下备用。

车牌识别服务费用

运行代码后的效果图

实现代码

python 复制代码
# 包的本质上是一个包含了一个或多个模块的文件夹,其中还包含一个特殊的文件 __init__.py
# 要注意引入第三方的包的路径: 包名.模块名 =》 aip.ocr
from aip.ocr import AipOcr

from PIL import Image, ImageDraw, ImageFont
import numpy as np
import cv2

# 修改成自己的
APP_ID = 'xxxxxxxxx'
API_KEY = 'xxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxx'
# 创建客户端对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 建立连接的超时时间,单位为毫秒
client.setConnectionTimeoutInMillis(5000)
# 通过打开的连接传输数据的超时时间,单位为毫秒
client.setSocketTimeoutInMillis(5000)


""" 解决中文乱码 """
def cv2ImgAddText(img, text, left, top, textColor, textSize):
    # 判断是否OpenCV图片类型
    if (isinstance(img, np.ndarray)):
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype("simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)


""" 读取文件内容 """
def getFileContent(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


""" 在图片上画出方框并写上文字 """
def imgDraw(imgPath):
    image_content = getFileContent(imgPath)
    # 百度返回的结果
    res = client.licensePlate(image_content)

    if res is not None:
        car_number = res['words_result']['number']
        car_color = res['words_result']['color']
        print('车牌号码:' + car_number)
        print('车牌颜色:' + car_color)

        # 车牌位置信息
        location = res['words_result']['vertexes_location']
        start_x = location[0]['x']
        start_y = location[0]['y']
        end_x = location[2]['x']
        end_y = location[2]['y']

        # 读取图片
        img = cv2.imread(imgPath)

        # 画长方形(图像矩阵,左上角坐标,右下角坐标,颜色,线条粗细)
        cv2.rectangle(img, (start_x, start_y), (end_x, end_y), (0, 0, 255), 5)

        # 在图片上绘制文字
        txt = car_number + ' ' + car_color
        # cv2ImgAddText(图像, 文字内容, 字体左边开始位置, 字体上面开始位置, (R, G, B), 字体大小)
        img_txt = cv2ImgAddText(img, txt, start_x+50, start_x+100, (0, 255, 0), 30)

        # 显示效果
        cv2.imshow('img', img_txt)

        # 等待按任意键关闭窗口
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    else:
        print('车牌识别失败!')


if __name__ == '__main__':
    # 图片路径
    imgPath = './output/car2.jpg'
    imgDraw(imgPath)

在代码中要注意引入百度云车牌识别SDK包的路径,否则会报错:ModuleNotFoundError: No module named 'ocr' 或者 ImportError: attempted relative import with no known parent package

相关推荐
databook15 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar17 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805117 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_17 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机1 天前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python