How can I properly detect LetsGoDigital font text? - python

I am on Windows 10, and I try to extract digits from this image
with the pytesseract library with language lets (cf. https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital or LetsGoDigital, cf. https://github.com/arturaugusto/display_ocr).
I preprocessed my image (grey, threshold and erosion) to get:
But the output of
pytesseract.image_to_string(img, lang='lets')
is empty.

You didn't set any specific page segmentation method. I'd opt for --psm 6 here:
Assume a single uniform block of text.
So, even without further pre-processing I get the proper result:
import cv2
import pytesseract
img = cv2.imread('RcVbM.jpg')
text = pytesseract.image_to_string(img, lang='lets', config='--psm 6')
print(text.replace('\n', '').replace('\f', ''))
# 004200
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.1
OpenCV: 4.5.2
pytesseract: 5.0.0-alpha.20201127
----------------------------------------

Related

Reading a CAPTCHA with opencv and pytesseract

I am trying to improve an image in order to making the text more readable for OCR, but the problem is that some images have some missing pixels and OCR doesn't recognized it.
Here is my code:
import cv2 as cv
import pytesseract
import numpy as np
img = cv.imread("image1.jpeg")
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
threshold = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 57, 13)
x = 255 - threshold
kernel = np.ones((3, 3),np.uint8)
closing = cv.morphologyEx(x, cv.MORPH_CLOSE, kernel)
captcha = pytesseract.image_to_string(closing, config="--psm 3")
print(captcha)
cv.imshow('close', closing)
cv.imshow('thresh', threshold)
cv.waitKey(0)
cv.destroyAllWindows()
This is the original image
This is threshold image
And this is the result after using closing morph
For some reason OCR returns the string le eth g
Any idea of how can I improve my code?
Sometimes preprocessing is not needed for the input images. When I tried the input image you gave:
I used the code:
import cv2 as cv
import pytesseract
img = cv.imread("/home/yns/Downloads/t.jpg")
captcha = pytesseract.image_to_string(img, config="--psm 6")
print(captcha)
and the result comes out as:
TTCo7
which is almos correct. it would be better to keep in mind tesseract is more accurate for the aligned texts so even in some CAPTCHA texts you get successful results it will not work fine at all.
For the reference here is the output of tesseract --version:
tesseract 4.1.3 leptonica-1.78.0 libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.2) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
Found AVX2
Found AVX
Found FMA
Found SSE
Found libarchive 3.2.2 zlib/1.2.11 liblzma/5.2.2 bz2lib/1.0.6 liblz4/1.7.1

Pytesseract wrong number detection

My code is:
import cv2,numpy
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # For Windows OS
def scan(image):
try:
img = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR)
except:
img = cv2.imread(image)
# Apply OCR
data = pytesseract.image_to_string(img, config="-c tessedit"
"_char_whitelist=1234567890"
" --psm 6"
" ")
return data
And when I make it scan this image it just gives me ''. Nothing. I don't know whats wrong, works on every other digit number, what should I change? If you have some python ocr that works on this image, you can also send it.
Using Tesseract or any OCR can get really tricky. The pictures you mentioned worked perfectly might have better quality or are closely related to the dataset version you are using in your code/computer.
Some basic steps you can do to improve this are:
Add a new trained data file that has similar font to the font you are trying to detect
Do some preprocessing on the image, sharpen it, change resolution and color, basically the whole routine till you find the perfect mix
Try a different OCR
Let me know if this works!
Read the documentation, understand what are you doing and you will get the correct result. Hint: pretending that the single character is a uniform block of text is not wise.
Your picture works for me. My guess is that you didn't successfully read the image? You can debug by print(img.shape) or if img is None: print('None'). Python might be operating in a different directory. os.getcwd() gets Pythons current working directory. You can also do os.path.isfile(image) to see if Python can find file where you are looking.
This is what I tried:
import cv2,numpy
import pytesseract
# ~ pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" # For Windows OS
img = cv2.imread('niner.png')
# Apply OCR
data = pytesseract.image_to_string(img, config="-c tessedit"
"_char_whitelist=1234567890"
" --psm 6"
" ")
print('tesseract version: ', pytesseract.get_tesseract_version())
print('=============================================')
print(data)
and the result is:
tesseract version: 4.0.0.20181030
leptonica-1.76.0
libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.2.0
=============================================
9
♀

Why is Tesseract unable to detect the single digit in that image?

I have this image, and I'm trying to read it with Tesseract:
My code is like that:
pytesseract.image_to_string(im)
But, what I get is only LOW: 56. So, Tesseract is unable to read the 1 in the first line. I've tried to specify also a whitelist of only digits like
pytesseract.image_to_string(im, config="tessedit_char_whitelist=0123456789.")
and to process the image with an erosion but nothing works. Any suggestions?
Improving the quality of the output is your "holy scripture" when working with Tesseract. Especially, the page segmentation method should always be explicitly set. Here (as most of the times), I'd opt for --psm 6:
Assume a single uniform block of text.
Even without further preprocessing of your image, you already get the desired result:
import cv2
import pytesseract
image = cv2.imread('gBrcd.png')
text = pytesseract.image_to_string(image, config='--psm 6')
print(text.replace('\f', ''))
# 1
# LOW: 56
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.1
OpenCV: 4.5.2
pytesseract: 5.0.0-alpha.20201127
----------------------------------------

Python Script, tesseract. No error but no output

I am trying to use tesseract to read text from an image, i have installed tesseract using home brew but i get no output. Here is my code;
import pytesseract
from PIL import Image
value = Image.open('/Users/director/Desktop/databoy/easytt.jpeg')
text = pytesseract.image_to_string(value, lang="eng")
print(text)
I don't get any errors, and it does not print out anything, i just get:
Process finished with exit code 0

pytesseract fail to recognise digits from binarized image

I am currently facing a problem with pytesseract where the software is unable to detect a number in this image:
For some reason, pytesseract doesn't want to recognise digits in this image. Any suggestions? Here is my code:
import pytesseract
from PIL import ImageEnhance, ImageFilter, Image
img = r'/content/inv_thresh.png'
​
str = pytesseract.image_to_string(Image.open(img), lang='eng', \
config='--psm 8 --oem 3 -c tessedit_char_whitelist=0123456789')
It returns a string COTO
Why you specify --oem 3 (Default, based on what is available.)
Which model you use? Which tesseract version?
Tesseract expect clear image without artifacts to provide correct results => you will need better preprocess image.
I got following result with tessdata_best mode with recent tesseract (4.1/5.0alpha):
tesseract a9Uq4.png - --psm 8 --dpi 70
00308

Categories