Andreas Ellsen
Andreas Ellsen

Reputation: 33

Correctly extract text from image using Tesseract OCR

I have been trying to extract the bold white text from this image but not able to get it working correctly, seems the 9 is read as a 3 and the I as 1.

Have been looking at various sites which has code to make the image better quality but not getting it to work, anyone able to help me with this one? The desired output should be "I6M-9U" enter image description here

def get_text_from_image(image: cv2.Mat) -> str:
    pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe'
    
    # Crop image to only get the piece I am interested in
    top, left, height, width = 25, 170, 40, 250

    try:
        crop_img = image[top:top + height, left:left + width]
        
        # Make it bigger
        resize_scaling = 1500
        resize_width = int(crop_img.shape[1] * resize_scaling / 100)
        resize_height = int(crop_img.shape[0] * resize_scaling / 100)
        resized_dimensions = (resize_width, resize_height)
    
        # Resize it
        crop_img = cv2.resize(crop_img, resized_dimensions, interpolation=cv2.INTER_CUBIC)
        
        return str(pytesseract.image_to_string(crop_img, config="--psm 6"))

UPDATED CODE

ret, thresh1 = cv.threshold(image, 120, 255, cv.THRESH_BINARY +
                                            cv.THRESH_OTSU)

cv.imshow("image", thresh1)

This now has all the background artifacts removed but it is now reading the first letter I as 1 and the 9 is read as 3

Upvotes: 1

Views: 1079

Answers (1)

Hermann12
Hermann12

Reputation: 3417

The I looks like a 1 for me. If you don't like the 1 remove it from filter:

import cv2
import pytesseract
 
img = cv2.imread('16M.png',cv2.IMREAD_UNCHANGED)

(thresh, blackAndWhiteImage) = cv2.threshold(img, 63, 255, cv2.THRESH_BINARY) 

# resize image
scale_percent = 4 # percent of original size
width = int(blackAndWhiteImage.shape[1] * scale_percent / 100)
height = int(blackAndWhiteImage.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(blackAndWhiteImage, dim, interpolation = cv2.INTER_AREA)

# OCR resized Black & White image
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Remove 1 from filter, than you will get I instead
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=-.ABCDEFTGHIJKLMNOPQRSTUVWXYZ0123456789' 

tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)

# Display cropped image
cv2.imshow("Image", resized)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

16M-9U-0.0

With removed "1" from filter:

I6M-9U-0.0

Upvotes: 0

Related Questions