From 812ffa114844242c945c6a6417f15efd0d5af69e Mon Sep 17 00:00:00 2001 From: XinJiang1 <1170701029@qq.com> Date: Wed, 25 Dec 2024 18:32:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=95=B4=E6=96=87=E4=BB=B6=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- detectionworker.cpp | 42 ++++++++++++++++++++++++++++++ detectionworker.h | 8 ++++++ storageworker.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++ storageworker.h | 23 +++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 detectionworker.cpp create mode 100644 detectionworker.h create mode 100644 storageworker.cpp create mode 100644 storageworker.h diff --git a/detectionworker.cpp b/detectionworker.cpp new file mode 100644 index 0000000..11e9358 --- /dev/null +++ b/detectionworker.cpp @@ -0,0 +1,42 @@ +// detection_worker.cpp +#include "detectionworker.h" +#include "globals.h" +#include +#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 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; +} diff --git a/detectionworker.h b/detectionworker.h new file mode 100644 index 0000000..c047923 --- /dev/null +++ b/detectionworker.h @@ -0,0 +1,8 @@ +#ifndef DETECTION_WORKER_H +#define DETECTION_WORKER_H +#include "onnxrunner.h" + +// 检测工作者函数声明 +void detectionWorker(int camera_id); + +#endif // DETECTION_WORKER_H diff --git a/storageworker.cpp b/storageworker.cpp new file mode 100644 index 0000000..88e9723 --- /dev/null +++ b/storageworker.cpp @@ -0,0 +1,62 @@ +// storageworker.cpp +#include "storageworker.h" +#include +#include +#include +#include + +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; + } + } +} diff --git a/storageworker.h b/storageworker.h new file mode 100644 index 0000000..67a25dc --- /dev/null +++ b/storageworker.h @@ -0,0 +1,23 @@ +// storageworker.h +#ifndef STORAGEWORKER_H +#define STORAGEWORKER_H + +#include +#include +#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