1

When I use Pytesseract to recognise the text in this image, Pytesseract returns 7A51k but the text in this image is 7,451k.

How can I fix this problem with code instead of providing a clearer source image?

enter image description here

my code

import pytesseract as pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D:\\App\\Tesseract-OCR\\tesseract'

img = Image.open("captured\\amount.png")
string = pytesseract.image_to_string(image=img, config="--psm 10")

print(string)
TylerH
  • 20,799
  • 66
  • 75
  • 101
Trung Kien Tran
  • 27
  • 1
  • 2
  • 8
  • 1
    Sometime OCR can fail to find the text. But in some case, the success rate can be improved with several little trick. One of the best-know trick is simply to resize the image. You can try it : https://stackoverflow.com/questions/43382174/increase-accuracy-of-text-recognition-through-pytesseract-pil – S. Brottes Jan 21 '21 at 13:57
  • but the image is too small so when I resize it, it will become blurry – Trung Kien Tran Jan 21 '21 at 14:23
  • You can try inverted image. – Alex Alex Jan 21 '21 at 18:01

2 Answers2

6

I have a two-step solution


    1. Resize the image
    1. Apply thresholding.

    1. Resizing the image
    • The input image is too small for recognizing the digits, punctuation, and character. Increasing the dimension will enable an accurate solution.
    1. Apply threshold
    • Thresholding will show the features of the image.

    • When you apply thresholding result will be:

      • enter image description here

When you read the threshold image:

7,451k

Code:


import cv2
from pytesseract import image_to_string

img = cv2.imread("4ARXO.png")
(h, w) = img.shape[:2]
img = cv2.resize(img, (w*3, h*3))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)
Ahmet
  • 7,527
  • 3
  • 23
  • 47
2

There is no problem if the image is blurry after resizing, you can threshold it, and invert it as AlexAlex has proposed:

output: 7,451k

import numpy as np
import pytesseract
import cv2

# Read Image
gray = cv2.imread('2.png', 0)

# Resize
gray = cv2.resize(gray, (600,200))

# Inverting
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray

# Thresholding
emp[emp==0] = 255
emp[emp<100] = 0

text = pytesseract.image_to_string(emp, config='outputbase digits')

print(text)
Bilal
  • 3,191
  • 4
  • 21
  • 49