tesseract_raw.py 19.9 KB
Newer Older
1
import ctypes
2
import locale
3
import logging
4 5 6
import os
import sys

7 8
from ..error import TesseractError

9

10 11
logger = logging.getLogger(__name__)

12
TESSDATA_PREFIX = os.getenv('TESSDATA_PREFIX', None)
13
libnames = []
14 15 16 17 18
# 70 is the minimum credible dpi for tesseract and force it to compute an
# estimate of the image dpi
DPI_DEFAULT = 70


Thomas Perret's avatar
Thomas Perret committed
19
if getattr(sys, 'frozen', False):  # pragma: no cover
20
    # Pyinstaller integration
21
    libnames += [os.path.join(sys._MEIPASS, "libtesseract-4.dll")]
22 23 24 25
    libnames += [os.path.join(sys._MEIPASS, "libtesseract-3.dll")]
    tessdata = os.path.join(sys._MEIPASS, "data")
    if not os.path.exists(os.path.join(tessdata, "tessdata")):
        logger.warning(
26 27 28
            "Running from container, but no tessdata ({}) found !".format(
                tessdata
            )
29 30 31 32
        )
    else:
        TESSDATA_PREFIX = tessdata

33

Thomas Perret's avatar
Thomas Perret committed
34
if sys.platform[:3] == "win":  # pragma: no cover
35
    libnames += [
36 37 38
        # Jflesch> Don't they have the equivalent of LD_LIBRARY_PATH on
        # Windows ?
        "../vs2010/DLL_Release/libtesseract302.dll",
39 40 41 42
        # prefer the most recent first
        "libtesseract305.dll",
        "libtesseract304.dll",
        "libtesseract303.dll",
43
        "libtesseract302.dll",
44 45
        "libtesseract400.dll",  # Tesseract 4 is still in alpha stage
        "libtesseract.dll",
46
        "C:\\Program Files (x86)\\Tesseract-OCR\\libtesseract-4.dll",
47
        "C:\\Program Files (x86)\\Tesseract-OCR\\libtesseract-3.dll",
48 49
    ]
else:
50
    libnames += [
51
        "libtesseract.so.4",
52 53 54 55 56 57
        "libtesseract.so.3",
    ]


g_libtesseract = None

58
lib_load_errors = []
Thomas Perret's avatar
Thomas Perret committed
59
for libname in libnames:  # pragma: no branch
60 61
    try:
        g_libtesseract = ctypes.cdll.LoadLibrary(libname)
62
        lib_load_errors = []
63
        break
Thomas Perret's avatar
Thomas Perret committed
64
    except OSError as ex:  # pragma: no cover
65 66 67 68 69 70
        if hasattr(ex, 'message'):
            # python 2
            lib_load_errors.append((libname, ex.message))
        else:
            # python 3
            lib_load_errors.append((libname, str(ex)))
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

class PageSegMode(object):
    OSD_ONLY = 0
    AUTO_OSD = 1
    AUTO_ONLY = 2
    AUTO = 3
    SINGLE_COLUMN = 4
    SINGLE_BLOCK_VERT_TEXT = 5
    SINGLE_BLOCK = 6
    SINGLE_LINE = 7
    SINGLE_WORD = 8
    CIRCLE_WORD = 9
    SINGLE_CHAR = 10
    SPARSE_TEXT = 11
    SPARSE_TEXT_OSD = 12
87 88
    PSM_RAW_LINE = 13
    COUNT = 14
89 90 91 92 93 94 95 96 97


class Orientation(object):
    PAGE_UP = 0
    PAGE_RIGHT = 1
    PAGE_DOWN = 2
    PAGE_LEFT = 3


98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
class PageIteratorLevel(object):
    BLOCK = 0
    PARA = 1
    TEXTLINE = 2
    WORD = 3
    SYMBOL = 4


class PolyBlockType(object):
    UNKNOWN = 0
    FLOWING_TEXT = 1
    HEADING_TEXT = 2
    PULLOUT_TEXT = 3
    TABLE = 4
    VERTICAL_TEXT = 5
    CAPTION_TEXT = 6
    FLOWING_IMAGE = 7
    HEADING_IMAGE = 8
    PULLOUT_IMAGE = 9
    HORZ_LINE = 10
    VERT_LINE = 11
    NOISE = 12
    COUNT = 13


