def initialize_tesseract(self):
     """Create tesseract api
     """
     self.tesseract = tess.get_tesseract(os.path.dirname(__file__))
     if not self.tesseract:
         self.show_msg('Tesseract initialization failed...')
         return
     self.api = self.tesseract.TessBaseAPICreate()
     tessdata_prefix = tess.get_tessdata_prefix()
     #current_locale = locale.getlocale()  # Save current locale
     # Switch to C locale to handle
     #  Error: Illegal min or max specification!
     #  "Fatal error encountered!" == NULL:Error:Assert failed:in file
     #   ../../tesseract-ocr/ccutil/globaloc.cpp, line 75
     locale.setlocale(locale.LC_ALL, 'C')
     retc = self.tesseract.TessBaseAPIInit3(self.api,
                                                tessdata_prefix,
                                                self.lang)
     # Restore saved locale
     # locale.setlocale(locale.LC_ALL, current_locale)
     if (retc):
         self.tesseract.TessBaseAPIDelete(self.api)
         self.show_msg('<span style="color:red">Could not initialize ' \
                      'tesseract.</span>')
         return
     self.show_msg('Tesseract %s initialized with language \'%s\'.' % \
                   (tess.VERSION, self.lang))
Ejemplo n.º 2
0
 def initialize_tesseract(self):
     """Create tesseract api
     """
     self.tesseract = tess.get_tesseract(os.path.dirname(__file__))
     if not self.tesseract:
         self.show_msg('Tesseract initialization failed...')
         return
     self.api = self.tesseract.TessBaseAPICreate()
     tessdata_prefix = tess.get_tessdata_prefix()
     #current_locale = locale.getlocale()  # Save current locale
     # Switch to C locale to handle
     #  Error: Illegal min or max specification!
     #  "Fatal error encountered!" == NULL:Error:Assert failed:in file
     #   ../../tesseract-ocr/ccutil/globaloc.cpp, line 75
     locale.setlocale(locale.LC_ALL, 'C')
     retc = self.tesseract.TessBaseAPIInit3(self.api, tessdata_prefix,
                                            self.lang)
     # Restore saved locale
     # locale.setlocale(locale.LC_ALL, current_locale)
     if (retc):
         self.tesseract.TessBaseAPIDelete(self.api)
         self.show_msg('<span style="color:red">Could not initialize ' \
                      'tesseract.</span>')
         return
     self.show_msg('Tesseract %s initialized with language \'%s\'.' % \
                   (tess.VERSION, self.lang))
Ejemplo n.º 3
0
 def __init__(self,parent = None):
     QtCore.QObject.__init__(self)
     '''
     initialize
     '''
     self.tesseract = tesstool.get_tesseract(os.path.dirname(__file__))
     if not self.tesseract:
         print('Tesseract initialization failed...')
         return
     
     self.api = self.tesseract.TessBaseAPICreate()
     self.tessdata_prefix = tesstool.get_tessdata_prefix()
     locale.setlocale(locale.LC_ALL, 'C')
         
     #print('Tesseract %s initialized!' % (tesstool.VERSION)) 
     
     self.pixImage = None
     self.VERSION = tesstool.VERSION
     
     self.parent = parent
    def on_pushButtonShow_pressed(self):
        """Display rectangles
        """
        tessdata_prefix = tess.get_tessdata_prefix()
        locale.setlocale(locale.LC_ALL, 'C')
        retc = 0
        if self.lang != str(self.comboBoxLang.currentText()):
            self.lang = str(self.comboBoxLang.currentText())
            retc = self.tesseract.TessBaseAPIInit3(self.api,
                                                   tessdata_prefix,
                                                   self.lang)
            self.show_msg('Using language \'%s\'.' % self.lang)
        if (retc):
            self.tesseract.TessBaseAPIDelete(self.api)
            self.show_msg('<span style="color:red">Could not re-initialize ' \
                         'tesseract.</span>')
            return

        # Shut up tesseract - there could be a lot of unwanted messages
        #tesseract.TessBaseAPISetVariable(api, "debug_file", "/dev/null")

        self.tesseract.TessBaseAPIClear(self.api)
        # Set PIX structure to tesseract api
        self.tesseract.TessBaseAPISetImage2(self.api, self.pix_image)

        self.tesseract.TessBaseAPISetPageSegMode(self.api,
                                            self.comboBoxPSM.currentIndex())

        # Get info(BOXA structure) about lines(RIL_TEXTLINE) from image in api
        boxa = self.tesseract.TessBaseAPIGetComponentImages(self.api,
                                            self.comboBoxRIL.currentIndex(),
                                            1, None, None)
        if not boxa:
            self.show_msg('No component found. Try to change PSM or RIL.')
            return

        # Get info about number of items on image
        n_items = self.leptonica.boxaGetCount(boxa)
        psm = tess.PSM[self.comboBoxPSM.currentIndex()]
        ril = tess.RIL[self.comboBoxRIL.currentIndex()]
        self.show_msg('<span style="color:green">' \
                     'Found %d image components with %s and %s.</span>' % \
                     (n_items, psm, ril))

        ocr_psm = tess.PSM_SINGLE_BLOCK
        if ril == 'RIL_PARA':
            ocr_psm = tess.PSM_SINGLE_BLOCK
        elif ril == 'RIL_TEXTLINE':
            ocr_psm = tess.PSM_SINGLE_LINE
        elif ril == 'RIL_WORD':
            ocr_psm = tess.PSM_SINGLE_WORD
        elif ril == 'RIL_SYMBOL':
            ocr_psm = tess.PSM_SINGLE_CHAR
        self.tesseract.TessBaseAPISetPageSegMode(self.api, ocr_psm)

        # Set up result type (BOX structure) for leptonica function boxaGetBox
        self.leptonica.boxaGetBox.restype = lept.BOX_PTR_T
        self.leptonica.boxaGetBox.argtypes = []
        n_boxes = len(self.box_data)
        if n_boxes:
            for idx in xrange(n_boxes):
                self.scene.removeItem(self.box_data[idx])

        # Display items and print its info
        box_items = []
        for item in range(0, n_items):
            lept_box = self.leptonica.boxaGetBox(boxa, item, lept.L_CLONE)
            box = lept_box.contents
            self.tesseract.TessBaseAPISetRectangle(self.api,
                                              box.x, box.y, box.w, box.h)
            ocr_result = self.tesseract.TessBaseAPIGetUTF8Text(self.api)
            result_text = ctypes.string_at(ocr_result).decode('utf-8').strip()
            conf = self.tesseract.TessBaseAPIMeanTextConf(self.api)
            self.show_msg('Box[%d]: x=%d, y=%d, w=%d, h=%d, ' \
                          'confidence: %d, ' \
                          'text: <span style="color:blue">%s</span>' % \
                          (item, box.x, box.y, box.w, box.h,
                           conf, result_text
                          ))
            box_items.append(self.scene.addRect(box.x, box.y, box.w, box.h,
                               QPen(QColor(255, 0, 0, 255)),
                               QBrush(QColor(255, 0, 0, 100))))
            box_items[item].setAcceptHoverEvents(True)
            box_items[item].setToolTip("Box[%d]: confidence:%s, text:%s" % \
                                      (item, conf, result_text))
        QCoreApplication.processEvents()
        self.box_data = box_items
