Linux下的motion detection(最简单的办公室监控系统)

来源:互联网 发布:贵阳花溪广电网络 编辑:程序博客网 时间:2024/04/29 14:49

2009-02-21 01:00:09|  分类: 学习|字号 订阅

 

这篇结合前面三篇(关于Linux内核的,关于motion介绍的,关于安装usb摄像头驱动的),介绍了一个Linux下,用来探测运动的软件包motion,它可以按照需要进行配置。实现在摄像头静止情况下,对视场的监控。

所需硬件,一台pc,一个普通usb摄像头。然后在Windows XP里装一个虚拟机,下一个虚拟机下用的Linux(见前篇motion介绍的blog),就可以应用motion了。本文里讲motion设置为,一检测到视场里有运动,就向设定邮箱发送监控avi视频。我曾经插着USB摄像头,启动motion,然后运行了一晚上。本想拍下同事早晨来时的视频,结果早晨我来了检查邮箱,除了我走和来时的样子,竟然还有清洁工大妈的样子,一看时间是早晨5:30,这个意外发现解决了我一直以来的一个疑问,到底是谁,什么时间来打扫办公室的。而整晚上发送监控邮件共12封,拍到的视频总大小不到10Mb,确实是一个简单易行的好工具。

首先,确保USB摄像头能在Linux下正常工作。

在Debian下安装Motion也是相当的简单。http://www.debian.com/distrib/packages里有提供motion的安装包,所以直接在终端下运行

# apt-get install motion

装好后,根据安装提示,修改/etc/motion/里的相关文件名,得到这个文件/etc/motion/motion.conf。motion.conf就是motion程序与操作者的接口文件。通过修改motion.conf里的配置,来控制motion的运行。当在终端里运行#motion时,会先在终端所示当前目录下寻找motion.conf,没有找到时,去寻找/etc/motion/motion.conf。所以,我把这个文件copy一份,放到了/root下,然后每次都进root里运行motion。

运行前,先修改这个文件/root/motion.conf,我的motion.conf

# Minimal motion example config file provided by the

# Debian motion package - for basic webcam operation.

#

# You most certainly want to investigate

# /usr/share/doc/motion/examples/motion-dist.conf.gz

# for further configuration options. Also, refer to the

# motion man page and /usr/share/doc/motion/motion_guide.html

# for detailed information on configuration options.

 

daemon off

quiet on

locate on

 

# You may very well need to change this (check with 'dmesg'

# after plugging in your webcam).

videodevice /dev/video0

 

# Image size in pixels (valid range is camera dependent).

width 320

height 240

 

framerate 25

quality 85

auto_brightness off

 

# General threshold level and noise threshold

# level (for distinguishing between noise and motion).

threshold_tune off

threshold 4500

noise_level 64

 

# Initial brightness, contrast, hue (NTSC), and saturation.

# 0 = disabled (valid range 0-255).

brightness 0

contrast 0

saturation 0

hue 0

 

# Encode movies in real-time (install ffmpeg before enabling).

ffmpeg_cap_new on

 

# Codec to be used by ffmpeg for the video compression.

# Supported formats: mpeg4, msmpeg4.

ffmpeg_video_codec msmpeg4

 

# Target base directory for pictures and films (you may need

# to change this (or change its permissions) depending on

# which system user runs motion).

target_dir /root/motion/snapshots

 

# Define a port number (e.g. 8000) to enable the mini-http server.

# 0 = disabled.

webcam_port 8081

 

# Set to 'off' to allow anybody (not just localhost) to view the

# webcam via the mini-http server (http://hostname:port).

webcam_localhost off

 

snapshot_interval 1

snapshot_filename snapshot

 

webcam_quality 50

webcam_maxrate 8

 

on_event_start /root/motion/on_motion_detected

on_event_end /root/motion/on_motion_end

gap 10

关于这些配置的具体意义,参见motion官方wikihttp://www.lavrsen.dk/twiki/bin/view/Motion/WebHome中的Config options。这里,我只对其中几个解释一下。

daemon off,关掉deamon模式。最好这项还是选off,否则运行motion后,就会直接在后台运行,需要用top命令查看出motion的进程号(pid),然后再手动kill掉这个进程。

locate on设置探测到图像中有运动时,把运动区域用矩形框起来。

videodevice /dev/video0 设置加载USB摄像头的设备,一般都是这个video0,当使用network webcam时,需要设置netcam_url,此时,videodevice选项自动失效。

threshold_tune off设置是否使用motion detection阈值自动调节。当设置为on时,下一个设置threshold 4500自动失效。设置off时,可以由threshold指定当探测到多少像素变化时,判断为图像中有运动。

ffmpeg_cap_new on这个选项是指,在detect到运动时,用视频纪录下来。

ffmpeg_video_codec msmpeg4 设定视频的编码器

target_dir /root/motion/snapshots当探测到运动时,图片和视频的保存路径,默认时为/var/lib/motion/snapshots。

snapshot_interval 1设定自动采集图片的周期,当有运动被检测到时,采集频率会自动变高。

