Tesseract OCR revisited

Started by StarGeek, August 27, 2024, 06:53:06 PM

Previous topic - Next topic

StarGeek

Taking what I did in this post a step further, I made a config file that will directly call tesseract allowing the resulting data to be used directly with exiftool.

For example
exiftool -config TesseractOCR.config "-Description<TesseractOCR" file.jpg

Obviously, the result is only as good as what tesseract can do, and I haven't played with it enough to figure out what might be the best options.

An example with the screenshot of a game I play

20240123160623_1.jpg

The resulting text
Oes

885

\¥{ 965 @ 2

Fw \@u2 Sou Ase Wis dus Sia | 5660

G52 116 (400 iss | Arses g4oas

Witch's Wits
Without a warning, an old, withered woman has appeared at

the gate. Nobody saw her arrive, but some explain that there
are rumors about an eccentric, old lady living in the woods.

The woman cackles. "Interested in riddles? Answer this, 'I
arrive unasked, unseen, and when | do it early, | bring death
with me. What am |?"

@ Frost
@Poison
@ Bad luck
@Asnake

Obviously, tesseract has problems with all the numbers across the top. But most of the main text is decent enough. The main problem is the "I" is exchanged for the pipe character and it doesn't read the circled numbers.

#------------------------------------------------------------------------------
# File:         TesseractOCR.config
#
# Description:  User-defined Composite tag to run tesseract on an image and return the results
#
# Tag definition and example:
#
#   TesseractOCR
#       This will be the text that tesseract returns after running OCR on the image
#
#       Example:
#           exiftool -config TesseractOCR.config "-Description<TesseractOCR" /path/to/files/
#
#
# Revisions:    Ver. 1.0 - 2024-08-27 - Bryan K. Williams (aka StarGeek) Created
#
#
# References:
#               Sourcecode
#               https://github.com/tesseract-ocr/tesseract
#               Installation page
#               https://tesseract-ocr.github.io/tessdoc/Installation.html
#               3rd party Windows binaries, includes installer
#               https://github.com/UB-Mannheim/tesseract/wiki

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        TesseractOCR   => {
            Require => {
                0 => 'Directory',
                1 => 'FileName',
            },
            ValueConv => q{
                # Generate a user-defined tag only if requested
                return undef unless $$self{REQ_TAG_LOOKUP}{tesseractocr};

                # Hide STDERR so it doesn't display while exiftool is running
                use File::Spec;
                open STDERR, '>', File::Spec->devnull() or warn "Warning: could not hide STDERR";

                # If you need to supply the full path to tesseract, you would edit this line
                # You would also place any needed options here
                my $PathToTesseract="tesseract";

                # Run the program
                my $OCR = `$PathToTesseract "$val[0]/$val[1]" stdout`;

                # (not ($? >> 8)) checks the return code to see if the command ran without error
                return ( (not ($? >> 8)) and $OCR)? $OCR : undef;
            },
        },
    },
);
#------------------------------------------------------------------------------
1;  #end
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype