optimizing tesseract for reading some numbers from photos

Viewed 43

I am a novice tesseract user.

I am trying to read some numbers from some photos taken by mobile photos, such as the following:

sample1 sample2 sample3

I am using tesseract / textclear.sh and a simple bash script. Important section of my shell script is as follows:

for PSM in  6 11 3
    do
        printf "CURRENT PSM is %d\n" "${PSM}"
        READ_CXX_ID=$(tesseract --psm ${PSM} "${file}" - | grep -oh "\w*0b79\w*" | tr -d '\n')
        if [ -z ${READ_CXX_ID} ]
        then
            printf "Couldn't read cxx ID for ${file} \n"
            printf "Lets check if converted file exists...\n"
            if [ ! -e "${file}.out.jpg" ]
            then
                printf "textcleaner not applied, lets create it...\n"
                # Here we will create JPG file instead of JPEG
                # So that it will not try to get CXX ID from the previous conversion
                ./textcleaner.sh -f 200 -i 1 ${file} "${file}.out.jpg"
            fi
            READ_CXX_ID=$(tesseract --psm ${PSM} "${file}.out.jpg" - | grep -oh "\w*0b79\w*" | tr -d '\n')
            if [ -z ${READ_CXX_ID} ]
            then
                printf "Even with textcleaner, ${PSM} couldn't read file..\n"
                printf "OCR ERROR , TRYING NEXT PSM...\n"

I tried to use different PSM values, cleaning photos with textclear but I can't reliably read these numbers from the photos ( Success rate within 1000+ photos is around %75 )

As an example, here is the output of running tesseract on

sample-1:

$  tesseract --psm 1 sample-1.jpeg -
Estimating resolution as 251
Too few characters. Skipping this page
OSD: Weak margin (0.00) for 24 blob text block, but using orientation anyway: 0
x

Trying to OCR this number

sample-2:

$  tesseract --psm 6 sample-2.jpeg -
f eel -
——
aa a sam
as -V2.3.0.1 — es
—- Ne 3
i lil ith ii ii
~~: F
, |e 22
> ) ine a 10592a0b79 :
"a | “rsomstonmar -
or. ieee
— i Ve a z
Ee Bl
: — " di NEG GAT he aaa a
PA e

sample-3:

$  tesseract --psm 6 sample-3.jpeg -
ea ort
]
V2.3.0.1
G
9 4
\ 2
’ H
4679894b59280b7?

Where does 46 coming from ??? Why it can't detect last character as 9(nine) instead of question mark?

Here is the tesseract version I am using:

$  tesseract --version
tesseract 5.1.0-72-gb8b6
 leptonica-1.79.0
  libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 2.0.3) : libpng 1.6.37 : libtiff 4.1.0 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.1
 Found AVX2
 Found AVX
 Found FMA
 Found SSE4.1
 Found OpenMP 201511
 Found libarchive 3.4.0 zlib/1.2.11 liblzma/5.2.4 bz2lib/1.0.8 liblz4/1.9.2 libzstd/1.4.4
 Found libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3

What could be the right way to use tesseract, how can I reliably OCR these photos ? Should I consider skew correction ? Can that be automated ?

Update-1: Tried blurring as suggested below:

$  convert -blur 0x1 sample-1.jpeg sample-1.blurred.jpeg
$  tesseract sample-1.jpeg -
Estimating resolution as 251
x

Trying to OCR this number

$  tesseract sample-1.blurred.jpeg -
Estimating resolution as 314
¥

Trying to OCR this number

$  
1 Answers

You might be suffering from Aliasing effects if you're taking pictures of another screen. Try applying a gaussian blur to the image in post, or try increasing the exposure with your camera while moving the camera slightly while taking the picture.

Related