Tesseract not detecting white characters

Viewed 33

Ive been trying to detect the white letters on a light blue background and it seems to work for same background but with slightly cleaner letters.

Here is the image - it returns no text from the OCR activity. If I make background darker blue, its OK. The text seems clear enough, maybe its some odd tesseract thing?

Thanks.

.enter image description here

import cv2
import pytesseract

image = cv2.imread('c3.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
thresh = cv2.threshold(gray,175 ,255, cv2.THRESH_BINARY_INV)[1]
thresh = 255 - thresh

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
result = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.imwrite('result.jpg', result)
invert = cv2.bitwise_not(result)

config = ''
text = pytesseract.image_to_string(invert, config=config)
cv2.imshow('result', invert)

print("Result: " + text)

cv2.waitKey()
1 Answers

There are 2 things which are needed to be known in your case.

  1. First of all, you need to get a clear image for input. Here is a beautiful documentation you can find for that. I see you did some pre-processing steps like threshold, but at the end you are giving to the tesseract engine the first input. Then why did you do those steps ?
  2. Second one and the most important one is that you need to know well about the tesseract configuration parameters. I see you are giving an empty config so the default one. I suggest you to use the config like below:

config = r'--oem 1 --psm 6'

  • –psm 6 - Assume a single uniform block of text.
  • --oem 1 for LSTM/neural network

If you change the config accordingly, I got the output:

Result: MeN aRS Se
GESU 132857 (4)
aes NEE” «Sa ee

At least you are getting some good results. You can improve it by improving the input image quality by the help of pre-processing steps.

Related