如果出现未知字符,Tesseract有时会返回一个空字符串

时间:2019-08-30 09:12:47

标签: python tesseract python-tesseract

我正在使用Tesseract来检测带有白色背景的黑色文字。在某些图像中,黑字出现后是一个信息符号。我对检测此符号不感兴趣,仅对单词感兴趣。有时将信息符号(带有圆圈的圆圈)检测为0或O,就可以了。但是在其他情况下(可能是tesseract不知道如何处理该符号),它只是返回一个空字符串,因此单词也不会返回。我使用给出的here代码,还尝试了建议的配置here

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

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to input image to be OCR'd")
ap.add_argument("-p", "--preprocess", type=str, default="thresh",
    help="type of preprocessing to be done")
args = vars(ap.parse_args())

# load the example image and convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# check to see if we should apply thresholding to preprocess the
# image
if args["preprocess"] == "thresh":
    gray = cv2.threshold(gray, 0, 255,
        cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

# make a check to see if median blurring should be done to remove
# noise
elif args["preprocess"] == "blur":
    gray = cv2.medianBlur(gray, 3)

# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)

# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
text = pytesseract.image_to_string(gray, config='--psm 7')
os.remove(filename)
print("Output: " + text)

如果有人知道我还能做什么,我将非常感激!

1 个答案:

答案 0 :(得分:0)

已解决:config必须为config ='-psm 7',且只有一个“-”