车牌识别OCR

3

pyterresect的最终调用没有返回一个字符串,而是仅输出该图像每个像素的值。

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

count = 0
for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx #This is our approx Number Plate Contour
            pl=NumberPlateCnt
            print(NumberPlateCnt)
            if(pl[0][0][1]+10>pl[2][0][1] or pl[0][0][0]+40>pl[2][0][0]):
                continue
            filter_img = image[pl[0][0][1]:pl[2][0][1],pl[0][0][0]:pl[2][0][0]]
            print("Number Plate Detected")
            cv2_imshow(filter_img)

            Number=pytesseract.image_to_string(filter_img,lang='eng')
            print("Number is :",Number)
            cv2.waitKey(0)
            cv2.drawContours(image, [NumberPlateCnt], -1, (0, 255, 0), 3)

print("Final Image With Number Plate Detected")
cv2_imshow(image)

cv2.waitKey(0) #Wait for user input before closing the images displayed

我在这里得到的数字应该是一些字符串,但它打印出来像一种矩阵,就像我们使用print打印图像时一样。

1个回答

0

你得到的矩阵很可能来自你代码中的这一行:

print(NumberPlateCnt)

pytesseract.image_to_string 无法识别您尝试获取的矩形轮廓上的任何文本,这意味着以下两行代码将输出空结果:

Number=pytesseract.image_to_string(filter_img,lang='eng')
print("Number is :",Number)

由于您正在遍历具有最大区域的轮廓,因此输出应该看起来像这样:

检测到车牌

号码是:

[[[223 278]]

[[272 279]]

[[274 282]]

[[224 281]]]

"号码是:"字符串为空,接下来的轮廓计算会得出上述矩阵结果。

为了解决这个问题,您可以检查 PyTesseract 返回的字符串是否包含任何内容,例如:

   if (len(Number)):
       print("Number is :", Number)

只有在包含PyTesseract识别的任何符号时,它才会打印数字。


在这种情况下,它也打印相同的矩阵。但是我传递给函数的图像仅包含边界和字母数字字符。 - Skand
1
你能分享一下你正在尝试的图片示例吗? - Michael Glazunov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接