为什么Tesseract无法识别牌照上的文字,而easyocr能够识别?

2024-10-03 06:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究自动车牌识别。我可以从初始图像中裁剪出Plate。但是,Tesseract无法识别此铭牌上的文字,而easyocr能够识别。原因是什么?提前感谢您的回答。我用密码从车上提取车牌并识别

import cv2 as cv
import pytesseract
import imutils
import numpy as np
import easyocr
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

img3 = cv.imread("4.png")
cv.imshow("Car", img3)
img3 = cv.cvtColor(img3, cv.COLOR_BGR2RGB)
gray = cv.cvtColor(img3, cv.COLOR_RGB2GRAY)

bfilter_img3 = cv.bilateralFilter(img3, 11, 17, 17)
edged_img3 = cv.Canny(bfilter_img3, 30, 200)

keypoints = cv.findContours(edged_img3.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(keypoints)
contours = sorted(contours, key=cv.contourArea, reverse=True)[:10]

location = None
for contour in contours:
    approx = cv.approxPolyDP(contour, 10, True)
    if len(approx) == 4:
        location = approx
        break

mask = np.zeros(gray.shape, np.uint8)
new_img = cv.drawContours(mask, [location], 0, 255, -1)
new_img = cv.bitwise_and(img3, img3, mask=mask)

print(location)
cv.imshow("Plate", new_img)


(x,y)=np.where(mask==255)
(x1,y1)=(np.min(x),np.min(y))
(x2,y2)=(np.max(x),np.max(y))
cropped_img=gray[x1:x2+1, y1:y2+1]
ret, cropped_img=cv.threshold(cropped_img,127,255,cv.THRESH_BINARY)
cv.imshow("Plate3", cropped_img)
cropped_img = cv.resize(cropped_img, None, fx=2/3, fy=2/3, interpolation=cv.INTER_AREA)
#"cropped_img= the plate image in the question"***********
text = pytesseract.image_to_string(cropped_img)
print("Text by tesseract: ",text)

""""
reader=easyocr.Reader(['en'])
text2=reader.readtext(cropped_img)
print(text2)
"""
k = cv.waitKey(0)

Tags: importimgnpmasklocationcvimshowtesseract
1条回答
网友
1楼 · 发布于 2024-10-03 06:21:26

我有点好奇你为什么用bilateralFilterCannyfindContours等等。?你看到每种方法的结果了吗

无论如何,如果将page-segmentation-mode设置为6,则:

Assume a single uniform block of text.

结果将是:

34 DUA34

代码:

import cv2
import pytesseract

# Load the image
img = cv2.imread("vHQ5q.jpg")

# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# OCR
print(pytesseract.image_to_string(gry, config=" psm 6"))

# Display
cv2.imshow("", gry)
cv2.waitKey(0)

你应该知道Page segmentation method

我使用pytesseract版本-0.3.7得到了结果

相关问题 更多 >