#pragma once #include #include #include #include "cuda_helper.hpp" #include "../common/logger.hpp" namespace pipeline { /** * @brief 图像预处理器 * * 负责将输入图像预处理为模型所需的格式: * 1. 图像缩放到指定尺寸 * 2. BGR转RGB * 3. 归一化 (0-255 -> 0-1) * 4. 批处理支持 */ class Preprocessor { public: struct Config { std::vector input_shape; // [channels, height, width] bool use_cuda{true}; // 是否使用CUDA加速 int max_batch_size{4}; // 最大批处理大小 float scale{1.0f/255.0f}; // 归一化系数 }; /** * @brief 构造函数 * @param config 预处理配置 */ explicit Preprocessor(const Config& config); ~Preprocessor(); // 禁用拷贝 Preprocessor(const Preprocessor&) = delete; Preprocessor& operator=(const Preprocessor&) = delete; /** * @brief 批量预处理图像 * @param images 输入图像列表 * @param output_buffer 输出缓冲区(CUDA设备内存) * @return 是否成功 */ bool process(const std::vector& images, detail::CudaBuffer& output_buffer); /** * @brief 获取配置 * @return 当前配置 */ const Config& getConfig() const { return config_; } private: // CPU预处理实现 bool processCPU(const std::vector& images, detail::CudaBuffer& output_buffer); // CUDA预处理实现 bool processGPU(const std::vector& images, detail::CudaBuffer& output_buffer); // 单张图像预处理(CPU版本) bool preprocessImageCPU(const cv::Mat& input, float* output, int target_height, int target_width); // 检查输入有效性 bool checkInput(const std::vector& images) const; private: Config config_; // 配置参数 std::unique_ptr stream_; // CUDA流 std::vector> temp_buffers_; // 临时缓冲区 }; } // namespace pipeline