简体   繁体   中英

Pytesseract image_to_string empty output

I have this image that was cropped from another image and I want to give this image as an input to image_to_string method:

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)

Here's the photo: Car Plate:
车牌

I used dilation for better performance, but it doesn't give me desired output (Sometimes gives me empty string and sometimes gives me weird output)

Does anybody know what's wrong?

You must threshold the image before passing it to pytesseract . That increases the accuracy. Here is a sample:

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))

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM