268 lines
6.5 KiB
C++
268 lines
6.5 KiB
C++
#include "widget.h"
|
|
#include "ui_widget.h"
|
|
#include <iostream>
|
|
#include <QTcpSocket>
|
|
#include <QPushButton>
|
|
#include <QTimer>
|
|
#include <QEventLoop>
|
|
#include <QElapsedTimer>
|
|
// #include <windows.h>
|
|
#include <QApplication>
|
|
#include <QLabel>
|
|
#include <QImage>
|
|
#include <QPixmap>
|
|
#include <QDateTime>
|
|
#include <img_utils.h>
|
|
#include <detectionworker.h>
|
|
|
|
using namespace std;
|
|
|
|
|
|
// 硬编码参数值
|
|
int file_delay = 1270; // 延迟时间(毫秒)
|
|
int file_encoder = 12000; // 编码器值++
|
|
int file_valve = 200; // 阀门通道
|
|
|
|
Widget::Widget(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::Widget)
|
|
{
|
|
this->isCamRunning = false;
|
|
|
|
ui->setupUi(this);
|
|
ui->camera_0_img->setScaledContents(false);
|
|
ui->camera_1_img->setScaledContents(false);
|
|
|
|
iniOnnx();
|
|
iniColor();
|
|
iniLowMac();
|
|
iniCamera();
|
|
|
|
// 初始化存储工作者和线程
|
|
storageWorker = new StorageWorker();
|
|
storageWorker->moveToThread(&storageThread);
|
|
|
|
connect(&storageThread, &QThread::started, storageWorker, &StorageWorker::process);
|
|
connect(this, &Widget::destroyed, &storageThread, &QThread::quit);
|
|
connect(&storageThread, &QThread::finished, storageWorker, &QObject::deleteLater);
|
|
|
|
storageThread.start();
|
|
|
|
// 启动显示定时器,每秒检查一次
|
|
QTimer* timer = new QTimer(this);
|
|
connect(timer, &QTimer::timeout, this, &Widget::refreshImage);
|
|
timer->start(50); // 每50毫秒秒刷新一次界面
|
|
}
|
|
|
|
Widget::~Widget()
|
|
{
|
|
// 停止存储线程
|
|
g_storageQueue.stop();
|
|
storageThread.quit();
|
|
storageThread.wait();
|
|
|
|
// 现有清理代码...
|
|
DestoryCamera();
|
|
DestoryLowMac();
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
void Widget::refreshImage()
|
|
{
|
|
// refresh Image 1 and image 0
|
|
refreshSingleImage(0, this->ui->img_0_overlay->isChecked(), this->ui->img_0_mirror->isChecked());
|
|
refreshSingleImage(1, this->ui->img_1_overlay->isChecked(), this->ui->img_1_mirror->isChecked());
|
|
// refresh buttons
|
|
this->ui->btn_start->setEnabled(!this->isCamRunning);
|
|
this->ui->btn_stop->setEnabled(this->isCamRunning);
|
|
this->ui->btn_take_photos->setEnabled(this->isCamRunning);
|
|
// refresh info
|
|
QString info;
|
|
if(this->isCamRunning)
|
|
{
|
|
if(SaveImg_Flag==1)
|
|
{
|
|
info = "存图中!!";
|
|
}
|
|
else
|
|
{
|
|
info = "运行";
|
|
};
|
|
}else {
|
|
info = "停止";
|
|
};
|
|
this->ui->lab_info->setText(info);
|
|
}
|
|
|
|
void Widget::refreshSingleImage(int camera_id, bool overlay_result, bool mirror)
|
|
{
|
|
// 验证摄像头ID的有效性
|
|
if (camera_id < 0 || camera_id >= 2) { // 假设只有两个摄像头
|
|
qWarning() << "Invalid Camera ID:" << camera_id;
|
|
return;
|
|
}
|
|
|
|
// 定义每个摄像头对应的变量数组
|
|
QMutex* dispPicMutexes[2] = { &gDispPicMutex0, &gDispPicMutex1 };
|
|
MIL_ID dispCurrentPicIds[2] = { gDispCurrentPicId0, gDispCurrentPicId1 };
|
|
QMutex* maskMutexes[2] = { &gMaskMutex0, &gMaskMutex1 };
|
|
MIL_ID maskIds[2] = { gMask0, gMask1 };
|
|
QLabel* cameraLabels[2] = { ui->camera_0_img, ui->camera_1_img };
|
|
|
|
// 获取当前摄像头的数据
|
|
QMutex* currentDispMutex = dispPicMutexes[camera_id];
|
|
MIL_ID current_id;
|
|
|
|
{
|
|
QMutexLocker locker(currentDispMutex);
|
|
current_id = dispCurrentPicIds[camera_id];
|
|
}
|
|
|
|
if (current_id == 0)
|
|
return;
|
|
|
|
// 将MIL图像转换为OpenCV Mat
|
|
cv::Mat img = ImageUtils::mil2Mat(current_id);
|
|
if (img.empty()) {
|
|
qWarning() << "Failed to convert MIL image to Mat for Camera ID:" << camera_id;
|
|
return;
|
|
}
|
|
|
|
// 如果需要叠加结果,处理掩码
|
|
if (overlay_result)
|
|
{
|
|
QMutex* currentMaskMutex = maskMutexes[camera_id];
|
|
MIL_ID mask_id;
|
|
|
|
{
|
|
QMutexLocker locker(currentMaskMutex);
|
|
mask_id = maskIds[camera_id];
|
|
}
|
|
|
|
if (mask_id != 0) {
|
|
cv::Mat mask = ImageUtils::mil2Mat(mask_id);
|
|
if (!mask.empty()) {
|
|
img = ImageUtils::overlayResultOnInput(img, mask, 0.5);
|
|
}
|
|
else {
|
|
qWarning() << "Failed to convert MIL mask to Mat for Camera ID:" << camera_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 如果需要镜像处理
|
|
if (mirror)
|
|
{
|
|
cv::flip(img, img, 1); // 水平翻转
|
|
}
|
|
|
|
// 将OpenCV Mat转换为QPixmap
|
|
QPixmap pixmap = ImageUtils::mat2QPixmap(img);
|
|
if (pixmap.isNull()) {
|
|
qWarning() << "Failed to convert Mat to QPixmap for Camera ID:" << camera_id;
|
|
return;
|
|
}
|
|
|
|
// 高质量缩放图像
|
|
QSize labelSize = cameraLabels[camera_id]->size();
|
|
QPixmap scaledPixmap = pixmap.scaled(labelSize);
|
|
|
|
// 更新UI标签
|
|
cameraLabels[camera_id]->setPixmap(scaledPixmap);
|
|
}
|
|
|
|
|
|
void Widget::on_pushButton_2_clicked()
|
|
{
|
|
SaveImg_Flag = 1;
|
|
}
|
|
|
|
|
|
void Widget::on_btn_goto_sort_clicked()
|
|
{
|
|
// 回复显示的图片
|
|
{
|
|
QMutexLocker locker(&gDispPicMutex0);
|
|
gDispCurrentPicId0 = 0;
|
|
}
|
|
ui->camera_0_img->clear();
|
|
{
|
|
QMutexLocker locker(&gDispPicMutex1);
|
|
gDispCurrentPicId1 = 0;
|
|
}
|
|
ui->camera_1_img->clear();
|
|
DestoryCamera();
|
|
DestoryLowMac();
|
|
}
|
|
|
|
|
|
void Widget::on_btn_stop_clicked()
|
|
{
|
|
this->isCamRunning = false;
|
|
// 恢复显示的图片
|
|
{
|
|
QMutexLocker locker(&gDispPicMutex0);
|
|
gDispCurrentPicId0 = 0;
|
|
}
|
|
ui->camera_0_img->clear();
|
|
{
|
|
QMutexLocker locker(&gDispPicMutex1);
|
|
gDispCurrentPicId1 = 0;
|
|
}
|
|
ui->camera_1_img->clear();
|
|
// 停止检测工作者线程
|
|
for(int i = 0; i < 2; ++i)
|
|
{
|
|
g_recognitionRunning[i]->store(false);
|
|
g_img_Queue[i]->stop(); // 停止队列以唤醒线程
|
|
}
|
|
|
|
// 等待检测工作者线程结束
|
|
for(int i = 0; i < 2; ++i)
|
|
{
|
|
if(g_recognitionThread[i] && g_recognitionThread[i]->joinable())
|
|
{
|
|
g_recognitionThread[i]->join();
|
|
delete g_recognitionThread[i];
|
|
g_recognitionThread[i] = nullptr;
|
|
}
|
|
|
|
if(g_recognitionRunning[i])
|
|
{
|
|
delete g_recognitionRunning[i];
|
|
g_recognitionRunning[i] = nullptr;
|
|
}
|
|
}
|
|
DestoryCamera();
|
|
DestoryLowMac();
|
|
}
|
|
|
|
|
|
void Widget::on_btn_start_clicked()
|
|
{
|
|
this->isCamRunning = true;
|
|
// 启动检测工作者线程
|
|
for(int i = 0; i < 2; ++i)
|
|
{
|
|
g_recognitionRunning[i]->store(true);
|
|
g_recognitionThread[i] = new std::thread(detectionWorker, i);
|
|
}
|
|
Start_camera();
|
|
|
|
}
|
|
|
|
|
|
void Widget::on_btn_take_photos_pressed()
|
|
{
|
|
SaveImg_Flag = true;
|
|
}
|
|
|
|
|
|
void Widget::on_btn_take_photos_released()
|
|
{
|
|
SaveImg_Flag = false;
|
|
}
|
|
|