2014-02-22 57 views
5

我試圖從下面的圖像的電話號碼(後調整:) enter image description here
我的代碼:OCR不與符號識別電話號碼( - )

from PIL import Image 
from pyocr import pyocr 
import pyocr.builders 
import cStringIO 
import os 
os.putenv("TESSDATA_PREFIX", "/usr/share/tesseract-ocr/") 
tools = pyocr.get_available_tools() 
tool = tools[0] 
langs = tool.get_available_languages() 
lang = langs[0] 
file = "test.png" 
txt = tool.image_to_string(Image.open(file), 
          lang=lang, 
          builder=pyocr.builders.TextBuilder()) 
print txt 

返回空串。 當電話號碼中沒有( - )時,它會正確返回。 我該怎麼辦? 謝謝!

+0

「 - 」是否總是在同一個地方?如果是這種情況,您可以分割圖像幾次以刪除「 - 」,然後連接每個單獨圖像上運行OCR的結果。 –

+0

你能提供一個代碼示例嗎? – Sekai

+0

已提供一些代碼 - 請查看 –

回答

4

好吧,當我用tesseract運行你的代碼時,你提供的圖像完美地返回了文本(包括破折號和空格)。在這一點上,你顯然可以使用txt = txt.replace("-", "").replace(" ", "")來擺脫破折號和空格。

Buuuuuut我知道OCR(即使與我們都使用tesseract)跨平臺會有所不同,所以我已經包含了我的評論建議的示例。

首先,我們在劃線分割的影像,然後我們讀到每個分割圖像,然後我們串接:

# I changed your imports a bit 
from PIL import Image 
from pyocr import pyocr 
from pyocr import builders 
import cStringIO 
import os 

# set up all your OCR stuff 
os.putenv("TESSDATA_PREFIX", "/usr/share/tesseract-ocr/") 
tools = pyocr.get_available_tools() 
tool = tools[0] 
langs = tool.get_available_languages() 
lang = "eng" #set language to english to simplify things 

# definte a function to return the text of a given image 
def doOCR(fName): 
    txt = tool.image_to_string(Image.open(fName), lang=lang, builder=builders.TextBuilder()) 
    return txt 

# define the path of the image we are going to read 
path = "test.png" 

# get the image dimensions 
im = Image.open(path) 
width, height = im.size 

# define the points we want to split the image at 
# these are the points where the dashes are 
split_points = [119, 158] 

# define the file names for the image parts 
split_names = ["split-1.png", "split-2.png", "split-3.png"] 

# define a function to crop the image and remove the dashes 
def doCrop(imagePath, cropPath, x, y, x2, y2): 
    im = Image.open(imagePath) 
    box = (x, y, x2, y2) 
    region = im.crop(box) # extract the box region 
    region.save(cropPath) # save it as a separate image 

# in the image you provided each "-" is ~10 pixels long 
lenpix = 10 

# crop the image at the split points 
doCrop(path, split_names[0], 0, 0, split_points[0], height) # get the first section 
doCrop(path, split_names[1], split_points[0] + lenpix, 0, split_points[1], height) # get the middle section 
doCrop(path, split_names[2], split_points[1] + lenpix, 0, width, height) # get the final section 

# define a variable for our final value 
finalValue = "" 

# finally iterate through split files 
# and add the OCR results from each split together 
for f in split_names: 
    finalValue += doOCR(f) # concatenate the ocr value with the final 
    os.remove(f) # remove the split file now that we've used it 

# display the final value 
print finalValue 

工作就像一個魅力對我來說:

希望這有助於!

+0

+1瞭解詳細的答案! 我現在就試試,我只想指出我沒有對我的OCR進行任何培訓,對吧? – Sekai

+0

@Sekai Nope甚至沒有安裝tesseract,直到我意識到我需要它來運行你的代碼。也許全新安裝對你有幫助? –

+0

如果它起作用,則應將其標記爲已接受^ _ ^ –