123 124 125 126 127 128 129 130 131 132 133 134 135 136
class OSResults(ctypes.Structure):
    _fields_ = [
        ("orientations", ctypes.c_float * 4),
        ("scripts_na", ctypes.c_float * 4 * (116 + 1 + 2 + 1)),
        ("unicharset", ctypes.c_void_p),
        ("best_orientation_id", ctypes.c_int),
        ("best_script_id", ctypes.c_int),
        ("best_sconfidence", ctypes.c_float),
        ("best_oconfidence", ctypes.c_float),
        # extra padding in case the structure is extended later
        ("padding", ctypes.c_char * 512),
    ]


Thomas Perret's avatar
Thomas Perret committed
137
if g_libtesseract:  # pragma: no cover
138 139 140
    g_libtesseract.TessVersion.argtypes = []
    g_libtesseract.TessVersion.restype = ctypes.c_char_p

141 142 143
    g_libtesseract.TessBaseAPICreate.argtypes = []
    g_libtesseract.TessBaseAPICreate.restype = ctypes.c_void_p  # TessBaseAPI*
    g_libtesseract.TessBaseAPIDelete.argtypes = [
144
        ctypes.c_void_p,  # TessBaseAPI*
145 146 147
    ]
    g_libtesseract.TessBaseAPIDelete.argtypes = None

148 149 150
    g_libtesseract.TessBaseAPIGetDatapath.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
    ]
151 152
    g_libtesseract.TessBaseAPIGetDatapath.restype = ctypes.POINTER(
        ctypes.c_char)
153

154 155 156 157 158 159 160 161
    g_libtesseract.TessBaseAPIInit1.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # datapath
        ctypes.c_char_p,  # language
        ctypes.c_int,  # TessOcrEngineMode
        ctypes.POINTER(ctypes.c_char_p),  # configs
        ctypes.c_int,  # configs_size
    ]
162
    g_libtesseract.TessBaseAPIInit1.restype = ctypes.c_int
163

164 165 166 167 168 169 170
    g_libtesseract.TessBaseAPIInit3.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # datapath
        ctypes.c_char_p,  # language
    ]
    g_libtesseract.TessBaseAPIInit3.restype = ctypes.c_int

171 172 173 174 175 176 177
    g_libtesseract.TessBaseAPISetSourceResolution.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_int,     # PPI
    ]

    g_libtesseract.TessBaseAPISetSourceResolution.restype = None

178 179 180 181 182 183 184
    g_libtesseract.TessBaseAPISetVariable.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # name
        ctypes.c_char_p,  # value
    ]
    g_libtesseract.TessBaseAPISetVariable.restype = ctypes.c_bool

185 186 187 188 189 190
    g_libtesseract.TessBaseAPIGetAvailableLanguagesAsVector.argtypes = [
        ctypes.c_void_p  # TessBaseAPI*
    ]
    g_libtesseract.TessBaseAPIGetAvailableLanguagesAsVector.restype = \
        ctypes.POINTER(ctypes.c_char_p)

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    g_libtesseract.TessBaseAPISetPageSegMode.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_int,  # See PageSegMode
    ]
    g_libtesseract.TessBaseAPISetPageSegMode.restype = None

    g_libtesseract.TessBaseAPIInitForAnalysePage.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
    ]
    g_libtesseract.TessBaseAPIInitForAnalysePage.restype = None

    g_libtesseract.TessBaseAPISetImage.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.POINTER(ctypes.c_char),  # imagedata
        ctypes.c_int,  # width
        ctypes.c_int,  # height
        ctypes.c_int,  # bytes_per_pixel
        ctypes.c_int,  # bytes_per_line
    ]
    g_libtesseract.TessBaseAPISetImage.restype = None

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    g_libtesseract.TessResultRendererAddImage.argtypes = [
        ctypes.c_void_p,  # TessResultRenderer* renderer
        ctypes.c_void_p  # TessBaseAPI* api
    ]
    g_libtesseract.TessResultRendererAddImage.restype = ctypes.c_bool

    g_libtesseract.TessBaseAPISetInputName.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI* handle
        ctypes.c_char_p  # const char* name
    ]
    g_libtesseract.TessBaseAPISetInputName.restype = None

    g_libtesseract.TessResultRendererBeginDocument.argtypes = [
        ctypes.c_void_p,  # TessResultRenderer* renderer
        ctypes.c_char_p  # const char* title
    ]
    g_libtesseract.TessResultRendererBeginDocument.restype = ctypes.c_bool

    g_libtesseract.TessResultRendererEndDocument.argtypes = [
        ctypes.c_void_p  # TessResultRenderer* renderer
    ]
    g_libtesseract.TessResultRendererEndDocument.restype = ctypes.c_bool

    g_libtesseract.TessPDFRendererCreate.argtypes = [
        ctypes.c_char_p,  # const char* outputbase
        ctypes.c_char_p,  # const char* datadir
        ctypes.c_bool  # BOOL textonly
    ]
    g_libtesseract.TessPDFRendererCreate.restype = ctypes.c_void_p

