Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我修改了一下推理逻辑,遇到一些问题 #140

Open
extreme99520-l opened this issue Jul 26, 2024 · 0 comments
Open

我修改了一下推理逻辑,遇到一些问题 #140

extreme99520-l opened this issue Jul 26, 2024 · 0 comments

Comments

@extreme99520-l
Copy link

我要想实现的是如果输入源是视频流,则获取到的每一帧先分割成小图片,然后小图片再送入batch进行推理,并返回推理的类别
我修改了task的返回值为类型,返回一个存储类别的列表
我遇到的问题是:

  • 输入的视频大小为1280:720,如果设置16:9的比例刚好,但是换成其他比例会直接报Segmentation fault (core dumped),resize和padding也无法解决,1:1也是可以完成正常推理的
  • 在16:9的设置下可以正常推理一部分,仍然还是会报错Segmentation fault (core dumped)

image

int rows = 16;
int cols = 9;

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

    // 计算新的宽度和高度
    int newWidth = ((frame.cols / cols) * cols);
    if (newWidth < frame.cols)
        newWidth += cols;

    int newHeight = ((frame.rows / rows) * rows); 
    if (newHeight < frame.rows)
        newHeight += rows;

    // 调整图像大小
    cv::resize(frame, frame, cv::Size(newWidth, newHeight));
    int width = newWidth / cols;
    int height = newHeight / rows;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++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 {
            // 如果达到批次大小,执行推理并清空批次
            auto predictions = task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);
            imgs_batch.clear();
            batchi++;

            // Print the predictions
            for (size_t bi = 0; bi < predictions.size(); bi++) {
                int classId = predictions[bi].first;
                std::cout << "Image " << bi << ": Predicted Class ID: " << classId << std::endl;
            }
        }
    }
} else { // 如果输入源是单张图片
    frame = cv::imread(image_path);
    if (!frame.empty()) {
        imgs_batch.emplace_back(frame);
        if (imgs_batch.size() == param.batch_size) {
            auto predictions = task(yolo, param, imgs_batch, delay_time, batchi, is_show, is_save);
            imgs_batch.clear();
            batchi++;

            // Print the predictions
            for (size_t bi = 0; bi < predictions.size(); bi++) {
                int classId = predictions[bi].first;
                std::cout << "Image " << bi << ": Predicted Class ID: " << classId << std::endl;
            }
        }
    }
}

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

    // Print the predictions
    for (size_t bi = 0; bi < predictions.size(); bi++) {
        int classId = predictions[bi].first;
        std::cout << "Image " << bi << ": Predicted Class ID: " << classId << std::endl;
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant