pytesseract.image_to_string 似乎无法从图像中提取文本

pytesseract.image_to_string doesn't seem to be able to extract text from the image

我正在尝试从图像中提取文本,但是,使用我在其他图像上尝试过的以下代码,它有效但不适用于此图像。代码有问题吗?

试图从中提取文本的图像: 这是代码:

import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

try:
    from PIL import Image
except ImportError:
    import Image

# Import image, convert,resize and noise removal
img = cv2.imread("sample01.png", cv2.IMREAD_GRAYSCALE)
print('Dimension of image: {}'.format(img.ndim))
img = cv2.resize(img, None, fx=2, fy=2)
blur = cv2.GaussianBlur(img, (5, 5), 0)

# Apply adaptiveThreshold (Mean)
th2 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2)
cv2.imwrite('resize_adaptive_threshmean.png', th2)

# Apply Tesseract to detect words
print(pytesseract.image_to_string(Image.open('resize_adaptive_threshmean.png')))   
print("=========================================================")

代码有问题吗?

import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

try:
    from PIL import Image
except ImportError:
    import Image
data = pytesseract.image_to_string(Image.open("sample01.png"))
print(data)

?

嗯,你可以用adaptive-thresholding

import cv2
import numpy as np
import pytesseract

img = cv2.imread("ACtBA.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flt = cv2.adaptiveThreshold(gry,
                            100, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY, 15, 16)
txt = pytesseract.image_to_string(flt)
print(txt)

图像将是:

结果:

Parking: You may park anywhere on the campus where there are no signs prohibiting par-
king. Keep in mind the carpool hours and park accordingly so you do not get blocked in the
afternoon

Under Schoo! Age Children.While we love the younger children, it can be disruptive and
inappropriate to have them on campus during school hours. There may be special times
that they may be invited or can accompany a parent volunteer, but otherwise we ask that
you adhere to our —_ policy for the benefit of the students and staff.

我测试了不同的参数,所以我认为最合适的参数是:

maxValue = 100  # Display pixels greater than maxValue

blockSize=15. # size of neighbourhood area.

C=16  #  just a constant which is subtracted from the mean or weighted mean calculated.