aboutsummaryrefslogtreecommitdiff
path: root/tests/functional/qemu_test/tesseract.py
blob: c4087b7c11988f33748a41a15392dc37823e82b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# ...
#
# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
#
# This work is licensed under the terms of the GNU GPL, version 2 or
# later. See the COPYING file in the top-level directory.

import re
import logging

from . import has_cmd, run_cmd

def tesseract_available(expected_version):
    if not has_cmd('tesseract'):
        return False
    (stdout, stderr, ret) = run_cmd([ 'tesseract', '--version'])
    if ret:
        return False
    version = stdout.split()[1]
    return int(version.split('.')[0]) >= expected_version

def tesseract_ocr(image_path, tesseract_args=''):
    console_logger = logging.getLogger('console')
    console_logger.debug(image_path)
    (stdout, stderr, ret) = run_cmd(['tesseract', image_path,
                                     'stdout'])
    if ret:
        return None
    lines = []
    for line in stdout.split('\n'):
        sline = line.strip()
        if len(sline):
            console_logger.debug(sline)
            lines += [sline]
    return lines