使用树莓派专用摄像头实现rtsp流的传输,并调用Opencv显示

      最近在做一个项目,想要使用树莓派实时的将摄像头内容发送给PC端,然后就开始了一个下午的漫漫调BUG路。

      网上关于树莓派实现rtsp流的视频传输的资料很多,但是由于版本以及一些源的问题,很多都存在一定的问题,但是也具有很大的参考价值。下面将我参考的文章贴出来,谢谢这些大佬前辈们的分享。

       opencv中VideoCapture的使用——打开网络摄像头/图像序列:        https://blog.csdn.net/tfygg/article/details/50404861(这篇文章的代码基本可以直接拷贝,只需要修改rstp的ip。可以初步实验是否已经成功配置成功)

      使用树莓派专用摄像头实现rtsp流的方法:https://blog.csdn.net/leeyunj/article/details/53872177(这篇文章的步骤流程是正确,但是由于时间较远,很多源无法更新库,我将在下面列举我的解决方法。)

      step1. 安装live555

(在Step1中基本没有出现什么问题)

       step2. 安装库文件

  • sudo apt-get update  
  • sudo apt-get install libavcodec54   
  • sudo apt-get install libswscale2  
  • sudo apt-get install libx264-142

       (在这第二部,我从2到4,全部失败,将2和3改用

         sudo apt-get install libavcodec-dev

         sudo apt-get install libswscale-dev 

          sudo apt-get install libavutil-dev

          4的libx264参考文章https://blog.csdn.net/a805995924/article/details/39435261。安装x264时候./configure --enable-shared --enable-pthread --enable-pic改成./configure

      Step3. 编译执行工具

下载源码后,copy到/opt/vc/src/hello_pi/

根据/hello_pi/下的README编译安装iclient库

  • cd live_camera_linux  
  • sudo make  
  • sudo make install  
  • sudo ./zk_v4l2_x264_source  
若出现
  • [2464] WebcamOndemandMediaSubsession .... calling  
  • using url "rtsp://192.168.1.102:9554/webcam"  

则成功实现

(在这一步中,下载完源码,要放到那个路径下,然后按照/opt/vc/src/hello_pi/中README步骤做,这里贴一张README注释的图

这里进入live_camera_linux后,基本都会出错,在保证前面步骤没出错后,这里会出现库文件丢失,不要慌,是因为版本更新,导致的文件名字改变,打开live_camera_linux中的Makefile,找到其中你确实的库,到它的路径中去寻找它的新的名字,一般都是在原来的名字的基础上加一点后标什么的,很容易找到。然后第三步的sudo make install我这里显示直接失败,但是运行第四步依然成功了,不知道为啥,啊哈哈)

       经历了一下午的折腾,最终运行了测试代码,成功实现了功能。如果大家在树莓派上不能下载库或者包什么的,可以电脑下载好传给树莓派。最后上一张结果图,人丑,打上粗糙的马赛克。


    

         

实现这个功能,首先需要安装FFmpeg和OpenCV库。然后可以按照以下步骤进行操作: 1. 导入头文件 ```c++ #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/videoio.hpp> #include "ffmpeg.h" ``` 2. 定义回调函数 ```c++ void display_frame(cv::Mat frame) { cv::imshow("Video", frame); cv::waitKey(1); } ``` 3. 定义获取视频流的函数 ```c++ void get_stream(const std::string& url) { AVFormatContext* format_ctx = avformat_alloc_context(); AVDictionary* options_dict = NULL; av_dict_set(&options_dict, "rtsp_transport", "tcp", 0); av_dict_set(&options_dict, "stimeout", "5000000", 0); av_dict_set(&options_dict, "max_delay", "500000", 0); av_dict_set(&options_dict, "buffer_size", "1024000", 0); av_dict_set(&options_dict, "rtbufsize", "1024000", 0); av_dict_set(&options_dict, "max_delay", "500000", 0); avformat_open_input(&format_ctx, url.c_str(), NULL, &options_dict); avformat_find_stream_info(format_ctx, NULL); AVCodec* codec = nullptr; int stream_index = av_find_best_stream(format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0); AVCodecContext* codec_ctx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(codec_ctx, format_ctx->streams[stream_index]->codecpar); avcodec_open2(codec_ctx, codec, NULL); AVPacket* packet = av_packet_alloc(); AVFrame* frame = av_frame_alloc(); while (av_read_frame(format_ctx, packet) >= 0) { if (packet->stream_index == stream_index) { avcodec_send_packet(codec_ctx, packet); while (avcodec_receive_frame(codec_ctx, frame) == 0) { cv::Mat mat(frame->height, frame->width, CV_8UC3, frame->data[0]); cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB); display_frame(mat); av_frame_unref(frame); } } av_packet_unref(packet); } av_packet_free(&packet); av_frame_free(&frame); avcodec_close(codec_ctx); avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); } ``` 4. 调用函数播放视频流 ```c++ int main() { std::string url = "rtsp://xxx.xxx.xxx.xxx:xxxx/xxx"; get_stream(url); return 0; } ``` 这样就可以获取RTSP视频流使用OpenCV显示图像了。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值