简体   繁体   English

如何强制 tesseract 从左到右读取,Python

[英]How to force tesseract to read left to right, Python

I have a barchart which I would like to extract the data points from.我有一个条形图,我想从中提取数据点。 在此处输入图像描述

However, when tesseract reads the image, it is reading left to right AND top to bottom.但是,当 tesseract 读取图像时,它是从左到右和从上到下读取的。 From my output, you can see that bars with the same height are read in the left to right order:从我的 output 中,您可以看到相同高度的条形按从左到右的顺序读取:

60.8
58.8 58.8 
58.1
56.9 56.8
54.6 547
51.8 52.2
51:3 
48.7

Jul 2019 Oct 2019 Jan 2020 Apr 2020

I do not want bars of the same height to be read at the same time.我不希望同时读取相同高度的条形图。 instead, I want tesseract to read only left to right (instead of the additional top to bottom it seems to be doing).相反,我希望 tesseract 只从左到右读取(而不是它似乎在做的额外的从上到下)。

I read in another post that the image should be transposed to achieve this, but if I do that, tesseract seems to have trouble reading the transposed image.我在另一篇文章中读到应该转置图像以实现此目的,但如果我这样做,则 tesseract 似乎无法读取转置后的图像。

Any insight you have would be helpful.您的任何见解都会有所帮助。 Thanks谢谢

from PIL import Image, ImageEnhance, ImageFilter
#from pytesseract import image_to_string
import pytesseract
import cv2


pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
from pytesseract import image_to_string
im = Image.open(r'C:\Users\Root\im.png')
print(im)

#Resizing and Transposing
new_size = tuple(6*y for y in im.size)
im = im.resize(new_size, Image.ANTIALIAS)
im  = im.transpose(Image.ROTATE_90)
im.save(r'C:\Users\Root\test.png', 'PNG')

#Grayscale for enhanced reading quality
im = cv2.imread(r'C:\Users\Root\test.png')
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

print(image_to_string(gray, lang='eng'))

This is unfortunately not the most portable solution, but if you are going to have a lot of bar graphs that look like this, you can slice the image into pieces for each bar, left to right, and run the OCR on each slice separately:不幸的是,这不是最便携的解决方案,但如果您要拥有很多看起来像这样的条形图,您可以将图像从左到右分割成每个条形图,并分别在每个切片上运行 OCR:

width, height = im.size
numBars = 12
bottomLabelHeight = 100
leftOffset = 10
rightOffset = 20
barWidth = (width - leftOffset - rightOffset) // numBars
for i in range(leftOffset + barWidth, width - rightOffset, barWidth):
    left = i - barWidth
    right = i
    top = 0
    bottom = height - bottomLabelHeight
    bar = im.crop((left, top, right, bottom)).convert('L') # Convert to grayscale
    # bar.show() # uncomment to show bar for testing

    # Need digits whitelist for OCR to work properly
    value = image_to_string(bar, lang='eng', 
        config='--psm 10 --oem 3 -c tessedit_char_whitelist=.0123456789')
    print(value)

    # Read label for bar
    top = height - bottomLabelHeight
    bottom = height
    label = im.crop((left, top, right, bottom)).convert('L')
    print(image_to_string(label, lang='eng'))

Output: Output:

60.8

58.1
Jul 2019
58.8

56.9

51.8
Oct 2019
54.6

56.8

58.8
Jan 2020
54.2

51.3

52.2
Apr 2020
48.7

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

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