在通过pytesseract ocr模块之前是否可以检查图像的方向 [英] Is it possible to check orientation of an image before passing it through pytesseract ocr module

查看:67
本文介绍了在通过pytesseract ocr模块之前是否可以检查图像的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For my current ocr project I tried using tesserect using the the python cover pytesseract for converting images into text files. Up till now I was only passing well straight oriented images into my module at it was able to properly figure out text in that image. But now as I am passing rotated images it is not able recognize even a single word. So to get good result I need to pass images only with proper orientation. Now I want to know that is there any method to figure out the orientation of an image before passing it in ocr module. Please let me know what methods can I used to do that orientation check.

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

This is the method which I am using to do conversion:

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中已经有一种方法可以完成这项工作.

I got the solution to check the orientation of an image. We already have an method in pytesseract to do this work.

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)方法的输出为:

Output of method pytesseract.image_to_osd(im) is:

Page number: 0
Orientation in degrees: 270
Rotate: 90
Orientation confidence: 4.21
Script: Latin
Script confidence: 1.90

我们仅需要旋转值来更改方向,因此使用正则表达式将做更多的工作.

And we need rotation value only for changing the orientation, so using regular expression will do further remaining work.

re.search('(?<=Rotate: )\d+', newdata).group(0)

这是旋转图像以使其变为0`方向的最终方法.

This would be the final method to rotate an image to bring it to 0` orientation.

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

这篇关于在通过pytesseract ocr模块之前是否可以检查图像的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