主要思路
每个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 *

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





