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

相关推荐
weixin_30777913几秒前
使用AWS IAM和Python自动化权限策略分析与导出
开发语言·python·自动化·云计算·aws
惜月_treasure5 分钟前
从零构建私域知识库问答机器人:Python 全栈实战(附完整源码)
开发语言·python·机器人
哈里谢顿1 小时前
threading模块学习
python
mit6.8242 小时前
[VoiceRAG] Azure | 使用`azd`部署应用 | Dockerfile
python
砥锋2 小时前
计算机人的雷达入门:零基础用Python+Cinrad可视化雷达数据【实战指南】
python
你们瞎搞2 小时前
arcgis矢量数据转为标准geojson格式
python·arcgis·json·地理空间数据
郝学胜-神的一滴2 小时前
Python中的鸭子类型:理解动态类型的力量
开发语言·python·程序人生·软件工程
2401_841495642 小时前
【计算机视觉】霍夫变换函数的参数调整
人工智能·python·算法·计算机视觉·霍夫变换·直线检测·调整策略
猫头虎3 小时前
如何解决 pip install -r requirements.txt extras 语法 ‘package[extra’ 缺少 ‘]’ 解析失败问题
开发语言·python·开源·beautifulsoup·virtualenv·pandas·pip
eqwaak03 小时前
动态图表导出与视频生成:精通Matplotlib Animation与FFmpeg
开发语言·python·ffmpeg·音视频·matplotlib