Compare commits

...

2 Commits

4 changed files with 135 additions and 0 deletions

42
detectionworker.cpp Normal file
View File

@ -0,0 +1,42 @@
// detection_worker.cpp
#include "detectionworker.h"
#include "globals.h"
#include <QDebug>
#include "onnxrunner.h"
// 声明外部ONNXRunner实例数组
extern ONNXRunner* g_runner_array[2];
// 定义检测工作者函数
void detectionWorker(int camera_id)
{
// 验证相机ID的有效性
if(camera_id < 0 || camera_id >= 2)
{
qCritical() << "Invalid camera_id:" << camera_id;
return;
}
// 运行检测工作者线程,直到停止信号被触发
while(g_recognitionRunning[camera_id]->load())
{
ImageData data;
// 从图像队列中获取图像数据
if(g_img_Queue[camera_id]->dequeue(data))
{
// 执行ONNX推理
std::vector<Detection> detections = g_runner_array[camera_id]->predict(data.image);
cv::Mat processed_img = g_runner_array[camera_id]->postProcess(detections, data.image);
// 构建检测结果
ImageData result;
result.camera_id = data.camera_id;
result.image = processed_img;
// 将检测结果推入结果队列
g_result_Queue[camera_id]->enqueue(result);
}
}
qDebug() << "DetectionWorker: Stopped for camera" << camera_id;
}

8
detectionworker.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef DETECTION_WORKER_H
#define DETECTION_WORKER_H
#include "onnxrunner.h"
// 检测工作者函数声明
void detectionWorker(int camera_id);
#endif // DETECTION_WORKER_H

62
storageworker.cpp Normal file
View File

@ -0,0 +1,62 @@
// storageworker.cpp
#include "storageworker.h"
#include <QDateTime>
#include <QDir>
#include <QDebug>
#include <QStandardPaths>
StorageWorker::StorageWorker(QObject *parent)
: QObject(parent)
{
}
StorageWorker::~StorageWorker()
{
}
QString StorageWorker::getSaveDirectory()
{
QString baseDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
return baseDir;
}
void StorageWorker::process()
{
while (true)
{
ImageData data;
if (!g_storageQueue.dequeue(data))
{
// 队列已停止且为空,退出循环
break;
}
if (data.image.empty())
{
qWarning() << "Received empty image for camera" << data.camera_id;
continue;
}
// 定义保存路径,例如使用时间戳
QString saveDir = (getSaveDirectory() + QString("/Cam_%1/")).arg(data.camera_id == 0 ? "1" : "2");
QDir dir;
if (!dir.exists(saveDir))
{
dir.mkpath(saveDir);
}
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss_zzz");
QString filePath = saveDir + QString("%1.bmp").arg(timestamp);
// 使用 OpenCV 直接保存图像
if (cv::imwrite(filePath.toStdString(), data.image))
{
qDebug() << "Successfully saved image to" << filePath;
}
else
{
qWarning() << "Failed to save image to" << filePath;
}
}
}

23
storageworker.h Normal file
View File

@ -0,0 +1,23 @@
// storageworker.h
#ifndef STORAGEWORKER_H
#define STORAGEWORKER_H
#include <QObject>
#include <opencv2/opencv.hpp>
#include "globals.h"
class StorageWorker : public QObject
{
Q_OBJECT
public:
explicit StorageWorker(QObject *parent = nullptr);
~StorageWorker();
public slots:
void process(); // 存储处理槽
private:
QString getSaveDirectory(); // 获取保存目录的函数
};
#endif // STORAGEWORKER_H