我正在尝试识别车牌,但出现错误,例如错误/无法读取字符

这是每个步骤的可视化:

通过颜色阈值+变形闭合获得蒙版

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

车牌轮廓过滤器以绿色突出显示

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

将板轮廓粘贴到空白蒙版上

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

Tesseract OCR的预期结果



但是我得到的结果是



我尝试将轮廓切成3片

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP
python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

是的,它正在工作,但是如果我将差异图像插入此方法,则某些图像无法识别,例如此图像

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

字母N无法识别,但是如果使用第一种方法,它将起作用

python - 无法正确识别车牌(Python,OpenCv,Tesseract)-LMLPHP

这是代码

import numpy as np
import pytesseract
import cv2
import os

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = "data"

for nama_file in sorted(os.listdir(image_path)):
    print(nama_file)
    # Load image, create blank mask, convert to HSV, define thresholds, color threshold
    I = cv2.imread(os.path.join(image_path, nama_file))
    dim = (500, 120)
    I = cv2.resize(I, dim, interpolation = cv2.INTER_AREA)
    (thresh, image) = cv2.threshold(I, 127, 255, cv2.THRESH_BINARY)
    result = np.zeros(image.shape, dtype=np.uint8)
    result = 255 - result
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    lower = np.array([0,0,0])
    upper = np.array([179,100,130])
    mask = cv2.inRange(hsv, lower, upper)
    slices = []
    slices.append(result.copy())
    slices.append(result.copy())
    slices.append(result.copy())
    i = 0
    j = 0
    xs = []

    # Perform morph close and merge for 3-channel ROI extraction
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
    close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
    extract = cv2.merge([close,close,close])

    # Find contours, filter using contour area, and extract using Numpy slicing
    cnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
    key=lambda b:b[1][0], reverse=False))
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        area = w * h
        ras = format(w / h, '.2f')
        if h >= 40 and h <= 70 and w >= 10 and w <= 65 and float(ras) <= 1.3:
            cv2.rectangle(I, (x, y), (x + w, y + h), (36,255,12), 3)
            result[y:y+h, x:x+w] = extract[y:y+h, x:x+w]
            # Slice
            xs.append(x)
            if i > 0:
                if (xs[i] - xs[i-1]) > 63:
                    j = j+1
            i = i + 1
            slices[j][y:y+h, x:x+w] = extract[y:y+h, x:x+w]

    # Split throw into Pytesseract
    j=0
    for s in slices:
        cv2.imshow('result', s)
        cv2.waitKey()
        if j != 1 :
            data = pytesseract.image_to_string(s, lang='eng',config='--psm 6 _char_whitelist=ABCDEFGHIJKLMNOPQRTUVWXYZ')
        else :
            data = pytesseract.image_to_string(s, lang='eng',config='--psm 6 _char_whitelist=1234567890')
        print(data)

    # Block throw into Pytesseract
    data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
    print(data)

    cv2.imshow('image', I)
    cv2.imshow('close', close)
    cv2.imshow('extract', extract)
    cv2.imshow('result', result)
    cv2.waitKey()

也许有人知道为什么会这样,应该怎么办?

提前致谢

最佳答案

我尝试了很多事情,找到了某种解决方案:

应用膨胀形态学运算使字母变薄:

# Split throw into Pytesseract
j=0
for s in slices:
    cv2.imshow('result', s)
    cv2.waitKey(1)
    if j != 1:
        data = pytesseract.image_to_string(s, config="-c tessedit"
                                                      "_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
                                                      " --psm 6"
                                                      " ")


        if data=='':
            s = cv2.dilate(s, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)))
            cv2.imshow('cv2.dilate(s)', s)
            cv2.waitKey(1)
            data = pytesseract.image_to_string(s, config="-c tessedit"
                                                         "_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
                                                         " --psm 6"
                                                         " ")
    else:
        pytesseract.pytesseract.tessedit_char_whitelist = '1234567890'
        data = pytesseract.image_to_string(s, lang='eng',config='--psm 6 _char_whitelist=1234567890')

    print(data)

这种行为很奇怪。
有很多抱怨,建议的解决方案不起作用。

例如,请参见以下帖子:Tesseract does not recognize single characters

至少我学会了如何使用_char_whitelist选项(您需要添加-c tessedit)...

我认为该解决方案不够强大(可能是偶然的)。
我认为当前版本的Tesseract中没有简单的解决方案。

关于python - 无法正确识别车牌(Python,OpenCv,Tesseract),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61161363/

10-12 18:42