control_panel.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import os
  2. from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
  3. QLabel, QComboBox, QGroupBox, QSlider, QCheckBox,
  4. QSpinBox, QDoubleSpinBox, QFormLayout, QTabWidget)
  5. from PyQt5.QtCore import Qt, pyqtSlot, QSize, QSettings
  6. from PyQt5.QtGui import QIcon, QFont
  7. class ControlPanel(QWidget):
  8. """控制面板组件,用于系统参数调整和控制"""
  9. def __init__(self, config):
  10. super().__init__()
  11. self.config = config
  12. self.init_ui()
  13. def init_ui(self):
  14. """初始化UI"""
  15. # 创建主布局
  16. layout = QVBoxLayout(self)
  17. layout.setContentsMargins(5, 5, 5, 5)
  18. # 创建标签页
  19. tab_widget = QTabWidget()
  20. # 添加检测控制页
  21. detection_tab = self.create_detection_tab()
  22. tab_widget.addTab(detection_tab, "检测控制")
  23. # 添加摄像头控制页
  24. camera_tab = self.create_camera_tab()
  25. tab_widget.addTab(camera_tab, "摄像头控制")
  26. # 添加告警控制页
  27. alert_tab = self.create_alert_tab()
  28. tab_widget.addTab(alert_tab, "告警控制")
  29. # 添加标签页到布局
  30. layout.addWidget(tab_widget)
  31. # 底部按钮区域
  32. button_layout = QHBoxLayout()
  33. # 启动按钮
  34. self.start_btn = QPushButton("启动监测")
  35. self.start_btn.setIcon(QIcon(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'assets', 'start.png')))
  36. self.start_btn.clicked.connect(self.start_monitoring)
  37. button_layout.addWidget(self.start_btn)
  38. # 停止按钮
  39. self.stop_btn = QPushButton("停止监测")
  40. self.stop_btn.setIcon(QIcon(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'assets', 'stop.png')))
  41. self.stop_btn.clicked.connect(self.stop_monitoring)
  42. self.stop_btn.setEnabled(False)
  43. button_layout.addWidget(self.stop_btn)
  44. # 添加按钮区域到布局
  45. layout.addLayout(button_layout)
  46. def create_detection_tab(self):
  47. """创建检测控制标签页"""
  48. widget = QWidget()
  49. layout = QVBoxLayout(widget)
  50. # 检测任务分组
  51. task_group = QGroupBox("检测任务")
  52. task_layout = QVBoxLayout(task_group)
  53. # 添加复选框
  54. self.fire_check = QCheckBox("火灾检测")
  55. self.fire_check.setChecked(True)
  56. task_layout.addWidget(self.fire_check)
  57. self.animal_check = QCheckBox("动物检测")
  58. self.animal_check.setChecked(True)
  59. task_layout.addWidget(self.animal_check)
  60. self.landslide_check = QCheckBox("滑坡检测")
  61. self.landslide_check.setChecked(True)
  62. task_layout.addWidget(self.landslide_check)
  63. self.pest_check = QCheckBox("病虫害检测")
  64. self.pest_check.setChecked(True)
  65. task_layout.addWidget(self.pest_check)
  66. layout.addWidget(task_group)
  67. # 检测参数分组
  68. param_group = QGroupBox("检测参数")
  69. param_layout = QFormLayout(param_group)
  70. # 置信度阈值
  71. self.conf_slider = QSlider(Qt.Horizontal)
  72. self.conf_slider.setRange(1, 100)
  73. self.conf_slider.setValue(int(self.config.get('conf_threshold', 0.25) * 100))
  74. self.conf_slider.setTickPosition(QSlider.TicksBelow)
  75. self.conf_slider.setTickInterval(10)
  76. self.conf_slider.valueChanged.connect(self.update_conf_threshold)
  77. self.conf_label = QLabel(f"{self.conf_slider.value() / 100:.2f}")
  78. conf_layout = QHBoxLayout()
  79. conf_layout.addWidget(self.conf_slider)
  80. conf_layout.addWidget(self.conf_label)
  81. param_layout.addRow("置信度阈值:", conf_layout)
  82. # IOU阈值
  83. self.iou_slider = QSlider(Qt.Horizontal)
  84. self.iou_slider.setRange(1, 100)
  85. self.iou_slider.setValue(int(self.config.get('iou_threshold', 0.45) * 100))
  86. self.iou_slider.setTickPosition(QSlider.TicksBelow)
  87. self.iou_slider.setTickInterval(10)
  88. self.iou_slider.valueChanged.connect(self.update_iou_threshold)
  89. self.iou_label = QLabel(f"{self.iou_slider.value() / 100:.2f}")
  90. iou_layout = QHBoxLayout()
  91. iou_layout.addWidget(self.iou_slider)
  92. iou_layout.addWidget(self.iou_label)
  93. param_layout.addRow("IOU阈值:", iou_layout)
  94. # 输入尺寸
  95. self.size_combo = QComboBox()
  96. self.size_combo.addItem("320x320", 320)
  97. self.size_combo.addItem("416x416", 416)
  98. self.size_combo.addItem("512x512", 512)
  99. self.size_combo.addItem("640x640", 640)
  100. self.size_combo.addItem("1280x1280", 1280)
  101. # 设置默认值
  102. default_size = self.config.get('image_size', 640)
  103. index = self.size_combo.findData(default_size)
  104. if index >= 0:
  105. self.size_combo.setCurrentIndex(index)
  106. param_layout.addRow("输入尺寸:", self.size_combo)
  107. # 批处理大小
  108. self.batch_spin = QSpinBox()
  109. self.batch_spin.setRange(1, 64)
  110. self.batch_spin.setValue(self.config.get('batch_size', 16))
  111. param_layout.addRow("批处理大小:", self.batch_spin)
  112. layout.addWidget(param_group)
  113. # 添加保存按钮
  114. self.save_params_btn = QPushButton("保存参数")
  115. self.save_params_btn.clicked.connect(self.save_detection_params)
  116. layout.addWidget(self.save_params_btn)
  117. return widget
  118. def create_camera_tab(self):
  119. """创建摄像头控制标签页"""
  120. widget = QWidget()
  121. layout = QVBoxLayout(widget)
  122. # 摄像头选择分组
  123. camera_group = QGroupBox("摄像头选择")
  124. camera_layout = QFormLayout(camera_group)
  125. # 摄像头列表
  126. self.camera_combo = QComboBox()
  127. self.camera_combo.addItem("默认摄像头", 0)
  128. self.camera_combo.addItem("USB摄像头1", 1)
  129. self.camera_combo.addItem("网络摄像头", "rtsp://admin:admin@192.168.1.100:554/stream")
  130. camera_layout.addRow("摄像头:", self.camera_combo)
  131. # 分辨率选择
  132. self.resolution_combo = QComboBox()
  133. self.resolution_combo.addItem("320x240", (320, 240))
  134. self.resolution_combo.addItem("640x480", (640, 480))
  135. self.resolution_combo.addItem("1280x720", (1280, 720))
  136. self.resolution_combo.addItem("1920x1080", (1920, 1080))
  137. camera_layout.addRow("分辨率:", self.resolution_combo)
  138. # 帧率选择
  139. self.fps_spin = QSpinBox()
  140. self.fps_spin.setRange(1, 60)
  141. self.fps_spin.setValue(30)
  142. camera_layout.addRow("帧率:", self.fps_spin)
  143. layout.addWidget(camera_group)
  144. # 图像调整分组
  145. adjust_group = QGroupBox("图像调整")
  146. adjust_layout = QFormLayout(adjust_group)
  147. # 亮度调整
  148. self.brightness_slider = QSlider(Qt.Horizontal)
  149. self.brightness_slider.setRange(0, 100)
  150. self.brightness_slider.setValue(50)
  151. adjust_layout.addRow("亮度:", self.brightness_slider)
  152. # 对比度调整
  153. self.contrast_slider = QSlider(Qt.Horizontal)
  154. self.contrast_slider.setRange(0, 100)
  155. self.contrast_slider.setValue(50)
  156. adjust_layout.addRow("对比度:", self.contrast_slider)
  157. # 饱和度调整
  158. self.saturation_slider = QSlider(Qt.Horizontal)
  159. self.saturation_slider.setRange(0, 100)
  160. self.saturation_slider.setValue(50)
  161. adjust_layout.addRow("饱和度:", self.saturation_slider)
  162. layout.addWidget(adjust_group)
  163. # 添加应用按钮
  164. self.apply_camera_btn = QPushButton("应用设置")
  165. self.apply_camera_btn.clicked.connect(self.apply_camera_settings)
  166. layout.addWidget(self.apply_camera_btn)
  167. return widget
  168. def create_alert_tab(self):
  169. """创建告警控制标签页"""
  170. widget = QWidget()
  171. layout = QVBoxLayout(widget)
  172. # 告警阈值分组
  173. threshold_group = QGroupBox("告警阈值设置")
  174. threshold_layout = QFormLayout(threshold_group)
  175. # 火灾告警阈值
  176. self.fire_threshold = QSlider(Qt.Horizontal)
  177. self.fire_threshold.setRange(50, 95)
  178. self.fire_threshold.setValue(75)
  179. self.fire_threshold.setTracking(True)
  180. self.fire_threshold.setTickPosition(QSlider.TicksBelow)
  181. threshold_layout.addRow("火灾告警阈值:", self.fire_threshold)
  182. # 动物告警阈值
  183. self.animal_threshold = QSlider(Qt.Horizontal)
  184. self.animal_threshold.setRange(50, 95)
  185. self.animal_threshold.setValue(70)
  186. self.animal_threshold.setTracking(True)
  187. self.animal_threshold.setTickPosition(QSlider.TicksBelow)
  188. threshold_layout.addRow("动物告警阈值:", self.animal_threshold)
  189. # 滑坡告警阈值
  190. self.landslide_threshold = QSlider(Qt.Horizontal)
  191. self.landslide_threshold.setRange(50, 95)
  192. self.landslide_threshold.setValue(80)
  193. self.landslide_threshold.setTracking(True)
  194. self.landslide_threshold.setTickPosition(QSlider.TicksBelow)
  195. threshold_layout.addRow("滑坡告警阈值:", self.landslide_threshold)
  196. # 病虫害告警阈值
  197. self.pest_threshold = QSlider(Qt.Horizontal)
  198. self.pest_threshold.setRange(50, 95)
  199. self.pest_threshold.setValue(70)
  200. self.pest_threshold.setTracking(True)
  201. self.pest_threshold.setTickPosition(QSlider.TicksBelow)
  202. threshold_layout.addRow("病虫害告警阈值:", self.pest_threshold)
  203. layout.addWidget(threshold_group)
  204. # 告警方式分组
  205. method_group = QGroupBox("告警方式")
  206. method_layout = QVBoxLayout(method_group)
  207. # 添加复选框
  208. self.ui_alert_check = QCheckBox("界面告警")
  209. self.ui_alert_check.setChecked(True)
  210. method_layout.addWidget(self.ui_alert_check)
  211. self.sound_alert_check = QCheckBox("声音告警")
  212. self.sound_alert_check.setChecked(True)
  213. method_layout.addWidget(self.sound_alert_check)
  214. self.sms_alert_check = QCheckBox("短信告警")
  215. self.sms_alert_check.setChecked(False)
  216. method_layout.addWidget(self.sms_alert_check)
  217. self.email_alert_check = QCheckBox("邮件告警")
  218. self.email_alert_check.setChecked(False)
  219. method_layout.addWidget(self.email_alert_check)
  220. layout.addWidget(method_group)
  221. # 添加应用按钮
  222. self.apply_alert_btn = QPushButton("应用设置")
  223. self.apply_alert_btn.clicked.connect(self.apply_alert_settings)
  224. layout.addWidget(self.apply_alert_btn)
  225. return widget
  226. @pyqtSlot(int)
  227. def update_conf_threshold(self, value):
  228. """更新置信度阈值显示"""
  229. self.conf_label.setText(f"{value / 100:.2f}")
  230. @pyqtSlot(int)
  231. def update_iou_threshold(self, value):
  232. """更新IOU阈值显示"""
  233. self.iou_label.setText(f"{value / 100:.2f}")
  234. @pyqtSlot()
  235. def save_detection_params(self):
  236. """保存检测参数"""
  237. # 获取参数
  238. conf_threshold = self.conf_slider.value() / 100
  239. iou_threshold = self.iou_slider.value() / 100
  240. image_size = self.size_combo.currentData()
  241. batch_size = self.batch_spin.value()
  242. # 更新配置
  243. self.config['conf_threshold'] = conf_threshold
  244. self.config['iou_threshold'] = iou_threshold
  245. self.config['image_size'] = image_size
  246. self.config['batch_size'] = batch_size
  247. # 保存检测任务
  248. self.config['enable_fire_detection'] = self.fire_check.isChecked()
  249. self.config['enable_animal_detection'] = self.animal_check.isChecked()
  250. self.config['enable_landslide_detection'] = self.landslide_check.isChecked()
  251. self.config['enable_pest_detection'] = self.pest_check.isChecked()
  252. # 保存到设置文件(实际项目中实现)
  253. print("检测参数已保存")
  254. @pyqtSlot()
  255. def apply_camera_settings(self):
  256. """应用摄像头设置"""
  257. # 获取参数
  258. camera_source = self.camera_combo.currentData()
  259. resolution = self.resolution_combo.currentData()
  260. fps = self.fps_spin.value()
  261. brightness = self.brightness_slider.value()
  262. contrast = self.contrast_slider.value()
  263. saturation = self.saturation_slider.value()
  264. # 更新配置
  265. self.config['camera_source'] = camera_source
  266. self.config['camera_resolution'] = resolution
  267. self.config['camera_fps'] = fps
  268. self.config['camera_brightness'] = brightness
  269. self.config['camera_contrast'] = contrast
  270. self.config['camera_saturation'] = saturation
  271. # 应用设置(实际项目中实现)
  272. print("摄像头设置已应用")
  273. @pyqtSlot()
  274. def apply_alert_settings(self):
  275. """应用告警设置"""
  276. # 获取参数
  277. fire_threshold = self.fire_threshold.value()
  278. animal_threshold = self.animal_threshold.value()
  279. landslide_threshold = self.landslide_threshold.value()
  280. pest_threshold = self.pest_threshold.value()
  281. # 获取告警方式
  282. alert_methods = []
  283. if self.ui_alert_check.isChecked():
  284. alert_methods.append('ui')
  285. if self.sound_alert_check.isChecked():
  286. alert_methods.append('sound')
  287. if self.sms_alert_check.isChecked():
  288. alert_methods.append('sms')
  289. if self.email_alert_check.isChecked():
  290. alert_methods.append('email')
  291. # 更新配置
  292. self.config['fire_alert_threshold'] = fire_threshold
  293. self.config['animal_alert_threshold'] = animal_threshold
  294. self.config['landslide_alert_threshold'] = landslide_threshold
  295. self.config['pest_alert_threshold'] = pest_threshold
  296. self.config['alert_methods'] = alert_methods
  297. # 应用设置(实际项目中实现)
  298. print("告警设置已应用")
  299. @pyqtSlot()
  300. def start_monitoring(self):
  301. """启动监测"""
  302. # 切换按钮状态
  303. self.start_btn.setEnabled(False)
  304. self.stop_btn.setEnabled(True)
  305. # 通知主窗口启动监测
  306. parent = self.parent()
  307. while parent:
  308. if hasattr(parent, 'start_monitoring'):
  309. parent.start_monitoring()
  310. break
  311. parent = parent.parent()
  312. @pyqtSlot()
  313. def stop_monitoring(self):
  314. """停止监测"""
  315. # 切换按钮状态
  316. self.start_btn.setEnabled(True)
  317. self.stop_btn.setEnabled(False)
  318. # 通知主窗口停止监测
  319. parent = self.parent()
  320. while parent:
  321. if hasattr(parent, 'stop_monitoring'):
  322. parent.stop_monitoring()
  323. break
  324. parent = parent.parent()