2

I'm trying to extract text from an image using pytesseract on Python. This is the image where I want to extract the text:

Original Image

This is the image after applying threshold:

Image with threshold

Console Output:

20 hours

20 hours

Bhours

Console Output Image

This is the code I'm using:

from pytesseract import *
import cv2

path = r"path where image is located"             #path of image
folderPath = r"path for saving output image" 

grey_image = cv2.imread(path,0)                   #import image

_,bt = cv2.threshold(grey_image, 150 ,255,cv2.THRESH_BINARY)   #variable means binary threshold

cv2.imwrite(folderPath + "\\" + "test.png", bt)   #Saving result

imageout = pytesseract.image_to_string(bt)        #Convert image to text

print(imageout)                                   #Print text in console

I've been trying different ranges of thresholding but still can't get a precise output.

What do you suggest for getting a precise result?

Kyokko
  • 33
  • 1
  • 5
  • Try [Otsu's threshold](https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html#otsus-binarization) or [adaptive threshold](https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html#adaptive-thresholding) – nathancy Mar 26 '22 at 00:53
  • I already tried with adaptative threshold, it was worst (- laugh-). I'll try with Otsu's threshold. – Kyokko Mar 26 '22 at 20:28

1 Answers1

0

Because you are dealing with an image containing white characters over a dark background, it is recommended to invert it prior using pytesseract.

This is done using inverted_grey_image = cv2.bitwise_not(grey_image).

You may then adjust the threshold in threshold: _,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)

Below is the full code:

from pytesseract import *
import cv2

path = r"path where image is located"             #path of image
folderPath = r"path for saving output image" 

grey_image = cv2.imread(path,0)                   #import image

inverted_grey_image = cv2.bitwise_not(grey_image)

_,bt = cv2.threshold(inverted_grey_image, 140 ,255,cv2.THRESH_BINARY)   #variable means binary threshold

cv2.imwrite(folderPath + "/" + "test.png", bt)   #Saving result

imageout = pytesseract.image_to_string(bt)        #Convert image to text


print(imageout)  

It returns:

20 hours

20 hours

3 hours

EDIT: I am currently dealing with a similar OCR problem: you may want to check this recent post for inspiration.

Sheldon
  • 4,084
  • 3
  • 20
  • 41
  • Thanks for your help! I'll do that! – Kyokko Mar 24 '22 at 15:13
  • 1
    I did what you suggest, but still, it's not perfect. There are some numbers that can't be imported from the image. I'll try to apply what you have in your post. – Kyokko Mar 25 '22 at 21:57