6

I have been experimenting latley with Tesseract OCR. I am able to find characters in an image, but I have trouble finding only the bold characters in an image (know if a character in a document image is bold or not). I saw the function WordFontAttributes() mentioned in another question (Can I use OCR to detect font style (bold, italic)?) from the Tesseract API but I am not able to implement it in Python.

braaterAfrikaaner
  • 1,072
  • 10
  • 20
  • 1
    [This](https://github.com/sirfz/tesserocr) tesseract wrapper for python can do the trick for you. – Dmitrii Z. Jan 02 '19 at 15:43
  • Welcome to Stack Overflow! Questions that ask "please help me" tend to be looking for highly localized guidance, or in some cases, ongoing or private assistance, which is not suited to our Q&A format. It is also rather vague, and is better replaced with a more specific question. Please read [Why is β€œCan someone help me?” not an actual question?](//meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – Patrick Artner Jan 02 '19 at 15:51

1 Answers1

0

before it install tesseract 3.05 (4-th version doesn't support WordFontAttributes)

from tesserocr import PyTessBaseAPI, RIL, iterate_level


def get_words_info(image_path, tessdata_path):
    """
    get path to image and path to tessdata and return dict with info about each word
    """
    # api = PyTessBaseAPI(path=tessdata_path)
    with PyTessBaseAPI(path=tessdata_path) as api:
        api.SetImageFile(image_path)
        api.Recognize()
        iter = api.GetIterator()
        level = RIL.WORD

        result = []

        for r in iterate_level(iter, level):
            element = r.GetUTF8Text(level)
            word_attributes = r.WordFontAttributes()
            base_line = r.BoundingBox(level)

            if element:
                word_attributes['word'] = element
                word_attributes['position'] = base_line

            result.append(word_attributes)

        return result
andkot
  • 48
  • 6