Ejemplo n.º 1
0
    def _create_row(self, index=None):
        """Create a new row.

        Parameters
        ----------
        index : :class:`int` or :obj:`None`
            If :obj:`None` then append a row, else the index number (0 based) for
            where to create and insert the row.
        """
        cb = QtWidgets.QComboBox()
        cb.addItems(self._actions)
        sb = QtWidgets.QSpinBox()
        sb.setRange(0, 999999)
        msg = QtWidgets.QLineEdit()
        reply = QtWidgets.QLineEdit()
        reply.setReadOnly(True)
        index = index if index is not None else self._table.rowCount()
        self._table.insertRow(index)
        self._table.setCellWidget(index, 0, cb)
        self._table.setCellWidget(index, 1, sb)
        self._table.setCellWidget(index, 2, msg)
        self._table.setCellWidget(index, 3, reply)
        cb.currentTextChanged.connect(
            lambda text: self._update_row_appearance(text, index))
        self._update_row_appearance(cb.currentText(), index)
Ejemplo n.º 2
0
    def __init__(self, client, **kwargs):
        super(Gui, self).__init__()
        self.path = kwargs.pop('path', None)
        self.client = client
        self.original_image = None
        self.delta = 0.01  # the amount to translate image on UP DOWN LEFT RIGHT key presses
        self.client_queue = {}  # a queue of requests to send to the RPi

        zoom = kwargs.get('zoom')
        if zoom:
            self.zoom_history = [zoom]  # (x, y, width, height) values between 0.0 and 1.0
        else:
            self.zoom_history = []

        self.ocr_params = {
            'zoom': zoom,
            'rotate': kwargs.get('rotate'),
            'threshold': kwargs.get('threshold'),
            'dilate': kwargs.get('dilate'),
            'erode': kwargs.get('erode'),
            'blur': kwargs.get('blur'),
            'algorithm': kwargs.get('algorithm', 'tesseract').lower(),
            'lang': kwargs.get('lang', 'eng').lower(),
        }

        self.ocr_label = QtWidgets.QLabel()
        self.ocr_label.setFont(QtGui.QFont('Helvetica', 14))

        # the canvas widget to display the image
        self.graphics_view = pg.GraphicsView(background=None)
        self.view_box = pg.ViewBox(invertY=True, lockAspect=True, enableMouse=False, enableMenu=False)
        self.graphics_view.setCentralItem(self.view_box)
        self.canvas = pg.ImageItem(axisOrder='row-major')
        self.view_box.setMouseMode(pg.ViewBox.RectMode)
        self.view_box.addItem(self.canvas)
        self.view_box.mouseDragEvent = self.select_zoom

        graphing_layout = QtWidgets.QVBoxLayout()
        graphing_layout.addWidget(self.ocr_label, alignment=QtCore.Qt.AlignCenter)
        graphing_layout.addWidget(self.graphics_view)

        # the container for all the image-processing widgets
        self.image_processing_group = QtWidgets.QGroupBox('Image Processing')

        # rotate
        self.rotate_label = QtWidgets.QLabel('<html>Rotate [0&deg;]</html>')
        self.rotate_slider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.rotate_slider.setToolTip('The angle to rotate the image')
        self.rotate_slider.setMinimum(-180)
        self.rotate_slider.setMaximum(180)
        self.rotate_slider.setSingleStep(1)
        self.rotate_slider.setPageStep(15)
        self.rotate_slider.valueChanged.connect(self.update_rotate)
        if self.ocr_params['rotate']:
            self.rotate_slider.setValue(self.ocr_params['rotate'])

        # Gaussian blur
        self.blur_label = QtWidgets.QLabel('Gaussian Blur [0]')
        self.blur_slider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.blur_slider.setToolTip('The pixel radius to use for the Gaussian blur')
        self.blur_slider.setMinimum(0)
        self.blur_slider.setMaximum(kwargs.pop('max_blur', 9))
        self.blur_slider.setSingleStep(1)
        self.blur_slider.valueChanged.connect(self.update_blur)
        if self.ocr_params['blur']:
            self.blur_slider.setValue(self.ocr_params['blur'])

        # threshold
        self.threshold_label = QtWidgets.QLabel('Threshold [0]')
        self.threshold_checkbox = QtWidgets.QCheckBox()
        self.threshold_checkbox.setToolTip('Apply thresholding?')
        self.threshold_checkbox.clicked.connect(self.update_threshold_checkbox)
        self.threshold_slider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.threshold_slider.setToolTip('The threshold value')
        self.threshold_slider.setMinimum(0)
        self.threshold_slider.setMaximum(255)
        self.threshold_slider.setSingleStep(1)
        self.threshold_slider.valueChanged.connect(self.update_threshold)
        if self.ocr_params['threshold']:
            self.threshold_slider.setValue(self.ocr_params['threshold'])
            self.threshold_checkbox.setChecked(True)
        else:
            self.threshold_slider.setEnabled(False)
            self.threshold_checkbox.setChecked(False)

        # dilate
        self.dilate_label = QtWidgets.QLabel('Dilate [0]')
        self.dilate_slider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.dilate_spinbox = QtWidgets.QSpinBox()
        self.dilate_spinbox.setToolTip('The number of iterations to apply dilation at the specified radius')
        self.dilate_spinbox.setMinimum(1)
        self.dilate_spinbox.setMaximum(99)
        self.dilate_spinbox.setSingleStep(1)
        self.dilate_spinbox.valueChanged.connect(self.update_dilate_iter)
        self.dilate_slider.setToolTip('The pixel radius to use for dilation')
        self.dilate_slider.setMinimum(0)
        self.dilate_slider.setMaximum(kwargs.pop('max_dilate', 9))
        self.dilate_slider.setSingleStep(1)
        self.dilate_slider.valueChanged.connect(self.update_dilate)
        if self.ocr_params['dilate']:
            self.dilate_slider.setValue(self.ocr_params['dilate'])

        # erode
        self.erode_label = QtWidgets.QLabel('Erode [0]')
        self.erode_slider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.erode_spinbox = QtWidgets.QSpinBox()
        self.erode_spinbox.setToolTip('The number of iterations to apply erosion at the specified radius')
        self.erode_spinbox.setMinimum(1)
        self.erode_spinbox.setMaximum(99)
        self.erode_spinbox.setSingleStep(1)
        self.erode_spinbox.valueChanged.connect(self.update_erode_iter)
        self.erode_slider.setToolTip('The pixel radius to use for erosion')
        self.erode_slider.setMinimum(0)
        self.erode_slider.setMaximum(kwargs.pop('max_erode', 9))
        self.erode_slider.setSingleStep(1)
        self.erode_slider.valueChanged.connect(self.update_erode)
        if self.ocr_params['erode']:
            self.erode_slider.setValue(self.ocr_params['erode'])

        # image-processing layout
        ip_layout = QtWidgets.QGridLayout()
        ip_layout.addWidget(self.rotate_label, 0, 0)
        ip_layout.addWidget(self.rotate_slider, 0, 1)
        ip_layout.addWidget(self.blur_label, 1, 0)
        ip_layout.addWidget(self.blur_slider, 1, 1)
        ip_layout.addWidget(self.threshold_label, 2, 0)
        ip_layout.addWidget(self.threshold_slider, 2, 1)
        ip_layout.addWidget(self.threshold_checkbox, 2, 2)
        ip_layout.addWidget(self.dilate_label, 3, 0)
        ip_layout.addWidget(self.dilate_slider, 3, 1)
        ip_layout.addWidget(self.dilate_spinbox, 3, 2)
        ip_layout.addWidget(self.erode_label, 4, 0)
        ip_layout.addWidget(self.erode_slider, 4, 1)
        ip_layout.addWidget(self.erode_spinbox, 4, 2)
        self.image_processing_group.setLayout(ip_layout)

        # the container for all the camera widgets
        self.camera_config_group = QtWidgets.QGroupBox('Camera Settings')

        # ISO
        self.iso_combobox = QtWidgets.QComboBox()
        self.iso_combobox.setToolTip('The ISO setting of the camera')
        self.iso_combobox.addItems(['auto', '100', '200', '320', '400', '500', '640', '800'])
        iso = str(kwargs.pop('iso', 'auto'))
        if iso == '0':
            iso = 'auto'
        self.iso_combobox.setCurrentText(iso)
        self.update_iso(self.iso_combobox.currentText())
        self.iso_combobox.currentTextChanged.connect(self.update_iso)
        self.iso_combobox.setEnabled(not self.path)

        # resolution
        self.resolution_combobox = QtWidgets.QComboBox()
        self.resolution_combobox.setToolTip('The resolution of the camera')
        self.resolution_combobox.addItems(['VGA', 'SVGA', 'XGA', '720p', 'SXGA', 'UXGA', '1080p', 'MAX'])
        self.resolution_combobox.setCurrentText(str(kwargs.pop('resolution', 'VGA')))
        self.update_resolution(self.resolution_combobox.currentText())
        self.resolution_combobox.currentTextChanged.connect(self.update_resolution)
        self.resolution_combobox.setEnabled(not self.path)

        # exposure mode
        self.exposure_mode_combobox = QtWidgets.QComboBox()
        self.exposure_mode_combobox.setToolTip('The exposure mode of the camera')
        self.exposure_mode_combobox.addItems(['off', 'auto', 'night', 'nightpreview',
                                              'backlight', 'spotlight', 'sports', 'snow',
                                              'beach', 'verylong', 'fixedfps', 'antishake', 'fireworks'])
        self.exposure_mode_combobox.setCurrentText(str(kwargs.pop('exposure_mode', 'auto')))
        self.update_exposure_mode(self.exposure_mode_combobox.currentText())
        self.exposure_mode_combobox.currentTextChanged.connect(self.update_exposure_mode)
        self.exposure_mode_combobox.setEnabled(not self.path)

        camera_layout = QtWidgets.QGridLayout()
        camera_layout.addWidget(QtWidgets.QLabel('ISO'), 0, 0)
        camera_layout.addWidget(self.iso_combobox, 0, 1)
        camera_layout.addItem(QtWidgets.QSpacerItem(1, 1, QtWidgets.QSizePolicy.MinimumExpanding), 0, 2)
        camera_layout.addWidget(QtWidgets.QLabel('Resolution'), 1, 0)
        camera_layout.addWidget(self.resolution_combobox, 1, 1)
        camera_layout.addWidget(QtWidgets.QLabel('Exposure Mode'), 2, 0)
        camera_layout.addWidget(self.exposure_mode_combobox, 2, 1)
        self.camera_config_group.setLayout(camera_layout)

        if self.client is None:
            self.iso_combobox.setEnabled(False)
            self.resolution_combobox.setEnabled(False)
            self.exposure_mode_combobox.setEnabled(False)

        # the container for all the OCR algorithm widgets
        self.ocr_config_group = QtWidgets.QGroupBox('OCR Algorithm Settings')

        # tesseract languages
        self.tess_lang_combobox = QtWidgets.QComboBox()
        self.tess_lang_combobox.addItems(['eng', 'letsgodigital'])
        self.tess_lang_combobox.currentTextChanged.connect(self.update_tess_lang)
        self.tess_lang_combobox.setToolTip('The language to use for Tesseract')

        # tesseract or ssocr
        self.tess_radio = QtWidgets.QRadioButton('Tesseract')
        self.tess_radio.setToolTip('Use Tesseract')
        self.tess_radio.toggled.connect(self.update_algorithm)
        self.ssocr_radio = QtWidgets.QRadioButton('SSOCR')
        self.ssocr_radio.setToolTip('Use SSOCR')
        self.ssocr_radio.toggled.connect(self.update_algorithm)
        if self.ocr_params['algorithm'] == 'ssocr':
            self.ssocr_radio.setChecked(True)
        else:
            self.tess_radio.setChecked(True)

        algo_layout = QtWidgets.QGridLayout()
        algo_layout.addWidget(self.tess_radio, 0, 0)
        algo_layout.addWidget(self.tess_lang_combobox, 0, 1)
        algo_layout.addItem(QtWidgets.QSpacerItem(1, 1, QtWidgets.QSizePolicy.MinimumExpanding), 0, 2)
        algo_layout.addWidget(self.ssocr_radio, 1, 0)
        self.ocr_config_group.setLayout(algo_layout)

        options_layout = QtWidgets.QVBoxLayout()
        options_layout.addWidget(self.image_processing_group)
        options_layout.addWidget(self.camera_config_group)
        options_layout.addWidget(self.ocr_config_group)
        options_layout.addStretch(1)

        layout = QtWidgets.QHBoxLayout()
        layout.addLayout(graphing_layout)
        layout.addLayout(options_layout)
        self.setLayout(layout)

        if self.path:
            self.setAcceptDrops(True)
            self.capture_thread = None
            self.original_image = utils.to_cv2(self.path)
            height, width = self.original_image.shape[:2]
            try:  # could pass in an already-opened image object (which isn't a path)
                basename = os.path.basename(self.path)
            except OSError:
                basename = 'UNKNOWN'
            self.setWindowTitle('OCR || {} [{} x {}]'.format(basename, width, height))
        else:
            self.setAcceptDrops(False)
            self.original_image = None
            self.capture_index = 0
            self.setWindowTitle('OCR || Capture 0')
            self.capture_thread = Capture()
            self.capture_thread.add_callback(self.capture)
            self.capture_thread.start(self.client)

        self.apply_ocr()