Beispiel #1
0
def ocr(img, level):
    """Use tesseract OCR to detection images.

    Args:
        imagePath: File path of image.
        level: Iteration level.

    Returns:
        An array with coordinate of boxes.

    """
    result = []
    with c_locale():
        from tesserocr import PyTessBaseAPI
        api = PyTessBaseAPI()
        api.SetPageSegMode(PSM.AUTO_OSD)
        # api.SetImageFile(imagePath)
        api.SetImage(Image.fromarray(img))
        blockIter = api.AnalyseLayout()
        while blockIter.Next(level):
            pt = blockIter.BlockType()
            #result.append(blockIter.Baseline(level))
            if pt in [1, 6]:
                result.append(blockIter.BoundingBox(level) + (pt, ))
        api.End()
    return result
Beispiel #2
0
    def run_tesseract(image_file):
        if tessdata:
            api = PyTessBaseAPI(path=tessdata, psm=PSM.AUTO_OSD)
        else:
            api = PyTessBaseAPI(psm=PSM.AUTO_OSD)

        api.SetImageFile(image_file)
        api.SetVariable("textord_tablefind_recognize_tables", "T")
        api.SetVariable("textord_tabfind_find_tables", "T")
        api.Recognize()

        document = {}
        it = api.AnalyseLayout()
        if it is not None:
            orientation, direction, order, deskew_angle = it.Orientation()
            api.Recognize()
            ri = api.GetIterator()
            if ri is not None:
                document = {
                    "orientation": orientation,
                    "writing_direction": direction,
                    "text_direction": order,
                    "deskew_angle": deskew_angle,
                    "blocks": []
                }
                while ri.IsAtBeginningOf(RIL.BLOCK):
                    block = {
                        "block_type": ri.BlockType(),
                        "block_type_str": BlockType[ri.BlockType()],
                        "box": ri.BoundingBox(RIL.BLOCK),
                        "ocr_text": ri.GetUTF8Text(RIL.BLOCK),
                        "confidence": ri.Confidence(RIL.BLOCK),
                        "paragraphs": []
                    }
                    break_para = False
                    while True:
                        if ri.IsAtFinalElement(RIL.BLOCK, RIL.PARA):
                            break_para = True
                        break_line = False
                        paragraph = {
                            "box": ri.BoundingBox(RIL.PARA),
                            "ocr_text": ri.GetUTF8Text(RIL.PARA),
                            "paragraph_info": list(ri.ParagraphInfo()),
                            "confidence": ri.Confidence(RIL.PARA),
                            "lines": []
                        }
                        while True:
                            if ri.IsAtFinalElement(RIL.PARA, RIL.TEXTLINE):
                                break_line = True
                            break_word = False
                            line = {
                                "box": ri.BoundingBox(RIL.TEXTLINE),
                                "ocr_text": ri.GetUTF8Text(RIL.TEXTLINE),
                                "confidence": ri.Confidence(RIL.TEXTLINE),
                                "words": []
                            }
                            while True:
                                word = {
                                    "box": ri.BoundingBox(RIL.WORD),
                                    "ocr_text": ri.GetUTF8Text(RIL.WORD),
                                    "confidence": ri.Confidence(RIL.WORD),
                                    "attributes": ri.WordFontAttributes()
                                }
                                if ri.IsAtFinalElement(RIL.TEXTLINE, RIL.WORD):
                                    break_word = True
                                line["words"].append(word)
                                if break_word:
                                    break
                                ri.Next(RIL.WORD)
                            paragraph["lines"].append(line)
                            if break_line:
                                break
                            ri.Next(RIL.TEXTLINE)
                        block["paragraphs"].append(paragraph)
                        if break_para:
                            break
                        ri.Next(RIL.PARA)
                    document["blocks"].append(block)
                    ri.Next(RIL.BLOCK)
        return document