簡體   English   中英

OCR無法識別帶有符號(-)的電話號碼

[英]OCR doesn't recognize phone numbers with the sign (-)

我正在嘗試從以下圖片中提取電話號碼(調整大小后:) 在此處輸入圖片說明
我的代碼:

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

它返回空字符串。 電話號碼中沒有(-)時,它將正確返回。 我該怎么辦 ? 謝謝 !

好的,當我使用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

對我來說就像一個魅力:

希望這對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM