Python PyteSeract图像\u到\u字符串空输出

Python PyteSeract图像\u到\u字符串空输出,python,python-3.x,opencv,cv2,python-tesseract,Python,Python 3.x,Opencv,Cv2,Python Tesseract,我有一张从另一张图片上剪下来的图片,我想把这张图片作为image_to_string方法的输入: import pytesseract import cv2 num_plate = cv2.imread('E:\Images\car_plate222.jpeg' , cv2.IMREAD_GRAYSCALE) pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' cv2.di

我有一张从另一张图片上剪下来的图片,我想把这张图片作为image_to_string方法的输入:

import pytesseract
import cv2
num_plate = cv2.imread('E:\Images\car_plate222.jpeg' , cv2.IMREAD_GRAYSCALE)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
cv2.dilate(num_plate, (15, 15), num_plate)
pytesseract.image_to_string(num_plate)
这是照片: 汽车牌照:

为了获得更好的性能,我使用了膨胀,但它并没有给我想要的输出(有时给我空字符串,有时给我奇怪的输出)


有人知道怎么回事吗?

在将图像传递给
pytesseract
之前,必须
设置阈值。这提高了准确性。
以下是一个示例:

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

# Grayscale image
img = Image.open('E:\\WorkDir\\KAVSEE\\Python\\test.jpg').convert('L')  
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)

# Older versions of pytesseract need a pillow image
# Convert back if needed
img = Image.fromarray(img.astype(np.uint8))

print(pytesseract.image_to_string(img))

希望这有帮助:)

在将图像传递到
pytesseract
之前,您必须
threshold
图像。这提高了准确性。 以下是一个示例:

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

# Grayscale image
img = Image.open('E:\\WorkDir\\KAVSEE\\Python\\test.jpg').convert('L')  
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)

# Older versions of pytesseract need a pillow image
# Convert back if needed
img = Image.fromarray(img.astype(np.uint8))

print(pytesseract.image_to_string(img))

希望这有帮助:)

所以没有实际的错误,而是函数
image\u to_string
的OCR性能很差,对吗?您在这里看到过类似的问题吗?您可以尝试多个参数,例如
--psm
。请参阅@Arnaud感谢您的回复。我尝试了各种“-psm”,但没有成功,很多时候它返回“ili”作为输出,我不知道为什么。是的,我认为此错误与OCR有关。请看一看,这样就没有实际的错误,而是函数
image\u to\u string
的OCR性能较差,是这样吗?您在这里看了类似的问题吗?您可以尝试使用多个参数,例如
--psm
。请参阅@Arnaud感谢您的回复。我尝试了各种“-psm”,但没有成功,很多时候它返回“ili”作为输出,我不知道为什么。是的,我认为此错误与OCR有关。请查看