コード例 #1
0
    def recognizeAll(self):
        try:
            if len(self.mainWindow.listOfElementsToRecognize) == 0:
                UiFunction.showErrorDialog("Najpierw wybierz pliki")
                return
            uiFunction = UiFunction(self.mainWindow)
            progressDialog = uiFunction.progressBarDialog(
                "Odczytywanie tekstu",
                len(self.mainWindow.listOfElementsToRecognize))

            progressDialog.setValue(0)

            self.mainWindow.ui.plainTextEditOutputImage.clear()
            for i in range(0, len(self.mainWindow.listOfElementsToRecognize)):
                progressDialog.setValue(i)
                if progressDialog.wasCanceled():
                    break
                image = Image.open(
                    self.mainWindow.listOfElementsToRecognize[i])
                self.mainWindow.ui.plainTextEditOutputImage.appendPlainText(
                    "---------Grafika " + str(i + 1) + "---------")
                try:
                    if self.mainWindow.ui.checkBoxSecondLanguageImage.isChecked(
                    ):
                        self.mainWindow.ui.plainTextEditOutputImage.appendPlainText(
                            pytesseract.image_to_string(
                                image,
                                lang=self.mainWindow.ui.comboBoxLanguageImage.
                                currentText().lower() + "+" +
                                self.mainWindow.ui.comboBoxSecondLanguageImage.
                                currentText().lower()))
                    else:
                        self.mainWindow.ui.plainTextEditOutputImage.appendPlainText(
                            pytesseract.image_to_string(
                                image,
                                lang=self.mainWindow.ui.comboBoxLanguageImage.
                                currentText().lower()))
                except Exception as inst:
                    UiFunction.showErrorDialog(
                        "Wystąpił problem z odczytywaniem tekstu \n" +
                        str(inst))
            progressDialog.setValue(
                len(self.mainWindow.listOfElementsToRecognize))
        except Exception as inst:
            UiFunction.showErrorDialog(
                "Wystąpił problem podczas rozpoznawania tekstu \n" + str(inst))
コード例 #2
0
    def openFileDialog(self):
        if self.mainWindow.ui.Pages.currentIndex() == 0:
            self.mainWindow.ui.plainTextEditFilePathImage.clear()
            self.mainWindow.ui.plainTextEditOutputImage.clear()
            self.mainWindow.whichImage = 0

            filters = "Obrazy (*.bmp *.png *.jpeg *.tiff)"
            self.dialog = QFileDialog.getOpenFileNames(self.mainWindow,
                                                       "/home", "", filters)

            if len(self.dialog[0]) > 0:
                self.mainWindow.listOfElementsToRecognize.clear()
                uiFunction = UiFunction(self.mainWindow)
                progressDialog = uiFunction.progressBarDialog(
                    "Ładowanie obrazów", len(self.dialog[0]))

                for i in range(0, len(self.dialog[0])):
                    self.mainWindow.ui.plainTextEditFilePathImage.appendPlainText(
                        str(self.dialog[0][i]) + "\n")
                    self.mainWindow.listOfElementsToRecognize.append(
                        str(self.dialog[0][i]))
                    progressDialog.setValue(i)
                    if progressDialog.wasCanceled():
                        break
                progressDialog.setValue(len(self.dialog[0]))

                try:
                    #to preview in QLabel image before must be change to QPixmap
                    pixmap = QPixmap(str(self.dialog[0][0])).scaled(
                        self.mainWindow.ui.previewImage.maximumHeight(),
                        self.mainWindow.ui.previewImage.maximumWidth(),
                        QtCore.Qt.KeepAspectRatio,
                        QtCore.Qt.SmoothTransformation)
                    self.mainWindow.ui.previewImage.setPixmap(pixmap)
                    self.mainWindow.ui.previewImage.setScaledContents(True)
                    self.mainWindow.whichImage = 0

                    self.mainWindow.ui.labelCurrentImageNumber.setText(
                        str(self.mainWindow.whichImage + 1))
                    self.mainWindow.ui.labelAllImageCountImage.setText(
                        str(len(self.mainWindow.listOfElementsToRecognize)))
                except Exception as inst:
                    UiFunction.showErrorDialog(inst)
            else:
                return
