2017-01-22 150 views
1

OCR号我几乎完成了sucessfull程序的代码,是用一个样本文件,但我不能让编辑我的表的照片,以便OCR工作。树莓派 - 从仪表

我发现我的输出图像是相当接近的工作模式,但我不知道还有什么我可以做形象得到它的工作。

这是我的代码:

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 

这是原始图像 original image

这是我迄今取得的进展,以处理图像 process so far

下面的图片可以正常工作

working correctly

+0

我猜正方体内二值化灰度图像和过程可能不给你的图像上很好地工作。尝试先将其正确二值化,然后将其传递给tesseract;我敢打赌它的效果好得多。 –

+0

我也建议将图像转换为1位像素('mode“1”')。请注意,您可能必须将其转换回8位灰度或rgb才能执行其余的处理。 – martineau

+0

我与#image = image.convert尝试( '1')\t \t \t \t#转换图像以黑色和白色。但是由于背景中留下了许多点,所以效果不佳。你能帮助我如何使用python二进制化? – Serge

回答

1

您可以考虑增加一个config user file与图案\d\d\d\d\d\d\d\d(8位数字)。此外,请记住默认值page segmentation method

默认情况下,Tesseract在分割图像时需要一页文本。如果您只是在寻找一个小区域的OCR,请使用-psm参数尝试不同的分段模式。请注意,添加白色边框的文本这是太紧裁剪也可以帮助,请参见发行398

由于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. 

所以,你一定希望-psm 7为您裁剪图像。

也看看this答案如何应用过滤器。