获取方向 pytesseract Python3

lad*_*ads 5 python tesseract python-tesseract

我想获取扫描文档的方向。我看到了这篇文章Pytesseract OCR multiple config options并尝试使用它--psm 0来获取方向。

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

但我收到一个错误:

FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/jy/np7p4twj4bx_k396hyc_bnxw0000gn/T/tess_dzgtpadd_out.txt'
Run Code Online (Sandbox Code Playgroud)

lad*_*ads 8

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

print(pytesseract.image_to_osd(Image.open(file_name)))
Run Code Online (Sandbox Code Playgroud)

这是输出:

Page number: 0
Orientation in degrees: 270
Rotate: 90
Orientation confidence: 21.27
Script: Latin
Script confidence: 4.14
Run Code Online (Sandbox Code Playgroud)


Mah*_*ran 6

不是编写正则表达式来从字符串中获取输出,而是传递参数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)
Run Code Online (Sandbox Code Playgroud)

示例输出如下所示: 使用 dict 键访问值

{
    'page_num': 0,
    'orientation': 90,
    'rotate': 270,
    'orientation_conf': 1.2,
    'script': 'Latin',
    'script_conf': 1.11
}
Run Code Online (Sandbox Code Playgroud)