Serge
Serge

Reputation: 332

raspberry pi - OCR numbers from meter

I have almost completed a sucessfull program as the code works with a sample file, but I can't get to edit the photograph of my meter in order for OCR to work.

I find my output image to be quite near a working mode, however I don't know what else I can do to the image to get it working.

This is my code:

import pytesseract
import Image
import sys

from PIL import Image
from PIL import ImageFilter
import PIL
import PIL.ImageOps

image_file_ocr = 'ocr_output.jpg'

image_file = 'image_original.jpg'
#image_file = 'ocr2.jpg'
#image_file = 'sample1.jpg'
#image_file = 'sample2.jpg'
#image_file = 'sample3.jpg'
#image_file = 'sample4.jpg'     # texto largo
#image_file = 'sample5.jpg'     #image_text = "1234567890"

print image_file

# LOAD THE IMAGE
#image = Image.open('sample5.jpg')
image = Image.open(image_file)              # open colour image
image = image.convert('L')               # convert image to monochrome - this works
#image = image.convert('1')              # convert image to black and white

image = image.rotate(-90)

# EDIT THE IMAGE
w, h = image.size
#image = image.crop((0, 30, w, h-30))
image = image.crop((350, 680, 1100, 770))
image.filter(ImageFilter.SHARPEN)

image = PIL.ImageOps.invert(image)

image.save(image_file_ocr,'jpeg')

# PROCESS THE IMAGE
print "\n\nProcessing image: " + image_file_ocr
image = Image.open(image_file_ocr)

print "Process method 1:"
text = pytesseract.image_to_string(image, config='outputbase digits')
print text

print "Process method 2:"
text = pytesseract.image_to_string(image)
print text

This is the original image original image

This is my progress so far to process the image process so far

The following image works correctly

working correctly

Upvotes: 1

Views: 2557

Answers (1)

not2qubit
not2qubit

Reputation: 16930

You may consider to add a config user file with the pattern \d\d\d\d\d\d\d\d (8-digits). In addition please keep in mind the default page segmentation method:

By default Tesseract expects a page of text when it segments an image. If you're just seeking to OCR a small region try a different segmentation mode, using the -psm argument. Note that adding a white border to text which is too tightly cropped may also help, see issue 398.

As of 3.04:

 0   Orientation and script detection (OSD) only.
 1   Automatic page segmentation with OSD.
 2   Automatic page segmentation, but no OSD, or OCR.
 3   Fully automatic page segmentation, but no OSD. (Default)
 4   Assume a single column of text of variable sizes.
 5   Assume a single uniform block of vertically aligned text.
 6   Assume a single uniform block of text.
 7   Treat the image as a single text line.
 8   Treat the image as a single word.
 9   Treat the image as a single word in a circle.
10   Treat the image as a single character.

So you'd probably want -psm 7 for your cropped image.

Also have a look at this answer for how to apply filters.

Upvotes: 1

Related Questions