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

2024-09-27 00:12:24 发布

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

我有一张从另一张图片上剪下来的图片,我想把这张图片作为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)

这是照片: 汽车牌照:
Car Plate

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

有人知道怎么了吗


Tags: to方法imageimportstring图片carcv2
1条回答
网友
1楼 · 发布于 2024-09-27 00:12:24

在将图像传递给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))

希望这有帮助:)

相关问题 更多 >

    热门问题