当前位置: 首页 > 知识库问答 >
问题:

在这个Python OpenCV项目中,我如何让Tesseract读取车牌?

白和泽
2023-03-14

我的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), (0, 255, 0), 3)
image = cv2.putText(image, text, (x-100, y-50), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 6, cv2.LINE_AA)

print("License Plate: ", text)

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

共有1个答案

沃侯林
2023-03-14

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

自适应阈值按位不操作应用于license_plate变量

结果将是:

现在如果你读到它:

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

结果:

277 BOY

不幸的是Q被识别为O

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

thr = cv2.adaptiveThreshold(license_plate, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY_INV, 91, 93)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
print(txt)
 类似资料:
  • 以下是我的Spring批处理用例。 > 处理记录。 将处理过的记录逐一写入。 我很清楚第2步和第3步的内容,但不知道如何实现一个可以一次性读取所有记录的读卡器。如何将记录逐一传递给项目处理者/编写者?我应该使用tasklet而不是reader/writer吗?

  • 问题内容: 我想让Maven2为我的应用程序生成.jnlp(webstart配置文件)。 有一个maven-jnlp-plugin,但自Maven 1.1-beta2以来的某个地方已不推荐使用。 有没有更优选的方法来执行此操作,或者我需要自己构建此功能? 问题答案: 还有用于maven2的webstart-maven-plugin

  • 在eclipse中,我有两个项目,每个项目都用servlet填充。项目A在其生成路径中包含项目B。 提前感谢!

  • 问题内容: 在发布此问题之前,我用Google搜索Spring项目(不是基于Web的项目)中的属性。我很困惑,因为每个人都在谈论application- context.xml并具有类似的配置 但是,我正在使用Spring进行普通的Java项目(没有Web应用程序之类的东西)。但是我想从属性文件中获取一些通用属性,并且需要在JAVA文件中使用。如何通过使用Spring / Spring注释实现此目

  • 我的看起来是: 并且我正在尝试使用以下命令访问连接字符串: 这将抛出一个空异常错误,我不确定原因。

  • 我想在我的应用程序中实现autolinktextview。github中有一个库,现在我想在我的应用程序中使用,但库语言是kotlin,我的应用程序项目语言是java。下面给出的库Url和我使用的依赖项。 https://github.com/armcha/AutoLinkTextViewV2