-
Notifications
You must be signed in to change notification settings - Fork 4
/
GUI.cpp
698 lines (611 loc) · 28.2 KB
/
GUI.cpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//
// Created by 绮名桑 on 2024/6/15.
//
#include "GUI.hpp"
GUI::GUI() {
uiMainWindow = new Ui_MainWindow();
this->ui = new UI::MainWindow();
uiMainWindow->setupUi(ui);
yolo = new YOLO();
UI_init();
drawQView = new UI::DrawQView();
pVideoWidget = new UI::VideoWidget(uiMainWindow->groupBox, this);
uiMainWindow->gridLayout_2->addWidget(drawQView, 0, 0, 1, 6);
// 应用QSS样式表
QFile file("styles.qss");
file.open(QIODevice::ReadOnly);
QString styleSheet = file.readAll();
this->ui->setStyleSheet(styleSheet);
file.close();
// "https://bkrc.oss-cn-beijing.aliyuncs.com/videos/b4ee9ab9715217b2d3bf0479765233f8.mp4"
// "rtsp://admin:[email protected]:10554/tcp/av0_0"
connect(uiMainWindow->GetVideo_Button, &QPushButton::clicked, [this]() {
QString urlString = uiMainWindow->Url_Edit->text();
QUrl url = QUrl(urlString);
if (!url.isValid()) {
std::cout << "无效URL:" << url.toString().toStdString() << std::endl;
return;
}
std::cout << "有效URL" << std::endl;
pVideoWidget->setVideoUrl(url);
});
connect(uiMainWindow->SelectModel_Button, &QPushButton::clicked, [this]() {
this->Model_flag = false;
QString modelPath = "Model/" + uiMainWindow->comboBox_2->currentText();
std::thread loadThread([this, modelPath] {
yolo->load(modelPath.toLatin1().data());
std::cout << "加载" << modelPath.toLatin1().data() << "模型已成功" << std::endl;
});
this->Model_flag = true;
loadThread.detach();
});
connect(uiMainWindow->connect_Button, &QPushButton::clicked, [this]() {
QString host = uiMainWindow->Host_Edit->text();
quint16 port = uiMainWindow->Port_Edit->text().toUShort();
// 主车IP地址192.168.40.254 端口号60000
if (tcpUi != nullptr) {
delete tcpUi; // 删除旧的实例
tcpUi = nullptr;
}
tcpUi = new TcpUI(host, port, this);
tcpUi->sendData("Hello, I am the Qt upper computer");
});
connect(uiMainWindow->Main_Button, &QPushButton::clicked, [this]() {
uiMainWindow->stackedWidget->setCurrentIndex(0);
UI::QButtonGroup D = *new UI::QButtonGroup({uiMainWindow->Main_Button, uiMainWindow->Log_Button, uiMainWindow->Tool_Button, uiMainWindow->Folder_Button, uiMainWindow->Setting_Button}, 0);
});
connect(uiMainWindow->Log_Button, &QPushButton::clicked, [this]() {
uiMainWindow->stackedWidget->setCurrentIndex(1);
UI::QButtonGroup D = *new UI::QButtonGroup({uiMainWindow->Main_Button, uiMainWindow->Log_Button, uiMainWindow->Tool_Button, uiMainWindow->Folder_Button, uiMainWindow->Setting_Button}, 1);
});
connect(uiMainWindow->Tool_Button, &QPushButton::clicked, [this]() {
uiMainWindow->stackedWidget->setCurrentIndex(1);
UI::QButtonGroup D = *new UI::QButtonGroup({uiMainWindow->Main_Button, uiMainWindow->Log_Button, uiMainWindow->Tool_Button, uiMainWindow->Folder_Button, uiMainWindow->Setting_Button}, 2);
});
connect(uiMainWindow->Folder_Button, &QPushButton::clicked, [this]() {
uiMainWindow->stackedWidget->setCurrentIndex(1);
UI::QButtonGroup D = *new UI::QButtonGroup({uiMainWindow->Main_Button, uiMainWindow->Log_Button, uiMainWindow->Tool_Button, uiMainWindow->Folder_Button, uiMainWindow->Setting_Button}, 3);
});
connect(uiMainWindow->Setting_Button, &QPushButton::clicked, [this]() {
uiMainWindow->stackedWidget->setCurrentIndex(1);
UI::QButtonGroup D = *new UI::QButtonGroup({uiMainWindow->Main_Button, uiMainWindow->Log_Button, uiMainWindow->Tool_Button, uiMainWindow->Folder_Button, uiMainWindow->Setting_Button}, 4);
});
connect(uiMainWindow->min_Button, &QPushButton::clicked, [this](){
this->ui->showMinimized();
});
connect(uiMainWindow->max_Button, &QPushButton::clicked, [this](){
// 判断当前窗口是否已经最大化
if (this->ui->isMaximized()) {
this->ui->showNormal();
this->uiMainWindow->gridLayout->setContentsMargins(18, 18, 18, 18);
} else {
this->ui->showMaximized();
this->uiMainWindow->gridLayout->setContentsMargins(0, 0, 0, 0);
}
});
connect(uiMainWindow->quit_Button, &QPushButton::clicked, [](){
QApplication::quit();
});
}
void GUI::UI_init() {
// 初始化模型选择列表
Model_List modelList("Model");
std::vector<std::string> matchedPrefixes = modelList.getBinAndParamPairs();
if (!matchedPrefixes.empty()) {
std::cout << "匹配的.bin和.param文件列表:" << std::endl;
for (const auto& prefix : matchedPrefixes) {
std::cout << prefix << std::endl;
uiMainWindow->comboBox_2->addItem(QString::fromStdString(prefix));
}
} else {
std::cout << "未找到匹配的.bin和.param文件列表." << std::endl;
}
// 设置图标
uiMainWindow->Main_Button->setIcon(QPixmap("Image/icon/首页.png"));
uiMainWindow->Log_Button->setIcon(QPixmap("Image/icon/日历.png"));
uiMainWindow->min_Button->setIcon(QPixmap("Image/icon/最小化.png"));
uiMainWindow->max_Button->setIcon(QPixmap("Image/icon/最大化.png"));
uiMainWindow->quit_Button->setIcon(QPixmap("Image/icon/关闭.png"));
uiMainWindow->Regress_Button->setIcon(QPixmap("Image/icon/后退.png"));
uiMainWindow->VideoState_Button->setIcon(QPixmap("Image/icon/暂停.png"));
uiMainWindow->Forward_Button->setIcon(QPixmap("Image/icon/快进.png"));
uiMainWindow->VideoClose_Button->setIcon(QPixmap("Image/icon/终止.png"));
uiMainWindow->verticalLayout_3->setAlignment(Qt::AlignCenter);
QPixmap pixmap("Image/头像.png");
// 创建和 QLabel 一样大小的 pixmap
QPixmap roundedPixmap(uiMainWindow->Head_QLabel->size());
roundedPixmap.fill(Qt::transparent); // 透明背景
QPainter painter(&roundedPixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
// 定义圆角矩形路径
QPainterPath path;
path.addRoundedRect(roundedPixmap.rect(), 40, 40); // 圆角半径为 20
painter.setClipPath(path);
// 绘制图片
painter.drawPixmap(0, 0, uiMainWindow->Head_QLabel->width(), uiMainWindow->Head_QLabel->height(), pixmap);
// 将圆角图片设置到 QLabel
uiMainWindow->Head_QLabel->setPixmap(roundedPixmap);
// 日志输出组件初始化
logWidget = new LogWidget(uiMainWindow->Log_PlainTextEdit);
}
FolderManager::FolderManager(std::filesystem::path folderPath) : path(std::move(folderPath)) {
if (!std::filesystem::exists(path)) {
std::filesystem::create_directory(path);
std::cout << "已创建文件夹: " << path << std::endl;
} else {
std::cout << "文件夹已存在: " << path << std::endl;
}
}
bool FolderManager::exists() const {
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
Model_List::Model_List(std::filesystem::path folderPath) : FolderManager(std::move(folderPath)) {}
std::vector<std::string> Model_List::getBinAndParamPairs() {
// 获取所有 .bin 文件的前缀
std::unordered_set<std::string> binPrefixes = getFilePrefixesWithExtension(".bin");
// 获取所有 .param 文件的前缀
std::unordered_set<std::string> paramPrefixes = getFilePrefixesWithExtension(".param");
std::vector<std::string> matchedPrefixes;
// 遍历 .bin 文件的前缀,检查是否存在对应的 .param 文件
for (const auto& prefix : binPrefixes) {
if (paramPrefixes.find(prefix) != paramPrefixes.end()) {
// 如果找到匹配的前缀,将其添加到结果中
matchedPrefixes.push_back(prefix);
}
}
return matchedPrefixes;
}
std::unordered_set<std::string> Model_List::getFilePrefixesWithExtension(const std::string& extension) {
std::unordered_set<std::string> prefixes;
// 遍历目录中的所有文件
for (const auto& entry : std::filesystem::directory_iterator(path)) {
// 检查文件的扩展名是否匹配
if (entry.is_regular_file() && entry.path().extension() == extension) {
// 将文件的前缀(不包含扩展名)插入到集合中
prefixes.insert(entry.path().stem().string());
}
}
return prefixes;
}
TcpUI::TcpUI(const QString &host, quint16 port, GUI *gui, QObject *parent) : TcpClient(host, port, parent), gui(gui){
// 连接信号和槽
connect(socket, &QTcpSocket::connected, this, &TcpUI::onConnected);
connect(socket, &QTcpSocket::disconnected, this, &TcpUI::onDisconnected);
connect(socket, &QTcpSocket::readyRead, this, &TcpUI::onReadyRead);
connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::errorOccurred), this,
&TcpUI::onErrorOccurred);
try {
// 连接到指定主机和端口
socket->connectToHost(host, port);
// 检查连接状态
if (!socket->waitForConnected(5000)) { // 等待最多 5 秒
std::cout << "连接异常:" << socket->errorString().toStdString() << std::endl;
}
} catch (const std::exception &e) {
std::cout << "异常:" << e.what() << std::endl;
}
std::cout << "连接成功" << std::endl;
}
void TcpUI::onConnected() {
std::cout << "Connected to: " << host.toStdString() << ", port: " << port << std::endl;
gui->logWidget->appendLog(QString("已连接到 %1:%2 设备").arg(host).arg(QString::number(port)));
gui->uiMainWindow->NetworkStatusLabel->setText("已连接");
gui->uiMainWindow->NetworkStatusLabel->setStyleSheet("QLabel { background-color: green; color: white; border-radius: 10px; font-weight: bold; padding: 10px; }");
}
void TcpUI::onDisconnected() {
std::cout << "Disconnected from: " << host.toStdString() << std::endl;
gui->logWidget->appendLog(QString("已断开和 %1:%2 设备的连接").arg(host).arg(QString::number(port)));
gui->uiMainWindow->NetworkStatusLabel->setText("未连接");
gui->uiMainWindow->NetworkStatusLabel->setStyleSheet("QLabel { background-color: red; color: white; border-radius: 10px; font-weight: bold; padding: 10px; }");
}
void TcpUI::onReadyRead() {
QByteArray data = socket->readAll();
std::cout << "Received: " << data.toStdString() << std::endl;
gui->logWidget->appendLog(QString("收到 " + data));
}
void TcpUI::onErrorOccurred(QAbstractSocket::SocketError socketError) {
std::cerr << "Error: " << socket->errorString().toStdString() << std::endl;
gui->logWidget->appendLog(QString("错误:") + QString::fromStdString(socket->errorString().toStdString()));
}
LogWidget::LogWidget(QPlainTextEdit *PlainTextEdit) : qPlainTextEdit(PlainTextEdit){
qPlainTextEdit->setReadOnly(true);
}
void LogWidget::appendLog(const QString &message) {
// 获取当前时间作为日志前缀
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
// 将日志消息添加到 QPlainTextEdit
qPlainTextEdit->appendPlainText(timestamp + " - " + message);
}
namespace UI {
MainWindow::MainWindow() {
// 去掉标题栏 去掉窗口边框
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
// 设置窗口透明化
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setAttribute(Qt::WA_StyledBackground, true);
// 创建阴影效果对象
//QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
//shadowEffect->setBlurRadius(20); // 阴影模糊半径
//shadowEffect->setOffset(10, 10); // 阴影的偏移量
//shadowEffect->setColor(Qt::black); // 阴影颜色
// 应用阴影效果
//this->setGraphicsEffect(shadowEffect);
}
void MainWindow::paintEvent(QPaintEvent *event) {
QWidget::paintEvent(event);
QPainter painter(this);
// 反锯齿
painter.setRenderHint(QPainter::Antialiasing);
// 获取坐标
QRect rect = this->rect();
// 内边距和圆角半径
int offset = 18; // 内边距
int cornerRadius = 10; // 圆角半径
if (this->isMaximized()) {
// 绘制圆角矩形
painter.setBrush(QBrush(QColor(255, 255, 255))); // 圆角矩形的填充颜色
QRect adjustedRect(rect);
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(adjustedRect, 0, 0);
}
else {
// 绘制圆角矩形
painter.setBrush(QBrush(QColor(255, 255, 255))); // 圆角矩形的填充颜色
QRect adjustedRect(rect.left() + offset, rect.top() + offset,
rect.width() - (offset * 2), rect.height() - (offset * 2));
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(adjustedRect, cornerRadius, cornerRadius);
}
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
QWidget::mousePressEvent(event);
this->setFocus();
if (Qt::LeftButton == event->button()) {
// 标记鼠标为按下状态
this->isMousePressed = true;
(new AM::clickEffect(this))->showEffect(emojis[QRandomGenerator::global()->bounded(emojiCount)], event->globalPos());
if (0 == (Qt::WindowMaximized & this->windowState())) {
this->mousePressPosition = event->globalPos();
event->ignore();
}
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
QWidget::mouseMoveEvent(event);
// 如果当前是最大化,则不允许移动
if (this->isMaximized())
return;
// 是否左击
if ((event->buttons() & Qt::LeftButton) && this->isMousePressed) {
// 当前鼠标全局位置
QPoint currentMousePos = event->globalPos();
// 计算鼠标移动的相对位移
QPoint delta = currentMousePos - this->mousePressPosition;
// 设定移动窗口的最小阈值,避免轻微抖动
if (delta.manhattanLength() > 20) {
// 移动窗口到新的位置
move(this->pos() + delta);
// 更新鼠标按下的位置,以便下次计算移动
this->mousePressPosition = currentMousePos;
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
QWidget::mouseReleaseEvent(event);
// 恢复鼠标指针状态
QApplication::restoreOverrideCursor();
event->ignore();
this->isMousePressed = false;
}
DrawQView::DrawQView() {
// 初始化 QGraphicsView
this->setScene(&scene);
// 设置 QGraphicsView 的背景透明
this->setStyleSheet("background: transparent");
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->viewport()->setStyleSheet("background: transparent");
// 开启抗锯齿
this->setRenderHint(QPainter::Antialiasing);
// 关闭平滑像素变换
this->setRenderHint(QPainter::SmoothPixmapTransform, false);
// 禁用滚动条
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// 禁用场景的滚动
setAlignment(Qt::AlignLeft | Qt::AlignTop);
// 禁用视图变换
this->setTransform(QTransform());
}
void DrawQView::wheelEvent(QWheelEvent *event) {
QGraphicsView::wheelEvent(event);
// 禁用滚轮事件
event->ignore();
}
void DrawQView::resizeEvent(QResizeEvent *event) {
QGraphicsView::resizeEvent(event);
this->setDrawQViemRect();
emit viewChanged();
}
void DrawQView::setDrawQViemRect() {
scene.setSceneRect(0, 0, this->width(), this->height());
}
void DrawQView::onUpdateAABB(const std::vector<STrack>& output_stracks) {
draw_AABB(output_stracks);
}
void DrawQView::draw_AABB(const std::vector<STrack>& output_stracks) {
// 绘制之前清空场景中的所有项
scene.clear();
if (scaleX == 0 & scaleY == 0) {
draw_clear();
return;
}
// 遍历 results 列表,绘制每个 AABB 和标签
for (const auto& strack : output_stracks) {
// 修正尺寸
float new_x = strack.byteTrackData.object.rect.x * scaleX;
float new_y = strack.byteTrackData.object.rect.y * scaleY;
float new_w = strack.byteTrackData.object.rect.width * scaleX;
float new_h = strack.byteTrackData.object.rect.height * scaleY;
// 绘制矩形框
QGraphicsRectItem* rectItem = new QGraphicsRectItem(new_x, new_y, new_w, new_h);
// 设置矩形框的颜色,可以根据需要更改
QPen pen(Qt::red);
// 设置线条宽度
pen.setWidth(2);
rectItem->setPen(pen);
scene.addItem(rectItem);
QGraphicsTextItem* textItem = new QGraphicsTextItem(strack.byteTrackData.object.class_name + " ID:" + QString::number(strack.track_id) + " " + strack.byteTrackData.FirstTime);
textItem->setFont(QFont("微软雅黑", 14));
textItem->setDefaultTextColor(Qt::yellow);
textItem->setPos(new_x, new_y-35);
scene.addItem(textItem);
}
// 更新视图以显示新绘制的内容
this->update();
}
void DrawQView::onUpdateSeg(const QImage& image, const std::vector<Object>& objects) {
draw_Seg(image, objects);
}
void DrawQView::draw_Seg(const QImage& image, const std::vector<Object>& objects) {
// 绘制之前清空场景中的所有项
scene.clear();
if (scaleX == 0 & scaleY == 0) {
draw_clear();
return;
}
cv::Mat rgb = user_CV::draw_seg(image, objects);
cv::resize(rgb, rgb, cv::Size(this->width(), this->height()));
// 将 QImage 转换为 QPixmap
QPixmap pixmap = QPixmap::fromImage(user_CV::cvMatToQImage(rgb, true, false));
// 将 pixmap 添加到场景
scene.addPixmap(pixmap);
this->update();
}
void DrawQView::draw_clear() {
// 清空绘制内容
scene.clear();
this->update();
}
YOLOThread::YOLOThread(GUI *gui, QVideoSink *videoSink, QObject *parent): QThread(parent), gui(gui), videoSink(videoSink){
connect(gui->drawQView, &DrawQView::viewChanged, this, &YOLOThread::onViewChanged);
}
void YOLOThread::run() {
if (modelType == 0) AABB();
else if (modelType == 1) Seg();
}
void YOLOThread::AABB() {
// 连接信号和槽
connect(this, &YOLOThread::updateAABB, gui->drawQView, &DrawQView::onUpdateAABB);
auto lastTime = std::chrono::high_resolution_clock::now();
int frameCount = 0;
double fps = 0.0;
this->onViewChanged();
BYTETracker tracker(60, 3000);
// 自动加锁
QMutexLocker locker(&mutex);
while (true) {
// 等待条件变量
while (paused) waitCondition.wait(&mutex);
if (!state) break;
frameCount++;
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastTime).count();
// 每处理一帧都记录当前时间
if (elapsedTime > 1000) {
fps = frameCount * 1000.0 / elapsedTime;
frameCount = 0;
lastTime = currentTime;
std::cout << "当前平均 FPS: " << fps << std::endl;
}
std::vector<Object> objects;
QImage image = videoSink->videoFrame().toImage();
if (image.isNull()) break;
gui->yolo->detect(image, objects);
vector<STrack> output_stracks = tracker.update(objects);
gui->yolo->result(objects);
this->updateByteTrack(tracker, output_stracks);
// 发射信号
emit updateAABB(output_stracks);
}
gui->drawQView->scaleX = 0;
gui->drawQView->scaleY = 0;
disconnect(this, &YOLOThread::updateAABB, gui->drawQView, &DrawQView::onUpdateAABB);
std::cout << "已释放YOLOThread线程run函数" << std::endl;
}
void YOLOThread::Seg() {
connect(this, &YOLOThread::updateSeg, gui->drawQView, &DrawQView::onUpdateSeg);
auto lastTime = std::chrono::high_resolution_clock::now();
int frameCount = 0;
double fps = 0.0;
this->onViewChanged();
QMutexLocker locker(&mutex);
while (true) {
// 等待条件变量
while (paused) waitCondition.wait(&mutex);
if (!state) break;
frameCount++;
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastTime).count();
// 每处理一帧都记录当前时间
if (elapsedTime > 1000) {
fps = frameCount * 1000.0 / elapsedTime;
frameCount = 0;
lastTime = currentTime;
std::cout << "当前平均 FPS: " << fps << std::endl;
}
std::vector<Object> objects;
QImage image = videoSink->videoFrame().toImage();
gui->yolo->detect_seg(image, objects);
emit updateSeg(image, objects);
}
gui->drawQView->scaleX = 0;
gui->drawQView->scaleY = 0;
disconnect(this, &YOLOThread::updateSeg, gui->drawQView, &DrawQView::onUpdateSeg);
std::cout << "已释放YOLOThread线程run函数" << std::endl;
}
void YOLOThread::updateByteTrack(BYTETracker &tracker, std::vector<STrack> &output_stracks) {
for (int i = 0; i < output_stracks.size(); i++)
{
std::vector<float> tlwh = output_stracks[i].tlwh;
bool vertical = tlwh[2] / tlwh[3] > 1.6;
if (tlwh[2] * tlwh[3] > 20 && !vertical)
{
}
}
}
void YOLOThread::pause() {
paused = true;
}
void YOLOThread::resume() {
paused = false;
waitCondition.wakeOne(); // 唤醒等待的线程
}
void YOLOThread::onViewChanged() {
// 调整缩放因子
std::cout << gui->drawQView->width() << "x" << gui->drawQView->height() << std::endl;
float scaleX = static_cast<float>(gui->drawQView->width()) / static_cast<float>(videoSink->videoFrame().toImage().width());
float scaleY = static_cast<float>(gui->drawQView->height()) / static_cast<float>(videoSink->videoFrame().toImage().height());
gui->drawQView->scaleX = scaleX;
gui->drawQView->scaleY = scaleY;
gui->drawQView->setDrawQViemRect();
}
VideoWidget::VideoWidget(QWidget *parent, GUI *gui) : QWidget(parent), m_frame(QImage()), gui(gui) {
this->player = new QMediaPlayer();
this->videoSink = new QVideoSink();
this->yoloThread = new YOLOThread(gui, videoSink, this);
connect(videoSink, &QVideoSink::videoFrameChanged, this, &UI::VideoWidget::processFrame);
this->player->setVideoOutput(videoSink);
gui->uiMainWindow->gridLayout_2->addWidget(this, 0, 0, 1, 6);
connect(gui->uiMainWindow->detect_Button, &QPushButton::clicked, [this]() {
this->yoloThread->state = !this->yoloThread->state;
if (this->gui->Model_flag && this->yoloThread->state) {
this->gui->uiMainWindow->detect_Button->setText("关闭推理");
yoloThread->start();
}
else {
this->gui->uiMainWindow->detect_Button->setText("开启推理");
this->yoloThread->state = false;
}
});
connect(gui->uiMainWindow->Regress_Button, &QPushButton::clicked, [this]() {
player->setPosition(player->position() - 5000);
});
connect(gui->uiMainWindow->VideoState_Button, &QPushButton::clicked, [this]() {
if (this->player->isPlaying()) {
this->pause();
yoloThread->pause();
}
else {
yoloThread->resume();
this->play();
}
});
connect(gui->uiMainWindow->Forward_Button, &QPushButton::clicked, [this]() {
player->setPosition(player->position() + 5000);
});
connect(gui->uiMainWindow->VideoClose_Button, &QPushButton::clicked, [this]() {
this->clear();
});
connect(player, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus status) {
if (status == QMediaPlayer::EndOfMedia) {
yoloThread->state = false;
yoloThread->requestInterruption();
this->gui->uiMainWindow->detect_Button->setText("开启推理");
}
});
};
void VideoWidget::setVideoFrame(const QImage &frame) {
m_frame = frame;
update();
}
void VideoWidget::setVideoUrl(const QUrl qUrl) {
this->clear();
player->setSource(qUrl);
connect(player, &QMediaPlayer::durationChanged, this, [this](qint64 duration) {
QTime time = QTime(0, 0).addMSecs(duration);
this->VideoTimer = time.toString("HH:mm:ss");
gui->uiMainWindow->VideoTimer_Label->setText("00:00:00 / " + this->VideoTimer);
gui->uiMainWindow->VideoTimer_Slider->setRange(0, duration);
});
connect(player, &QMediaPlayer::positionChanged, this, [this](qint64 position) {
// 将当前播放位置转换为 HH:mm:ss 格式
QTime time(0, 0, 0);
time = time.addMSecs(position);
QString timeString = time.toString("HH:mm:ss");
// 更新标签,显示当前播放时间和总时长
gui->uiMainWindow->VideoTimer_Label->setText(timeString + " / " + this->VideoTimer);
gui->uiMainWindow->VideoTimer_Slider->setValue(position);
});
connect(gui->uiMainWindow->VideoTimer_Slider, &QSlider::sliderPressed, this, [this]() {
player->pause();
yoloThread->pause();
});
connect(gui->uiMainWindow->VideoTimer_Slider, &QSlider::sliderReleased, this, [this]() {
// 获取当前值
int newValue = gui->uiMainWindow->VideoTimer_Slider->value();
// 禁用 QSlider 的所有信号
gui->uiMainWindow->VideoTimer_Slider->blockSignals(true);
qDebug() << "滑块释放,新值为:" << newValue;
// 设置播放位置
player->setPosition(newValue);
// 恢复 QSlider 的信号
gui->uiMainWindow->VideoTimer_Slider->blockSignals(false);
yoloThread->resume();
player->play();
});
}
void VideoWidget::processFrame(const QVideoFrame &frame) {
m_frame = frame.toImage();
update();
}
void VideoWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
if (!m_frame.isNull()) {
painter.drawImage(rect(), m_frame);
}
}
void VideoWidget::play() {
player->play();
}
void VideoWidget::pause() {
player->pause();
}
void VideoWidget::clear() {
qDebug() << "播放终止,执行任务";
yoloThread->state = false;
qDebug() << "标志位完成设置";
yoloThread->requestInterruption();
qDebug() << "线程强制中断完毕";
player->stop();
this->gui->uiMainWindow->detect_Button->setText("开启推理");
this->VideoTimer.clear();
gui->uiMainWindow->VideoTimer_Label->setText("00:00:00 / 00:00:00");
// 设置为QUrl() 将导致播放器丢弃与当前媒体源有关的所有信息并停止与该媒体相关的所有 I/O 操作。
player->setSource(QUrl());
disconnect(player, &QMediaPlayer::durationChanged, nullptr, nullptr);
disconnect(player, &QMediaPlayer::positionChanged, nullptr, nullptr);
disconnect(gui->uiMainWindow->VideoTimer_Slider, &QSlider::sliderPressed, nullptr, nullptr);
disconnect(gui->uiMainWindow->VideoTimer_Slider, &QSlider::sliderReleased, nullptr, nullptr);
}
}