Tesseract OCR cant recognize simple math symbols - python

so basicly im trying to make tesseract ocr do some elementary school math, but it wont work it prints out the math but not the answer to the math, here is my code
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\alloa026\Downloads\project\photo.png'
content = pytesseract.image_to_string(r'C:\Users\alloa026\Downloads\project\photo.png')
print(eval("content.strip()"))
anyone know how to fix this?

have you note that, you should correct the last line
print(eval("content.strip()")
to
print(eval(content.strip())

Related

Pytesseract can not recognize even very simple textline

Binary image B2
Binary image Y2
I think these images are quite simple and clear. Still pytesseract does not work. I really wonder why.
Here is my code
from pytesseract import pytesseract as tesseract
import cv2 as cv
binary = cv.imread(filepath)
lang = 'eng'
config = 'tessedit_char_whitelist=RGB123'
print(tesseract.image_to_string(binary, lang=lang, config=config))
The output is just blank string.
To Dennlinger's point, I would definitely rotate it before sending it through PyTess. PyTess should rotate it automatically though. Should.
Alternatively, I see in your configuration that you have white listed "RGB123" which, correct me if I'm wrong, may mean that PyTess is mainly looking for those specific numbers and characters.
I'd try changing your configuration by omiting that configuration so that it can pick up the "Y" in there.

Why does reading text from image using pytesseract doesnt work?

Here is my code:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'F:\Installations\tesseract'
print(pytesseract.image_to_string('images/meme1.png', lang='eng'))
And here is the image:
And the output is as follows:
GP.
ed <a
= va
ay Roce Thee .
‘ , Pe ship
RCAC Tm alesy-3
Pein Reg a
years —
? >
ee bs
I see the word years in the output so it does recognize the text but why doesn't it recognize it fully?
OCR is still a very hard problem in cluttered scenes. You probably won't get better results without doing some preprocessing on the image. In this specific case it makes sense to threshold the image first, to only extract the white regions (i.e. the text). You can look into opencv for this: https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
Additionally, in your image, there are only two lines of text in arbitrary positions, so it might make sense to play around with page segmentation modes: https://github.com/tesseract-ocr/tesseract/issues/434

pyTesseract not outputing text from image

maybe someone could help me! When I run the following code
import pytesseract
from pytesseract import image_to_string
from PIL import Image
import PIL
file = Image.open('/usr/local/Cellar/tesseract/4.1.0/share/tessdata/cap.png')
we_will = pytesseract.image_to_string(file)
print(we_will)
all that gets outputed is:
Process finished with exit code 0
which is no help. What am I doing wrong?
Sounds like we_will is the empty string. Try printing repr(we_will) to understand this idea more clearly.
IIRC, PyTesseract does this when it can't figure out what the text is inside the image. You can get the best results for a cropped image with a single-color background.

How to restrict the recognized characters in tesserocr?

When using tesserocr how do I limit the set of characters that Tesseract will recognize to just the digits?
I know from this that if I were using c++ I could set a tessedit_char_whitelist in the config file, but I don't know the analogous approach in tesserocr within Python.
In general, the tesserocr documentation gives help that works if the reader already knows the Tesseract API for c++. As I am not fluent in c++, I am hoping to avoid having to read the c++ source code in order to use tesserocr.
If anyone can give me what I actually need to write in python or a general rule for going from config settings to Python code that would be great. Thanks in advance.
Tesserocr works as the C++ API, you can set a whitelist with the function SetVariable.
An example:
from tesserocr import PyTessBaseAPI
from string import digits
with PyTessBaseAPI() as api:
api.SetVariable('tessedit_char_whitelist', digits)
api.SetImageFile('image.png')
print api.GetUTF8Text() # it will print only digits
If you want another approach that is more straightforward and independent from the C++ API, try with the pytesseract module.
An example with pytesseract:
import pytesseract
from PIL import Image
from string import digits
image = Image.open('image.png')
print pytesseract.image_to_string(
image, config='-c tessedit_char_whitelist=' + digits)

Recognize simple digits with pytesser

I'm learning OCR using PyTesser and Tesseract. As the first milestone, I want to write a tool to recognize captcha that simply consists of some digits. I read some tutorials and wrote such a test program.
from pytesser.pytesser import *
from PIL import Image, ImageFilter, ImageEnhance
im = Image.open("test.tiff")
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
text = image_to_string(im)
print "text={}".format(text)
I tested my code with the image below. But the result is 2(T?770. And I've tested some other similar images as well, in 80% case the results are incorrect.
I'm not familiar with imaging processing. I've two questions here:
Is it possible to tell PyTesser to guess digits only?
I think the image is quite easy for human to read. If it is so difficult for PyTesser to read digits only image, is there any alternatives can do a better OCR?
Any hints are very appreciated.
I think your code is quite okay. It can recognize 207770. The problem is at pytesser installation. The Tesseract in pytesser is out-of-date. You'd download a most recent version and overwrite corresponding files. You'd also edit pytesser.py and change
tesseract_exe_name = 'tesseract'
to
import os.path
tesseract_exe_name = os.path.join(os.path.dirname(__file__), 'tesseract')

Categories