7

Any suggestions on converting these images to text? I'm using pytesseract and it's working wonderfully in most cases except this. Ideally I'd read these numbers exactly. Worst case I can just try to use PIL to determine if the number to the left of the '/' is a zero. Start from the left and find the first white pixel, then

enter image description here enter image description here

from PIL import Image
from pytesseract import image_to_string

myText = image_to_string(Image.open("tmp/test.jpg"),config='-psm 10')
myText = image_to_string(Image.open("tmp/test.jpg"))

The slash in the middle causes issues here. I've also tried using PIL's '.paste' to add lots of extra black around the image. There might be a few other PIL tricks I could try, but i'd rather not go that route unless I have to.

I tried using config='-psm 10' but my 8's were coming through as ":" sometimes, and random characters other times. And my 0's were coming through as nothing.

Reference to: pytesseract don't work with one digit image for the -psm 10

_____________EDIT_______________ Additional samples:

enter image description here 1BJ2I]

enter image description here DIS

enter image description here 10.I'10

enter image description here 20.I20

So I'm doing some voodoo conversions that seem to be working for now. But looks very error prone:

def ConvertPPTextToReadableNumbers(text):
    text = RemoveNonASCIICharacters(text)
    text = text.replace("I]", "0")
    text = text.replace("|]", "0")
    text = text.replace("l]", "0")
    text = text.replace("B", "8")
    text = text.replace("D", "0")
    text = text.replace("S", "5")
    text = text.replace(".I'", "/")
    text = text.replace(".I", "/")
    text = text.replace("I'", "/")
    text = text.replace("J", "/")
    return text

Ultimately generates:

ConvertPPTextToReadableNumbers return text =  18/20
ConvertPPTextToReadableNumbers return text =  0/5
ConvertPPTextToReadableNumbers return text =  10/10
ConvertPPTextToReadableNumbers return text =  20/20
Community
  • 1
  • 1
LampShade
  • 2,675
  • 5
  • 30
  • 60
  • `-psm 10` is for single character recognition, you could try `psm 7` (single text line) Ref: https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage – Gwen Aug 30 '16 at 09:31
  • I left out a few details there, what I did with psm 10 was attempt to crop out the first character and then use psm 10. I was successfully able to crop out the first character sometimes, but it was inconsistent. Even when I could crop out an 8 all by itself, it would be interpreted as : or something else so. And other characters weren't coming through either. Even when I crop out a 0 all by itself, it come through as a blank. – LampShade Aug 30 '16 at 14:08
  • Using psm 7, it's consistently reading "0\5" as DIS which honestly might be good enough for my worst case. I can just check for a D and I'll know that's 0/. If anyone else has a better solution, feel free to chime in. I wish there was a way to say "Interpret everything as numbers" – LampShade Aug 30 '16 at 16:21
  • it show some path error, how to resolve it..? – Rishabh Jhalani Sep 17 '18 at 12:06

2 Answers2

5

Generally speaking, most OCR tools (like Tesseract) are tuned for working with high-resolution scans of printed text. They do not perform well on low-resolution or pixellated images.

Two possible approaches here are:

  1. If the font, background, and layout of your images are completely predictable, you don't need Tesseract at all; it's just complicating matters. Build a library of images representing each character you need to recognize, and check whether parts of the image are equal to the reference image.

  2. If that isn't an option, or if it seems too hard, you could upscale the pixellated image using one of the hq*x algorithms. The added detail may be sufficient to get Tesseract to reliably recognize the characters.

  • Thanks for the info. The font and size is consistent, the spacing changes a bit. I'll consider trying to upscale if my current implementation becomes a problem. My only concern with upscale would be that it might take a long time to process. I prefer something faster since my script is a real-time thing – LampShade Aug 30 '16 at 18:27
5

If anyone still stumbles over this... The problem is that the letters are white and the background is black...in my application, I got an about 98% accuracy after switching that (white background black letters) I just used cv2 for that

Julius
  • 91
  • 1
  • 8