Tesseract OCR LCD digits doesn't work

85 views
Skip to first unread message

Hervé

unread,
Jun 24, 2022, 10:59:21 AMJun 24
to tesseract-ocr
Hi, I need some help to make tesseract-OCR recognize digits : can't achieve to make this work with


here is my code :



import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd ="C:\\Program Files\\Tesseract-OCR\\tesseract.exe"

def process_image(img):
    #cv2.imshow('Img',img)
    #cv2.waitKey(0)

    ### passage en niveau de gris
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #cv2.imshow('Img',gray)
    #v2.waitKey(0)

    ###analyse de l'image
    valeur = pytesseract.image_to_string(gray)
    print(valeur)

    ##passage en noir et blanc
    (thresh, im_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    im_bw = cv2.bitwise_not(im_bw)
    #cv2.imshow('Img',im_bw)
    #cv2.waitKey(0)
    # cv2.imwrite('ph.png',im_bw)
    print(pytesseract.image_to_string(im_bw))


###ouverture de l'image
img = cv2.imread('ocr5.png')
# cv2.imshow('Img',imgcoupee)


###on rogne
imgcoupee = img[1056:1517,950:1862]
#img = cv2.imwrite('ocrcoupee.png',imgcoupee)
# cv2.imshow('Img',imgcoupee)

### decoupage de la partie correspondant au PH
ph= img[516:625, 616:815]

#cv2.imwrite('pH.jpg', image_pH)

### partie chlore
cl = img[516:625, 882:1056]

### partie dÃ:copyright:faut flow
#flow= img[1302:1398,1054:1400]

### process
#process_image(imgcoupee)
process_image(ph)
process_image(cl)
#process_image(flow)

digits seems to be clear enough, but it does'nt work, if someone could help me ?

thanks !

Zdenko Podobny

unread,
Jun 24, 2022, 11:12:44 AMJun 24
to tesser...@googlegroups.com
Did try to implement suggestion from documentation?

pi 24. 6. 2022 o 16:59 Hervé <herve....@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/a05712a5-e6ed-411f-a072-e389ea7095efn%40googlegroups.com.

Hervé

unread,
Jun 25, 2022, 2:15:22 AMJun 25
to tesseract-ocr
Hi
I just tried some, without real success

could I learn digits from pictures ? maybe this font is not well recognized

thanks

Zdenko Podobny

unread,
Jun 25, 2022, 4:32:14 AMJun 25
to tesser...@googlegroups.com


so 25. 6. 2022 o 8:15 Hervé <herve....@gmail.com> napísal(a):
Hi
I just tried some, without real success

Please be specific: what did you try and what was the result?

 
could I learn digits from pictures ? maybe this font is not well recognized

Any training is useless if the failure is at the image preprocessing stage.

Hervé

unread,
Jun 25, 2022, 4:56:56 AMJun 25
to tesseract-ocr
I tried gray image, black and white, and I use

 custom_psm = r'--psm 7'

didn't try others parameters

Zdenko Podobny

unread,
Jun 25, 2022, 5:25:50 AMJun 25
to tesser...@googlegroups.com
Why you did not try more relevant hits like inverting and resizing?

Zdenko


so 25. 6. 2022 o 10:56 Hervé <herve....@gmail.com> napísal(a):

Hervé

unread,
Jun 25, 2022, 6:17:40 AMJun 25
to tesseract-ocr
I am on tesseract 5

Inverting images

While tesseract version 3.05 (and older) handle inverted image (dark background and light text) without problem, for 4.x version use dark text on light background.

isn'it the same than :
    (thresh, im_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    im_bw = cv2.bitwise_not(im_bw)

for resizing, I take my picture in full HD, do increasing resolution will allow tesseract to better OCR ?

thanks

Zdenko Podobny

unread,
Jun 25, 2022, 8:34:06 AMJun 25
to tesser...@googlegroups.com
Sorry - I mean Rescaling:

Tesseract works best on images which have a DPI of at least 300 dpi, so it may be beneficial to resize images. For more information see the FAQ.
"Willus Dotkom" made interesting test for Optimal image resolution with suggestion for optimal Height of capital letter in pixels:

After that, you can get output (but the dot is missing) with the command line: "tesseract pH_treshr.png -"

I was able to get the decimal point separator with the letsgodigital data file https://github.com/arturaugusto/display_ocr/blob/master/letsgodigital/letsgodigital.traineddata
tesseract pH_treshr.png - -l letsgodigital


Zdenko


so 25. 6. 2022 o 12:17 Hervé <herve....@gmail.com> napísal(a):
pH_treshr.png

Hervé

unread,
Jun 25, 2022, 6:23:49 PMJun 25
to tesseract-ocr
Sorry I am really noob

When I do : tesseract pH_treshr.png -
I have :
Empty page!!
Empty page!!

How do you achieve to have this image ? and why can't I tesseract it like you ? I am on buster with tesseract 5.1

is there a way to discuss ? discord ?

thanks for your patience and help

Zdenko Podobny

unread,
Jun 26, 2022, 9:24:01 AMJun 26
to tesser...@googlegroups.com
Check your tesseract version (tesseract -v). Here is mine:
tesseract 5.1.0-70-g0df5
 leptonica-1.83.0 (Jun 24 2022, 17:48:50) [MSC v.1929 LIB Release x64]
  libgif 5.2.1 : libjpeg 6b (libjpeg-turbo 2.0.91) : libpng 1.6.37 : libtiff 4.4.0 : zlib 1.2.12 : libwebp 1.2.2 : libopenjp2 2.5.0
 Found AVX2
 Found AVX
 Found FMA
 Found SSE4.1
 Found libarchive 3.5.1 zlib/1.2.11 liblzma/5.2.4 bz2lib/1.0.6 libzstd/1.4.9
 Found libcurl/7.75.0 zlib/1.2.12 libssh2/1.10.1_DEV

+ try to use (eng) data file from tessdata_best[1] (also just tessdata[2] produce a result)

Regarding image: 
  1. I took output from your code "cv2.imwrite('pH.jpg', ph)" (jpg is not good format for ocr)
  2. I opened it as grayscale and I see 2 problems covered by documentation:
    • it needs to be inverted
    • it needs to be resized to the height of letters is between 30-40 points.
  3. I guess sharpening (to increase space between dot and 3) would help to recognize dot.
  4. Binarize/threshold image by yourself. Tesseract has some binarize algorithms, but you can another one that better fit your case.
I suggest doing image preprocessing in the image editor (to check what helps) and then implementing it into code.


Zdenko


ne 26. 6. 2022 o 0:23 Hervé <herve....@gmail.com> napísal(a):

Hervé

unread,
Jun 27, 2022, 6:59:57 AMJun 27
to tesseract-ocr
Hi

I don't achieve to have a 300dpi image, I tried with increasing picam resolution, I only have 96. I tried with

img = cv2.resize(img, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_AREA)

but it only grows the image size, not the DPI.

Thanks
default.png
resize2.png
resize1.5.png

Zdenko Podobny

unread,
Jun 27, 2022, 2:44:42 PMJun 27
to tesser...@googlegroups.com
not sure what are you doing, but try something like this:

def autoinvert(binarized_img, tresh=0.5):
    """Invert binarized image if amount of black pixels is higher than tresh.
    """
    height, width = binarized_img.shape
    non_zero = cv2.countNonZero(binarized_img)
    white_rate = non_zero/(height*width)
    if  white_rate < tresh:
        return ~binarized_img
    else:
        return binarized_img

filename = 'default.png'
test = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
binarized = cv2.threshold(test, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = np.ones((5,5), np.uint8)
img_erosion = cv2.dilate(autoinvert(binarized), kernel, iterations=1)
ratio = round(40/img_erosion.shape[0], 2)
ocr_image = cv2.resize(img_erosion, (0,0), fx=ratio, fy=ratio)

output = pytesseract.image_to_string(ocr_image,
                            config=f'--tessdata-dir "{tessdata}" --psm 6')

print(output)

Which produces '733 124', so there is still a problem with the decimal point...

Zdenko


po 27. 6. 2022 o 13:00 Hervé <herve....@gmail.com> napísal(a):

Hervé

unread,
Jun 27, 2022, 3:04:51 PMJun 27
to tesseract-ocr
decimal point is not a problem, I can devide by 100 or 10 and it works :)

could you share my the whole code ? thanks
Reply all
Reply to author
Forward
0 new messages