简体   繁体   中英

Why pytesseract can't recognize this image

I am testing the pytesseract OCR on this image

在此处输入图像描述

but the result is always 30770.0 but I would have wanted this number: 997,70 FYI: this image has already been transformed:

img = img.convert('L')  # greyscale
img = img.resize((img.size[0] * 3, img.size[1] * 3), 1)
img = ImageEnhance.Contrast(img).enhance(5.0)
img = ImageOps.equalize(img)
img = ImageOps.invert(img)

Below the code:

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img2 = "full_snap__1609584655.png"
numStr2 = pytesseract.image_to_string(img2, lang='eng',config='--psm 10 --oem 1 digits -c tessedit_char_whitelist=0123456789')
print('997,70 :',float(numStr2))

I have already tried to adjust the --psm parameter of the pytesseract function image_to_string but it doesn't work.

Thank you for your help

My solution to the problem is morphological-transformation.

If you apply erosion

the thickness or size of the foreground object decreases or simply white region decreases in the image. It is useful for removing small white noises (as we have seen in colorspace chapter), detach two connected objects etc.

erd = cv2.erode(gry, None, iterations=1)

Result:

在此处输入图像描述

Now, if you read it:

print(pytesseract.image_to_string(erd))

Result:

997 70€

Code:


import cv2
import pytesseract

img = cv2.imread("R83OY.png")
h, w, c = img.shape
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
erd = cv2.erode(gry, None, iterations=1)
print(pytesseract.image_to_string(erd))

Possible Question: Why do you set kernel to None?

If you initialize a kernel (ie (5, 5) ) and apply it to the image, result will be:

在此处输入图像描述

As you can see, applying kernel is not improving the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM