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原吾4 分钟前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
毕设木哥6 分钟前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
weixin_455446177 分钟前
Python学习的主要知识框架
开发语言·python·学习
D11_37 分钟前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.1 小时前
python基础知识(四)--if语句,for\while循环
python
IT毕设梦工厂2 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
luthane3 小时前
python 实现average mean平均数算法
开发语言·python·算法
码农研究僧3 小时前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录
Ylucius3 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
凡人的AI工具箱3 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python