242
    g_libtesseract.TessBaseAPIRecognize.argtypes = [
243 244 245 246 247
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_void_p,  # ETEXT_DESC*
    ]
    g_libtesseract.TessBaseAPIRecognize.restype = ctypes.c_int

248 249 250 251 252 253 254
    g_libtesseract.TessBaseAPIGetIterator.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
    ]
    g_libtesseract.TessBaseAPIGetIterator.restype = \
        ctypes.c_void_p  # TessResultIterator

    g_libtesseract.TessBaseAPIAnalyseLayout.argtypes = [
255 256 257 258 259
        ctypes.c_void_p,  # TessBaseAPI*
    ]
    g_libtesseract.TessBaseAPIAnalyseLayout.restype = \
        ctypes.c_void_p  # TessPageIterator*

260
    g_libtesseract.TessBaseAPIGetUTF8Text.argtypes = [
261 262
        ctypes.c_void_p,  # TessBaseAPI*
    ]
263
    g_libtesseract.TessBaseAPIGetUTF8Text.restype = ctypes.c_void_p
264

265
    g_libtesseract.TessPageIteratorDelete.argtypes = [
266 267 268 269
        ctypes.c_void_p,  # TessPageIterator*
    ]
    g_libtesseract.TessPageIteratorDelete.restype = None

270
    g_libtesseract.TessPageIteratorOrientation.argtypes = [
271 272 273 274 275 276 277 278
        ctypes.c_void_p,  # TessPageIterator*
        ctypes.POINTER(ctypes.c_int),  # TessOrientation*
        ctypes.POINTER(ctypes.c_int),  # TessWritingDirection*
        ctypes.POINTER(ctypes.c_int),  # TessTextlineOrder*
        ctypes.POINTER(ctypes.c_float),  # deskew_angle
    ]
    g_libtesseract.TessPageIteratorOrientation.restype = None

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    g_libtesseract.TessPageIteratorNext.argtypes = [
        ctypes.c_void_p,  # TessPageIterator*
        ctypes.c_int,  # TessPageIteratorLevel
    ]
    g_libtesseract.TessPageIteratorNext.restype = ctypes.c_bool

    g_libtesseract.TessPageIteratorIsAtBeginningOf.argtypes = [
        ctypes.c_void_p,  # TessPageIterator*
        ctypes.c_int,  # TessPageIteratorLevel
    ]
    g_libtesseract.TessPageIteratorIsAtBeginningOf.restype = ctypes.c_bool

    g_libtesseract.TessPageIteratorIsAtFinalElement.argtypes = [
        ctypes.c_void_p,  # TessPageIterator*
        ctypes.c_int,  # TessPageIteratorLevel (level)
        ctypes.c_int,  # TessPageIteratorLevel (element)
    ]
    g_libtesseract.TessPageIteratorIsAtFinalElement.restype = ctypes.c_bool

    g_libtesseract.TessPageIteratorBlockType.argtypes = [
        ctypes.c_void_p,  # TessPageIterator*
    ]
    g_libtesseract.TessPageIteratorBlockType.restype = \
        ctypes.c_int  # PolyBlockType

    g_libtesseract.TessPageIteratorBoundingBox.args = [
        ctypes.c_void_p,  # TessPageIterator*
        ctypes.c_int,  # TessPageIteratorLevel (level)
        ctypes.POINTER(ctypes.c_int),  # left
        ctypes.POINTER(ctypes.c_int),  # top
        ctypes.POINTER(ctypes.c_int),  # right
        ctypes.POINTER(ctypes.c_int),  # bottom
    ]
    g_libtesseract.TessPageIteratorBoundingBox.restype = ctypes.c_bool

    g_libtesseract.TessResultIteratorGetPageIterator.argtypes = [
        ctypes.c_void_p,  # TessResultIterator*
    ]
    g_libtesseract.TessResultIteratorGetPageIterator.restype = \
        ctypes.c_void_p  # TessPageIterator*

    g_libtesseract.TessResultIteratorGetUTF8Text.argtypes = [
        ctypes.c_void_p,  # TessResultIterator*
        ctypes.c_int,  # TessPageIteratorLevel (level)
    ]
    g_libtesseract.TessResultIteratorGetUTF8Text.restype = \
