获取方向 pytesseract Python3

Get orientation pytesseract Python3

我想获取扫描文档的方向。我看到了 post 并尝试使用 --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'

我找到了另一种使用 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

@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)

不是编写 regex 来获取字符串的输出,而是传递参数 Output.DICT 来获取 dict[=14= 的结果]

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
}