Trying to get the best result with imagemagic and tesseract OCR, image recognition

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
metal0077
Posts: 4
Joined: 2017-07-04T00:06:24-07:00
Authentication code: 1151

Trying to get the best result with imagemagic and tesseract OCR, image recognition

Post by metal0077 »

Hello I'm trying to use OCR tesseract to recognize some letters in a image.

I did a convert using imagemagick and image seems to be good but its not enough to recognize

The original images:

Image
Image
Image
Image

The command used with imagemagick to convert(with a lot of thanks to @fmw42)

Code: Select all

convert input.jpg -fuzz 50% -fill black -opaque black -bordercolor white -border 2 -fill black -draw "color 0,0 floodfill" -alpha off -negate -units pixelsperinch -density 72 output.jpg
The result images:

Image
Image
Image
Image

The OCR tesseract command:

Code: Select all

$ tesseract output.jpg out -psm 7
Output/result:

Text: AUGU -> AUOU

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica Page 1

Text: VEGU -> VOR-OU

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica Page 1

Text: EGUV -> E6UV

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica Page 1

Text: USEA -> USSOEA


May I can use some filter to get better results? what do you think?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to get the best result with imagemagic and tesseract OCR, image recognition

Post by fmw42 »

Your problem is really with your input quality. You should never use JPG since it is a lossy compression. You need to have scans at higher resolutions/density to get better quality results. Also I suspect the main issue is that your characters are rotated and I think OCR does not like rotated letters or numbers. The quality should have been adequate.
metal0077
Posts: 4
Joined: 2017-07-04T00:06:24-07:00
Authentication code: 1151

Re: Trying to get the best result with imagemagic and tesseract OCR, image recognition

Post by metal0077 »

fmw42 wrote: 2017-07-04T21:03:11-07:00 Your problem is really with your input quality. You should never use JPG since it is a lossy compression. You need to have scans at higher resolutions/density to get better quality results. Also I suspect the main issue is that your characters are rotated and I think OCR does not like rotated letters or numbers. The quality should have been adequate.
I converted jpg to tiff before processing...

Code: Select all

convert input.jpg -units pixelsperinch -density 300 -depth 8 output.tiff
The result's change a little be but without correct..

Text: AUGU -> AUOU

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica
Page 1

Text: VEGU -> VOR°OU

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica
Page 1

Text: EGUV -> E6UV

Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica
Page 1

Text: USEA -> USSOEA
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to get the best result with imagemagic and tesseract OCR, image recognition

Post by fmw42 »

Once you have compressed the image using JPG going to tif will not help. You have already lost information. If you scanned these image, you would have been better of saving as tif or pdf.

But the real issue I believe is that the characters are rotated.
mathieularose
Posts: 1
Joined: 2017-08-10T18:40:13-07:00
Authentication code: 1151

Re: Trying to get the best result with imagemagic and tesseract OCR, image recognition

Post by mathieularose »

I tried with your images and it looks good:

Code: Select all

$ tesseract -psm 8 augu.jpg -
Ausu
With whitelisting:

Code: Select all

$ tesseract -psm 8 -c tessedit_char_whitelist=ABCDEFGHIJKLMOPQRSTUVWXYZ augu.jpg -
AUGU

Code: Select all

$ tesseract -psm 8 vegu.jpg -
VEGU

Code: Select all

$ tesseract -psm 8 eguv.jpg -
EGUV

Code: Select all

$ tesseract -psm 8 usea.jpg -
USEA
Here is my tesseract version:

Code: Select all

$ tesseract --version
tesseract 3.04.01
 leptonica-1.74.1
  libgif 4.1.6(?) : libjpeg 6b (libjpeg-turbo 1.5.1) : libpng 1.6.27 : libtiff 4.0.7 : zlib 1.2.8 : libwebp 0.5.2

I posted your example at https://mathieularose.com/decoding-captchas/
Post Reply