xref: /qemu/tests/functional/qemu_test/tesseract.py (revision ba182a693fe15a4f6f2a04e8ecb865c2630e5a16)
1576fffbcSThomas Huth# ...
2576fffbcSThomas Huth#
3576fffbcSThomas Huth# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
4576fffbcSThomas Huth#
5576fffbcSThomas Huth# This work is licensed under the terms of the GNU GPL, version 2 or
6576fffbcSThomas Huth# later. See the COPYING file in the top-level directory.
7576fffbcSThomas Huth
8576fffbcSThomas Huthimport logging
9*37e9b19cSDaniel P. Berrangéfrom subprocess import run
10576fffbcSThomas Huth
11576fffbcSThomas Huth
12576fffbcSThomas Huthdef tesseract_ocr(image_path, tesseract_args=''):
13576fffbcSThomas Huth    console_logger = logging.getLogger('console')
14576fffbcSThomas Huth    console_logger.debug(image_path)
15*37e9b19cSDaniel P. Berrangé    proc = run(['tesseract', image_path, 'stdout'],
16*37e9b19cSDaniel P. Berrangé               capture_output=True, encoding='utf8')
17*37e9b19cSDaniel P. Berrangé    if proc.returncode:
18576fffbcSThomas Huth        return None
19576fffbcSThomas Huth    lines = []
20*37e9b19cSDaniel P. Berrangé    for line in proc.stdout.split('\n'):
21576fffbcSThomas Huth        sline = line.strip()
22576fffbcSThomas Huth        if len(sline):
23576fffbcSThomas Huth            console_logger.debug(sline)
24576fffbcSThomas Huth            lines += [sline]
25576fffbcSThomas Huth    return lines
26