-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.cpp
More file actions
764 lines (636 loc) · 25.3 KB
/
Copy pathgui.cpp
File metadata and controls
764 lines (636 loc) · 25.3 KB
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// 解决Qt和TBB的emit冲突
#define QT_NO_EMIT
#include "gui.hpp"
#include "NLKiller.hpp"
#include "ytensor.hpp"
#include <QApplication>
#include <QDir>
#include <QStandardPaths>
#include <QTextStream>
#include <QDateTime>
#include <algorithm>
#include <cstring>
#include <chrono>
#include <thread>
#include <queue>
#include <condition_variable>
#include <mutex>
// 将QImage转换为YTensor<u_char, 3>
YTensor<u_char, 3> qImageToYTensor(const QImage& qimg) {
// 确保图像是RGB格式
QImage rgbImage = qimg.convertToFormat(QImage::Format_RGB888);
int height = rgbImage.height();
int width = rgbImage.width();
int channels = 3;
YTensor<u_char, 3> tensor(height, width, channels);
// 复制像素数据
const uchar* srcData = rgbImage.constBits();
int bytesPerLine = rgbImage.bytesPerLine();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const uchar* pixel = srcData + y * bytesPerLine + x * channels;
// QImage的RGB顺序和我们需要的一致
tensor.at(y, x, 0) = pixel[0]; // R
tensor.at(y, x, 1) = pixel[1]; // G
tensor.at(y, x, 2) = pixel[2]; // B
}
}
return tensor;
}
// InferenceWorker实现
InferenceWorker::InferenceWorker(NLKiller* killer, std::vector<ImageInfo>* images)
: killer(killer), images(images), shouldStop(false) {
}
InferenceWorker::~InferenceWorker() {
shouldStop = true;
// 等待所有解码线程结束
for (auto& thread : decoderThreads) {
if (thread.joinable()) {
thread.join();
}
}
decoderThreads.clear();
}
YTensor<unsigned char, 3> InferenceWorker::loadImageToTensor(const QString& path) {
// 禁用Qt的ICC警告
qputenv("QT_LOGGING_RULES", "qt.gui.icc.debug=false");
QImage qimg(path);
if (qimg.isNull()) {
return YTensor<unsigned char, 3>(1, 1, 1);
}
return qImageToYTensor(qimg);
}
void InferenceWorker::decoderWorker(int startIndex, int endIndex) {
for (int i = startIndex; i < endIndex && i < static_cast<int>(images->size()) && !shouldStop; ++i) {
// 加载图片
auto tensor = std::make_shared<YTensor<unsigned char, 3>>(loadImageToTensor((*images)[i].filePath));
if (tensor && tensor->data != nullptr) {
// 使用lock_guard锁住队列,添加解码结果
while(pause){
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::lock_guard<std::mutex> lock(queueMutex);
decodedQueue.emplace(static_cast<int>(i), tensor);
pause = static_cast<int>(decodeQueue.size()) >= bufferSize;
}
}
}
void InferenceWorker::inferenceLoop() {
auto startTime = std::chrono::high_resolution_clock::now();
int processedCount = 0;
int totalCount = static_cast<int>(images->size());
while (processedCount < totalCount && !shouldStop) {
std::vector<DecodedResult> batch;
// 从队列中取出所有已解码的图片
{
std::lock_guard<std::mutex> lock(queueMutex);
while (!decodedQueue.empty()) {
batch.push_back(decodedQueue.front());
decodedQueue.pop();
}
pause = false;
}
// 对每个解码好的图片进行推理
// for (const auto& decoded : batch) {
// if (shouldStop) break;
// float confidence = killer->infer(*decoded.tensor);
// processedCount++;
// // 发出单个图片处理完成的信号
// Q_EMIT imageProcessed(decoded.index, confidence);
// // 计算并发出进度更新信号
// auto currentTime = std::chrono::high_resolution_clock::now();
// auto elapsed = std::chrono::duration<float>(currentTime - startTime).count();
// float speed = elapsed > 0 ? processedCount / elapsed : 0.0f;
// Q_EMIT progressUpdated(processedCount, totalCount, speed);
// }
if(!shouldStop){
std::vector<YTensor<u_char, 3>> tensors;
tensors.reserve(batch.size());
for (int a = 0; a < batch.size(); a++) {
tensors.emplace_back((*batch[a].tensor).move());
}
auto confs = killer->infer(tensors, NLKiller::InferenceMode::ASYNC_MULTI);
processedCount += confs.size();
for (int a = 0; a < confs.size(); a++) {
// 发出单个图片处理完成的信号
Q_EMIT imageProcessed(batch[a].index, confs[a]);
}
// 计算并发出进度更新信号
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration<float>(currentTime - startTime).count();
float speed = elapsed > 0 ? processedCount / elapsed : 0.0f;
Q_EMIT progressUpdated(processedCount, totalCount, speed);
}
// 如果没有可处理的图片,短暂等待
if (batch.empty()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
Q_EMIT allImagesProcessed();
}
void InferenceWorker::processImages() {
if (!images || images->empty()) {
Q_EMIT allImagesProcessed();
return;
}
shouldStop = false;
// 清空解码队列
{
std::lock_guard<std::mutex> lock(queueMutex);
while (!decodedQueue.empty()) {
decodedQueue.pop();
}
}
// 计算解码线程数量(逻辑核心数-2,最少1个)
int numDecoderThreads = std::max(1, static_cast<int>(std::thread::hardware_concurrency()) / 2 - 1);
int imagesPerThread = (images->size() + numDecoderThreads - 1) / numDecoderThreads;
// 启动解码线程,每个线程处理一段图片
decoderThreads.clear();
for (int i = 0; i < numDecoderThreads; ++i) {
int startIndex = i * imagesPerThread;
int endIndex = std::min(startIndex + imagesPerThread, (int)images->size());
if (startIndex < static_cast<int>(images->size())) {
decoderThreads.emplace_back(&InferenceWorker::decoderWorker, this, startIndex, endIndex);
}
}
// 在当前线程中运行推理循环
inferenceLoop();
}
void InferenceWorker::processSingleImage(int index) {
if (index < 0 || index >= static_cast<int>(images->size())) {
return;
}
auto tensor = loadImageToTensor((*images)[index].filePath);
if (tensor.data == nullptr) {
return;
}
float result = killer->infer(tensor);
Q_EMIT singleImageProcessed(index, result);
}
// NLKillerGUI实现
NLKillerGUI::NLKillerGUI(QWidget *parent)
: QMainWindow(parent), currentImageIndex(0), confidenceThreshold(0.5f),
workerThread(nullptr), worker(nullptr) {
// 初始化支持的图像格式
supportedFormats << "jpg" << "jpeg" << "png" << "bmp" << "tiff" << "tga";
// 初始化模型路径
modelPaths << "../models/helicopter_simplified.onnx"
<< "../models/NLK-s_simplified.onnx"
<< "../models/yvgg_simplified.onnx";
// 初始化AI推理器
killer = std::make_unique<NLKiller>(false);
killer->setNumThreads(4);
// 设置UI
setupUI();
// 默认加载第一个模型
loadModel(modelPaths[0]);
// 设置窗口属性
setWindowTitle("NLKiller GUI");
setMinimumSize(1200, 800);
resize(1400, 900);
// 设置焦点策略以接收键盘事件
setFocusPolicy(Qt::StrongFocus);
}
NLKillerGUI::~NLKillerGUI() {
// 安全地停止和清理工作线程
if (workerThread != nullptr) {
if (workerThread->isRunning()) {
workerThread->quit();
workerThread->wait(3000); // 最多等待3秒
}
delete workerThread;
workerThread = nullptr;
}
// worker已经被信号机制删除了
}
void NLKillerGUI::setupUI() {
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
mainLayout = new QHBoxLayout(centralWidget);
setupImagePreview();
setupControlPanel();
}
void NLKillerGUI::setupImagePreview() {
// 左侧图像预览区域
imageScrollArea = new QScrollArea();
imageLabel = new QLabel();
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setMinimumSize(600, 600);
imageLabel->setStyleSheet("QLabel { background-color: #f0f0f0; border: 1px solid #ccc; }");
imageLabel->setText("请选择文件夹加载图像");
imageScrollArea->setWidget(imageLabel);
imageScrollArea->setWidgetResizable(true);
mainLayout->addWidget(imageScrollArea, 2);
}
void NLKillerGUI::setupControlPanel() {
// 右侧控制面板
controlPanel = new QWidget();
controlPanel->setMaximumWidth(350);
controlLayout = new QVBoxLayout(controlPanel);
// 打开文件夹按钮
openFolderBtn = new QPushButton("打开文件夹");
openFolderBtn->setFocusPolicy(Qt::NoFocus);
connect(openFolderBtn, &QPushButton::clicked, this, &NLKillerGUI::openFolder);
controlLayout->addWidget(openFolderBtn);
// 一键查杀按钮
batchInferenceBtn = new QPushButton("一键查杀");
batchInferenceBtn->setEnabled(false);
batchInferenceBtn->setFocusPolicy(Qt::NoFocus);
connect(batchInferenceBtn, &QPushButton::clicked, this, &NLKillerGUI::batchInference);
controlLayout->addWidget(batchInferenceBtn);
// 进度条和速度显示
progressWidget = new QWidget();
progressLayout = new QHBoxLayout(progressWidget);
progressLayout->setContentsMargins(0, 0, 0, 0);
progressBar = new QProgressBar();
progressBar->setVisible(false);
progressBar->setRange(0, 100);
progressBar->setValue(0);
speedLabel = new QLabel("0.0 img/s");
speedLabel->setVisible(false);
speedLabel->setMinimumWidth(80);
speedLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
progressLayout->addWidget(progressBar);
progressLayout->addWidget(speedLabel);
controlLayout->addWidget(progressWidget);
// 置信度阈值滑条
confidenceLabel = new QLabel("置信度阈值:");
controlLayout->addWidget(confidenceLabel);
QHBoxLayout* sliderLayout = new QHBoxLayout();
confidenceSlider = new QSlider(Qt::Horizontal);
confidenceSlider->setRange(0, 1000);
confidenceSlider->setValue(500);
confidenceSlider->setFocusPolicy(Qt::NoFocus);
connect(confidenceSlider, &QSlider::valueChanged, this, &NLKillerGUI::onConfidenceChanged);
confidenceValueLabel = new QLabel("0.500");
confidenceValueLabel->setMinimumWidth(50);
sliderLayout->addWidget(confidenceSlider);
sliderLayout->addWidget(confidenceValueLabel);
controlLayout->addLayout(sliderLayout);
// 刷新按钮
refreshBtn = new QPushButton("刷新");
refreshBtn->setEnabled(false);
refreshBtn->setFocusPolicy(Qt::NoFocus);
connect(refreshBtn, &QPushButton::clicked, this, &NLKillerGUI::refreshResults);
controlLayout->addWidget(refreshBtn);
// 图像列表表格
imageTable = new QTableWidget();
imageTable->setColumnCount(2);
imageTable->setHorizontalHeaderLabels(QStringList() << "文件名" << "状态");
imageTable->horizontalHeader()->setStretchLastSection(false);
imageTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
imageTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
imageTable->setColumnWidth(1, 60);
imageTable->setSelectionBehavior(QAbstractItemView::SelectRows);
imageTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
imageTable->setFocusPolicy(Qt::NoFocus);
connect(imageTable, &QTableWidget::itemSelectionChanged, this, &NLKillerGUI::onTableSelectionChanged);
controlLayout->addWidget(imageTable);
// 当前图像状态标签
currentImageStatusLabel = new QLabel("请加载图像");
currentImageStatusLabel->setAlignment(Qt::AlignCenter);
currentImageStatusLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; padding: 10px; }");
controlLayout->addWidget(currentImageStatusLabel);
// 统计信息标签
statisticsLabel = new QLabel("统计: 未加载图像");
statisticsLabel->setAlignment(Qt::AlignCenter);
statisticsLabel->setStyleSheet("QLabel { font-size: 12px; color: #666;}");
controlLayout->addWidget(statisticsLabel);
// 导出按钮
exportBtn = new QPushButton("导出结果");
exportBtn->setEnabled(false);
exportBtn->setFocusPolicy(Qt::NoFocus);
connect(exportBtn, &QPushButton::clicked, this, &NLKillerGUI::exportResults);
controlLayout->addWidget(exportBtn);
// 模型选择
modelLabel = new QLabel("模型选择:");
controlLayout->addWidget(modelLabel);
modelComboBox = new QComboBox();
modelComboBox->addItems(QStringList() << "质量最高 (helicopter)" << "平衡 (NLK-s)" << "速度最快 (yvgg)");
modelComboBox->setFocusPolicy(Qt::NoFocus);
connect(modelComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &NLKillerGUI::onModelChanged);
controlLayout->addWidget(modelComboBox);
mainLayout->addWidget(controlPanel, 1);
}
void NLKillerGUI::openFolder() {
QString folderPath = QFileDialog::getExistingDirectory(this, "选择图像文件夹");
if (!folderPath.isEmpty()) {
loadImagesFromFolder(folderPath);
}
}
void NLKillerGUI::loadImagesFromFolder(const QString& folderPath) {
images.clear();
currentImageIndex = 0;
QDir dir(folderPath);
QStringList filters;
for (const QString& format : supportedFormats) {
filters << QString("*.%1").arg(format);
filters << QString("*.%1").arg(format.toUpper());
}
QStringList fileNames = dir.entryList(filters, QDir::Files, QDir::Name);
for (const QString& fileName : fileNames) {
ImageInfo info;
info.fileName = fileName;
info.filePath = dir.absoluteFilePath(fileName);
images.push_back(info);
}
if (!images.empty()) {
batchInferenceBtn->setEnabled(true);
refreshBtn->setEnabled(true);
exportBtn->setEnabled(true);
updateImageTable();
updateImagePreview();
updateCurrentImageStatus();
updateStatistics();
// 对第一张图片立即进行推理
if (!images.empty()) {
processCurrentImage();
}
} else {
QMessageBox::information(this, "提示", "未找到支持的图像文件");
updateStatistics();
}
}
void NLKillerGUI::updateImagePreview() {
if (images.empty() || currentImageIndex < 0 || currentImageIndex >= static_cast<int>(images.size())) {
imageLabel->setText("无图像");
return;
}
QPixmap pixmap = loadImageAsPixmap(images[currentImageIndex].filePath);
if (!pixmap.isNull()) {
// 缩放图像以适应预览区域,保持宽高比
QPixmap scaledPixmap = pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
imageLabel->setPixmap(scaledPixmap);
} else {
imageLabel->setText("无法加载图像");
}
// 高亮当前行
imageTable->selectRow(currentImageIndex);
}
void NLKillerGUI::updateImageTable() {
imageTable->setRowCount(static_cast<int>(images.size()));
for (int i = 0; i < static_cast<int>(images.size()); ++i) {
QTableWidgetItem* nameItem = new QTableWidgetItem(images[i].fileName);
imageTable->setItem(i, 0, nameItem);
QString statusText = "?";
if (images[i].processed) {
statusText = images[i].isPositive ? "✅" : "❌";
}
QTableWidgetItem* statusItem = new QTableWidgetItem(statusText);
statusItem->setTextAlignment(Qt::AlignCenter);
imageTable->setItem(i, 1, statusItem);
}
}
void NLKillerGUI::updateCurrentImageStatus() {
if (images.empty() || currentImageIndex < 0 || currentImageIndex >= static_cast<int>(images.size())) {
currentImageStatusLabel->setText("请加载图像");
currentImageStatusLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; padding: 10px; color: black; }");
return;
}
const ImageInfo& info = images[currentImageIndex];
if (!info.processed) {
currentImageStatusLabel->setText("未处理");
currentImageStatusLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; padding: 10px; color: gray; }");
} else if (info.isPositive) {
currentImageStatusLabel->setText("NAILONG FOUND");
currentImageStatusLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; padding: 10px; color: red; }");
} else {
currentImageStatusLabel->setText("pass");
currentImageStatusLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; padding: 10px; color: green; }");
}
}
void NLKillerGUI::switchToImage(int index) {
if (index < 0 || index >= static_cast<int>(images.size())) {
return;
}
currentImageIndex = index;
updateImagePreview();
updateCurrentImageStatus();
// 如果当前图像未处理过,进行单张推理
if (!images[currentImageIndex].processed) {
processCurrentImage();
}
}
void NLKillerGUI::processCurrentImage() {
if (currentImageIndex < 0 || currentImageIndex >= static_cast<int>(images.size())) {
return;
}
if (images[currentImageIndex].processed) {
return; // 已经处理过了
}
// 直接同步推理,避免快速切换时的漏识别问题
auto tensor = InferenceWorker::loadImageToTensor(images[currentImageIndex].filePath);
if (tensor.data != nullptr) {
float confidence = killer->infer(tensor);
onSingleImageProcessed(currentImageIndex, confidence);
}
}
void NLKillerGUI::batchInference() {
if (images.empty()) {
return;
}
batchInferenceBtn->setEnabled(false);
batchInferenceBtn->setText("推理中...");
modelComboBox->setEnabled(false);
// 显示进度条
progressBar->setValue(0);
progressBar->setVisible(true);
speedLabel->setText("0.0 img/s");
speedLabel->setVisible(true);
// 停止之前的推理线程
if (workerThread != nullptr) {
if (workerThread->isRunning()) {
workerThread->quit();
workerThread->wait(3000); // 最多等待3秒
}
delete workerThread;
workerThread = nullptr;
}
// 创建新的线程和worker
workerThread = new QThread(this);
worker = new InferenceWorker(killer.get(), &images);
worker->moveToThread(workerThread);
connect(workerThread, &QThread::started, worker, &InferenceWorker::processImages);
connect(worker, &InferenceWorker::imageProcessed, this, &NLKillerGUI::onImageProcessed);
connect(worker, &InferenceWorker::progressUpdated, this, &NLKillerGUI::onProgressUpdated);
connect(worker, &InferenceWorker::allImagesProcessed, this, &NLKillerGUI::onAllImagesProcessed);
// 确保worker在线程结束时被清理
connect(workerThread, &QThread::finished, worker, &QObject::deleteLater);
workerThread->start();
}
void NLKillerGUI::refreshResults() {
for (ImageInfo& info : images) {
if (info.processed) {
info.isPositive = (info.confidence >= confidenceThreshold);
}
}
updateImageTable();
updateCurrentImageStatus();
updateStatistics();
}
void NLKillerGUI::onConfidenceChanged(int value) {
confidenceThreshold = value / 1000.0f;
QString text = QString::number(confidenceThreshold, 'f', 3);
confidenceValueLabel->setText(text);
}
void NLKillerGUI::onImageProcessed(int index, float confidence) {
if (index >= 0 && index < static_cast<int>(images.size())) {
images[index].confidence = confidence;
images[index].processed = true;
images[index].isPositive = (confidence >= confidenceThreshold);
// 更新表格中的单个项目
QString statusText = images[index].isPositive ? "✅" : "❌";
QTableWidgetItem* statusItem = new QTableWidgetItem(statusText);
statusItem->setTextAlignment(Qt::AlignCenter);
imageTable->setItem(index, 1, statusItem);
// 如果是当前显示的图像,更新状态
if (index == currentImageIndex) {
updateCurrentImageStatus();
}
updateStatistics();
}
}
void NLKillerGUI::onAllImagesProcessed() {
batchInferenceBtn->setEnabled(true);
batchInferenceBtn->setText("一键查杀");
// 隐藏进度条
progressBar->setVisible(false);
speedLabel->setVisible(false);
modelComboBox->setEnabled(true);
updateStatistics();
QMessageBox::information(this, "完成", "批量推理完成!");
}
void NLKillerGUI::onSingleImageProcessed(int index, float confidence) {
if (index >= 0 && index < static_cast<int>(images.size())) {
images[index].confidence = confidence;
images[index].processed = true;
images[index].isPositive = (confidence >= confidenceThreshold);
updateImageTable();
updateCurrentImageStatus();
updateStatistics();
}
}
void NLKillerGUI::onModelChanged(int index) {
if (index >= 0 && index < modelPaths.size()) {
loadModel(modelPaths[index]);
// 清空所有图片的推理结果
for (ImageInfo& info : images) {
info.processed = false;
info.confidence = 0.0f;
info.isPositive = false;
}
// 更新表格显示
updateImageTable();
// 如果有当前图片,立即重新推理
if (!images.empty() && currentImageIndex >= 0 && currentImageIndex < static_cast<int>(images.size())) {
processCurrentImage();
}
}
}
void NLKillerGUI::loadModel(const QString& modelPath) {
if (killer->loadModel(modelPath.toStdString())) {
// 模型加载成功,可以在状态栏或其他地方显示提示
setWindowTitle(QString("NLKiller GUI - %1").arg(QFileInfo(modelPath).baseName()));
} else {
QMessageBox::critical(this, "错误", QString("无法加载模型: %1").arg(modelPath));
}
}
void NLKillerGUI::exportResults() {
if (images.empty()) {
return;
}
QString fileName = QFileDialog::getSaveFileName(this, "导出结果",
QDir::homePath() + "/nlkiller_results.csv",
"CSV files (*.csv)");
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "错误", "无法创建文件");
return;
}
QTextStream out(&file);
out << "file_path,score,result\n";
for (const ImageInfo& info : images) {
if (info.processed) {
out << info.filePath << ","
<< QString::number(info.confidence, 'f', 6) << ","
<< (info.isPositive ? "True" : "False") << "\n";
}
}
QMessageBox::information(this, "完成", "结果已导出到: " + fileName);
}
QPixmap NLKillerGUI::loadImageAsPixmap(const QString& path) {
// 禁用Qt的ICC警告
qputenv("QT_LOGGING_RULES", "qt.gui.icc.debug=false");
QPixmap pixmap;
if (!pixmap.load(path)) {
// 如果直接加载失败,尝试先加载为QImage再转换
QImage qimg(path);
if (!qimg.isNull()) {
pixmap = QPixmap::fromImage(qimg);
}
}
return pixmap;
}
void NLKillerGUI::keyPressEvent(QKeyEvent *event) {
if (images.empty()) {
QMainWindow::keyPressEvent(event);
return;
}
switch (event->key()) {
case Qt::Key_S:
case Qt::Key_D:
case Qt::Key_Down:
case Qt::Key_Right:
switchToImage((currentImageIndex + 1) % images.size());
break;
case Qt::Key_W:
case Qt::Key_A:
case Qt::Key_Up:
case Qt::Key_Left:
switchToImage((currentImageIndex - 1 + images.size()) % images.size());
break;
default:
QMainWindow::keyPressEvent(event);
}
}
void NLKillerGUI::onProgressUpdated(int current, int total, float speed) {
if (total > 0) {
int percentage = (current * 100) / total;
progressBar->setValue(percentage);
}
QString speedText = QString::number(speed, 'f', 1) + " img/s";
speedLabel->setText(speedText);
}
void NLKillerGUI::onTableSelectionChanged() {
QList<QTableWidgetItem*> selectedItems = imageTable->selectedItems();
if (!selectedItems.isEmpty()) {
int row = selectedItems.first()->row();
if (row >= 0 && row < static_cast<int>(images.size()) && row != currentImageIndex) {
switchToImage(row);
}
}
}
void NLKillerGUI::updateStatistics() {
if (images.empty()) {
statisticsLabel->setText("统计: 未加载图像");
return;
}
int unscanned = 0;
int passed = 0;
int detected = 0;
for (const auto& img : images) {
if (!img.processed) {
unscanned++;
} else if (img.isPositive) {
detected++;
} else {
passed++;
}
}
QString statsText = QString("未扫描 %1 | 通过 %2 | 发现奶龙 %3").arg(unscanned).arg(passed).arg(detected);
statisticsLabel->setText(statsText);
}