简体   繁体   English

没有从 pytesseract 得到正确的结果

[英]Not getting correct results from pytesseract

I am trying to do OCR in python but not getting the correct output.我正在尝试在 python 中进行 OCR,但没有得到正确的 output。 Here is the code.这是代码。 I tried with original image, grayscale also but not getting any result我尝试使用原始图像,灰度也但没有得到任何结果

from PIL import Image
import pytesseract

def convert_to_monochrome(image):
    pixels = image.load()
    for i in range(image.size[0]): # for every pixel:
        for j in range(image.size[1]):
            r, g, b = pixels[i, j]
            if r > 200 and g > 200 and b > 200:
                pixels[i, j] = (255, 255, 255)
            else:
                pixels[i, j] = (0, 0, 0)
    return image

def interpret_chips(image):
    #image = image.resize((image.size[0] * 10, image.size[1] * 10), Image.ANTIALIAS)
    #image = image.convert("LA")
    #image.show()
    _image = convert_to_monochrome(image)
    _image.show()
    _image.save("chips.jpg")
    config = "--psm 7 -c tessedit_char_whitelist=0123456789KMT"
    rank_string = pytesseract.image_to_string(_image, config=config)  # expensive
    return _image, rank_string

for i in range(1, 6):
    print(i)
    img = Image.open("temp/sample" + str(i) + ".jpg")
    img, text = interpret_chips(img)
    print(text)
    img.save("temp/monochrome" + str(i) + ".jpg")

Thanks for your help谢谢你的帮助

I am attaching some original images for which it is giving wrong results.我附上了一些给出错误结果的原始图像。 Pre processed images are obtained after applying monochrome function defined please have a look.应用单色 function 定义后获得预处理图像,请查看。 Text can be of type 4, 400, 4000, 459K, 29M etc. I am getting very awkward results.文本可以是4、400、4000、459K、29M等类型。我得到的结果非常尴尬。

Raw Image 1原始图像 1

Raw Image 2原始图像 2

Raw Image 3原始图像 3

Pre processed 1预处理 1

Pre processed 2预处理 2

Pre processed 3预处理 3

The problem is that tesseract expects an image with dark text on a light background.问题是 tesseract 需要在浅色背景上带有深色文本的图像。 The preprocessed image in your case is just the opposite.您的情况下的预处理图像正好相反。 So you can just invert the preprocessed image.所以你可以反转预处理的图像。 Below code worked for me:下面的代码对我有用:

from PIL import Image
import pytesseract


def convert_to_monochrome(image):
    pixels = image.load()
    for i in range(image.size[0]): # for every pixel:
        for j in range(image.size[1]):
            r, g, b = pixels[i, j]
            if r > 200 and g > 200 and b > 200:
                pixels[i, j] = (0, 0, 0)
            else:
                pixels[i, j] = (255, 255, 255)
    return image


def interpret_chips(image):
    #image = image.resize((image.size[0] * 10, image.size[1] * 10), Image.ANTIALIAS)
    #image = image.convert("LA")
    #image.show()
    _image = convert_to_monochrome(image)
    _image.show()
    _image.save("chips.jpg")
    config = "--psm 6 -c tessedit_char_whitelist=0123456789KMT"
    rank_string = pytesseract.image_to_string(_image, config=config)  # expensive
    return _image, rank_string


img = Image.open("orig.jpg")
img, text = interpret_chips(img)
print(text)


orig.jpg:原始文件.jpg: 在此处输入图像描述

text is 23.000, text23.000,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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