-
Notifications
You must be signed in to change notification settings - Fork 471
/
visual_interface.py
479 lines (398 loc) · 18.1 KB
/
visual_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# -*- coding: utf-8 -*-
# @Time : 2021/3/6 15:36
# @Author : PeterH
# @Email : [email protected]
# @File : visual_interface.py
# @Software: PyCharm
# @Brief :
# 解决 exe 打包 Can't get source for 的问题 start ======
# https://github.com/pytorch/vision/issues/1899#issuecomment-598200938
import torch.jit
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
torch.jit.script_method = script_method
torch.jit.script = script
# 解决 exe 打包 Can't get source for 的问题 end ======
import os
import time
import sys
from pathlib import Path
from GPUtil import GPUtil
from PyQt5.QtCore import QThread, pyqtSignal, QUrl, pyqtSlot, QTimer, QDateTime, Qt
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtGui import QColor, QBrush, QIcon, QPixmap
from PyQt5.QtChart import QDateTimeAxis, QValueAxis, QSplineSeries, QChart
import torch
from UI.main_window import Ui_MainWindow
from detect_visual import YOLOPredict
from utils.datasets import img_formats
CODE_VER = "V2.0"
PREDICT_SHOW_TAB_INDEX = 0
REAL_TIME_PREDICT_TAB_INDEX = 1
def get_gpu_info():
"""
获取 GPU 信息
:return:
"""
gpu_list = []
# GPUtil.showUtilization()
# 获取多个GPU的信息,存在列表里
for gpu in GPUtil.getGPUs():
# print('gpu.id:', gpu.id)
# print('GPU总量:', gpu.memoryTotal)
# print('GPU使用量:', gpu.memoryUsed)
# print('gpu使用占比:', gpu.memoryUtil * 100) # 内存使用率
# print('gpu load:', gpu.load * 100) # 使用率
# 按GPU逐个添加信息
gpu_list.append({"gpu_id": gpu.id,
"gpu_memoryTotal": gpu.memoryTotal,
"gpu_memoryUsed": gpu.memoryUsed,
"gpu_memoryUtil": gpu.memoryUtil * 100,
"gpu_load": gpu.load * 100})
return gpu_list
class PredictDataHandlerThread(QThread):
"""
打印信息的线程
"""
predict_message_trigger = pyqtSignal(str)
def __init__(self, predict_model):
super(PredictDataHandlerThread, self).__init__()
self.running = False
self.predict_model = predict_model
def __del__(self):
self.running = False
# self.destroyed()
def run(self):
self.running = True
over_time = 0
while self.running:
if self.predict_model.predict_info != "":
self.predict_message_trigger.emit(self.predict_model.predict_info)
self.predict_model.predict_info = ""
over_time = 0
time.sleep(0.01)
over_time += 1
if over_time > 100000:
self.running = False
class PredictHandlerThread(QThread):
"""
进行模型推理的线程
"""
def __init__(self, input_player, output_player, out_file_path, weight_path,
predict_info_plain_text_edit, predict_progress_bar, fps_label,
button_dict, input_tab, output_tab, input_image_label, output_image_label,
real_time_show_predict_flag):
super(PredictHandlerThread, self).__init__()
self.running = False
'''加载模型'''
self.predict_model = YOLOPredict(weight_path, out_file_path)
self.output_predict_file = ""
self.parameter_source = ''
# 传入的QT插件
self.input_player = input_player
self.output_player = output_player
self.predict_info_plainTextEdit = predict_info_plain_text_edit
self.predict_progressBar = predict_progress_bar
self.fps_label = fps_label
self.button_dict = button_dict
self.input_tab = input_tab
self.output_tab = output_tab
self.input_image_label = input_image_label
self.output_image_label = output_image_label
# 是否实时显示推理图片
self.real_time_show_predict_flag = real_time_show_predict_flag
# 创建显示进程
self.predict_data_handler_thread = PredictDataHandlerThread(self.predict_model)
self.predict_data_handler_thread.predict_message_trigger.connect(self.add_messages)
def __del__(self):
self.running = False
# self.destroyed()
def run(self):
self.predict_data_handler_thread.start()
self.predict_progressBar.setValue(0) # 进度条清零
for item, button in self.button_dict.items():
button.setEnabled(False)
image_flag = os.path.splitext(self.parameter_source)[-1].lower() in img_formats
qt_input = None
qt_output = None
if not image_flag and self.real_time_show_predict_flag:
qt_input = self.input_image_label
qt_output = self.output_image_label
# tab 设置显示第二栏
self.input_tab.setCurrentIndex(REAL_TIME_PREDICT_TAB_INDEX)
self.output_tab.setCurrentIndex(REAL_TIME_PREDICT_TAB_INDEX)
with torch.no_grad():
self.output_predict_file = self.predict_model.detect(self.parameter_source,
qt_input=qt_input,
qt_output=qt_output)
if self.output_predict_file != "":
# 将 str 路径转为 QUrl 并显示
self.input_player.setMedia(QMediaContent(QUrl.fromLocalFile(self.parameter_source))) # 选取视频文件
self.input_player.pause() # 显示媒体
self.output_player.setMedia(QMediaContent(QUrl.fromLocalFile(self.output_predict_file))) # 选取视频文件
self.output_player.pause() # 显示媒体
# tab 设置显示第一栏
self.input_tab.setCurrentIndex(PREDICT_SHOW_TAB_INDEX)
self.output_tab.setCurrentIndex(PREDICT_SHOW_TAB_INDEX)
# video_flag = os.path.splitext(self.parameter_source)[-1].lower() in vid_formats
for item, button in self.button_dict.items():
if image_flag and item in ['play_pushButton', 'pause_pushButton']:
continue
button.setEnabled(True)
# self.predict_data_handler_thread.running = False
@pyqtSlot(str)
def add_messages(self, message):
if message != "":
self.predict_info_plainTextEdit.appendPlainText(message)
if ":" not in message:
# 跳过无用字符
return
split_message = message.split(" ")
# 设置进度条
if "video" in message:
percent = split_message[2][1:-1].split("/") # 提取图片的序号
value = int((int(percent[0]) / int(percent[1])) * 100)
value = value if (int(percent[1]) - int(percent[0])) > 2 else 100
self.predict_progressBar.setValue(value)
else:
self.predict_progressBar.setValue(100)
# 设置 FPS
second_count = 1 / float(split_message[-1][1:-2])
self.fps_label.setText(f"--> {second_count:.1f} FPS")
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, weight_path, out_file_path, real_time_show_predict_flag: bool, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.setWindowTitle("Intelligent Monitoring System of Construction Site Software " + CODE_VER)
self.showMaximized()
'''按键绑定'''
# 输入媒体
self.import_media_pushButton.clicked.connect(self.import_media) # 导入
self.start_predict_pushButton.clicked.connect(self.predict_button_click) # 开始推理
# 输出媒体
self.open_predict_file_pushButton.clicked.connect(self.open_file_in_browser) # 文件中显示推理视频
# 下方
self.play_pushButton.clicked.connect(self.play_pause_button_click) # 播放
self.pause_pushButton.clicked.connect(self.play_pause_button_click) # 暂停
self.button_dict = dict()
self.button_dict.update({"import_media_pushButton": self.import_media_pushButton,
"start_predict_pushButton": self.start_predict_pushButton,
"open_predict_file_pushButton": self.open_predict_file_pushButton,
"play_pushButton": self.play_pushButton,
"pause_pushButton": self.pause_pushButton,
"real_time_checkBox": self.real_time_checkBox
})
'''媒体流绑定输出'''
self.input_player = QMediaPlayer() # 媒体输入的widget
self.input_player.setVideoOutput(self.input_video_widget)
self.input_player.positionChanged.connect(self.change_slide_bar) # 播放进度条
self.output_player = QMediaPlayer() # 媒体输出的widget
self.output_player.setVideoOutput(self.output_video_widget)
'''初始化GPU chart'''
self.series = QSplineSeries()
self.chart_init()
'''初始化GPU定时查询定时器'''
# 使用QTimer,0.5秒触发一次,更新数据
self.timer = QTimer(self)
self.timer.timeout.connect(self.draw_gpu_info_chart)
self.timer.start(1000)
# 播放时长, 以 input 的时长为准
self.video_length = 0
self.out_file_path = out_file_path
# 推理使用另外一线程
self.predict_handler_thread = PredictHandlerThread(self.input_player,
self.output_player,
self.out_file_path,
weight_path,
self.predict_info_plainTextEdit,
self.predict_progressBar,
self.fps_label,
self.button_dict,
self.input_media_tabWidget,
self.output_media_tabWidget,
self.input_real_time_label,
self.output_real_time_label,
real_time_show_predict_flag
)
self.weight_label.setText(f" Using weight : ****** {Path(weight_path[0]).name} ******")
# 界面美化
self.gen_better_gui()
self.media_source = "" # 推理媒体的路径
self.predict_progressBar.setValue(0) # 进度条归零
'''check box 绑定'''
self.real_time_checkBox.stateChanged.connect(self.real_time_checkbox_state_changed)
self.real_time_checkBox.setChecked(real_time_show_predict_flag)
self.real_time_check_state = self.real_time_checkBox.isChecked()
def gen_better_gui(self):
"""
美化界面
:return:
"""
# Play 按钮
play_icon = QIcon()
play_icon.addPixmap(QPixmap("./UI/icon/play.png"), QIcon.Normal, QIcon.Off)
self.play_pushButton.setIcon(play_icon)
# Pause 按钮
play_icon = QIcon()
play_icon.addPixmap(QPixmap("./UI/icon/pause.png"), QIcon.Normal, QIcon.Off)
self.pause_pushButton.setIcon(play_icon)
# 隐藏 tab 标题栏
self.input_media_tabWidget.tabBar().hide()
self.output_media_tabWidget.tabBar().hide()
# tab 设置显示第一栏
self.input_media_tabWidget.setCurrentIndex(PREDICT_SHOW_TAB_INDEX)
self.output_media_tabWidget.setCurrentIndex(PREDICT_SHOW_TAB_INDEX)
# 设置显示图片的 label 为黑色背景
self.input_real_time_label.setStyleSheet("QLabel{background:black}")
self.output_real_time_label.setStyleSheet("QLabel{background:black}")
def real_time_checkbox_state_changed(self):
"""
切换是否实时显示推理图片
:return:
"""
self.real_time_check_state = self.real_time_checkBox.isChecked()
self.predict_handler_thread.real_time_show_predict_flag = self.real_time_check_state
def chart_init(self):
"""
初始化 GPU 折线图
:return:
"""
# self.gpu_info_chart._chart = QChart(title="折线图堆叠") # 创建折线视图
self.gpu_info_chart._chart = QChart() # 创建折线视图
# chart._chart.setBackgroundVisible(visible=False) # 背景色透明
self.gpu_info_chart._chart.setBackgroundBrush(QBrush(QColor("#FFFFFF"))) # 改变图背景色
# 设置曲线名称
self.series.setName("GPU Utilization")
# 把曲线添加到QChart的实例中
self.gpu_info_chart._chart.addSeries(self.series)
# 声明并初始化X轴,Y轴
self.dtaxisX = QDateTimeAxis()
self.vlaxisY = QValueAxis()
# 设置坐标轴显示范围
self.dtaxisX.setMin(QDateTime.currentDateTime().addSecs(-300 * 1))
self.dtaxisX.setMax(QDateTime.currentDateTime().addSecs(0))
self.vlaxisY.setMin(0)
self.vlaxisY.setMax(100)
# 设置X轴时间样式
self.dtaxisX.setFormat("hh:mm:ss")
# 设置坐标轴上的格点
self.dtaxisX.setTickCount(5)
self.vlaxisY.setTickCount(10)
# 设置坐标轴名称
self.dtaxisX.setTitleText("Time")
self.vlaxisY.setTitleText("Percent")
# 设置网格不显示
self.vlaxisY.setGridLineVisible(False)
# 把坐标轴添加到chart中
self.gpu_info_chart._chart.addAxis(self.dtaxisX, Qt.AlignBottom)
self.gpu_info_chart._chart.addAxis(self.vlaxisY, Qt.AlignLeft)
# 把曲线关联到坐标轴
self.series.attachAxis(self.dtaxisX)
self.series.attachAxis(self.vlaxisY)
# 生成 折线图
self.gpu_info_chart.setChart(self.gpu_info_chart._chart)
def draw_gpu_info_chart(self):
"""
绘制 GPU 折线图
:return:
"""
# 获取当前时间
time_current = QDateTime.currentDateTime()
# 更新X轴坐标
self.dtaxisX.setMin(QDateTime.currentDateTime().addSecs(-300 * 1))
self.dtaxisX.setMax(QDateTime.currentDateTime().addSecs(0))
# 当曲线上的点超出X轴的范围时,移除最早的点
remove_count = 600
if self.series.count() > remove_count:
self.series.removePoints(0, self.series.count() - remove_count)
# 对 y 赋值
# yint = random.randint(0, 100)
gpu_info = get_gpu_info()
yint = gpu_info[0].get("gpu_load")
# 添加数据到曲线末端
self.series.append(time_current.toMSecsSinceEpoch(), yint)
def import_media(self):
"""
导入媒体文件
:return:
"""
self.media_source = QFileDialog.getOpenFileUrl()[0]
self.input_player.setMedia(QMediaContent(self.media_source)) # 选取视频文件
# 设置 output 为一张图片,防止资源被占用
path_current = str(Path.cwd().joinpath("area_dangerous\1.jpg"))
self.output_player.setMedia(QMediaContent(QUrl.fromLocalFile(path_current)))
# 将 QUrl 路径转为 本地路径str
self.predict_handler_thread.parameter_source = self.media_source.toLocalFile()
self.input_player.pause() # 显示媒体
image_flag = os.path.splitext(self.predict_handler_thread.parameter_source)[-1].lower() in img_formats
for item, button in self.button_dict.items():
if image_flag and item in ['play_pushButton', 'pause_pushButton']:
button.setEnabled(False)
else:
button.setEnabled(True)
# self.output_player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # 选取视频文件
def predict_button_click(self):
"""
推理按钮
:return:
"""
# 启动线程去调用
self.predict_handler_thread.start()
def change_slide_bar(self, position):
"""
进度条移动
:param position:
:return:
"""
self.video_length = self.input_player.duration() + 0.1
self.video_horizontalSlider.setValue(round((position / self.video_length) * 100))
self.video_percent_label.setText(str(round((position / self.video_length) * 100, 2)) + '%')
@pyqtSlot()
def play_pause_button_click(self):
"""
播放、暂停按钮回调事件
:return:
"""
name = self.sender().objectName()
if self.media_source == "":
return
if name == "play_pushButton":
print("play")
self.input_player.play()
self.output_player.play()
elif name == "pause_pushButton":
self.input_player.pause()
self.output_player.pause()
@pyqtSlot()
def open_file_in_browser(self):
os.system(f"start explorer {self.out_file_path}")
@pyqtSlot()
def closeEvent(self, *args, **kwargs):
"""
重写关闭事件
:param args:
:param kwargs:
:return:
"""
print("Close")
if __name__ == '__main__':
app = QApplication(sys.argv)
weight_root = Path.cwd().joinpath("weights")
if not weight_root.exists():
raise FileNotFoundError("weights not found !!!")
weight_file = [item for item in weight_root.iterdir() if item.suffix == ".pt"]
weight_root = [str(weight_file[0])] # 权重文件位置
out_file_root = Path.cwd().joinpath(r'inference/output')
out_file_root.parent.mkdir(exist_ok=True)
out_file_root.mkdir(exist_ok=True)
real_time_show_predict = True # 是否实时显示推理图片,有可能导致卡顿,软件卡死
main_window = MainWindow(weight_root, out_file_root, real_time_show_predict)
# 设置窗口图标
icon = QIcon()
icon.addPixmap(QPixmap("./UI/icon/icon.ico"), QIcon.Normal, QIcon.Off)
main_window.setWindowIcon(icon)
main_window.show()
sys.exit(app.exec_())