1

screenshot.png:

enter image description here

modified_image.png: enter image description here

I am trying to extract text from an image but seems however I do it tessaract gives me some random values even though I think I have processed the image to a very good format. I am only after the white text and want to disregard the red text.

import cv2 as cv
import pytesseract
from PIL import Image

image = cv.imread("screenshot.png", cv.IMREAD_GRAYSCALE)

ret, modified_image = cv.threshold(image, 120, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)

modified_image= cv.resize(modified_image, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)

#cv.imshow("image", image)
#cv.imshow("modified_image", modified_image)
cv.imwrite("modified_image.png", modified_image)

pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe'

text = pytesseract.image_to_string(Image.open('modified_image.png'), config="--psm 6 --oem 3", lang="eng")

print(f'Text: {text}')

This will incorrectly print "CWS-1Y" instead of "CW9-1Y". From what I have understood the font in use is Shentox but seems like quite the task to train tessaract on it from what I could find online

7
  • @user24714692 All images are like that, the format of letters/digits can be different though (they come from a game) Commented May 14, 2024 at 18:13
  • more context: stackoverflow.com/questions/78450023/… Commented May 14, 2024 at 18:42
  • Send me your ISK and I will double it! Jokes aside, have you tried not resizing the image, or using different scale factors, or different interpolation, or higher threshold? Commented May 14, 2024 at 18:54
  • I'm out of idea for using Tesseract. Can't do better than this. Instead, you can clean, segment your images, and write grid algorithms to find these letters (character by char) with a much higher accuracy. (Fails to recognize between S and 9 which have similar patterns). Commented May 14, 2024 at 19:05
  • @GSazheniuk haha :) Yeah been trying different things for hours Commented May 14, 2024 at 19:34

2 Answers 2

2

Tesseract, while being "good" is not the best. For critical applications I tend to use EasyOCR so maybe give that a shot.

It's free and opensource as well.

Here's an example:

import easyocr
reader = easyocr.Reader(['en']) # this needs to run only once to load the model into memory



result = reader.readtext('chinese.jpg')
print(result)
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried it also with similar results so not optimal either
0
  1. Read the docs: https://github.com/tesseract-ocr/tessdoc/blob/main/ImproveQuality.md
  2. Adjust image according docs:
    • Resize image to meet optimal letter size
    • Invert image (dark letters on bright background
    • (optional) convert to grayscale (or binarize)

enter image description here

tesseract modified_image_based_on_docs.png - --psm 6
CW9-1Y

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.