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

相关推荐
精灵vector18 小时前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain
java1234_小锋18 小时前
Scikit-learn Python机器学习 - 特征降维 压缩数据 - 特征选择 - 移除低方差特征(VarianceThreshold)
python·机器学习·scikit-learn
万邦科技Lafite19 小时前
实战演练:通过API获取商品详情并展示
大数据·数据库·python·开放api接口
愈努力俞幸运19 小时前
uv教程 虚拟环境
python·uv
IMER SIMPLE19 小时前
人工智能-python-深度学习-经典网络模型-LeNets5
人工智能·python·深度学习
企业软文推广19 小时前
奥迪A5L×华为:品牌营销视角下的燃油车智能突围战!
python·华为
Pocker_Spades_A20 小时前
Python快速入门专业版(十五):数据类型实战:用户信息录入程序(整合变量、输入与类型转换)
数据库·python
IMER SIMPLE20 小时前
人工智能-python-深度学习-神经网络-GoogLeNet
人工智能·python·深度学习
小宁爱Python20 小时前
Django 从环境搭建到第一个项目
后端·python·django