on_event_start /root/motion/on_motion_detected当探测到运动时,执行所设定目录里的文件,这里设定为文件/root/motion/on_motion_detected,该文件可以是一个程序,可以是一段脚本,只要是能执行的就可以。

on_event_end /root/motion/on_motion_end当on_event_start开始后,即检测到运动后, 若有连续10秒不再能检测到运动时,执行该选项设定的文件。10秒参数是由以下gap 10语句设置而来。

以上两个设置参见http://www.lavrsen.dk/twiki/bin/view/Motion/ExternalCommands

gap 10设置,在探测到运动后,多长时间没有运动的话就触发运动结束指令on_event_end。

这里我的on_motion_detected和on_motion_end都是shell脚本。

on_motion_detected脚本的作用是,记录下探测到运动时的时间,即拍摄的监控视频文件的文件名的一部分。把这个时间存到/root/tmp/videotime文件中。on_motion_detected文件如下:

#!/bin/bash

echo "111111111111111on_motion_detected1111111111111111"

DATE=$(date +"%Y%m%d%H%M%S")

#DATE=$(date -d "-1 sec" +%Y%m%d%H%M%S)

 

ALARM_TIME="/root/tmp/videotime"

echo "$DATE" > $ALARM_TIME

而on_motion_end就是用来发送邮件的。它会在检测到的运动结束后,讲拍下来的运动的avi视频发送到指定邮箱里。avi视频的文件名为一个序号+检测到运动的时间+.avi,而检测到运动的时间,根据on_motion_detected脚本,存在/root/tmp/videotime里,理论上说只要从文件里读出时间,然后补全文件名(该序号由*号替代),便能发出邮件。但是,由于程序运动效率原因,有时会出现,记录的时间同开始录avi的时间差1秒的情况,虽然只有一秒,但是足以导致找不到avi文件,无法正确发出监控视频。由于我们设置了gap为10,即10秒内最多只有一个视频。所以,解决这个问题的办法可以是,去寻找videotime中所记录时间及其上一秒,连续两秒的视频,找到哪个发哪个。当然,结果永远是只会找到一个。on_motion_end这个shell脚本文件如下:

#!/bin/bash

echo "111111111111111on_motion_end1111111111111111"

DIRC="/root/motion/snapshots/"

VIDEOTIME="/root/tmp/videotime"

TIME=$(cat $VIDEOTIME)

ALARM_EMAIL="/root/tmp/myalarm.txt"

 

echo "Subject: Motion detected - $TIME - $DIRC" > $ALARM_EMAIL

echo "">> $ALARM_EMAIL

echo "Motion detected - check $TIME.avi">>$ALARM_EMAIL

MAILBODY=$(cat $ALARM_EMAIL)

 

#first trying of sending the avi video

echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi skpsun@163.com

 

#second trying of sending the avi video

TIME=$(expr $TIME - 1)

echo $MAILBODY | mutt -s $TIME -a $DIRC*$TIME.avi skpsun@163.com

当然,在能成功自动发送邮件前,先要确认是否正确安装配置了mailx和ssmtp。否则,需要先安装这两个程序。

# apt-get install mailx ssmtp

然后在/etc/ssmtp/ssmtp.conf里如下进行配置:

In /etc/ssmtp/ssmtp.conf, set the following:

root=youraccount@gmail.com

mailhub=smtp.gmail.com:587

rewriteDomain=

hostname=smtp.gmail.com:587

UseSTARTTLS=YES

AuthUser=youraccount

AuthPass=yourpasword

FromLineOverride=YES # optional

In /etc/ssmtp/revaliases:

root:youraccount@gmail.com:smtp.gmail.com:587

mainuser:youraccount@gmail.com:smtp.gmail.com:587

并在命令行里输入

# mail youraccount@gmail.com进行测试,写完邮件正文后按ctrl+D发送,详见

http://www.kedy.cn/show-378-1.html

 监控到清洁工大妈的视频截图

Linux下的motion detection(最简单的办公室监控系统) - freetrain_sk - sk

 

 

Reference webpages:

CLI Magic: Getting into Motion: http://www.linux.com/feature/114118

HOWTO Gmail and sSMTP: http://www.kedy.cn/show-378-1.html

send email with attachment: http://logjiang.itpub.net/post/38081/473118

Motion external commads: http://www.lavrsen.dk/twiki/bin/view/Motion/ExternalCommands

linux中怎么才能获得下一秒的时间:http://www.9php.com/FAQ/cxsjl/shell/2009/01/3120364137960.html

linux date 命令详解: http://www.admin99.net/read.php/188.htm

linux下date命令用法: http://pds911.itpub.net/post/782/454526

shell 十三問?: http://bbs.chinaunix.net/viewthread.php?tid=218853

[精华] 跟我一起写 Makefile: http://www.chinaunix.net/jh/23/408225.html

http://www.stlchina.org/twiki/bin/view.pl/ScriptProgram/LearnMakefile

 

原创粉丝点击