// 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;
}
