pythonocrtesseractimage-preprocessinggame-automation

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


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,
)

Solution

  • 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!