325
        ctypes.c_void_p
326

327 328 329 330 331 332
    g_libtesseract.TessResultIteratorConfidence.argtypes = [
        ctypes.c_void_p,
        ctypes.c_int,
    ]
    g_libtesseract.TessResultIteratorConfidence.restype = ctypes.c_float

333
    g_libtesseract.TessDeleteText.argtypes = [
334
        ctypes.c_void_p
335 336 337
    ]
    g_libtesseract.TessDeleteText.restype = None

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    if hasattr(g_libtesseract, 'TessBaseAPIDetectOrientationScript'):
        g_libtesseract.TessBaseAPIDetectOrientationScript.argtypes = [
            ctypes.c_void_p,  # TessBaseAPI*
            ctypes.POINTER(ctypes.c_int),  # orient_deg
            ctypes.POINTER(ctypes.c_float),  # orient_conf
            ctypes.POINTER(ctypes.c_char_p),  # script_name
            ctypes.POINTER(ctypes.c_float),  # script_conf
        ]
        g_libtesseract.TessBaseAPIDetectOrientationScript.restype = \
            ctypes.c_bool
    else:
        g_libtesseract.TessBaseAPIDetectOS.argtypes = [
            ctypes.c_void_p,  # TessBaseAPI*
            ctypes.POINTER(OSResults),
        ]
        g_libtesseract.TessBaseAPIDetectOS.restype = ctypes.c_bool
354 355 356


def init(lang=None):
357
    assert(g_libtesseract)
358 359 360 361 362

    # Tesseract 4 workaround
    if get_version() == "4.0.0":
        locale.setlocale(locale.LC_ALL, "C")

363 364 365 366 367
    handle = g_libtesseract.TessBaseAPICreate()
    try:
        if lang:
            lang = lang.encode("utf-8")
        prefix = None
Thomas Perret's avatar
Thomas Perret committed
368
        if TESSDATA_PREFIX:  # pragma: no cover
369 370
            prefix = TESSDATA_PREFIX.encode("utf-8")
        g_libtesseract.TessBaseAPIInit3(
371
            ctypes.c_void_p(handle),
372 373 374
            ctypes.c_char_p(prefix),
            ctypes.c_char_p(lang)
        )
375
        g_libtesseract.TessBaseAPISetVariable(
376
            ctypes.c_void_p(handle),
377 378 379
            b"tessedit_zero_rejection",
            b"F"
        )
Jerome Flesch's avatar
Jerome Flesch committed
380
    except:  # noqa: E722
381
        g_libtesseract.TessBaseAPIDelete(ctypes.c_void_p(handle))
382 383 384 385
        raise
    return handle


386
def cleanup(handle):
387
    assert(g_libtesseract)
388
    g_libtesseract.TessBaseAPIDelete(ctypes.c_void_p(handle))
389

390

391
def is_available():
392
    return g_libtesseract is not None
393 394 395


def get_version():
396
    assert(g_libtesseract)
397
    return g_libtesseract.TessVersion().decode("utf-8")
398 399


400 401 402
def get_available_languages(handle):
    assert(g_libtesseract)

403
    langs = []
404
    c_langs = g_libtesseract.TessBaseAPIGetAvailableLanguagesAsVector(
405
        ctypes.c_void_p(handle)
406 407 408 409 410 411
    )
    i = 0
    while c_langs[i]:
        langs.append(c_langs[i].decode("utf-8"))
        i += 1

412
    return langs
413 414


415
def set_is_numeric(handle, mode):
416 417
    assert(g_libtesseract)

418
    if mode:
419
        wl = b"0123456789."
420 421 422
    else:
        wl = b""

423
    g_libtesseract.TessBaseAPISetVariable(
424 425 426
        ctypes.c_void_p(handle),
        b"tessedit_char_whitelist",
        wl
427 428 429
    )


430 431 432 433 434 435 436 437 438 439 440 441 442
def set_debug_file(handle, filename):
    assert(g_libtesseract)

    if not isinstance(filename, bytes):
        filename = filename.encode('utf-8')

    g_libtesseract.TessBaseAPISetVariable(
        ctypes.c_void_p(handle),
        b"debug_file",
        filename
    )


