车牌识别算法demo

cpp 复制代码
import cv2
from paddleocr import PaddleOCR
import os
from matplotlib import pyplot as plt
def Morph_Distinguish(img):

    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 17))
    tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)

    y = cv2.Sobel(tophat, cv2.CV_16S, 1, 0)
    absY = cv2.convertScaleAbs(y)

    ret, binary = cv2.threshold(absY, 75, 255, cv2.THRESH_BINARY)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 15))
    Open = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (41, 15))
    close = cv2.morphologyEx(Open, cv2.MORPH_CLOSE, kernel)

    # 中远距离车牌识别
    kernel_x = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 7))
    kernel_y = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 11))
    # 近距离车牌识别
    # kernel_x = cv2.getStructuringElement(cv2.MORPH_RECT, (79, 15))
    # kernel_y = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 31))

    erode_y = cv2.morphologyEx(close, cv2.MORPH_ERODE, kernel_y)
    dilate_y = cv2.morphologyEx(erode_y, cv2.MORPH_DILATE, kernel_y)

    dilate_x = cv2.morphologyEx(dilate_y, cv2.MORPH_DILATE, kernel_x)
    erode_x = cv2.morphologyEx(dilate_x, cv2.MORPH_ERODE, kernel_x)

    kernel_e = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 9))
    erode = cv2.morphologyEx(erode_x, cv2.MORPH_ERODE, kernel_e)
    kernel_d = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 11))
    dilate = cv2.morphologyEx(erode, cv2.MORPH_DILATE, kernel_d)

    img_copy = img.copy()
    contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(img_copy, contours, -1, (255, 0, 255), 2)

    count = 0
    for contour in contours:
        area = cv2.contourArea(contour)

        x, y, w, h = cv2.boundingRect(contour)

        perimeter = cv2.arcLength(contour, True)

        approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
        CornerNum = len(approx)

        if h * 3 < w < h * 7 and area > 1000:
            ROI = img[(y - 5):(y + h + 5), (x - 5):(x + w + 5)]

            ocr = PaddleOCR(use_angle_cls=False, use_gpu=False, lang="ch", show_log=False)
            ocr_text = ocr.ocr(ROI, cls=False)
            for line in ocr_text:
                number_plate = line[-1][-1][0]
            print("车牌内容是:", number_plate)

            try:
                count += 1
                fig = plt.figure(figsize=(18, 14))
                plt.subplot(241), plt.imshow(img, 'gray'), plt.axis('off'), plt.title("erode")
                plt.subplot(242), plt.imshow(gray, 'gray'), plt.axis('off'), plt.title("gray")
                plt.subplot(243), plt.imshow(tophat, "gray"), plt.axis('off'), plt.title("tophat")
                plt.subplot(244), plt.imshow(absY, 'gray'), plt.axis('off'), plt.title("sobel")
                plt.subplot(245), plt.imshow(binary, "gray"), plt.axis('off'), plt.title("binary")
                plt.subplot(246), plt.imshow(dilate, 'gray'), plt.axis('off'), plt.title("morphology")
                plt.subplot(247), plt.imshow(img_copy, "gray"), plt.axis('off'), plt.title("contour")
                plt.subplot(248), plt.imshow(ROI, 'gray'), plt.axis('off'), plt.title("licence plate")
                plt.show()

                return ROI
            except:
                print("ROI提取出错!")
                return
                pass


if __name__ == '__main__':

    dirPath = "test_img"
    imgsName = os.listdir(dirPath)

    # 1 文件夹
    for imgName in imgsName:

        imgPath = os.path.join(os.getcwd(),dirPath,imgName)
        print(imgPath)
        img = cv2.imread(imgPath)
        img = cv2.resize(img, (int(img.shape[1] * 0.5), int(img.shape[0] * 0.5)))

        try:
            img = Morph_Distinguish(img.copy())  # 获取车牌ROI

            if img is None:
                print("没有提取到车牌")
                continue
        except:
            print("没有提取到车牌")
相关推荐
老艾的AI世界2 小时前
AI翻唱神器,一键用你喜欢的歌手翻唱他人的曲目(附下载链接)
人工智能·深度学习·神经网络·机器学习·ai·ai翻唱·ai唱歌·ai歌曲
sp_fyf_20245 小时前
【大语言模型】ACL2024论文-19 SportsMetrics: 融合文本和数值数据以理解大型语言模型中的信息融合
人工智能·深度学习·神经网络·机器学习·语言模型·自然语言处理
CoderIsArt5 小时前
基于 BP 神经网络整定的 PID 控制
人工智能·深度学习·神经网络
z千鑫6 小时前
【人工智能】PyTorch、TensorFlow 和 Keras 全面解析与对比:深度学习框架的终极指南
人工智能·pytorch·深度学习·aigc·tensorflow·keras·codemoss
EterNity_TiMe_6 小时前
【论文复现】神经网络的公式推导与代码实现
人工智能·python·深度学习·神经网络·数据分析·特征分析
思通数科多模态大模型7 小时前
10大核心应用场景,解锁AI检测系统的智能安全之道
人工智能·深度学习·安全·目标检测·计算机视觉·自然语言处理·数据挖掘
数据岛7 小时前
数据集论文:面向深度学习的土地利用场景分类与变化检测
人工智能·深度学习
学不会lostfound7 小时前
三、计算机视觉_05MTCNN人脸检测
pytorch·深度学习·计算机视觉·mtcnn·p-net·r-net·o-net
红色的山茶花7 小时前
YOLOv8-ultralytics-8.2.103部分代码阅读笔记-block.py
笔记·深度学习·yolo
白光白光7 小时前
凸函数与深度学习调参
人工智能·深度学习