簡體   English   中英

從帶有 opencv python 的車牌識別字符

[英]character recognition from license plate with opencv python

我正在開展一個關於使用 opencv python 從車牌識別字符的項目。 我有這張圖片: 在此處輸入圖像描述

我嘗試了 tesseract,結果是:7G285274-AF 現在我真的不知道該怎么辦,如果有人知道請告訴我

首先,我從汽車圖像中檢測車牌,然后我必須從車牌中識別字符。 這是我的代碼:

import numpy as np
import cv2
from PIL import Image
import pytesseract 

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'


def ratioCheck(area, width, height):
    ratio = float(width) / float(height)
    if ratio < 1:
        ratio = 1 / ratio
    if (area < 1063.62 or area > 73862.5) or (ratio < 3 or ratio > 6):
        return False
    return True
    



def ratio_and_rotation(rect):
    (x, y), (width, height), rect_angle = rect
    if(width>height):
        angle = -rect_angle
    else:
        angle = 90 + rect_angle
    if angle>15:
         return False
    if height == 0 or width == 0:
        return False
    area = height*width
    if not ratioCheck(area,width,height):
        return False
    else:
        return True


def clean2_plate(plate):
    gray_img = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray_img, 110, 255, cv2.THRESH_BINARY)
    if cv2.waitKey(0) & 0xff == ord('q'):
        pass
    num_contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    if num_contours:
        contour_area = [cv2.contourArea(c) for c in num_contours]
        max_cntr_index = np.argmax(contour_area)
        max_cnt = num_contours[max_cntr_index]
        max_cntArea = contour_area[max_cntr_index]
        x,y,w,h = cv2.boundingRect(max_cnt)
        if not ratioCheck(max_cntArea,w,h):
            return plate,None
        final_img = thresh[y:y+h, x:x+w]
        return final_img,[x,y,w,h]
    else:
        return plate, None
        

def isMaxWhite(plate):
    avg = np.mean(plate)
    if(avg>=115):
        return True
    else:
         return False
         

img = cv2.imread("car.jpg")
cv2.imshow("input",img)
img2 = cv2.GaussianBlur(img, (3,3), 0)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
img2 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3)   
_,img2 = cv2.threshold(img2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3))
morph_img_threshold = img2.copy()
cv2.morphologyEx(src=img2, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
num_contours, hierarchy= cv2.findContours(morph_img_threshold,mode=cv2.RETR_EXTERNAL,method=cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img2, num_contours, -1, (0,255,0), 1)



for i,cnt in enumerate(num_contours):

    min_rect = cv2.minAreaRect(cnt)

    if ratio_and_rotation(min_rect):
        x,y,w,h = cv2.boundingRect(cnt)
        plate_img = img[y:y+h,x:x+w]
        print("Number  identified number plate...")
        cv2.imshow("num plate image",plate_img)
        if(isMaxWhite(plate_img)):
            clean_plate, rect = clean2_plate(plate_img)
            if rect:
                fg=0
                x1,y1,w1,h1 = rect
                x,y,w,h = x+x1,y+y1,w1,h1
                plate_im = Image.fromarray(clean_plate)
                text = pytesseract.image_to_string(plate_im, lang='eng', config='--psm 7 --oem 3')
                print("Number  Detected Plate Text : ",text)





cv2.waitKey()

使用easyocr 它使用深度學習模型:

# install using python -m pip install easyocr

import easyocr

# create reader
reader = easyocr.Reader(['en'])

# read characters 
img = "https://i.stack.imgur.com/2Kzu8.png"

characters = reader.readtext(img, detail=0)
print(characters)
# ['ZG', '8524AF']

有關更多詳細信息、參數和語言,請參閱文檔

Colab 的更新和結果

為避免捕獲無車牌號字符,請使用允許列表參數

#Narrowing characters 
import string

ALLOWED_LIST = string.ascii_uppercase+string.digits
characters = reader.readtext(img, detail=0, allowlist=ALLOWED_LIST )
print(characters)
# ['ZG', '8524AF']

我從 Google Colab 獲得的結果

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM