简体   繁体   English

OCR无法识别带有符号(-)的电话号码

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

I'm trying to extract phone number from the following image (after resize:) 我正在尝试从以下图片中提取电话号码(调整大小后:) 在此处输入图片说明
my code : 我的代码:

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

it returns Empty string. 它返回空字符串。 When there is no (-) in the phone number, it returns it correctly. 电话号码中没有(-)时,它将正确返回。 What should I do ? 我该怎么办 ? Thanks ! 谢谢 !

Okay, when I ran your code with tesseract and the image you provided it perfectly returned the text (dashes and spaces included). 好的,当我使用tesseract运行代码时,您提供的图像完美地返回了文本(包括短划线和空格)。 At that point you could obviously just use txt = txt.replace("-", "").replace(" ", "") to get rid of the dashes and whitespace. 那时,您显然可以只使用txt = txt.replace("-", "").replace(" ", "")来消除破折号和空格。

Buuuuuut I know that OCR (even with us both using tesseract) is going to be different across platforms, so I've included an example of my comment suggestion. Buuuuuut我知道OCR(甚至我们俩都使用tesseract)在各个平台上都将有所不同,因此我在此列举了一个我的评论建议示例。

First we split the images at the dashes, then we read each split image, then we concatenate: 首先,我们在虚线处分割图像,然后读取每个分割的图像,然后进行串联:

# 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

Worked like a charm for me: 对我来说就像一个魅力:

Hope this helped! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM