IordanouGiannis
IordanouGiannis

Reputation: 4357

Why pytesseract does not recognise single digits?

I am performing ocr on a site and specifically on these two images: image1.png

image2.png

I am fairly new to OCR, I use the following:

from PIL import Image
import pytesseract

my_image = '....png'
text = pytesseract.image_to_string(Image.open(my_image))

In the second image it recognises everything except the single digits 3, 4, 5, 6.

In the first image it does not recognises the single digits too.

I preprocess the images by resizing them, inverting them and using threshold.

It's a standard font so I know there are other ways to do this, but until a certain degree it works for me, so I want to keep it simple before going to something more advanced.

Upvotes: 1

Views: 7000

Answers (1)

Ahx
Ahx

Reputation: 8005

For the both image, you can

  1. upsample the image : For accurate recognition.
  2. apply simple-thresholding: To display the features.

For the first image, you can take part of the image selecting a range:

enter image description here

Result will be:

62001
33000

Code:


import cv2
import pytesseract

img1 = cv2.imread("lNKH4.png")  # "FX2in.png"
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*2, h*2))
gry1 = gry1[30:(h*2), w+50:w*2]
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt1 = pytesseract.image_to_string(thr1, config="--psm 6 digits")
print(txt1)
cv2.imshow("thr1", thr1)
cv2.waitKey(0)

For the 2nd image:

enter image description here

Result will be:

2
3 1.28 4.50 9.00
4 2.00 3.75 3.00
5 3.50 4.33 1.72
6 7.00 6.00 1.28

Same code, just remove the following line:

gry1 = gry1[30:(h*2), w+50:w*2]

Upvotes: 4

Related Questions