如何获得Tesseract来阅读此Python OpenCV项目中的车牌?

问题描述

enter image description here

enter image description here

我的OpenCV代码可以正常工作。它找到车牌,使用轮廓提取其黑白版本,然后将其传递给PyTesseract时,它不会读取任何字母。我已经在代码的每一行中跟踪了该程序,并且OpenCV可以正常工作,但是PyTesseract不会从图像中提取文本。没有错误,只是不读取任何文本。车牌是我的。

import cv2
# pip install imutils
import imutils
import PyTesseract

PyTesseract.PyTesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'

# Read the image file
image = cv2.imread('LP.jpg')
# image = imutils.resize(image,width=500)

# Convert to Grayscale Image
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# Removes Noise
gray_image = cv2.bilateralFilter(gray_image,11,17,17)

# Canny Edge Detection
canny_edge = cv2.Canny(gray_image,100,200)

# Find contours based on Edges
# The code below needs an - or else you'll get a ValueError: too many values to unpack (expected 2) or a numpy error
_,contours,new = cv2.findContours(canny_edge.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours,key=cv2.contourArea,reverse=True)[:30]

# # Initialize license Plate contour and x,y coordinates
contour_with_license_plate = None
license_plate = None
x = None
y = None
w = None
h = None

# Find the contour with 4 potential corners and create a Region of Interest around it
for contour in contours:
    # Find Perimeter of contour and it should be a closed contour
    perimeter = cv2.arcLength(contour,True)
    approx = cv2.approxpolyDP(contour,0.02 * perimeter,True)
    # This checks if it's a rectangle
    if len(approx) == 4:
        contour_with_license_plate = approx
        x,y,w,h = cv2.boundingRect(contour)
        license_plate = gray_image[y:y + h,x:x + w]
        break


# # approximate_contours = cv2.drawContours(image,[contour_with_license_plate],-1,(0,255,0),3)

# Text Recognition
text = PyTesseract.image_to_string(license_plate,lang='eng')
print(text)
# Draw License Plate and write the Text
image = cv2.rectangle(image,(x,y),(x+w,y+h),3)
image = cv2.putText(image,text,(x-100,y-50),cv2.FONT_HERShey_SIMPLEX,3,6,cv2.LINE_AA)

print("License Plate: ",text)

cv2.imshow("License Plate Detection",image)
cv2.waitKey(0)

解决方法

这是我的部分答案,也许您可​​以完善它。

aws apigateway get-export --rest-api-id ${API_ID} --stage-name ${STAGE_NAME} --export-type swagger swagger.json + adaptive-threshold操作应用于bitwise-not变量。

结果将是:

enter image description here

现在,如果您阅读它:

license_plate

结果:

txt = pytesseract.image_to_string(bnt,config="--psm 6")  
print(txt)

很遗憾,277 BOY 被识别为Q

代码:(只需将文本识别注释部分替换为下面的内容)


O