Published on

Tesseract and Python on Fedora


Fedora, while including a comprehensive tesseract set of rpms, doesn't have the equivalent of tesseract-python, so I needed something to build/import easily.

However, quick Google searches offer several solutions that now look inappropriate :

However, reading the tesseract project's wiki pages on github indicate that there are several other choices available, and I (somewhat arbitrarily) chose tesserocr, which is MIT licensed, and has a fairly comprehensive API into the 'raw' tesseract C/C++ code.

Installation of tesserocr

sudo dnf install tesseract-devel
pip install tesserocr


The following also includes hints for interoperability with opencv using pillow (for PIL), which can be helpful in cleaning up the image prior to textextraction. It's useful to pre-clean, even though tesseract iteself does some cleaning, because there's often application-specific knowledge that can be used more effectively than the tesseract generic methods.

from tesserocr import PyTessBaseAPI, RIL, PSM

im = cleaned_view

from PIL import Image
im_pil = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))

plt.imshow(im_pil, 'gray')

#  PSM :
with PyTessBaseAPI(psm=PSM.SINGLE_BLOCK) as ocr:

    boxes = ocr.GetComponentImages(RIL.TEXTLINE, True)
    print 'Found {} textline image components.'.format(len(boxes))

    for i, (im, box, _, _) in enumerate(boxes):
        # box is a dict with x, y, w and h keys
        ocr.SetRectangle(box['x'], box['y'], box['w'], box['h'])

        ocrResult = ocr.GetUTF8Text()
        conf = ocr.MeanTextConf()

        print (u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, "
               "confidence: {1}, text: {2}").format(i, conf, ocrResult.replace('\n',''), **box)