Raspberry PiとUSBカメラで監視カメラを作る

スポンサーリンク

今回は、Raspberry PiとUSB接続タイプのWebカメラを使って、監視カメラシステムの構築にチャレンジします。

今回使用したPiは、Raspberry Pi 3 Model Bです。

Amazon.co.jp

OSはRaspberry Pi公式OS「Raspbian 10 Buster」です。

Raspberry Pi OS – Raspberry Pi
From industries large and small, to the kitchen table tinkerer, to the classroom coder, we make computing accessible and affordable for everybody.

使用したWebカメラは、BUFFALO製の広角Webカメラ BSW200MBKです。非常に画角が広く、監視カメラとしてぴったりです。

Amazon.co.jp

初期設定はすでに済んでいると仮定しています。済んでいない方はこちらへどうぞ。

早速WebカメラをRaspberry Piにつなぎます。

lsusb

lsusbコマンドで、現在Raspberry Piに接続されているUSBデバイスを表示できます。

$ lsusb
Bus 001 Device 004: ID 0b95:1790 ASIX Electronics Corp. AX88179 Gigabit Ethernet
Bus 001 Device 005: ID 0458:708c KYE Systems Corp. (Mouse Systems) Genius WideCam F100
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

KYE Systems Corp. (Mouse Systems) Genius WideCam F100とあるのが、今回のUSBカメラです。しっかりと認識されているようです。次に、

$ v4l2-ctl --list-devices
bcm2835-codec (platform:bcm2835-codec):
        /dev/video10
        /dev/video11
        /dev/video12

USB_Camera: USB_Camera (usb-3f980000.usb-1.5):
        /dev/video0
        /dev/video1

USBカメラが、/dev/video0として認識されていることがわかります。
今回用いるプログラムは、「motion」と呼ばれるプログラムです。定期的な写真撮影や、動体検知して撮影、リアルタイムストリーミングなどがこなせるシステムが構築できます。早速インストールします。

スポンサーリンク

motionの導入

sudo apt-get -y install motion

次に、/etc/motion/motion.confを編集します。かなり細やかな設定ができるようです。

sudo vi /etc/motion/motion.conf

直接変数を書き換えてもよいですが、あとからややこしくなるので、ファイル末尾に追記して上塗りする形で変更します。

################################
# MY SETTINGS
################################
# General
daemon off
norm 1
width 640
height 480
framerate 30
webcontrol_localhost off
stream_localhost off
text_double on

# Motion detect
threshold 3000
event_gap 60
output_pictures best
max_movie_time 300
ffmpeg_video_codec mp4

# Make automated snapshot every N seconds (default: 0 = disabled)
snapshot_interval 60
quality 100

# Motion box
locate_motion_mode off

# Streaming
stream_quality 100
stream_motion on
stream_maxrate 30
stream_preview_scale 100

# Save dir
target_dir /var/lib/motion
logfile /var/log/motion/motion.log

; webcontrol_authentication username:password

とくに、target_dirは自身で変更してください。このままではSDカードに負担をかけることになります。

sudo chmod 777 /var/lib/motion

target_dirの権限も編集しておきます。さらに/etc/motion/motion.confの権限も設定します。

sudo chmod 755 /etc/motion/motion.conf

ここで、一旦動作テストです。

sudo motion -n

でmotionを起動します。この状態で、http://[Raspberry PiのIP]:8081にアクセスし、うまく動作すれば成功です。

次回のRaspberry Piの起動時にmotionも自動起動する設定にするには、先程の/etc/motion/motion.confのdaemon offを、daemon onに変更し、さらに

sudo vi /etc/default/motion

の中の、

start_motion_daemon=no
↓
start_motion_daemon=yes

へ変更します。

自動削除スクリプト

motionには撮影した動画を削除する機能はないため、一定期間経過したファイルを探し、削除するスクリプトを作り、crontabに登録します。以下は一例です。ファイル名やユーザー名は修正してください。

vi /home/ctrluser/motion-autodelete.sh
#!/bin/sh

target_dir="/var/lib/motion"
# +:izyou mae,  -:yori atarashii
days="+2"

find ${target_dir} -type f -mtime ${days} -exec rm -f {} \;
chmod +x /home/ctrluser/motion-autodelete.sh
crontab -e
分 時 日 月 曜日 <実行コマンド>

0 4 * * * /home/ctrluser/motion-autodelete.sh

これで毎朝4時0分にスクリプトが実行されます。

参考サイト
https://qiita.com/tatsuki1112/items/1f69a286b48b2a06a956
https://damelog.com/computer/linux/linux-motion-webcam%E3%81%A7%E3%83%A9%E3%82%A4%E3%83%96%E7%9B%A3%E8%A6%96%E3%82%AB%E3%83%A1%E3%83%A9%E3%82%92%E8%A8%AD%E7%BD%AE%E3%81%99%E3%82%8B/
http://marublo.com/raspberry-pi-camera
https://qiita.com/nara256/items/e4dd4f36df449a0a82e2
https://raspberry.mirukome.com/raspberry-pi-camera/
https://qiita.com/westvirginia/items/ba79f7549b43da116467
https://wisdomtrees.net/?p=38
http://teppei.hateblo.jp/entry/2013/08/10/114147
https://ozuma.hatenablog.jp/entry/20120711/1342014448
https://qiita.com/hikouki/items/e744b3a4d356d2af12cf
https://centossrv.com/motion.shtml
https://www.ishikawa-lab.com/RasPi_camera.html
https://peta.okechan.net/blog/archives/513
http://safe-linux.homeip.net/web/motion/motion-03.html
https://blog.hello-world.jp.net/raspberrypi/1949/
https://uratouri6.github.io/2015/12/09/004_raspi-camera1/
http://gataro-avr-ken.cocolog-nifty.com/blog/2017/03/raspberrypimoti.html
http://gataro-avr-ken.cocolog-nifty.com/blog/2017/03/raspberrypi3-bd.html

コメント

タイトルとURLをコピーしました