首页 > 编程语言 >【Python&文字识别】基于HyperLPR3实现车牌检测和识别(Python版本快速部署)

【Python&文字识别】基于HyperLPR3实现车牌检测和识别(Python版本快速部署)

时间:2024-05-02 17:24:38浏览次数:18  
标签:font Python image cv2 y1 识别 车牌 HyperLPR3


        闲来无事,想复现一下网上的基于YOLO v5的单目测距算法。然后就突然想在这个场景下搞一下车牌识别,于是就有了这篇文章。今天就给大家分享基于HyperLPR3实现车牌检测和识别。

原创作者:RS迷途小书童

博客地址:https://blog.csdn.net/m0_56729804?type=blog

1、HyperLPR3介绍

        HyperLPR3是一个高性能开源中文车牌识别框架,由北京智云视图科技有限公司开发。它是一个基于Python的深度学习实现,用于中文车牌的识别。与开源的EasyPR相比,HyperLPR3在检测速度、鲁棒性和多场景的适应性方面都有更好的表现。

        HyperLPR3支持多种类型的车牌,包括新能源汽车等。其安装和使用都非常方便,可以通过Python的pip工具直接进行安装,并使用命令行工具对本地图像或在线URL进行快速测试。

        此外,HyperLPR3还支持PHP、C/C++、Python语言,以及Windows/Mac/Linux/Android/IOS平台,具有广泛的适用性。

2、HyperLPR3安装

2.1 Github地址

HyperLPR- 基于深度学习高性能中文车牌识别

2.2 快速安装

pip install hyperlpr3

2.3 支持的车牌类别

  • 单行蓝牌
  • 单行黄牌
  • 新能源车牌
  • 教练车牌
  • 白色警用车牌
  • 使馆/港澳车牌
  • 双层黄牌
  • 武警车牌

3、代码

        Github里可以下载各类语言的demo,也有开放的接口可以直接线上检测车牌。我这里基于官方demo写了一份图片和视频的车牌识别代码。

3.1 辅助函数

def draw_plate_on_image(img, box1, text1, font):
    x1, y1, x2, y2 = box1  # 识别框的四至范围
    # random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA)  # 车牌外框
    # cv2.rectangle(img, (x1, y1 - 25), (x2, y1-3), (139, 139, 102), -1)  # 识别文本底色
    data = Image.fromarray(img)  # 读取图片
    draw = ImageDraw.Draw(data)  # PIL绘制图片
    draw.text((x1, y1 - 27), text1, (0, 0, 255), font=font)  # 添加识别文本
    res = np.asarray(data)  # 返回叠加识别结果的图片
    return res

3.2 图片识别

def license_recognition_image(path):
    image = cv2.imread(path)  # 读取图片
    results = catcher(image)  # 执行识别算法
    for code, confidence, type_idx, box in results:
        # [['京Q58A77', 0.9731929, 0, [150, 160, 451, 259]]]
        text = f"{code} - {confidence:.2f}"
        image = draw_plate_on_image(image, box, text, font=font_ch)  # 绘制识别结果
    cv2.imshow("License Plate Recognition(Directed By RSran)", image)  # 显示检测结果
    cv2.waitKey(0)

3.3 视频识别

def license_recognition_video(path):
    video = cv2.VideoCapture()
    video.open(path)
    i = 0
    while True:
        i += 1
        ref, image = video.read()  # 组帧打开视频
        if ref:
            if i % 10 == 0:
                results = catcher(image)  # 执行识别算法
                for code, confidence, type_idx, box in results:
                    # [['京Q58A77', 0.9731929, 0, [150, 160, 451, 259]]]
                    text = f"{code} - {confidence:.2f}"
                    image = draw_plate_on_image(image, box, text, font=font_ch)  # 绘制识别结果
                cv2.imshow("License Plate Recognition(Directed By RSran)", image)  # 显示检测结果
                if cv2.waitKey(10) & 0xFF == ord('q'):
                    break  # 退出
        else:
            break

3.4 效果展示

下图为百度图片库中检索的案例,如有侵权请联系作者删除。

4、完整代码

# -*- coding: utf-8 -*-
"""
@Time : 2024/4/19 13:59
@Auth : RS迷途小书童
@File :License Plate Recognition.py
@IDE :PyCharm
@Purpose:车辆拍照识别
@Web:博客地址:https://blog.csdn.net/m0_56729804
"""
# 导入cv相关库
import cv2
import random
import warnings
import numpy as np
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import hyperlpr3 as lpr3


def draw_plate_on_image(img, box1, text1, font):
    x1, y1, x2, y2 = box1  # 识别框的四至范围
    # random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA)  # 车牌外框
    # cv2.rectangle(img, (x1, y1 - 25), (x2, y1-3), (139, 139, 102), -1)  # 识别文本底色
    data = Image.fromarray(img)  # 读取图片
    draw = ImageDraw.Draw(data)  # PIL绘制图片
    draw.text((x1, y1 - 27), text1, (0, 0, 255), font=font)  # 添加识别文本
    res = np.asarray(data)  # 返回叠加识别结果的图片
    return res


def license_recognition_video(path):
    video = cv2.VideoCapture()
    video.open(path)
    i = 0
    while True:
        i += 1
        ref, image = video.read()  # 组帧打开视频
        if ref:
            if i % 10 == 0:
                results = catcher(image)  # 执行识别算法
                for code, confidence, type_idx, box in results:
                    # [['京Q58A77', 0.9731929, 0, [150, 160, 451, 259]]]
                    text = f"{code} - {confidence:.2f}"
                    image = draw_plate_on_image(image, box, text, font=font_ch)  # 绘制识别结果
                cv2.imshow("License Plate Recognition(Directed By RSran)", image)  # 显示检测结果
                if cv2.waitKey(10) & 0xFF == ord('q'):
                    break  # 退出
        else:
            break


def license_recognition_image(path):
    image = cv2.imread(path)  # 读取图片
    results = catcher(image)  # 执行识别算法
    for code, confidence, type_idx, box in results:
        # [['京Q58A77', 0.9731929, 0, [150, 160, 451, 259]]]
        text = f"{code} - {confidence:.2f}"
        image = draw_plate_on_image(image, box, text, font=font_ch)  # 绘制识别结果
    cv2.imshow("License Plate Recognition(Directed By RSran)", image)  # 显示检测结果
    cv2.waitKey(0)


if __name__ == "__main__":
    warnings.filterwarnings("ignore", message="Mean of empty slice")  # 忽略“Mean of empty slice”的警告
    warnings.filterwarnings("ignore", message="invalid value encountered in scalar divide")
    # 忽略“invalid value encountered in scalar divide”的警告
    font_ch = ImageFont.truetype("resource/font/platech.ttf", 20, 0)  # 中文字体加载
    catcher = lpr3.LicensePlateCatcher(detect_level=lpr3.DETECT_LEVEL_HIGH)  # 实例化识别对象
    file = r"Y:\2024-04-19 14-49-09.mp4"
    license_recognition_video(file)

标签:font,Python,image,cv2,y1,识别,车牌,HyperLPR3
From: https://www.cnblogs.com/RSran/p/18170344

相关文章

  • 用 Python 开发一个【GIF表情包制作神器】
    用python成为了微信斗图届的高手然后,好多人表示:虽然存了很多表情包但似乎还不是很过瘾因为它不可以自己来定制我们可不可以根据一些表情素材然后自己制作专属表情包呢像这样本来小帅b想自己实现一个表情包制作器后来发现已经有人在GitHub 分享了   主要功能就是可以......
  • 使用 python matplotlib 将 LaTex 公式转为 svg
    使用pythonmatplotlib将LaTex公式转为svg,从而方便插入无法打出所需公式的ppt中。importmatplotlib.pyplotaspltdeflatex_formula2svg(text,font_size=12,save_fig='formula.svg'):plt.rc('text',usetex=True)#使用LaTeX渲染文本plt.rc('f......
  • Python连接访问mongodb副本集
    代码如下:frompymongoimportMongoClient#配置副本集的地址replica_set_hosts=["192.168.10.135:27017","192.168.10.136:27018","192.168.10.137:27019"]#创建MongoClient连接client=MongoClient(replica_set_hosts,userna......
  • python3使用dpkt生成PCMA格式rtp流
    操作系统:CentOS7.6_x64Python版本:3.9.12dpkt版本:1.9.8PCMA编码是VoIP通信中常见的格式,今天整理下CentOS7环境下,python3如何使用dpkt生成PCMA格式rtp流的笔记,并提供相关示例代码、运行效果视频和配套文件下载。我将从以下几方面进行展开:背景材料使用dpkt生成PCMA格式rt......
  • 推荐一个教程,适用于想学python但是只学点基础知识用于刷题的
    省流:https://www.bilibili.com/video/BV1Lk4y117Cb?p=1&vd_source=4a339d299e165d8fe38b9926c5240eae我以前一直使用Java刷题,但是随着刷题的数量越来越多,越发感觉Java真的不适合用来刷题,看leetcode里面的大佬们基本都是清一色的c++和python,所以我也是想学点python用于刷题,花了......
  • 用python写一个 将指定目录下以及其下所有子目录下的srt文件复制一份并重命名带上文件
    代码:importosimportshutildefcopy_and_rename_files(src_directory,target_directory):#确保目标目录存在ifnotos.path.exists(target_directory):os.makedirs(target_directory)#遍历指定目录及其所有子目录forroot,dirs,file......
  • Python学习之路 第五篇 基本数据类型
    int类型:在python3里不论数有多大,永远都是int类型。在python2里整形(数字),在范围内叫int,超出范围叫long,也叫长整型。在python3里所有整形(数字)的功能都包含在int里。int功能展示:输入int摁住ctrl键然后同时将鼠标箭头放在int上出现小手后点击进去就能看到int所具有的功能。表示所有的数......
  • 有遇到过吗?同样的规则 Excel 中 比Python 结果大
    大家好,我是Python进阶者。一、前言前几天在Python白银交流群【JethroShen】问了一个Python处理Excel数据读取的问题。问题如下:有遇到过吗?同样的规则 Excel中 比Python 结果大?二、实现过程这里【瑜亮老师】和【论草莓如何成为冻干莓】给了一个指导,指出是字符串的问题......
  • 一个用Python将视频变为表情包的工具
    这是一个将视频转变为表情包的工具,现实生活中当我们看到一段搞笑的视频,我们可以将这段视频喂给这段程序,生成gif表情包,这样就可以用来舍友斗图了1、一些限制1、这个程序不能转化超过15秒以上的视频,因为占用的内存较高,会被终端杀死(除非你的计算机性能很好,也许1分钟的短视频都可以),......
  • Python - 模块包
    目录包导入示例包的from语句vs包的import语句包导入示例下列三个文件分别位于目录dir1以及dir1的子目录dir2中,这些文件的路径名在注释中给出:#dir1\__init__.pyprint('dir1init')x=1#dir1\dir2\__init__.pyprint('dir2init')y=2#dir1\dir2\mod.pypr......