23

How to represent:

  1. Create new image with paint (any size)
  2. Add letter A to this image
  3. Try to recognize -> tesseract will not find any letters
  4. Copy-paste this letter 5-6 times to this image
  5. Try to recognize -> tesseract will find all the letters

Why?

artem
  • 16,382
  • 34
  • 113
  • 189

4 Answers4

23

You must set the "page segmentation mode" to "single char".

For example, in Android you do the following:

api.setPageSegMode(TessBaseAPI.pageSegMode.PSM_SINGLE_CHAR);
Marco Bonifazi
  • 623
  • 2
  • 8
  • 10
15

python code to do that configuration is like this:

import pytesseract
import cv2
img = cv2.imread("path to some image")
pytesseract.image_to_string(
     img, config=("-c tessedit"
                  "_char_whitelist=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                  " --psm 10"
                  " -l osd"
                  " "))

the --psm flag defines the page segmentation mode.

according to documentaion of tesseract, 10 means :

Treat the image as a single character.

so to recognize a single character you just need to use : --psm 10 flag.

nimig18
  • 797
  • 7
  • 10
Shahryar Saljoughi
  • 2,599
  • 22
  • 41
10

You need to set Tesseract's page segmentation mode to "single character."

rmtheis
  • 5,992
  • 12
  • 61
  • 78
  • 5
    Well, it depends entirely on how you're using tesseract. If you're calling it from the shell, you would say `tesseract $image $outbase -psm 10`. The -psm sets the page segmentation mode, and mode 10 is for single characters. It's all in the man page. – ACK_stoverflow Mar 26 '15 at 04:00
  • In some cases, mode 13 works better. Alternatively make the image smaller/larger might help. – user202729 Jan 15 '21 at 03:50
  • setting the psm to 10 does not seem to make a difference. My use case is a single large character on a larger white background. A clear 'A' in this example yields '-\n' with a psm of 10 at 200 DPI. – Chris Sep 08 '21 at 20:56
7

Have you seen this?

https://code.google.com/p/tesseract-ocr/issues/detail?id=581

The bug list shows it as "no longer an issue".

  • Be sure to have high resolution images.
  • If you are resizing the image, be sure to keep a high DPI and don't resize too small
  • Be sure to train your tesseract system
  • use the baseApi.setVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); code before the init Tesseract
  • Also, you may look into which font to use with OCR
Community
  • 1
  • 1
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82