python-3.xtextocrpython-tesseractstring-decoding

Difficulty reading text with pytesseract


I need to read the highest temperature on thermographic images, as shown below:

IR_1544_INFRA.jpg

IR_1546_INFRA.jpg

IR_1560_INFRA.jpg

IR_1564_INFRA.jpg

I used the following code, this was the best result. I also tried several other ways, such as: blur, gray scale, binarization, and others but they all failed.

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Users\User\AppData\Local\Tesseract-OCR\tesseract.exe"

# Load image, grayscale, Otsu's threshold
entrada = cv2.imread('IR_1546_INFRA.jpg')

image = entrada[40:65, 277:319]

#image = cv2.imread('IR_1546_INFRA.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.waitKey()

In the first image, I found this

In the second image, I found this.

The imagem layout is always the same, that is, the temperature is always in the same place, so I cropped the image to isolate only the number. I would like (97.7 here, and 85.2 here).

My code needs to find from these images to always detect this temperature and generate a list indicating from highest to lowest.

What do you indicate for me to improve the assertiveness of pytesseract in the case of these images?

Note 1: When I annalyze the entire image (without cropping), it returns data that is not even present.

Note 2: In some images even with the binary number, pytesseract (image_to_string) does not return any data.

Thank you all and sorry for the typos, writing in english is still a challenge for me.


Solution

  • Because you have same images, you can crop the area you want and then do processing there. The processing is also simple. Change to gray, get threshold, invert, resize, and then do the OCR. You can see it in my code below. It works on all your attached images.

    import cv2
    import pytesseract
    import os
    
    image_path = "temperature"
    
    for nama_file in sorted(os.listdir(image_path)):
        print(nama_file)
    
        img = cv2.imread(os.path.join(image_path, nama_file))
        crop = img[43:62, 278:319]
        gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.bitwise_not(thresh)
        double = cv2.resize(thresh, None, fx=2, fy=2)
    
        custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist="1234567890." '
        text = pytesseract.image_to_string(double, config=custom_config)
        print("detected: " + text)
    
        cv2.imshow("img", img)
        cv2.imshow("double", double)
    
        cv2.waitKey(0)
    
    cv2.destroyAllWindows()