python+opencv-tessert OCR 实现简易的车牌的检测与识别全


花了十天时间零基础学习了opencv,并做了一个车牌检测与识别的设计,效果不错,与大家分享一下。源代码,原图片,tessert-OCR安装包以及OCR的中文包都在这里面
资源截图
代码片段和文件信息
# 车牌识别
import cv2 as cv
import numpy as np
import pytesseract as tess
from PIL import Image


def license_prepation(image):
    image_hsv = cv.cvtColor(image cv.COLOR_BGR2HSV)  # 将图像转换为hsv色彩空间
    low_hsv = np.array([108 43 46])  # 设置颜色
    high_hsv = np.array([124 255 255])
    mask = cv.inRange(image_hsv lowerb=low_hsv upperb=high_hsv)  # cv.inrange 函数用来追踪image
    image_dst = cv.bitwise_and(image image mask=mask)  # 取frame与mask中不为0的相与,mask=mask必须有
    # cv.imshow(‘license_dst‘ image_dst)
    image_blur = cv.GaussianBlur(image_dst (7 7) 0)
    # cv.imshow(‘license_blur‘image_blur)
    image_gray = cv.cvtColor(image_blur cv.COLOR_BGR2GRAY)
    ret binary = cv.threshold(image_gray 0 255 cv.THRESH_BINARY | cv.THRESH_OTSU)
    # cv.imshow(‘binary‘ binary)
    kernel1 = cv.getStructuringElement(cv.MORPH_RECT (4 6))
    image_opened = cv.morphologyEx(binary cv.MORPH_OPEN kernel1)
    # cv.imshow(‘license_opened‘ image_opened)
    kernel2 = cv.getStructuringElement(cv.MORPH_RECT (7 7))
    image_closed = cv.morphologyEx(image_opened cv.MORPH_CLOSE kernel2)
    # cv.imshow(‘license_closed‘ image_closed)
    return image_closed


def choose_license_area(contours Min_Area):
    temp_contours = []
    for contour in contours:
        if cv.contourArea(contour) > Min_Area:  # 面积大于MIN_AREA的区域保留
            temp_contours.append(contour)
    license_area = []
    for temp_contour in temp_contours:
        rect_tupple = cv.minAreaRect(temp_contour)
        # print(rect_tupple)
        rect_width rect_height = rect_tupple[1]  # 0为中心点,1为长和宽,2为角度
        if rect_width < rect_height:
            rect_width rect_height = rect_height rect_width
        aspect_ratio = rect_width / rect_height
        # 车牌正常情况下宽高比在2 - 5.5之间
        if aspect_ratio > 2 and aspect_ratio < 5.5:
            license_area.append(temp_contour)
            rect_vertices = cv.boxPoints(rect_tupple)
            rect_vertices = np.int0(rect_vertices)
    return license_area


def license_segment(license_area):
    if (len(license_area)) == 1:
        for car_plate in license_area:
            # print(car_plate.shape)
            # print(car_plate)
            row_min col_min = np.min(car_plate[: 0 :] axis=0)  # 行是row 列是col
            row_max col_max = np.max(car_plate[: 0 :] axis=0)
            # cv.rectangle(license (row_min col_min) (row_max col_max) (0 255 0)2)
            card_img = license[col_min:col_max row_min:row_max :]
            # cv.imshow(“card_img“ card_img)
            cv.imwrite(“card_img.jpg“ card_img)
    return card_img


def recognize_text(image):
    gray = cv.cvtColor(image cv.COLOR_BGR2GRAY)
    ret binary = cv.threshold(gray 120 255 cv.THRESH_BINARY_INV)
    cv.imshow(‘bin‘ binary)  # 白底黑字
    bin1 = cv.resize(binary (370 82))
    kernel1 = cv.getStructuringElement(cv.MORPH_RECT (2 5))
    dilated = cv.dilate(bin1 kernel1)  # 白色膨胀
    te

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件   52662579  2020-09-25 10:19  CSDNchi_sim.traineddata

     文件       3951  2020-09-25 13:11  CSDNlicense recognition_2.py

     文件    5549925  2020-09-23 14:33  CSDNlicense.png

     文件   42424562  2020-09-22 19:21  CSDN esseract-ocr-setup-4.00.00dev.exe

     目录          0  2020-09-29 13:41  CSDN

----------- ---------  ---------- -----  ----

            100641017                    5


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)