need to OCR red text on black background with pytesseract: program don`t see red color

83 Views Asked by At

Let`s start with image:

photo_normal

My current task is to print the text from image with using pytesseract.

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = "tesseract/tesseract.exe"

image_path = 'img4.png'

img = cv2.imread(image_path)

# cropped_img = img[195:820, 760:1160] this is for other photo

gray_img = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)  # COLOR_BGRA2GRAY

text = pytesseract.image_to_string(gray_img, config='--oem 3 --psm 6')

print(text)

And program print this:
Day 1382, 03:23:17:
Because the rad text is converting to soo dark gray color and tesseract don`t see it.

photo_black&white

I`ve already tried many ways to convert this dark color but, all of them are too old and nothing helped me.

for ex. this code:

new_img = np.where(
    (gray_img == 31).all(axis=2),
    np.full_like(gray_img, 255),
    gray_img,
)
2

There are 2 best solutions below

2
toyota Supra On

program don`t see red color

The problem can be fixed.

Snippet:

import cv2
import pytesseract

#pytesseract.pytesseract.tesseract_cmd = "tesseract/tesseract.exe"

image = cv2.imread("img4.png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# use Tesseract to OCR the image
text = pytesseract.image_to_string(image)
print(text)

cv2.waitKey(0)

Output:

Day_1382, 03:23:17: Your Baby Dodo.-.t.vi.20
(Dodo) was killed by 123 - Lvl 75= COA ry
AKnights of apocalypse)!
1
Tino D On

You need to do some pre-processing to be more comprehensive in your text recognition. Here is one way to do it:

import cv2
import pytesseract
%matplotlib notebook
import matplotlib.pyplot as plt
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract-OCR/tesseract.exe"
im = cv2.cvtColor(cv2.imread("text.png"), cv2.COLOR_BGR2RGB)
r, g, b = cv2.split(im) # maybe group text according to color if you want?
plt.imshow(r)
rThreshold = cv2.threshold(r, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
text = pytesseract.image_to_string(rThreshold)
print(text)

For example, using the red channel:

reed channel

And a threshold after that, you can get this text:

Day 1382, 03:23.17. Your Baby Dodo - Li 20
(Dodo) was killed by 123 - Lvl 75-364901198
(Knights of apocalypse)!

I would say, it would be much better to have many examples, to know how your text generally looks. Hope this helps you further!