user3340459
user3340459

Reputation: 435

Python 3 OCR with custom characters

I have images of roughly this format that I would like to parse into numbers: Image that should be parsed.

I have attempted to use the pytesseract module but found the results to be lacking. Occasionally a 5 would be read as a 6 and so on. I was also forced to manually detect the colored circles, as they were generally interpreted as 0.

Sample code used:

import pytesseract
from PIL import Image
img = Image.open("foo.png")
print(pytesseract.image_to_string(img))

> 150150150

Is there a way that allows me to specify that, for instance, the yellow circle would map to a custom character that would be represented as, say, yellow? An expected result of parsing the sample image would result in something like 15 yellow 15 gray 15 brown

Also, since the font is mostly constant and only background color varies slightly, is there a way to train tesseract with images of digits that I would manually feed it before using it to identify actual images?

Upvotes: 2

Views: 2937

Answers (1)

Ovidiu Dolha
Ovidiu Dolha

Reputation: 5413

Tesseract is recognizing the characters based on some training data, which is typically created for a specific language.

In your case, I assume the english standard set is used by pytesseract.

But since you only need digits and the custom circles, your results would improve if you'd make your own training data set - see these guides.

You will then need to make pytesseract work with your custom data instead of the standard language data.

Creating a custom tessdata for your use case will improve recognition of digits and circles, however an OCR is not designed to detect the colors, so you might have to do some extra work there (e.g. use the tesseract report of the location of the "circle" in order to detect the color using some other tool or a simple hue-detection algorithm)

Upvotes: 2

Related Questions