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 = 0for 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 += 1fig = 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 ROIexcept:print("ROI提取出错!")returnpassif __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()) # 获取车牌ROIif img is None:print("没有提取到车牌")continueexcept:print("没有提取到车牌")