Exemple #1
0
    def __init__(self, parent: QWidget = None) -> None:
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.sharedMemory = QSharedMemory("QSharedMemoryExample")

        self.ui.loadFromFileButton.clicked.connect(self.loadFromFile)
        self.ui.loadFromSharedMemoryButton.clicked.connect(self.loadFromMemory)
        self.setWindowTitle(self.tr("SharedMemory Example"))
Exemple #2
0
    def __init__(self, parent=None):
        super(ControlDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.setLayout(self.ui.gridLayout)

        self.ui.groupBox_3.setEnabled(False)
        self.ui.cell_size_edit.setValidator(
            QtGui.QDoubleValidator(0, 100, 4, self))

        self.setFixedSize(self.size())
        self.initial_width = self.width()
        self.initial_height = self.height()
        self.calculate_matrices_height = self.ui.groupBox_2.height()
        self.last_path = ControlDialog.DEFAULT_LOCATION
        self.translator = None

        self.progress = QtGui.QProgressDialog(
            self.tr("Calibrating images..."), self.tr("Cancel"), 0, 100, self)
        self.progress.setWindowTitle('Calibration progress')
        self.progress.setWindowModality(QtCore.Qt.WindowModal)
        self.progress.canceled.connect(self.calibration_canceled)
        self.progress.resize(self.progress.sizeHint() + QtCore.QSize(30, 0))
        self.worker = crp.CalibratorThread()
        self.worker.update_progress.connect(self.set_progress)
        self.clear()
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.t_form = False
        self.a_form = False
        self.ag_form = False

        self.tests = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(1, self.tests)

        self.loader = Loader()
        self.loadTests()

        self.act_lable = QtWidgets.QLabel(self.ui.formWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.act_lable.setFont(font)
        self.ui.formWidgetLayout.insertWidget(3, self.act_lable)
        self.actions = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(4, self.actions)
        self.loadActions()

        self.agg_lable = QtWidgets.QLabel(self.ui.formWidget)
        self.agg_lable.setFont(font)
        self.ui.formWidgetLayout.insertWidget(6, self.agg_lable)
        self.aggregation = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(7, self.aggregation)
        self.loadAggregation()

        self.loadKeys()

        self.tests.currentIndexChanged.connect(self.loadTestFile)
        self.actions.currentIndexChanged.connect(self.loadActionFile)
        self.aggregation.currentIndexChanged.connect(self.loadAggFile)

        self.ui.fromDate.dateChanged.connect(self.updateResult)
        self.ui.toDate.dateChanged.connect(self.updateResult)
        self.ui.fromTime.timeChanged.connect(self.updateResult)
        self.ui.toTime.timeChanged.connect(self.updateResult)
        self.ui.statusCheckbox.stateChanged.connect(self.updateResult)
        self.ui.periodSpinBox.valueChanged.connect(self.updateResult)
        self.ui.resultCombo.currentIndexChanged.connect(self.updateResult)
Exemple #4
0
class Dialog(QDialog):
    def __init__(self, parent: QWidget = None) -> None:
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.sharedMemory = QSharedMemory("QSharedMemoryExample")

        self.ui.loadFromFileButton.clicked.connect(self.loadFromFile)
        self.ui.loadFromSharedMemoryButton.clicked.connect(self.loadFromMemory)
        self.setWindowTitle(self.tr("SharedMemory Example"))

    @Slot()
    def loadFromFile(self):
        if self.sharedMemory.isAttached():
            self.detach()

        self.ui.label.setText(self.tr("Select an image file"))

        fileName, _ = QFileDialog.getOpenFileName(
            None, "", "", self.tr("Images (*.png *.xpm *.jpg)"))

        image = QImage()
        if not image.load(fileName):
            self.ui.label.setText(
                self.tr(
                    "Selected file is not an image, please select another."))
            return
        self.ui.label.setPixmap(QPixmap.fromImage(image))

        buffer = QBuffer()
        buffer.open(QBuffer.ReadWrite)
        out = QDataStream(buffer)
        out << image
        size = buffer.size()

        if not self.sharedMemory.create(size):
            self.ui.label.setText(
                self.tr("Unable to create shared memory segment."))
            return

        self.sharedMemory.lock()
        size = min(self.sharedMemory.size(), size)
        self.sharedMemory.data()[:size] = buffer.data()[:size]
        self.sharedMemory.unlock()

    @Slot()
    def loadFromMemory(self):
        if not self.sharedMemory.attach():
            self.ui.label.setText(
                self.tr("Unable to attach to shared memory segment.\n"
                        "Load an image first."))
            return

        buffer = QBuffer()
        _in = QDataStream(buffer)
        image = QImage()

        self.sharedMemory.lock()
        buffer.setData(self.sharedMemory.data())
        buffer.open(QBuffer.ReadOnly)
        _in >> image
        self.sharedMemory.unlock()

        self.sharedMemory.detach()
        self.ui.label.setPixmap(QPixmap.fromImage(image))

    def detach(self):
        if not self.sharedMemory.detach():
            self.ui.label.setText(
                self.tr("Unable to detach from shared memory."))
Exemple #5
0
class ControlDialog(QtGui.QDialog):
    MIN_CALIBRATION_FILES = 1
    PHOTO_CORRESPONDENCE = 2
    CALIBRATION_CORRESPONDENCE = 1
    WHEN_CAN_START = CALIBRATION_CORRESPONDENCE | PHOTO_CORRESPONDENCE

    DEFAULT_LOCATION = "/home/plaz/Thermal_vision/samples/rgb"
    DEFAULT_MATRICES_FILE = temp_directory + os.sep + 'calib_data.txt'

    def __init__(self, parent=None):
        super(ControlDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.setLayout(self.ui.gridLayout)

        self.ui.groupBox_3.setEnabled(False)
        self.ui.cell_size_edit.setValidator(
            QtGui.QDoubleValidator(0, 100, 4, self))

        self.setFixedSize(self.size())
        self.initial_width = self.width()
        self.initial_height = self.height()
        self.calculate_matrices_height = self.ui.groupBox_2.height()
        self.last_path = ControlDialog.DEFAULT_LOCATION
        self.translator = None

        self.progress = QtGui.QProgressDialog(
            self.tr("Calibrating images..."), self.tr("Cancel"), 0, 100, self)
        self.progress.setWindowTitle('Calibration progress')
        self.progress.setWindowModality(QtCore.Qt.WindowModal)
        self.progress.canceled.connect(self.calibration_canceled)
        self.progress.resize(self.progress.sizeHint() + QtCore.QSize(30, 0))
        self.worker = crp.CalibratorThread()
        self.worker.update_progress.connect(self.set_progress)
        self.clear()

    def calibration_canceled(self):
        self.worker.terminate()
        print('Calibration canceled')
        while not self.worker.isFinished():
            time.sleep(0.1)

    def show(self):
        super(ControlDialog, self).show()
        self.clear()

    def set_progress(self, progress):
        self.progress.setValue(progress)

    def set_translator(self, translator):
        self.translator = translator

    def clear_calculate_matrices_data(self):
        self.ui.rgb_photos_ok_checkbox.setChecked(False)
        self.ui.tv_photos_ok_checkbox.setChecked(False)
        self.rgb_checkboxes = []
        self.tv_comboboxes = []
        self.tv_calibration_files = None
        self.rgb_calibration_files = None
        self.rgb_short_file_names = None
        self.tv_short_file_names = None
        self.checked_correpsonfing_photos = 0
        self.ui.rgb_tv_table.clearContents()
        self.ui.rgb_tv_table.setRowCount(0)
        self.ui.cell_size_edit.setText("0.1")
        self.file_name_to_save_matrices = ControlDialog.DEFAULT_MATRICES_FILE
        self.ui.save_matrices_file_edit.setText("")
        self.can_start_flag &= ~ControlDialog.CALIBRATION_CORRESPONDENCE

    def clear_load_matrices_data(self):
        self.file_name_to_load_matrices = None
        self.ui.select_matrices_file_edit.setText("")
        self.can_start_flag &= ~ControlDialog.CALIBRATION_CORRESPONDENCE

    def clear(self):
        self.can_start_flag = 0
        self.clear_calculate_matrices_data()
        self.clear_load_matrices_data()

        self.can_start_flag = 0
        self.ui.matching_file_edit.setText("")
        self.ui.ok_button.setEnabled(False)
        self.photo_matching_file = None
        self.ui.matching_file_edit.setText("")
        self.want_calculate = True
        self.progress.hide()

    def perform_calibration(self):
        rgb_images = self.rgb_calibration_files
        tv_images = self.tv_calibration_files

        rgb_relative_file_names = []
        tv_relative_file_names = []
        for i, chbox in enumerate(self.rgb_checkboxes):
            if not chbox.isChecked():
                continue
            rgb_relative_file_names.append(self.rgb_calibration_files[i])
            tv_relative_file_names.append(
                self.tv_calibration_files[self.tv_comboboxes[i].currentIndex()])

        cell_size = float(self.ui.cell_size_edit.text())

        config_abs_path = self.write_config(
            rgb_images, tv_images, rgb_relative_file_names, tv_relative_file_names, cell_size)

        save_file = str(self.file_name_to_save_matrices.encode(
            sys.getfilesystemencoding()), encoding)

        if not has_numpy_and_cv():
            commandline_args = ["--config", config_abs_path]
            commandline_args += ["--save-file", save_file]
            if os.name != 'nt':
                p = subprocess.call(
                    [support_directory + os.sep + "run_calibration.sh"] + commandline_args)
            else:
                pass  # TODO write bat file
            return False
        else:  # windows
            self.worker.reset(config_abs_path, save_file)
            self.set_progress(0)
            self.worker.start()
            self.progress.show()
            while self.worker.isRunning():
                QtGui.qApp.processEvents()
                time.sleep(0.1)
            self.progress.hide()
            return self.progress.wasCanceled()

    def ok_pressed(self):
        cur_dir = os.getcwd()
        os.chdir(temp_directory)

        if self.want_calculate:
            has_embedded_cv_and_numpy = self.perform_calibration()
            self.file_name_to_load_matrices = str(self.file_name_to_save_matrices.encode(
                sys.getfilesystemencoding()), encoding)
            if has_embedded_cv_and_numpy:
                self.progress.reset()
                os.chdir(cur_dir)
                return

        try:
            tv_to_rgb_matrix, cameraMatrix_tv, distCoeffs_tv, tv_image_width, \
                tv_image_height = read_matrices(
                    self.file_name_to_load_matrices)
        except:
            print(
                "Error loading calibrations file. Make sure file was produced with this software.")
            os.chdir(cur_dir)
            return

        calibration_file_name = temp_directory + os.sep + 'tv_calibration.txt'
        write_tv_calibration_to_file(
            calibration_file_name, cameraMatrix_tv, distCoeffs_tv, tv_image_width, tv_image_height)

        os.chdir(cur_dir)

        perform_relative_alignment(
            tv_to_rgb_matrix, self.photo_matching_file, calibration_file_name)  # uncomment
        msgBox = QtGui.QMessageBox()
        print('Relative alignment finished')
        msgBox.setText("Succesfully calibrated cameras.")
        msgBox.setStandardButtons(QtGui.QMessageBox.Ok)
        msgBox.buttonClicked.connect(self.hide)
        msgBox.exec()

    def write_config(self, rgb_images, tv_images, rgb_relative_file_names, tv_relative_file_names, cell_size):
        config_file_name = 'config.txt'
        abs_path = os.path.abspath(config_file_name)
        abs_path = str(abs_path.encode(sys.getfilesystemencoding()), encoding)

        f = open(config_file_name, "w")
        f.write(str(len(rgb_images)) + '\n')
        for name in rgb_images:
            n = str(name.encode(sys.getfilesystemencoding()), encoding)
            f.write(n + '\n')

        f.write(str(len(tv_images)) + '\n')
        for name in tv_images:
            n = str(name.encode(sys.getfilesystemencoding()), encoding)
            f.write(n + '\n')
        f.write(str(cell_size) + '\n')
        f.write(str(self.ui.corners_rows_spinbox.value()) + " " + str(self.ui.corners_cols_spinbox.value()) + "\n")

        f.write(str(len(rgb_relative_file_names)) + '\n')
        for r, t in zip(rgb_relative_file_names, tv_relative_file_names):
            rgb = str(r.encode(sys.getfilesystemencoding()), encoding)
            tv = str(t.encode(sys.getfilesystemencoding()), encoding)
            f.write(rgb + '\n')
            f.write(tv + '\n')
        f.close()
        return abs_path

    # def tr(self, str):
    #    return self.translator.translate('dlg', str)

    def save_matrices_to_file_clicked(self):
        options = QtGui.QFileDialog.Options()

        file_name, filtr = QtGui.QFileDialog.getSaveFileName(self,
                                                             "QFileDialog.getSaveFileName()",
                                                             self.last_path + os.sep +
                                                             "calibration.txt",
                                                             "All Files (*);;Text Files (*.txt)", "", options)
        if file_name is not None and check_can_write_file(file_name):

            self.file_name_to_save_matrices = file_name
            self.ui.save_matrices_file_edit.setText(file_name)
        else:
            self.file_name_to_save_matrices = ControlDialog.DEFAULT_MATRICES_FILE

            self.ui.save_matrices_file_edit.setText("")

    def use_matrices_from_file_clicked(self):
        file_name = self.select_text_file_clicked()
        if file_name is None:
            return
        self.file_name_to_load_matrices = file_name
        self.ui.select_matrices_file_edit.setText(file_name)
        self.can_start_flag |= ControlDialog.CALIBRATION_CORRESPONDENCE

    def calculate_matrices_radio_clicked(self):
        self.ui.groupBox_2.setEnabled(True)
        self.ui.groupBox_3.setEnabled(False)
        self.clear_load_matrices_data()
        self.want_calculate = True

    def select_matrices_radio_clicked(self):
        self.ui.groupBox_3.setEnabled(True)
        self.ui.groupBox_2.setEnabled(False)
        self.clear_calculate_matrices_data()
        self.want_calculate = False

    def select_calibration_files_clicked(self):
        files = QtGui.QFileDialog.getOpenFileNames(
            self,
            "Select one or more files to open",
            self.last_path,
            "Images (*.png *.xpm *.jpg *.bmp)")[0]

        if files is not None and len(files) > 0:
            self.last_path = os.path.dirname(files[0])
        if len(files) >= ControlDialog.MIN_CALIBRATION_FILES:
            return files
        else:
            ret = QtGui.QMessageBox.information(
                self, self.tr("Calibration files"),
                self.tr("Not enough files for calibration. Should be at least " + str(
                        ControlDialog.MIN_CALIBRATION_FILES)),
                QtGui.QMessageBox.Ok)

            return None

    def select_text_file_clicked(self):
        file_name = QtGui.QFileDialog.getOpenFileName(
            self,
            "Select file to open",
            self.last_path,
            "Text files (*.txt)")[0]
        if file_name is not None:
            self.last_path = os.path.dirname(file_name)
        return file_name

    def update_tv_comboboxes(self):
        for i, cbox in enumerate(self.tv_comboboxes):
            cbox.clear()
            cbox.addItems(self.tv_short_file_names)
            self.rgb_checkboxes[i].stateChanged.connect(cbox.setEnabled)
            self.rgb_checkboxes[i].setEnabled(True)

    def select_rgb_calib_files_clicked(self):
        files = self.select_calibration_files_clicked()
        if files is not None:
            self.checked_correpsonfing_photos = 0
            self.rgb_calibration_files = files
            self.ui.rgb_photos_ok_checkbox.setChecked(True)
            self.ui.rgb_tv_table.setRowCount(len(files))
            self.rgb_short_file_names = [s.split(os.sep)[-1] for s in files]

            self.rgb_checkboxes = []
            self.tv_comboboxes = []

            self.checked_correpsonfing_photos = 0
            for i, f in enumerate(self.rgb_short_file_names):
                new_check_box = QtGui.QCheckBox(self)
                new_check_box.setChecked(False)
                new_check_box.setEnabled(False)
                new_check_box.setText(f)
                new_check_box.stateChanged.connect(self.table_checkbox_clicked)

                self.rgb_checkboxes.append(new_check_box)
                self.ui.rgb_tv_table.setCellWidget(i, 0, new_check_box)

                new_combo_box = QtGui.QComboBox(self)
                self.tv_comboboxes.append(new_combo_box)
                self.ui.rgb_tv_table.setCellWidget(i, 1, new_combo_box)
                new_combo_box.setEnabled(False)

            if self.tv_calibration_files:
                self.update_tv_comboboxes()

    def select_tv_calib_files_clicked(self):
        files = self.select_calibration_files_clicked()
        if files is not None:
            self.tv_short_file_names = [s.split(os.sep)[-1] for s in files]
            self.tv_calibration_files = files
            self.ui.tv_photos_ok_checkbox.setChecked(True)
            self.update_tv_comboboxes()

        self.ui.ok_button.setEnabled(
            self.can_start_flag == ControlDialog.WHEN_CAN_START)

    def table_checkbox_clicked(self, checked):
        if checked and self.tv_calibration_files:
            self.can_start_flag |= ControlDialog.CALIBRATION_CORRESPONDENCE
            self.checked_correpsonfing_photos += 1
        else:
            self.checked_correpsonfing_photos -= 1
            if self.checked_correpsonfing_photos == 0:
                self.can_start_flag &= (
                    ~ControlDialog.CALIBRATION_CORRESPONDENCE)

        self.ui.ok_button.setEnabled(
            self.can_start_flag == ControlDialog.WHEN_CAN_START)

    def on_select_matching_file_button_clicked(self):
        self.photo_matching_file = self.select_text_file_clicked()
        if self.photo_matching_file:
            self.can_start_flag |= ControlDialog.PHOTO_CORRESPONDENCE
            self.ui.matching_file_edit.setText(self.photo_matching_file)
        else:
            self.can_start_flag &= (~ControlDialog.PHOTO_CORRESPONDENCE)
            self.ui.matching_file_edit.setText("")
        self.ui.ok_button.setEnabled(
            self.can_start_flag == ControlDialog.WHEN_CAN_START)

    def match_photos_by_file_radio_clicked(self):
        if self.photo_matching_file:
            self.can_start_flag |= ControlDialog.PHOTO_CORRESPONDENCE
        else:
            self.can_start_flag &= (~ControlDialog.PHOTO_CORRESPONDENCE)
        self.ui.matching_file_edit.setEnabled(True)
        self.ui.matching_file_button.setEnabled(True)
        self.ui.ok_button.setEnabled(
            self.can_start_flag == ControlDialog.WHEN_CAN_START)

    def match_photos_by_location_radio_clicked(self):
        self.can_start_flag |= ControlDialog.PHOTO_CORRESPONDENCE
        self.ui.matching_file_edit.setEnabled(False)
        self.ui.matching_file_button.setEnabled(False)

        self.ui.ok_button.setEnabled(
            self.can_start_flag == ControlDialog.WHEN_CAN_START)
Exemple #6
0
import sys
from PySide.QtGui import *
from dialog_ui import Ui_Dialog

qt_app = QApplication(sys.argv)

dialog = Ui_Dialog(qt_app)
dialog.show()
sys.exit(qt_app.exec_())

class MainWindow(QtWidgets.QDialog):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.t_form = False
        self.a_form = False
        self.ag_form = False

        self.tests = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(1, self.tests)

        self.loader = Loader()
        self.loadTests()

        self.act_lable = QtWidgets.QLabel(self.ui.formWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.act_lable.setFont(font)
        self.ui.formWidgetLayout.insertWidget(3, self.act_lable)
        self.actions = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(4, self.actions)
        self.loadActions()

        self.agg_lable = QtWidgets.QLabel(self.ui.formWidget)
        self.agg_lable.setFont(font)
        self.ui.formWidgetLayout.insertWidget(6, self.agg_lable)
        self.aggregation = QtWidgets.QComboBox(self)
        self.ui.formWidgetLayout.insertWidget(7, self.aggregation)
        self.loadAggregation()

        self.loadKeys()

        self.tests.currentIndexChanged.connect(self.loadTestFile)
        self.actions.currentIndexChanged.connect(self.loadActionFile)
        self.aggregation.currentIndexChanged.connect(self.loadAggFile)

        self.ui.fromDate.dateChanged.connect(self.updateResult)
        self.ui.toDate.dateChanged.connect(self.updateResult)
        self.ui.fromTime.timeChanged.connect(self.updateResult)
        self.ui.toTime.timeChanged.connect(self.updateResult)
        self.ui.statusCheckbox.stateChanged.connect(self.updateResult)
        self.ui.periodSpinBox.valueChanged.connect(self.updateResult)
        self.ui.resultCombo.currentIndexChanged.connect(self.updateResult)

    def loadTests(self):
        for i in self.loader.t_names:
            self.tests.addItem(i)
        self.loadTestFile()

    def loadTestFile(self):
        if self.t_form:
            self.t_form.deleteLater()
        item_list = self.loader.readFile(
            "Test/", self.loader.t_names.get(self.tests.currentText()))
        self.t_form = Ui_Form()
        self.t_form.english = True
        self.t_form.setupUi(item_list)
        self.t_form.flagged.connect(self.updateResult)
        self.ui.formWidgetLayout.insertWidget(2, self.t_form)
        self.updateResult()

    def loadActions(self):
        self.actions.addItem("NULL")
        for i in self.loader.a_names:
            self.actions.addItem(i)
        self.loadActionFile()

    def loadActionFile(self):
        if self.a_form:
            self.a_form.deleteLater()
        if self.actions.currentText() != "NULL":
            item_list = self.loader.readFile(
                "Action/", self.loader.a_names.get(self.actions.currentText()))
        else:
            item_list = []
        self.a_form = Ui_Form()
        self.a_form.english = True
        self.a_form.setupUi(item_list)
        self.a_form.flagged.connect(self.updateResult)
        self.ui.formWidgetLayout.insertWidget(5, self.a_form)
        self.updateResult()

    def loadAggregation(self):
        self.aggregation.addItem("NULL")
        for i in self.loader.ag_names:
            self.aggregation.addItem(i)
        self.loadAggFile()

    def loadAggFile(self):
        if self.ag_form:
            self.ag_form.deleteLater()
        if self.aggregation.currentText() != "NULL":
            item_list = self.loader.readFile(
                "Aggregation/",
                self.loader.ag_names.get(self.aggregation.currentText()))
        else:
            item_list = []
        self.ag_form = Ui_Form()
        self.ag_form.english = True
        self.ag_form.setupUi(item_list)
        self.ag_form.flagged.connect(self.updateResult)
        self.ui.formWidgetLayout.insertWidget(8, self.ag_form)
        spacer_item = QtWidgets.QSpacerItem(20, 40,
                                            QtWidgets.QSizePolicy.Minimum,
                                            QtWidgets.QSizePolicy.Expanding)
        self.ui.formWidgetLayout.addItem(spacer_item)
        self.updateResult()

    def loadKeys(self):
        keys = self.loader.readFile("", "keys.json")
        self.ui.resultCombo.addItem("All")
        for i in keys:
            self.ui.resultCombo.addItem(i)

    def updateResult(self):
        result = Result(self)
        self.ui.resultLineEdit.setText(result.final_result)
class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
 def __init__(self):
     super().__init__()
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)