443 444 445 446
def set_page_seg_mode(handle, mode):
    assert(g_libtesseract)

    g_libtesseract.TessBaseAPISetPageSegMode(
447
        ctypes.c_void_p(handle), ctypes.c_int(mode)
448 449 450 451 452 453
    )


def init_for_analyse_page(handle):
    assert(g_libtesseract)

454
    g_libtesseract.TessBaseAPIInitForAnalysePage(ctypes.c_void_p(handle))
455 456 457 458 459 460 461


def set_image(handle, image):
    assert(g_libtesseract)

    image = image.convert("RGB")
    image.load()
462
    imgdata = image.tobytes("raw", "RGB")
463 464

    g_libtesseract.TessBaseAPISetImage(
465
        ctypes.c_void_p(handle),
466
        imgdata,
467 468
        ctypes.c_int(image.width),
        ctypes.c_int(image.height),
469
        ctypes.c_int(3),  # RGB = 3 * 8
470
        ctypes.c_int(image.width * 3)
471 472
    )

473
    dpi = image.info.get("dpi", [DPI_DEFAULT])[0]
474
    g_libtesseract.TessBaseAPISetSourceResolution(ctypes.c_void_p(handle), dpi)
475

476 477 478 479

def recognize(handle):
    assert(g_libtesseract)

480 481 482
    return g_libtesseract.TessBaseAPIRecognize(
        ctypes.c_void_p(handle), ctypes.c_void_p(None)
    )
483 484 485 486 487


def analyse_layout(handle):
    assert(g_libtesseract)

488
    return g_libtesseract.TessBaseAPIAnalyseLayout(ctypes.c_void_p(handle))
489 490 491


def get_utf8_text(handle):
492
    assert(g_libtesseract)
493
    ptr = g_libtesseract.TessBaseAPIGetUTF8Text(ctypes.c_void_p(handle))
494
    val = ctypes.cast(ptr, ctypes.c_char_p).value.decode("utf-8")
495
    g_libtesseract.TessDeleteText(ptr)
496
    return val
497 498 499 500 501


def page_iterator_delete(iterator):
    assert(g_libtesseract)

502
    return g_libtesseract.TessPageIteratorDelete(ctypes.c_void_p(iterator))
503 504


505 506 507
def page_iterator_next(iterator, level):
    assert(g_libtesseract)

Jerome Flesch's avatar
Jerome Flesch committed
508 509
    return g_libtesseract.TessPageIteratorNext(ctypes.c_void_p(iterator),
                                               level)
510 511 512 513 514


def page_iterator_is_at_beginning_of(iterator, level):
    assert(g_libtesseract)

515 516 517
    return g_libtesseract.TessPageIteratorIsAtBeginningOf(
        ctypes.c_void_p(iterator), level
    )
518 519 520 521 522 523


def page_iterator_is_at_final_element(iterator, level, element):
    assert(g_libtesseract)

    return g_libtesseract.TessPageIteratorIsAtFinalElement(
524
        ctypes.c_void_p(iterator), level, element
525 526 527 528 529 530 531
    )


def page_iterator_block_type(iterator):
    assert(g_libtesseract)

    return g_libtesseract.TessPageIteratorBlockType(
532
        ctypes.c_void_p(iterator)
533 534 535 536 537 538 539
    )


def page_iterator_bounding_box(iterator, level):
    assert(g_libtesseract)

    left = ctypes.c_int(0)
540
    left_p = ctypes.pointer(left)
541
    top = ctypes.c_int(0)
542
    top_p = ctypes.pointer(top)
543
    right = ctypes.c_int(0)
544
    right_p = ctypes.pointer(right)
545
    bottom = ctypes.c_int(0)
546
    bottom_p = ctypes.pointer(bottom)
547 548

    r = g_libtesseract.TessPageIteratorBoundingBox(
549
        ctypes.c_void_p(iterator),
550
        level,
551 552 553 554
        left_p,
        top_p,
        right_p,
        bottom_p
555 556 557 558 559 560
    )
    if not r:
        return (False, (0, 0, 0, 0))
    return (True, (left.value, top.value, right.value, bottom.value))


