车牌识别算法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产品老杨3 小时前
深度学习模型量化原理
开发语言·人工智能·python·深度学习·安全·音视频
坠金4 小时前
神经网络的常用layer
人工智能·深度学习·神经网络
LeapMay6 小时前
3D Gaussian Splatting代码详解(一):模型训练、数据加载
人工智能·深度学习·3d
中杯可乐多加冰8 小时前
【AI应用落地实战】智能文档处理本地部署——可视化文档解析前端TextIn ParseX实践
人工智能·深度学习·大模型·ocr·智能文档处理·acge·textin
懒惰才能让科技进步8 小时前
从零学习大模型(九)-----P-Tuning(下)
人工智能·深度学习·学习·chatgpt·prompt·transformer
蜡笔小新星8 小时前
PyTorch的基础教程
开发语言·人工智能·pytorch·经验分享·python·深度学习·学习
QQ_5192923289 小时前
【动植物毒性数据集】毒蛇识别 蘑菇毒性分类 人工智能 深度学习 目标检测 Python(含数据集)
深度学习·目标检测·数据集·动植物毒性数据集
STRANGEX-039 小时前
深度学习案例:带有一个隐藏层的平面数据分类
深度学习·平面·分类