2

I have tested with sample text both alphanumeric and digits only. I am using digits mode.

How do I recognize digits like in the following image:

enter image description here

I think it is because of full height. I have also tried converting it to .jpg using some online tools (not code)

I am using pytesseract 0.1.6, but I think this is Tesseract problem.

Here is my code:

def classify(hash):
  socket = urllib.urlopen(hash)
  image = StringIO(socket.read())
  socket.close()
  image = Image.open(image)
  number = image_to_string(image, config='digits')
  mapping[hash] = number
  return number

classify('any url')
Almo
  • 15,538
  • 13
  • 67
  • 95
user3002996
  • 329
  • 4
  • 18

2 Answers2

1

I think you've got two problems here.

First is that the text is rather small. You can scale the image up by making it 2x as tall and 2x as wide (preferably using AA or cubic interpolation to try and make the letters clearer).

Next there isn't enough white around the edge of the numbers for tesseract to know that it's actually an edge. So you need to add some blank whitespace image around what you've already got.

You can do that manually using photoshop or GIMP or ImageMagick or whatever to validate that it'll actually help. But if you need to do a bunch of images then you'll probably want to use PIL and ImageOps to help.

How do I resize an image using PIL and maintain its aspect ratio?

If you make the new sizes bigger rather than smaller, PIL will grow the image rather than shrink it. Grow it by 2x or 3x both width and height rather than 20% as that'll cause artifacts.

Here's one way to add extra white border: http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.expand

This question might help you with adding the extra whitespace also: In Python, Python Image Library 1.1.6, how can I expand the canvas without resizing?

Community
  • 1
  • 1
Mike Sandford
  • 1,315
  • 10
  • 22
1

The input image is too small for recognition. Here is my solution:

Upsampling the image is required for the accurate recognition. Adding contant borders will center the digits. Applying adaptive-threhsold will result the features (digit-strokes) more available. Result will be:

enter image description here

When you read:

049

Code:


import cv2
import pytesseract

img = cv2.imread("0cLW9.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(h, w) = gry.shape[:2]
gry = cv2.resize(gry, (w * 2, h * 2))
gry = cv2.copyMakeBorder(gry, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=255)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 81, 12)
txt = pytesseract.image_to_string(thr, config="digits") 
print(txt)
cv2.imshow("thr", thr)
cv2.waitKey(0)

You can achieve the same result using other pre-processing methods.

Ahmet
  • 7,527
  • 3
  • 23
  • 47