车牌识别算法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("没有提取到车牌")
相关推荐
TURING.DT12 分钟前
模型部署:TF Serving 的使用
深度学习·tensorflow
励志去大厂的菜鸟2 小时前
系统相关类——java.lang.Math (三)(案例详细拆解小白友好)
java·服务器·开发语言·深度学习·学习方法
liuhui2442 小时前
Pytorch深度学习指南 卷I --编程基础(A Beginner‘s Guide) 第1章 一个简单的回归
pytorch·深度学习·回归
睡不着还睡不醒3 小时前
【深度学习】神经网络实战分类与回归任务
深度学习·神经网络·分类
编码浪子3 小时前
Transformer的编码机制
人工智能·深度学习·transformer
IE063 小时前
深度学习系列76:流式tts的一个简单实现
人工智能·深度学习
m0_743106467 小时前
【论文笔记】MV-DUSt3R+:两秒重建一个3D场景
论文阅读·深度学习·计算机视觉·3d·几何学
m0_743106468 小时前
【论文笔记】TranSplat:深度refine的camera-required可泛化稀疏方法
论文阅读·深度学习·计算机视觉·3d·几何学
AI浩11 小时前
【面试总结】FFN(前馈神经网络)在Transformer模型中先升维再降维的原因
人工智能·深度学习·计算机视觉·transformer
IE0613 小时前
深度学习系列75:sql大模型工具vanna
深度学习