Ejemplo n.º 5
0
    def on_pushButtonShow_pressed(self):
        """Display rectangles
        """
        tessdata_prefix = tess.get_tessdata_prefix()
        locale.setlocale(locale.LC_ALL, 'C')
        retc = 0
        if self.lang != str(self.comboBoxLang.currentText()):
            self.lang = str(self.comboBoxLang.currentText())
            retc = self.tesseract.TessBaseAPIInit3(self.api, tessdata_prefix,
                                                   self.lang)
            self.show_msg('Using language \'%s\'.' % self.lang)
        if (retc):
            self.tesseract.TessBaseAPIDelete(self.api)
            self.show_msg('<span style="color:red">Could not re-initialize ' \
                         'tesseract.</span>')
            return

        # Shut up tesseract - there could be a lot of unwanted messages
        #tesseract.TessBaseAPISetVariable(api, "debug_file", "/dev/null")

        self.tesseract.TessBaseAPIClear(self.api)
        # Set PIX structure to tesseract api
        self.tesseract.TessBaseAPISetImage2(self.api, self.pix_image)

        self.tesseract.TessBaseAPISetPageSegMode(
            self.api, self.comboBoxPSM.currentIndex())

        # Get info(BOXA structure) about lines(RIL_TEXTLINE) from image in api
        boxa = self.tesseract.TessBaseAPIGetComponentImages(
            self.api, self.comboBoxRIL.currentIndex(), 1, None, None)
        if not boxa:
            self.show_msg('No component found. Try to change PSM or RIL.')
            return

        # Get info about number of items on image
        n_items = self.leptonica.boxaGetCount(boxa)
        psm = tess.PSM[self.comboBoxPSM.currentIndex()]
        ril = tess.RIL[self.comboBoxRIL.currentIndex()]
        self.show_msg('<span style="color:green">' \
                     'Found %d image components with %s and %s.</span>' % \
                     (n_items, psm, ril))

        ocr_psm = tess.PSM_SINGLE_BLOCK
        if ril == 'RIL_PARA':
            ocr_psm = tess.PSM_SINGLE_BLOCK
        elif ril == 'RIL_TEXTLINE':
            ocr_psm = tess.PSM_SINGLE_LINE
        elif ril == 'RIL_WORD':
            ocr_psm = tess.PSM_SINGLE_WORD
        elif ril == 'RIL_SYMBOL':
            ocr_psm = tess.PSM_SINGLE_CHAR
        self.tesseract.TessBaseAPISetPageSegMode(self.api, ocr_psm)

        # Set up result type (BOX structure) for leptonica function boxaGetBox
        self.leptonica.boxaGetBox.restype = lept.BOX_PTR_T
        self.leptonica.boxaGetBox.argtypes = []
        n_boxes = len(self.box_data)
        if n_boxes:
            for idx in xrange(n_boxes):
                self.scene.removeItem(self.box_data[idx])

        # Display items and print its info
        box_items = []
        for item in range(0, n_items):
            lept_box = self.leptonica.boxaGetBox(boxa, item, lept.L_CLONE)
            box = lept_box.contents
            self.tesseract.TessBaseAPISetRectangle(self.api, box.x, box.y,
                                                   box.w, box.h)
            ocr_result = self.tesseract.TessBaseAPIGetUTF8Text(self.api)
            result_text = ctypes.string_at(ocr_result).decode('utf-8').strip()
            conf = self.tesseract.TessBaseAPIMeanTextConf(self.api)
            self.show_msg('Box[%d]: x=%d, y=%d, w=%d, h=%d, ' \
                          'confidence: %d, ' \
                          'text: <span style="color:blue">%s</span>' % \
                          (item, box.x, box.y, box.w, box.h,
                           conf, result_text
                          ))
            box_items.append(
                self.scene.addRect(box.x, box.y, box.w, box.h,
                                   QPen(QColor(255, 0, 0, 255)),
                                   QBrush(QColor(255, 0, 0, 100))))
            box_items[item].setAcceptHoverEvents(True)
            box_items[item].setToolTip("Box[%d]: confidence:%s, text:%s" % \
                                      (item, conf, result_text))
        QCoreApplication.processEvents()
        self.box_data = box_items