561 562 563 564 565 566 567 568 569
def page_iterator_orientation(iterator):
    assert(g_libtesseract)

    orientation = ctypes.c_int(0)
    writing_direction = ctypes.c_int(0)
    textline_order = ctypes.c_int(0)
    deskew_angle = ctypes.c_float(0.0)

    g_libtesseract.TessPageIteratorOrientation(
570
        ctypes.c_void_p(iterator),
571 572 573 574
        ctypes.pointer(orientation),
        ctypes.pointer(writing_direction),
        ctypes.pointer(textline_order),
        ctypes.pointer(deskew_angle)
575 576 577 578 579 580 581 582
    )

    return {
        "orientation": orientation.value,
        "writing_direction": writing_direction.value,
        "textline_order": textline_order.value,
        "deskew_angle": deskew_angle.value,
    }
583 584


585 586 587
def get_iterator(handle):
    assert(g_libtesseract)

588 589
    i = g_libtesseract.TessBaseAPIGetIterator(ctypes.c_void_p(handle))
    return i
590 591 592 593 594


def result_iterator_get_page_iterator(res_iterator):
    assert(g_libtesseract)

595 596 597
    return g_libtesseract.TessResultIteratorGetPageIterator(
        ctypes.c_void_p(res_iterator)
    )
598 599 600


def result_iterator_get_utf8_text(iterator, level):
601
    assert(g_libtesseract)
602 603 604
    ptr = g_libtesseract.TessResultIteratorGetUTF8Text(
        ctypes.c_void_p(iterator), level
    )
605 606
    if ptr is None:
        return None
607
    val = ctypes.cast(ptr, ctypes.c_char_p).value.decode("utf-8")
608
    g_libtesseract.TessDeleteText(ptr)
609 610
    return val

611

612
def result_iterator_get_confidence(iterator, level):
613
    assert(g_libtesseract)
614 615 616 617 618 619 620
    ptr = g_libtesseract.TessResultIteratorConfidence(
        ctypes.c_void_p(iterator), level
    )
    if ptr is None:
        return None
    val = ctypes.c_float(ptr).value
    return val
621

622

623 624 625
def detect_os(handle):
    assert(g_libtesseract)

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    # Use the new API function if it is available, because since Tesseract
    # 3.05.00 the old API function _always_ returns False.
    if hasattr(g_libtesseract, 'TessBaseAPIDetectOrientationScript'):
        orientation_deg = ctypes.c_int(0)
        orientation_confidence = ctypes.c_float(0.0)

        r = g_libtesseract.TessBaseAPIDetectOrientationScript(
            ctypes.c_void_p(handle),
            ctypes.byref(orientation_deg),
            ctypes.byref(orientation_confidence),
            None,  # script_name
            None  # script_confidence
        )

        if not r:
            raise TesseractError("detect_orientation failed",
                                 "TessBaseAPIDetectOrientationScript() failed")
        return {
            "orientation": round(orientation_deg.value / 90),
            "confidence": orientation_confidence.value,
        }
    else:  # old API (before Tesseract 3.05.00)
        results = OSResults()
        r = g_libtesseract.TessBaseAPIDetectOS(
            ctypes.c_void_p(handle),
            ctypes.pointer(results)
        )
        if not r:
            raise TesseractError("detect_orientation failed",
                                 "TessBaseAPIDetectOS() failed")
        return {
            "orientation": results.best_orientation_id,
            "confidence": results.best_oconfidence,
        }
660 661 662 663 664 665 666 667 668 669 670


def set_input_name(handle, input_file):
    assert(g_libtesseract)

    g_libtesseract.TessBaseAPISetInputName(
        ctypes.c_void_p(handle),
        input_file.encode()
    )


671
def init_pdf_renderer(handle, output_file, textonly):
672 673
    assert(g_libtesseract)

674
    tessdata_dir = g_libtesseract.TessBaseAPIGetDatapath(handle)
675

676 677
    renderer = g_libtesseract.TessPDFRendererCreate(
        output_file.encode(),
678
        tessdata_dir,
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        ctypes.c_bool(textonly)
    )

    return renderer


def begin_document(renderer, doc_name):
    assert(g_libtesseract)

    g_libtesseract.TessResultRendererBeginDocument(
        ctypes.c_void_p(renderer),
        doc_name.encode()
    )


def add_renderer_image(handle, renderer):
    assert(g_libtesseract)

    g_libtesseract.TessResultRendererAddImage(
        ctypes.c_void_p(renderer),
        ctypes.c_void_p(handle)
    )


def end_document(renderer):
    assert(g_libtesseract)

    g_libtesseract.TessResultRendererEndDocument(
        ctypes.c_void_p(renderer)
    )