Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

python - PyTesseract OCR unable to read digits from a simple image

I'm trying to get PyTesseract OCR to read digits from this simple and well cropped Image, but for some reason it's just not able to do this.

from PIL import Image
import pytesseract as p

def obtain_balance(a):
    im = Image.open(a)
    width,height = im.size
    a = 300*5 - 120
    # print(width,height)
    left = 155+a
    top = 5
    right = 360+a 
    bottom = 120
    m1 = im.crop((left, top, right, bottom)) 
    text = p.image_to_string(m1,lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789').split()
    print(text)
    m1.show()
    return text

obtain_balance('cur.jpg')

Image I'm trying to read

Output :

[]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When performing OCR, it is important to prepossess the image so that the desired foreground text is in black with the background in white. To do this, we can use OpenCV to Otsu's threshold the image and obtain a binary image. We then apply a slight Gaussian blur to smooth the image before throwing it into Pytesseract. We use --psm 6 config to treat the image as a single uniform block of text. See here for more configuration options.


Here's the preprocessed image and the result from Pytesseract

enter image description here

PRACTICE ACCOUNT
$9,047.26~ i

Code

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:Program FilesTesseract-OCResseract.exe"

image = cv2.imread('1.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...