コード例 #3
0
 def recognizeCurrent(self):
     try:
         self.mainWindow.ui.plainTextEditOutputPDF.clear()
         if len(self.mainWindow.queueToRecognize) == 0:
             self.mainWindow.ui.plainTextEditOutputPDF.clear()
             if len(self.mainWindow.listOfElementsToRecognize) == 0:
                 UiFunction.showErrorDialog("Najpierw wybierz plik")
                 return
             image = self.mainWindow.listOfElementsToRecognize[
                 self.mainWindow.whichImage]
             try:
                 if self.mainWindow.ui.checkBoxSecondLanguagePDF.isChecked(
                 ):
                     self.mainWindow.ui.plainTextEditOutputPDF.appendPlainText(
                         pytesseract.image_to_string(
                             image,
                             lang=self.mainWindow.ui.comboBoxLanguagePDF.
                             currentText().lower() + "+" +
                             self.mainWindow.ui.comboBoxSecondLanguagePDF.
                             currentText().lower()))
                 else:
                     self.mainWindow.ui.plainTextEditOutputPDF.appendPlainText(
                         pytesseract.image_to_string(
                             image,
                             lang=self.mainWindow.ui.comboBoxLanguagePDF.
                             currentText().lower()))
             except Exception as inst:
                 UiFunction.showErrorDialog(
                     "Wystąpił problem z odczytywaniem tekstu \n" +
                     str(inst))
         else:
             number = self.mainWindow.ui.textEditQueuePDF.toPlainText(
             ).split(',')
             uiFunction = UiFunction(self.mainWindow)
             progressDialog = uiFunction.progressBarDialog(
                 "Odczytywanie tekstu", len(number))
             k = 0
             progressDialog.setValue(k)
             self.mainWindow.ui.plainTextEditOutputImage.clear()
             for i in number:
                 k = k + 1
                 progressDialog.setValue(k)
                 if progressDialog.wasCanceled():
                     break
                 if len(self.mainWindow.listOfElementsToRecognize) == 0:
                     UiFunction.showErrorDialog("Najpierw wybierz plik")
                     return
                 image = self.mainWindow.listOfElementsToRecognize[int(i) -
                                                                   1]
                 self.mainWindow.ui.plainTextEditOutputPDF.appendPlainText(
                     "---------Strona " + str(int(i)) + "---------")
                 try:
                     if self.mainWindow.ui.checkBoxSecondLanguagePDF.isChecked(
                     ):
                         self.mainWindow.ui.plainTextEditOutputPDF.appendPlainText(
                             pytesseract.image_to_string(
                                 image,
                                 lang=self.mainWindow.ui.
                                 comboBoxLanguagePDF.currentText().lower() +
                                 "+" + self.mainWindow.ui.
                                 comboBoxSecondLanguagePDF.currentText(
                                 ).lower()))
                     else:
                         self.mainWindow.ui.plainTextEditOutputPDF.appendPlainText(
                             pytesseract.image_to_string(
                                 image,
                                 lang=self.mainWindow.ui.
                                 comboBoxLanguagePDF.currentText().lower()))
                 except Exception as inst:
                     UiFunction.showErrorDialog(
                         "Wystąpił problem z odczytywaniem tekstu \n" +
                         str(inst))
             progressDialog.setValue(len(number))
     except Exception as inst:
         UiFunction.showErrorDialog(
             "Wystąpił problem podczas rozpoznawania tekstu \n" + str(inst))
コード例 #4
0
    def openFileDialog(self):
        self.mainWindow.ui.plainTextEditFilePathPDF.clear()
        self.mainWindow.ui.plainTextEditOutputPDF.clear()

        filters = "Plik PDF (*.PDF)"
        self.dialog = QFileDialog.getOpenFileName(self.mainWindow, "/home", "",
                                                  filters)

        if len(self.dialog[0]) > 0:
            self.mainWindow.listOfElementsToRecognize.clear()
            self.mainWindow.ui.plainTextEditFilePathPDF.appendPlainText(
                str(self.dialog[0]))
            self.mainWindow.whichImage = 0

            pdfFile = PdfFileReader(open(self.dialog[0], "rb"))

            uiFunction = UiFunction(self.mainWindow)
            progressDialog = uiFunction.progressBarDialog(
                "Ładowanie pliku PDF", pdfFile.numPages)

            #This is not fastest solution, but work very well

            for i in range(pdfFile.numPages):
                progressDialog.setValue(i)
                if progressDialog.wasCanceled():
                    break
                try:
                    output = PdfFileWriter()
                    output.addPage(pdfFile.getPage(i))
                    with open("temp.pdf", "wb") as outputStream:
                        output.write(outputStream)
                    image = pdf2image.convert_from_path(
                        "temp.pdf",
                        poppler_path="./bin/poppler/poppler-0.68.0/bin",
                        thread_count=4)
                    self.mainWindow.listOfElementsToRecognize.append(image[0])
                except Exception as inst:
                    UiFunction.showErrorDialog(
                        "Otwieranie PDF nie powiodło się \n" + str(inst))
                if os.path.exists("temp.pdf"):
                    os.remove("temp.pdf")

            progressDialog.setValue(pdfFile.numPages)

            # to make QPixmap object, before from JPEG or other format necessary is make ImageQt object
            # or normal Image object
            try:
                imageqt = ImageQt(self.mainWindow.listOfElementsToRecognize[
                    self.mainWindow.whichImage])
                imgagePixmap = QPixmap(imageqt).scaled(
                    self.mainWindow.ui.previewPDF.height(),
                    self.mainWindow.ui.previewPDF.width(),
                    QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)
                self.mainWindow.ui.previewPDF.setPixmap(imgagePixmap)
                self.mainWindow.ui.previewPDF.setScaledContents(True)
                self.mainWindow.whichImage = 0
                self.mainWindow.ui.labelCurrentPageNumberPDF.setText(
                    str(self.mainWindow.whichImage + 1))
                self.mainWindow.ui.labelAllPageNumberPDF.setText(
                    str(len(self.mainWindow.listOfElementsToRecognize)))
            except Exception as inst:
                UiFunction.showErrorDialog(
                    "Tworzenie podglądu nie powiodło się \n" + str(inst))
        else:
            return