FeiYull / TensorRT-Alpha

🔥🔥🔥TensorRT for YOLOv8、YOLOv8-Pose、YOLOv8-Seg、YOLOv8-Cls、YOLOv7、YOLOv6、YOLOv5、YOLONAS......🚀🚀🚀CUDA IS ALL YOU NEED.🍎🍎🍎
GNU General Public License v2.0
1.3k stars 201 forks source link

分区域推理 #138

Closed extreme99520-l closed 2 months ago

extreme99520-l commented 2 months ago

我修改了app_yolov8.cpp中while循环的逻辑,试图实现将从视频流或者摄像头获取的帧分割成小的区域再进行推理,为什么会报错?求大佬解答一下疑惑 while (capture.isOpened()) { if (batchi >= total_batches && source != utils::InputStream::CAMERA) { break; }

// 如果输入源是视频流或摄像头
if (source != utils::InputStream::IMAGE) {
    capture.read(frame);
    if (frame.empty()) {
        sample::gLogWarning << "no more video or camera frame" << std::endl;
        break;
    }

    // 分割帧为m * n个子图片

    int width = frame.cols / n;
    int height = frame.rows / m;

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cv::Rect roi(j * width, i * height, width, height);
            subFrames.push_back(frame(roi).clone());
        }
    }

    // 将子图片逐个加入到imgs_batch中
    for (auto& subFrame : subFrames) {
        if (imgs_batch.size() < param.batch_size) {
            imgs_batch.emplace_back(subFrame);
        } else {
            // 如果达到批次大小,执行推理并清空批次
            task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);
            imgs_batch.clear();
            batchi++;
        }
    }
} else { // 如果输入源是单张图片
    frame = cv::imread(image_path);
    if (!frame.empty()) {
        imgs_batch.emplace_back(frame);
        if (imgs_batch.size() == param.batch_size) {
            task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);
            imgs_batch.clear();
            batchi++;
        }
    }
}

// 如果imgs_batch未满但所有子图片已添加完毕
if (source != utils::InputStream::IMAGE && !subFrames.empty() && imgs_batch.size() > 0) {
    task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);
    imgs_batch.clear();
    batchi++;
}

}