def _create_int_float(self, val, is_float: bool = False, key: str = None) -> QWidget: """ Creates the widget for integer or float. Parameters ---------- val : list list of type, default value, description is_float : bool, optional is the value suposed to be a float, by default False key : str, optional The variable name, by default None Returns ------- QWidget The given widget correctly set up """ default_val = val[1] if val[1] is not None else 0 if is_float: widget = QDoubleSpinBox() else: widget = QSpinBox() widget.setMinimum(-1000) widget.setMaximum(1000) # Needed to return to default if it is ever needed widget.setMinimum(-1000) widget.setMaximum(1000) widget.setValue(self._set_default(widget, default_val)) tmp_val = self.config_val.get(key, None) if isinstance(tmp_val, str): if 'None' not in tmp_val: widget.setValue(float(tmp_val)) elif tmp_val is not None: widget.setValue(float(tmp_val)) widget.setToolTip(val[2]) return widget
class Ui_Transformation(object): def setupUi(self, Transformation, instrument): Transformation.setObjectName("Transformation") Transformation.resize(361, 171) self.frame_layout = QVBoxLayout(Transformation) self.frame_layout.setContentsMargins(4, 4, 4, 4) self.main_layout = QVBoxLayout() self.main_layout.setSpacing(4) self.name_layout = QHBoxLayout() self.name_layout.setSpacing(-1) self.name_label = QLabel("Name", Transformation) self._make_text_bold(self.name_label) self.name_layout.addWidget(self.name_label) self.name_line_edit = QLineEdit(Transformation) self.name_layout.addWidget(self.name_line_edit) self.main_layout.addLayout(self.name_layout) self._add_line() self.vector_label = QLabel("", Transformation) self._make_text_bold(self.vector_label) self.main_layout.addWidget(self.vector_label) self._set_up_vector_box(Transformation) self._add_line() self.value_label = QLabel("") self._make_text_bold(self.value_label) self.main_layout.addWidget(self.value_label) self.magnitude_widget = FieldWidget(hide_name_field=True, instrument=instrument) self.magnitude_widget.setFrameShape(QFrame.NoFrame) self.magnitude_widget.setMinimumHeight(40) self.main_layout.addWidget(self.magnitude_widget) self.ui_placeholder_layout = QFormLayout() self.value_spinbox = QDoubleSpinBox(Transformation) self.value_spinbox.setToolTip("Placeholder value for 3D view to use") self.value_spinbox.setDecimals(8) self.value_spinbox.setMaximumSize(QSize(100, 16777215)) self.ui_placeholder_layout.addRow("Value to use in 3D view:", self.value_spinbox) self.main_layout.addLayout(self.ui_placeholder_layout) self.set_spinbox_ranges() self.frame_layout.addLayout(self.main_layout) self.retranslateUi(Transformation) QMetaObject.connectSlotsByName(Transformation) def _add_line(self): line = QFrame() line.setFrameShape((QFrame.HLine)) line.setFrameShadow(QFrame.Sunken) self.main_layout.addWidget(line) @staticmethod def _make_text_bold(label: QLabel): font = label.font() font.setBold(True) label.setFont(font) def _set_up_vector_box(self, Transformation): self.xyz_layout = QHBoxLayout() self.x_layout = QFormLayout() self.x_spinbox = QDoubleSpinBox(Transformation) self.x_layout.addRow("x:", self.x_spinbox) self.xyz_layout.addLayout(self.x_layout) self.y_layout = QFormLayout() self.y_spinbox = QDoubleSpinBox(Transformation) self.y_layout.addRow("y:", self.y_spinbox) self.xyz_layout.addLayout(self.y_layout) self.z_layout = QFormLayout() self.z_spinbox = QDoubleSpinBox(Transformation) self.z_layout.addRow("z:", self.z_spinbox) self.xyz_layout.addLayout(self.z_layout) self.main_layout.addLayout(self.xyz_layout) def set_spinbox_ranges(self): self.spinboxes = [ self.x_spinbox, self.y_spinbox, self.z_spinbox, self.value_spinbox, ] for spinbox in self.spinboxes: spinbox.setRange(-10000000, 10000000) spinbox.setDecimals(5) def retranslateUi(self, Transformation): Transformation.setWindowTitle( QApplication.translate("Transformation", "GroupBox", None, -1)) Transformation.setTitle( QApplication.translate("Transformation", "Translation", None, -1))
class SettingsDialog(QDialog): def __init__(self): super(SettingsDialog, self).__init__() self.setWindowTitle('Settings') self.setWindowIcon(QIcon('img/yologo.png')) self.setWindowFlags(Qt.WindowCloseButtonHint) self.setMinimumWidth(600) HEIGHT = 30 grid = QGridLayout() # 选择权重文件 label_weights = QLabel('Weights') self.line_weights = QLineEdit() self.line_weights.setFixedHeight(HEIGHT) self.btn_weights = QPushButton('...') self.btn_weights.setFixedWidth(40) self.btn_weights.setFixedHeight(HEIGHT) self.btn_weights.clicked.connect(self.choose_weights_file) grid.addWidget(label_weights, 2, 0) grid.addWidget(self.line_weights, 2, 1, 1, 2) grid.addWidget(self.btn_weights, 2, 3) # 是否使用GPU label_device = QLabel('CUDA device') self.line_device = QLineEdit('cpu') self.line_device.setToolTip('cuda device, i.e. 0 or 0,1,2,3 or cpu') self.line_device.setPlaceholderText('cpu or 0 or 0,1,2,3') self.line_device.setFixedHeight(HEIGHT) grid.addWidget(label_device, 3, 0) grid.addWidget(self.line_device, 3, 1, 1, 3) # 设置图像大小 label_size = QLabel('Img Size') self.combo_size = QComboBox() self.combo_size.setToolTip('inference size (pixels)') self.combo_size.setFixedHeight(HEIGHT) self.combo_size.setStyleSheet( 'QAbstractItemView::item {height: 40px;}') self.combo_size.setView(QListView()) self.combo_size.addItem('320', 320) self.combo_size.addItem('384', 384) self.combo_size.addItem('448', 448) self.combo_size.addItem('512', 512) self.combo_size.addItem('576', 576) self.combo_size.addItem('640', 640) grid.addWidget(label_size, 4, 0) grid.addWidget(self.combo_size, 4, 1, 1, 3) # 设置置信度阈值 label_conf = QLabel('Confidence') self.spin_conf = QDoubleSpinBox() self.spin_conf.setToolTip('confidence threshold') self.spin_conf.setFixedHeight(HEIGHT) self.spin_conf.setDecimals(1) self.spin_conf.setRange(0.1, 0.9) self.spin_conf.setSingleStep(0.1) grid.addWidget(label_conf, 5, 0) grid.addWidget(self.spin_conf, 5, 1, 1, 3) # 设置IOU阈值 label_iou = QLabel('IOU') self.spin_iou = QDoubleSpinBox() self.spin_iou.setToolTip('NMS IoU threshold') self.spin_iou.setFixedHeight(HEIGHT) self.spin_iou.setDecimals(1) self.spin_iou.setRange(0.1, 0.9) self.spin_iou.setSingleStep(0.1) grid.addWidget(label_iou, 6, 0) grid.addWidget(self.spin_iou, 6, 1, 1, 3) # maximum detections per image label_max_det = QLabel('maximum detections') self.spin_max_det = QDoubleSpinBox() self.spin_max_det.setToolTip('Maximum detections per image') self.spin_max_det.setFixedHeight(HEIGHT) self.spin_max_det.setDecimals(0) self.spin_max_det.setRange(10, 1000) self.spin_max_det.setSingleStep(10) grid.addWidget(label_max_det, 7, 0) grid.addWidget(self.spin_max_det, 7, 1, 1, 3) # class-agnostic NMS self.check_agnostic = QCheckBox('Agnostic') self.check_agnostic.setToolTip('class-agnostic NMS') # augmented inference self.check_augment = QCheckBox('Augment') self.check_augment.setToolTip('augmented inference') # half self.check_half = QCheckBox('Half') self.check_half.setToolTip('use FP16 half-precision inference') grid.addWidget(self.check_agnostic, 8, 0) grid.addWidget(self.check_augment, 8, 1) grid.addWidget(self.check_half, 8, 2) # use OpenCV DNN for ONNX inference self.check_dnn = QCheckBox('DNN') self.check_dnn.setToolTip('Use OpenCV DNN for ONNX inference') grid.addWidget(self.check_dnn, 9, 0) box = QGroupBox() box.setLayout(grid) hbox = QHBoxLayout() self.btn_cancel = QPushButton('Cancel') self.btn_cancel.clicked.connect(self.restore) self.btn_ok = QPushButton('Ok') self.btn_ok.clicked.connect(self.save_settings) hbox.addStretch() hbox.addWidget(self.btn_cancel) hbox.addWidget(self.btn_ok) vbox = QVBoxLayout() vbox.addWidget(box) vbox.addLayout(hbox) self.setLayout(vbox) self.load_settings() def choose_weights_file(self): """从系统中选择权重文件""" weights_path = '.' weights = gb.get_config('weights', '') if os.path.exists(weights): weights_path = os.path.dirname(weights) file = QFileDialog.getOpenFileName( self, "Pre-trained YOLOv5 Weights", weights_path, "Weights Files (*.pt);;All Files (*)") if file[0] != '': self.line_weights.setText(file[0]) def load_settings(self): self.line_weights.setText(gb.get_config('weights', '')) self.line_device.setText(gb.get_config('device', 'cpu')) self.combo_size.setCurrentText(gb.get_config('img_size', '640')) self.spin_conf.setValue(gb.get_config('conf_thresh', 0.5)) self.spin_iou.setValue(gb.get_config('iou_thresh', 0.5)) self.spin_max_det.setValue(gb.get_config('max_det', 50)) self.check_agnostic.setChecked(gb.get_config('agnostic', True)) self.check_augment.setChecked(gb.get_config('augment', True)) self.check_half.setChecked(gb.get_config('half', True)) self.check_dnn.setChecked(gb.get_config('dnn', False)) def save_settings(self): """更新配置""" config = { 'weights': self.line_weights.text(), 'device': self.line_device.text(), 'img_size': self.combo_size.currentText(), 'conf_thresh': round(self.spin_conf.value(), 1), 'iou_thresh': round(self.spin_iou.value(), 1), 'max_det': int(self.spin_max_det.value()), 'agnostic': self.check_agnostic.isChecked(), 'augment': self.check_augment.isChecked(), 'half': self.check_half.isChecked(), 'dnn': self.check_dnn.isChecked() } gb.record_config(config) self.accept() def restore(self): """恢复原配置""" self.load_settings() self.reject() def closeEvent(self, event): self.restore()