Exemple #1
0
class AreaEditWidget(TransparentWidget):

    # add a signal that emits the area selected
    areaSelected = Signal(QRect)
    areaRemoved = Signal(QPoint)

    def __init__(self, parent=None):
        super().__init__(opacity=0.25)
        # select area
        self.rubberband = QRubberBand(QRubberBand.Rectangle, self)
        # coords of mouse click
        self.origin = QPoint()

    def mousePressEvent(self, event):
        # left click starts the rubber band
        if event.button() == Qt.LeftButton:
            self.origin = QPoint(event.pos())
            self.rubberband.setGeometry(QRect(self.origin, QSize()))
            self.rubberband.show()
        # right click on a selected area to remove it
        if event.button() == Qt.RightButton:
            self.areaRemoved.emit(event.pos())

    def mouseMoveEvent(self, event):
        if not self.origin.isNull():
            self.rubberband.setGeometry(
                QRect(self.origin, event.pos()).normalized())

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.rubberband.hide()
            area_selected = self.rubberband.geometry()
            self.areaSelected.emit(area_selected)
Exemple #2
0
class MainGui(QWidget):
    def __init__(self):
        super(MainGui, self).__init__()
        self.config = ('-l eng --oem 1 --psm 3')
        pytesseract.pytesseract.tesseract_cmd = r"C:\Users\test\AppData\Local\Tesseract-OCR\tesseract.exe"
        self.screenshot_path = take_screenshot()
        self.widget_image = QImage(self.screenshot_path)
        palette = QPalette()
        palette.setBrush(QPalette.Window, QBrush(self.widget_image))
        self.setPalette(palette)
        self.edit_box = QTextEdit()
        self.v_box = QVBoxLayout()
        self.edit_box.hide()
        self.v_box.addWidget(self.edit_box)
        self.setLayout(self.v_box)
        # self.set_window_properties()

    def set_window_properties(self):
        self.setWindowFlags(Qt.CustomizeWindowHint)

    def mousePressEvent(self, eventQMouseEvent):
        self.originQPoint = eventQMouseEvent.pos()
        self.currentQRubberBand = QRubberBand(QRubberBand.Rectangle, self)
        self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QSize()))
        self.edit_box.hide()
        self.currentQRubberBand.show()

    def mouseMoveEvent(self, eventQMouseEvent):
        self.currentQRubberBand.setGeometry(QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())

    def mouseReleaseEvent(self, eventQMouseEvent):
        self.currentQRubberBand.hide()
        selected_area = self.currentQRubberBand.geometry()
        self.currentQRubberBand.deleteLater()
        crop_save_path = self.crop_selected_area(selected_area)
        self.set_edit_box_ui(crop_save_path, selected_area)

    def crop_selected_area(self, selected_area):
        crop_image = QPixmap(self.widget_image).copy(selected_area)
        save_name = "selected_shot.jpg"
        crop_save_path = os.path.join("temp", save_name)
        crop_image.save(crop_save_path)
        return crop_save_path

    def set_edit_box_ui(self, crop_save_path: str, selected_area: QRect):
        s2 = os.path.join(os.getcwd(), crop_save_path).replace("\\", "/")
        self.edit_box. setStyleSheet(f'background-image: url({s2});border: 0px;font: 11pt "Calibri"')
        self.edit_box.setReadOnly(True)
        self.edit_box.show()
        self.edit_box.setGeometry(selected_area)
        self.set_text(crop_save_path)

    def set_text(self, file):
        text = self.get_text(file)
        self.edit_box.setText(text)
        self.edit_box.setFocus()
        self.edit_box.selectAll()

    def closeEvent(self, event: QCloseEvent):
        os.remove(self.screenshot_path)

    def get_text(self, file):
        im = cv2.imread(file, 0)
        return pytesseract.image_to_string(im, config=self.config)