C/C++ TensorRT引擎多线程推理多个rtsp流并显示或保存

该博客介绍了一个使用OpenCV从RTSP流中捕获帧,并通过队列传递到多线程进行处理的系统。每个线程负责一个RTSP流,获取的帧经过预处理后输入到预先训练好的TensorRT模型进行推理,识别出目标物体。主要涉及超参数设置、数据包装、OpenCV流获取、模型推理和主函数流程。整个系统实现了异步推理,提高了实时性。

主要思路

每个thread负责一个rtsp流,主线程里定义WrapData类型的消息队列(目的是包含frame和对应的rtsp信息),传到每个线程里面去通过opencv获取frame,将frame和rtsp信息包装到WrapData后push到队列中,主函数中捕捉到队列长度大于1则进行.front()推理并在推理结束后.pop()

一、超参数和常量


#define NMS_THRESH 0.4
#define CONF_THRESH 0.5
#define BATCH_SIZE 1

// stuff we know about the network and the input/output blobs
static const int INPUT_H = Yolo::INPUT_H;
static const int INPUT_W = Yolo::INPUT_W;
static const int OUTPUT_SIZE = Yolo::MAX_OUTPUT_BBOX_COUNT * sizeof(Yolo::Detection) / sizeof(float) + 1;  // we assume the yololayer outputs no more than MAX_OUTPUT_BBOX_COUNT boxes that conf >= 0.1
const char* INPUT_BLOB_NAME = "data";
const char* OUTPUT_BLOB_NAME = "prob";
static Logger gLogger;

const char *class_name[4] = {
   
   "1","2","3","4"};

二、简易的WrapData类

class WrapData{
   
   
public:
    Mat img;
    string channel;
};

三、opencv获取rtsp流

int capture_show(string& rtsp_address, queue<WrapData>& Wrap_images){
   
   
    WrapData Wrap_img;
    Mat frame;
    VideoCapture cap;
    cap.open(rtsp_address);
    if (!cap.isOpened()) {
   
   
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    for (;;)
    {
   
   
        // wait for a new frame from camera and store it into 'frame'
        cap.read(frame);
        // check if we succeeded
        if (frame.empty()) {
   
   
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        Wrap_img.channel = rtsp_address;
        Wrap_img.img = frame;
        Wrap_images.push(Wrap_img);
    }
    cap.release();
    return 1;
}

四、.engine 推理部分

void doInference(IExecutionContext& context, cudaStream_t& stream, void *
评论 20
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值