获取方向 pytesseract Python3

7

我希望能够获取扫描文件的方向。我看到了这篇文章Pytesseract OCR multiple config options,并尝试使用--psm 0来获取方向。

target = pytesseract.image_to_string(text, lang='eng', boxes=False, \
config='--psm 0 tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyz')

但是我遇到了一个错误:
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/jy/np7p4twj4bx_k396hyc_bnxw0000gn/T/tess_dzgtpadd_out.txt'
3个回答

10

我找到了另一种使用pytesseract获取方向的方法:

print(pytesseract.image_to_osd(Image.open(file_name)))

这是输出结果:

Page number: 0
Orientation in degrees: 270
Rotate: 90
Orientation confidence: 21.27
Script: Latin
Script confidence: 4.14

它能检测脚本或字体吗?如果文档包含不同的字体会怎样? - alyssaeliyah
这是一个不错的解决方案,但发现它并不是很准确。在我进行的一个小实验中,我测试了9个旋转(右、左、下)的PNG文档页面,它只正确识别了6个页面的旋转。 - arun

8

不要写正则表达式从字符串中获取输出,而是传递参数 Output.DICT 以将结果作为 dict 获取。

from pytesseract import Output

im = cv2.imread(str(imPath), cv2.IMREAD_COLOR)
newdata=pytesseract.image_to_osd(im, output_type=Output.DICT)

示例输出如下:使用字典键访问值。
{
    'page_num': 0,
    'orientation': 90,
    'rotate': 270,
    'orientation_conf': 1.2,
    'script': 'Latin',
    'script_conf': 1.11
}

3

@lads已经提到了可以找到方向的方法。 我只是使用了re模块来获取我们需要将图像旋转多少度。

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接