xref: /qemu/tests/functional/qemu_test/tesseract.py (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1# ...
2#
3# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
4#
5# This work is licensed under the terms of the GNU GPL, version 2 or
6# later. See the COPYING file in the top-level directory.
7
8import logging
9from subprocess import run
10
11
12def tesseract_ocr(image_path, tesseract_args=''):
13    console_logger = logging.getLogger('console')
14    console_logger.debug(image_path)
15    proc = run(['tesseract', image_path, 'stdout'],
16               capture_output=True, encoding='utf8')
17    if proc.returncode:
18        return None
19    lines = []
20    for line in proc.stdout.split('\n'):
21        sline = line.strip()
22        if len(sline):
23            console_logger.debug(sline)
24            lines += [sline]
25    return lines
26