LoginSignup
38
40

More than 5 years have passed since last update.

RaspberryPiでWebカメラ使って,人の動作感知してみた

Last updated at Posted at 2014-10-31

両親から、
「高齢の祖母がベッドから立ち上がった際に、それを感知してメールか何かで送れないかな?」
っていう相談受けました。
すでに、色々なメーカーから、そのような介護製品がでているのですが、
ちょうどいいと思い、RaspberryPiで動作センサを自作してみました。
(信頼性上、購入したメーカー製品が届くまでの繋ぎ程度の物ですけど)

Webカメラとmotion

RaspberryPiへのWebカメラ導入は以前にやっていました。

今回は、motionというパッケージを利用します。
Webカメラからの映像を監視して、何か動きがあれば検知するっていうソフトウェア。

参考:
[Raspberry Pi]motionでお手軽監視カメラをつくる
Motion の導入手順 ~ 指定した内容、パラメータの意味について

インストール

$ sudo apt-get install motion

設定ファイルを編集
今回は、映像を取ることではなく、検知によりメールを送信することが目的なので
画像、動画保存はしません(容量も取られますし)
動きが検知された際にメール送信のスクリプトを動かします。

$ sudo vi /etc/motion/motion.conf
motion.conf
# 略
daemon on          # RaspberryPi起動時のmotion起動設定
threshold 3000     # 検知の閾値,値が大きいほど鈍感になるみたいです。
output_normal off  #検知の際の画像保存設定
ffmpeg_cap_new off  #検知の際の動画保存設定
on_event_start /home/pi/sentmail.sh #検知が合った際に、実行されるシェルスクリプトを指定

メール送信用のスクリプト
motion.confではシェルスクリプト指定した方が楽なのかな?(未確認)
メール送信は、後々色々動作を追加するかもしれないので、Rubyで書きました。
なので、シェルスクリプトからRubyスクリプトを起動
参考:
RubyでGmailのSMTPサーバを使ってメールを送信する

sentmail.sh
ruby /home/pi/sentmail.rb
sentmail.rb
require 'mail'

mail = Mail.new

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => "smtp.gmail.com",
            :user_name            => '<username>@gmail.com',
            :password             => '<password>',
            :authentication       => :plain,
            :enable_starttls_auto => true  }        
mail.charset = 'utf-8'
mail.from "from@example.com"
mail.to "to@example.com"    
mail.subject "メールタイトル"
mail.body "メール本文"
mail.delivery_method(:smtp, options)
mail.deliver

動きました。

38
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
40