使用 motion 和 mjpg 做视频监控器

概述

买了一个罗技的usb接口的摄像头,想通过raspberry pi做一个视频的实时监控器,看了一下这各功能可以通过两款软件实现:motion和mjpg-streamer,先来简单介绍下这两个软件。

motion

是一个基于命令行的,在Linux系统下使用的,捕捉图片或者视频流的软件,可以吧摄像头设备的画面捕捉下来。下面是wikipedia的介绍:

[html]view plaincopy

Motion, a software motion detector, is a free, open source CCTV software application developed for Linux.

It can monitor video signal from one or more cameras and is able to detect if a significant part of the picture has changed saving away video when it detects that motion is occurring (it can also do time lapse videos, et al.).

The program is written in C and is made for Linux (exploiting video4linux interface). Motion is a command line based tool whose output can be either jpeg, netpbm files or mpeg video sequences. It is strictly command line driven and can run as a daemon with a rather small footprint[1] and low CPU usage.

mjpg-streamer

同样,也是一款linux下的图片,视频捕捉软件:

[html]view plaincopy

MJPG-streamer takes JPGs from Linux-UVC compatible webcams, filesystem or other input plugins and streams them as M-JPEG via HTTP to webbrowsers, VLC and other software. It is the successor of uvc-streamer, a Linux-UVC streaming application with Pan/Tilt

引自:https://sourceforge.net/projects/mjpg-streamer/的介绍。

我使用以上的两者都做了尝试,事实证明mjpg的效果比motion好太多,主要体现在delay上。motion卡顿太致命了,延迟在30s+,而mjpg就好很多。但是无奈motion实现很简单,mjpg则是废了一些功夫才成功。下面上干货

使用motion实现

[html]view plaincopy

sudo apt-get update

[html]view plaincopy

sudo apt-get install motion

[html]view plaincopy

sudo nano /etc/default/motion

把“start_motion_daemon=no”的选项改为yes。然后

[html]view plaincopy

sudo nano /etc/motion/motion.conf

daemon on

width 640

height 480

framerate 100

stream_localhost off

主要把上面这几项修改了,这个文件很长,可以使用ctl+v翻页。最后ctl+o保存,ctl+x退出。

然后启动motion:

[html]view plaincopy

sudo motion

然后打开浏览器

ip:8081即可访问。

下面的是我的截图

如果要关闭,可以:

[html]view plaincopy

sudo killall -TERM motion

之前也提到了,这个方案的致命伤是太卡了,根本不能满足需求。。。后来我就上google,有人提到了使用motion确实很卡,无法避免,除非使用mjpg,这个效果最好,无奈之下,又开启了默默探索以旅。。。

关于motion的参考:

http://www.techradar.com/how-to/computing/use-a-raspberry-pi-to-remotely-watch-your-home-1314466

使用mjpg-streamer实现

这个稍微麻烦点,而且一般资源在google,大家查的时候要注意,google上很多资源是针对树莓派自带的摄像机模块来的,但是对于像我这样的穷屌丝来说,还不如买个其他摄像头来的划算,如果不用自带的摄像头,那么配置是不一样的,这点要注意。

[html]view plaincopy

sudo apt-get update

sudo apt-get install libjpeg8-dev imagemagick libv4l-dev

ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip

unzip mjpg-streamer-code-182.zip

cd mjpg-streamer

make mjpg_streamer input_file.so input_uvc.so output_http.so

sudo cp mjpg_streamer /usr/local/bin

sudo cp output_http.so input_file.so input_uvc.so /usr/local/lib/

sudo cp -R www /usr/local/www

启动:

[html]view plaincopy

/usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so -y” -o “/usr/local/lib/output_http.so -w /usr/local/www”

最终:http://192.168.2.12:8080/stream.html,当然要成你的ip。截图:

在这里插入图片描述
这是mjpg的主页,点击stream可以看到视频流,如下图:
在这里插入图片描述
转自:蝈胖胖

参考:http://blog.cudmore.io/post/2015/03/15/Installing-mjpg-streamer-on-a-raspberry-pi/

http://electronsfree.blogspot.com/2016/04/raspberry-pi-3-with-better-raspicam.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要接收mjpg streamer视频流,可以使用Qt的网络模块和多媒体模块。 在Qt中,可以通过QNetworkAccessManager类来进行网络请求,并使用QBuffer类来缓存网络数据。接收到数据后,可以使用QMovie类来解码mjpg数据流,然后将解码后的帧显示在Qt的界面上。 具体的实现步骤如下: 1. 创建一个Qt网络请求对象QNetworkRequest,并设置请求的URL; 2. 使用QNetworkAccessManager发送网络请求,并在回调函数中接收网络数据; 3. 将接收到的数据缓存到QBuffer对象中; 4. 使用QMovie对象解码mjpg数据流,并将解码后的帧显示在Qt界面上; 5. 循环执行步骤2-4,直到停止接收数据流。 注意事项: 1. 在接收数据流的过程中,需要处理数据流的边界,即分隔符; 2. 需要注意解码mjpg数据流的性能,避免卡顿; 3. 需要注意内存泄漏,及时释放资源。 参考代码如下: ```cpp #include <QtWidgets> #include <QtNetwork> #include <opencv2/opencv.hpp> class MJPGViewer : public QWidget { Q_OBJECT public: explicit MJPGViewer(QWidget *parent = nullptr) : QWidget(parent) { setWindowTitle("MJPGViewer"); label = new QLabel(this); label->setAlignment(Qt::AlignCenter); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(label); QNetworkRequest request(QUrl("http://localhost:8080")); manager = new QNetworkAccessManager(this); reply = manager->get(request); connect(reply, &QNetworkReply::readyRead, this, &MJPGViewer::onReadyRead); } private: QLabel *label; QNetworkAccessManager *manager; QNetworkReply *reply; QBuffer buffer; QMovie movie; void onReadyRead() { QByteArray data = reply->readAll(); buffer.write(data); int pos = 0; while ((pos = buffer.indexOf("--frame", pos)) != -1) { int start = pos + 8; int end = buffer.indexOf("\r\n\r\n", start); if (end == -1) { break; } QByteArray header = buffer.mid(start, end - start); int contentStart = end + 4; int contentEnd = buffer.indexOf("--frame", contentStart); if (contentEnd == -1) { contentEnd = buffer.size(); } QByteArray content = buffer.mid(contentStart, contentEnd - contentStart); pos = contentEnd; if (header.startsWith("Content-Type: image/jpeg")) { cv::Mat frame = cv::imdecode(cv::Mat(content.toStdString()), cv::IMREAD_COLOR); if (!frame.empty()) { QImage image(frame.data, frame.cols, frame.rows, QImage::Format_RGB888); movie.setScaledSize(label->size()); movie.setCacheMode(QMovie::CacheAll); movie.setSpeed(100); movie.setFileName(""); movie.setPaused(true); movie.setFormat("mjpg"); movie.setDevicePixelRatio(devicePixelRatio()); movie.setQuality(100); movie.setScaledSize(label->size()); movie.setPixmap(QPixmap::fromImage(image)); movie.start(); } } } buffer.buffer().remove(0, buffer.pos()); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MJPGViewer viewer; viewer.show(); return a.exec(); } #include "main.moc" ``` 上面的代码使用了Qt的网络模块和多媒体模块,实现了一个简单的mjpg streamer视频流接收。运行程序后,可以在Qt界面中查看摄像头的实时画面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值