简体   繁体   中英

Pytesseract OCR wrong text recognition

When I use Pytesseract to recognise the text in this image, Pytesseract returns 7A51k but the text in this image is 7,451k .

How can I fix this problem with code instead of providing a clearer source image?

在此处输入图像描述

my code

import pytesseract as pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D:\\App\\Tesseract-OCR\\tesseract'

img = Image.open("captured\\amount.png")
string = pytesseract.image_to_string(image=img, config="--psm 10")

print(string)

I have a two-step solution


    1. Resize the image
    1. Apply thresholding.

    1. Resizing the image
    • The input image is too small for recognizing the digits, punctuation, and character. Increasing the dimension will enable an accurate solution.
    1. Apply threshold
    • Thresholding will show the features of the image.

    • When you apply thresholding result will be:

      • 在此处输入图像描述

When you read the threshold image:

7,451k

Code:


import cv2
from pytesseract import image_to_string

img = cv2.imread("4ARXO.png")
(h, w) = img.shape[:2]
img = cv2.resize(img, (w*3, h*3))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)

There is no problem if the image is blurry after resizing, you can threshold it, and invert it as AlexAlex has proposed:

output: 7,451k

import numpy as np
import pytesseract
import cv2

# Read Image
gray = cv2.imread('2.png', 0)

# Resize
gray = cv2.resize(gray, (600,200))

# Inverting
gray = 255 - gray
emp = np.full_like(gray, 255)
emp -= gray

# Thresholding
emp[emp==0] = 255
emp[emp<100] = 0

text = pytesseract.image_to_string(emp, config='outputbase digits')

print(text)

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