Pytesseract读取文本时出现随机错误 [英] Pytesseract random bug when reading text

查看:99
本文介绍了Pytesseract读取文本时出现随机错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为视频游戏创建机器人,我必须阅读屏幕上显示的一些信息.鉴于信息总是在同一位置,因此我可以截取屏幕截图并将图片裁剪到正确的位置.

I'm creating a bot for a video game and I have to read some information displayed on the screen. Given that the information is always at the same position, I have no issue to take a screenshot and crop the picture to the right position.

90%的时间,识别将是完美的,但是有时它会返回看起来完全随机的东西(请参见下面的示例).

90% of the time, the recognition will be perfect, but sometimes it will return something that seems totally random (see the example below).

我尝试将图片转换为黑白没有成功,并尝试更改pytesseract配置(config = ("-l fra --oem 1 --psm 6"))

I've tried to turn the picture into black and white with no success, and tried to change the pytesseract config (config = ("-l fra --oem 1 --psm 6"))

def readScreenPart(x,y,w,h):
    monitor = {"top": y, "left": x, "width": w, "height": h}
    output = "monitor.png"
    with mss.mss() as sct:
        sct_img = sct.grab(monitor)        
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)

    img = cv2.imread("monitor.png")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite("result.png", img)
    config = ("-l fra --oem 1 --psm 6")

    return pytesseract.image_to_string(img,config=config)

示例:此图片生成错误,它返回字符串"IRPMV/LEIILK"

Example : this picture generates a bug, it returns the string "IRPMV/LEIILK"

另一张图片

现在,我不知道问题的根源,因为它不仅是一个错误的字符,而且是一个完全随机的结果.

Now I don't know where the issue comes from, given that it is not just a single wrong character but a totally random result..

感谢您的帮助

推荐答案

正如评论所说,这与您的文本和背景色有关. Tesseract对于深色背景上的浅色文本基本上是没有用的,这是我在将其提供给tesseract之前适用于任何文本图像的几行内容:

As the comment said, it's about your text and background color. Tesseract is basically useless with light text on dark background, here is the few lines i apply to any text image before giving it to tesseract :

# convert color image to grayscale
grayscale_image = cv2.cvtColor(your_image, cv2.COLOR_BGR2GRAY)

# Otsu Tresholding method find perfect treshold, return an image with only black and white pixels
_, binary_image = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)

# we just don't know if the text is in black and background in white or vice-versa
# so we count how many black pixels and white pixels there are
count_white = numpy.sum(binary > 0)
count_black = numpy.sum(binary == 0)

# if there are more black pixels than whites, then it's the background that is black so we invert the image's color
if count_black > count_white:
    binary_image = 255 - binary_image

black_text_white_background_image = binary_image

现在,无论原始颜色是哪种颜色,您都肯定在白色背景上有黑色文字,如果字符高度为35像素,Tesseract也是(最奇怪的)最有效的方法,较大的字符不会显着降低精度,但短一些像素会使tesseract失去作用!

Now you're sure to have black text on white background no matter wich colors was the original image, also Tesseract is (weirdly) the most efficient if the characters have an height of 35pixels, larger characters doesn't significantly reduce the accuracy, but just a few pixels shorter can make tesseract useless!

这篇关于Pytesseract读取文本时出现随机错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