image-processing - 是否可以在通过 pytesseract ocr 模块之前检查图像的方向

标签 image-processing ocr tesseract python-tesseract

对于我当前的 ocr 项目,我尝试使用 tesserect 使用 python 封面 pytesseract 将图像转换为文本文件。到目前为止,我只是将直向的图像传递到我的模块中,因为它能够正确地找出该图像中的文本。但是现在当我传递旋转的图像时,它甚至无法识别一个单词。所以为了得到好的结果,我只需要以正确的方向传递图像。
现在我想知道在将图像传递到 ocr 模块之前是否有任何方法可以确定图像的方向。请让我知道我可以使用哪些方法来进行方向检查。

这是我用来进行转换的方法:

def images_to_text(testImg):
    print('Reading images form the directory..........')
    dataFile=[]
    for filename in os.listdir(testImg):
        os.chdir(testImg)
        # Define config parameters.
        # '-l eng'  for using the English language 
        # '--oem 1' for using LSTM OCR Engine
        config = ('-l eng --oem 1 --psm 3')
        # Read image from disk
        im = cv2.imread(str(filename), cv2.IMREAD_COLOR)
        # Run tesseract OCR on image
        text = pytesseract.image_to_string(im, config=config)
        #basic preprocessing of the text
        text = text.replace('\t',' ')
        text= text.rstrip()
        text= text.lstrip()
        text = text.replace(' +',' ')
        text = text.replace('\n+','\n')
        text = text.replace('\n+ +',' ')

        #writing data to file
        os.chdir(imgTxt)
        rep=filename[-3:]
        name=filename.replace(rep,'txt')
        with open(name, 'w') as writeFile:
            writeFile.write("%s\n" % text)
        text = text.replace('\n',' ')
        dataFile.append(text)
    print('writing data to file done')    
    return dataFile

最佳答案

我得到了检查图像方向的解决方案。我们已经在 pytesseract 中有一个方法来完成这项工作。

imPath='path_to_image'
im = cv2.imread(str(imPath), cv2.IMREAD_COLOR)
newdata=pytesseract.image_to_osd(im)
re.search('(?<=Rotate: )\d+', newdata).group(0)

方法 pytesseract.image_to_osd(im) 的输出是:
Page number: 0
Orientation in degrees: 270
Rotate: 90
Orientation confidence: 4.21
Script: Latin
Script confidence: 1.90

而且我们只需要旋转值来改变方向,所以使用正则表达式将做进一步的剩余工作。
re.search('(?<=Rotate: )\d+', newdata).group(0)

这将是旋转图像以使其达到 0` 方向的 final方法。
def rotate(image, center = None, scale = 1.0):
    angle=360-int(re.search('(?<=Rotate: )\d+', pytesseract.image_to_osd(image)).group(0))
    (h, w) = image.shape[:2]

    if center is None:
        center = (w / 2, h / 2)

    # Perform the rotation
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    return rotated

关于image-processing - 是否可以在通过 pytesseract ocr 模块之前检查图像的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55119504/

相关文章:

python-2.7 - 如何检测透明容器中的水位?

android - 避免冗余构建以制作 tessaract android 库 && 将 tessaract 库链接到 c++ NDK

image-processing - 降低图片中的噪点以使用 tesseract 启用 OCR

制作半色调图像的算法?

python - 如果感兴趣,提取区域的处理时间较慢

ocr - abbyy云ocr SDK

image-processing - 训练 tesseract 3 获取字母表

Azure 表单识别器对 Office 文档的主线支持

python - 通过 Boost 将图像从 Python 发送到 C++

python - 从图像中去除虚假文本区域