python 用hyperlpr3 进行车牌识别

2023-12-25 17:18:10

开源项目

https://github.com/zeusees/HyperLPR
下面按装相关的模块

pip install opencv-python
pip install hyperlpr3
pip install cvlib

代码

在这里插入图片描述

download_image_from_url 将网上图片存本地。

import urllib.request
import cv2
import hyperlpr3 as lpr3
import matplotlib.pyplot as plt
import tempfile
import os

font_path = 'simhei.ttf'

# 加载字体文件
font = cv2.FONT_HERSHEY_SIMPLEX

def download_image_from_url(url):
    with urllib.request.urlopen(url) as url_obj:
        data = url_obj.read()
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        tmp_file.write(data)
        return tmp_file.name

def get_car_info():
    url = "https://pics.jjsos.cn/FgHtNacqa808NZTRklrWJNrmryiA"
    image_path = download_image_from_url(url)

    catcher = lpr3.LicensePlateCatcher()
    image = cv2.imread(image_path)

    result = catcher(image)

    os.remove(image_path)

    if isinstance(result, list):
        result_list = []
        for item in result:
            license_plate = item[0]
            confidence = item[1]
            coordinates = item[3]
            x1, y1, x2, y2 = map(int, coordinates)

            cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 255), 2)
            text = f"{license_plate} "

            # Use a specific font for Chinese characters (you can adjust the font size if needed)
            # cv2.putText(image, text, (x1, y1 - 10), font, 0.5, (255, 0, 255), 2)

            result_list.append({"license_plate": license_plate, "confidence": str(confidence)})

        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.show()
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        vreturn = {"code": 0, "results": result_list}
        print(vreturn)
        return vreturn
    else:
        license_plate = result[0]
        confidence = result[1]
        coordinates = result[3]
        x1, y1, x2, y2 = map(int, coordinates)

        cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 255), 2)
        text = f"{license_plate} "

        # Use a specific font for Chinese characters (you can adjust the font size if needed)
        # cv2.putText(image, text, (x1, y1 - 10), font, 0.5, (255, 0, 255), 2)

        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.show()
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        vreturn = {"code": 0, "license_plate": license_plate, "confidence": str(confidence)}
        print(vreturn)
        return vreturn

# Call the function to get car information
get_car_info()

识别返回:
在这里插入图片描述

文章来源:https://blog.csdn.net/hzether/article/details/135199864
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。