6

I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.

Here is the image:

TestImage

I have tried scaling it, grayscaling it, and adjusting the contrast, thresholding, blurring, everything it says in other posts, but my problem is that I don't know what the OCR wants to work better. Does it want blurry text? High contrast?

Code to try:

import pytesseract
from PIL import Image

print pytesseract.image_to_string(Image.open(IMAGE FILE))

As you can see in my code, the image is stored locally on my computer, hence Image.open()

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
hegash
  • 833
  • 1
  • 7
  • 16
  • Have you seen https://stackoverflow.com/questions/53797130/empty-string-with-tesseract – TheWanderer Jan 19 '19 at 12:44
  • Yeah, that still didn't work for me – hegash Jan 19 '19 at 13:08
  • Can you list what you've tried in your question? – TheWanderer Jan 19 '19 at 13:30
  • 3
    According to new facts(https://meta.stackoverflow.com/questions/379138/told-i-would-only-get-correct-answer-if-i-accepted-incorrect-one/379145#comment663818_379145) , this question should be edited or closed, because Questioner requires solution with his original image stored on his computer. However we do not know how does original image looks like and Questionar doesnt accepts solution with an image he posted in the original question. Therefore from logical point of view this question cannot be solved. – Martin Jan 19 '19 at 16:00
  • 12
    @Martin well, that's wrong. OP showed the picture in question, and the relevant code. OP also shows the opening of the image -- the name of the file is irrelevant; it's all local. That should give you the main pointer: "I want to use a local image" -- and again, it is the image in the question, but it's stored locally, and only uploaded to the question for the sake of a [mcve] – Zoe Jan 19 '19 at 16:12
  • 3
    your suggestion is also wrong. I provided , just like other guy, image processing with the uploaded image, because thats THE absolute point with which you can work. Because answer requires image processing, I need to know what is my starting image and I do not know if his local image is in its original form or after everything he 'tried'. If you cannot be sure of the starting image, it cannot be solved – Martin Jan 19 '19 at 16:20

2 Answers2

20

There are several reasons:

  1. Edges are not sharp and continuous (By sharp I mean smooth, not with teeth)

  2. Image is too small, you need to resize

  3. Font is missing (not mandatory, but trained font incredibly improve possibility of recognition)

Based on points 1) and 2) I was able to recognize text.

1) I resized image 3x and 2) I blurred the image to make edges smooth

import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
from PIL import Image

def url_to_image(url):
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    return image

url = 'https://i.stack.imgur.com/J2ojU.png'

img = url_to_image(url)



retval, img = cv2.threshold(img,200,255, cv2.THRESH_BINARY)
img = cv2.resize(img,(0,0),fx=3,fy=3)
img = cv2.GaussianBlur(img,(11,11),0)
img = cv2.medianBlur(img,9)
cv2.imshow('asd',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
txt = pytesseract.image_to_string(img)
print('recognition:', txt)
>> recognition: Gm

Note:

This script is good for testing any image on web

Note 2:

All processing is based on your posted image

Note 3:

Text recognition is not easy. Every recognition requires special processing. If you try this steps with different image, it may not work at all. Important is to try a lot of recognition on images so you understand what tesseract wants

Zoe
  • 27,060
  • 21
  • 118
  • 148
Martin
  • 3,333
  • 2
  • 18
  • 39
  • How would I do this for an image stored on my computer? – hegash Jan 18 '19 at 22:18
  • 3
    I'm sorry, but currently this does not work for me, as the file is on my computer; I'm not going to accept the answer until it works. I appreciate you helping me, but it feels like you're trying to trade the working answer for reputation. – hegash Jan 19 '19 at 08:21
  • You can read the image with img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) instead of img= Image.open('image.jpg') ... then you can continue working with cv2 functions. – Phil Apr 10 '20 at 09:03
20

Trying something along the lines of

import pytesseract 
from PIL import Image 
import requests 
import io

response = requests.get('https://i.stack.imgur.com/J2ojU.png') 
img = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(img, lang='eng', config='--psm 7')

print(text)

with --psm values equal or larger than 6 did yield "Gm" for me.

If the image is stored locally (and in your working directory), just drop the response variable and change the definition of text with the lines

image_name = "J2ojU.png" # or whatever appropriate
text = pytesseract.image_to_string(Image.open(image_name), lang='eng', config='--psm 7')
Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72