| 1 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | # |
| 3 | # Copyright (c) 2019 Philippe Mathieu-Daudé |
| 4 | ''' |
| 5 | Tesseract is an program for doing Optical Character Recognition (OCR), |
| 6 | which can be used in the tests to extract text from screenshots. |
| 7 | ''' |
| 8 | |
| 9 | import logging |
| 10 | from subprocess import run |
| 11 | |
| 12 | |
| 13 | def tesseract_ocr(image_path): |
| 14 | ''' Run the tesseract OCR to extract text from a screenshot ''' |
| 15 | console_logger = logging.getLogger('console') |
| 16 | console_logger.debug(image_path) |
| 17 | proc = run(['tesseract', image_path, 'stdout'], |
| 18 | capture_output=True, encoding='utf8', check=False) |
| 19 | if proc.returncode: |
| 20 | return None |
| 21 | lines = [] |
| 22 | for line in proc.stdout.split('\n'): |
| 23 | sline = line.strip() |
| 24 | if len(sline): |
| 25 | console_logger.debug(sline) |
| 26 | lines += [sline] |
| 27 | return lines |