开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

open-source/Smart_Construction

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (3)
标签 (8)
master
merge_5_0
dev
V3.1
V3.0
V2.2
V2.1
V2.0.1
V2.0
V1.1
V1.0
master
分支 (3)
标签 (8)
master
merge_5_0
dev
V3.1
V3.0
V2.2
V2.1
V2.0.1
V2.0
V1.1
V1.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (3)
标签 (8)
master
merge_5_0
dev
V3.1
V3.0
V2.2
V2.1
V2.0.1
V2.0
V1.1
V1.0
Smart_Construction
/
visual_interface.py
Smart_Construction
/
visual_interface.py
visual_interface.py 18.14 KB
一键复制 编辑 原始数据 按行查看 历史
PerterH 提交于 2021年03月27日 16:20 +08:00 . 完善注释
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 : peterhuang0323@outlook.com
# @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_dangerous1円.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_())
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python-open-source/Smart_Construction.git
git@gitee.com:python-open-source/Smart_Construction.git
python-open-source
Smart_Construction
Smart_Construction
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /