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

相关推荐
AI蜗牛之家1 小时前
Qwen系列之Qwen3解读:最强开源模型的细节拆解
人工智能·python
whyeekkk1 小时前
python打卡第48天
开发语言·python
Eiceblue4 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
weixin_527550404 小时前
初级程序员入门指南
javascript·python·算法
程序员的世界你不懂4 小时前
Appium+python自动化(十)- 元素定位
python·appium·自动化
CryptoPP5 小时前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
树叶@5 小时前
Python数据分析7
开发语言·python
老胖闲聊6 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点6 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
浠寒AI6 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python