在PC上显示picamera视频

我想实现用树莓派摄像头picamera采集数据,发送到远程主机上显示或进行其他处理。picamera示例程序中已经有相应的实现。由于项目需要,远程PC端要使用OpenCV库处理图像,我尝试在树莓派上使用picamera库都没有得到满意的效果。

服务端

终于找到了一个用C++写的rtsp服务器程序——h264_v4l2_rtspserver。首先运行如下命令:

1
2
sudo apt-get install v4l-utils
sudo modprobe bcm2835-v4l2

这样就可以通过/dev/video0设备文件来获取摄像头的数据。注意每次重启后都要运行第二条命令,如果想省略这一步,需要在/etc/modules添加一行:bcm2835-v4l2

安装h264_v4l2_rtspserver之后运行如下命令:

1
$ sudo h264_v4l2_rtspserver/h264_v4l2_rtspserver -F 15 -W 800 -H 600 -P 8555 /dev/video0

即在树莓派上运行了一个rtsp服务器,图像大小是800x600,fps是15,端口号为8555。

客户端

在远程PC上打开vlc播放器并输入如下url即可看到视频:

1
rtsp://ip-address-of-your-rPI:8555/unicast

OpenCV的VideoCapture类也可以读取上述视频流。下面的源代码实现了打开一个rtsp链接,读取并显示每一帧图像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
const string sourceUrl = "rtsp://ip-address:8555/unicast";
int delay = 20;
int frameNum = -1; // Frame counter
char c;

VideoCapture capt(sourceUrl);
if (!capt.isOpened()) {
cout << "Could not open url " << sourceUrl << endl;
return -1;
}
Size refS = Size((int) capt.get(CV_CAP_PROP_FRAME_WIDTH),
(int) capt.get(CV_CAP_PROP_FRAME_HEIGHT));

const char* WIN_RF = "RTSP";
// Windows
namedWindow(WIN_RF, CV_WINDOW_AUTOSIZE);

cout << "Frame resolution: Width="
<< refS.width << " Height=" << refS.height << " of nr#: "
<< capt.get(CV_CAP_PROP_FRAME_COUNT) << endl;

Mat frame;

// Show the image captured in the window and repeat
for (;;) {
capt >> frame;
if (frame.empty()) {
cout << " < < < Game over! > > > " << endl;
break;
}
++frameNum;
cout << "Frame: " << frameNum << "# " << endl;
imshow(WIN_RF, frame);
c = (char)cvWaitKey(delay);
if (c == 27)
break;
}

return 0;
}

参考

  1. Making a Raspberry Pi HD Camera
  2. Video Input with OpenCV and similarity measurement