Motionを使ったRaspberry Pi Zero監視カメラ(失敗)
Raspberry Pi Zeroに接続したカメラで監視カメラシステムを構築する方法
(結論: Raspberry Pi Zeroではストリーミングでのフレームレートが1FPS、SDカード上に記録された動画でも320×240で10FPS程度が限界と思っていたような性能がでませんでした。残念) -> omxmotionで性能を改善した記事はこちら
下準備
Raspberry Pi Zeroへのカメラモジュールの接続とraspivid
によるビデオキャプチャまでは別記事を参照
Motionのインストール
今回はMotionというプログラムで監視カメラシステムを構築してみます。Motionは動きを検出した前後の画像キャプチャやWebからの現在画像の確認などセキュリティカメラやベビーモニタを構築するために必要な機能を提供してくれます。
apt
で簡単にインストール可能です
% sudo apt install motion
Motionの設定
続いて必要な設定を行っていきます。今回はMotionプログラムの実行はコマンドラインから行い、サービスによる自動起動の設定は行っていません
モジュールの確認
bcm2835_v4l2
モジュールがロードされていることを確認します
% lsmod | grep bcm2835_v4l2 bcm2835_v4l2 45056 0
されていなければモジュールをロードします
% sudo modprobe bcm2835-v4l2
motion.conf設定
motion.conf
を設定します。
最初に接続されたカメラの仕様をv4l2-ctl
コマンドで確認します
% v4l2-ctl -V Format Video Capture: Width/Height : 1024/768 Pixel Format : 'JPEG' (JFIF JPEG) Field : None Bytes per Line : 0 Size Image : 786432 Colorspace : JPEG Transfer Function : Default (maps to sRGB) YCbCr/HSV Encoding: Default (maps to ITU-R 601) Quantization : Default (maps to Full Range) Flags :
/etc/motion/motion.conf
を直接編集してもいいですが、サービスとして起動する予定がないため、カレントユーザのホームディレクトリにmotion.confをコピーして使用します
% mkdir ~/.motion % cp /etc/motion/motion.conf .motion/
次にファイルを編集します
% nano .motion/motion.conf
--- /etc/motion/motion.conf +++ .motion/motion.conf @@ -22,7 +22,7 @@ # Use a file to save logs messages, if not defined stderr and syslog is used. (default: not defined) -logfile /var/log/motion/motion.log +logfile /home/pi/Motion/motion.log # Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL). (default: 6 / NTC) log_level 6 @@ -76,14 +76,14 @@ flip_axis none # Image width (pixels). Valid range: Camera dependent, default: 320 -width 320 +width 640 # Image height (pixels). Valid range: Camera dependent, default: 240 -height 240 +height 480 # Maximum number of frames to be captured per second. # Valid range: 2-100. Default: 100 (almost no limit). -framerate 2 +framerate 10 # Minimum time in seconds between capturing picture frames from the camera. # Default: 0 = disabled - the capture rate is given by the camera framerate. @@ -401,7 +401,7 @@ # Target base directory for pictures and films # Recommended to use absolute path. (Default: current working directory) -target_dir /var/lib/motion +target_dir /home/pi/Motion # File path for snapshots (jpeg, ppm or webp) relative to target_dir # Default: %v-%Y%m%d%H%M%S-snapshot @@ -458,7 +458,7 @@ stream_maxrate 1 # Restrict stream connections to localhost only (default: on) -stream_localhost on +stream_localhost off # Limits the number of images per connection (default: 0 = unlimited) # Number can be defined by multiplying actual stream rate by desired number of seconds
ストリームサーバ起動
コマンドラインから起動します
% motion -n
次にホストのブラウザからhttp://raspberrypi.local:8081
にアクセスして動画が見れることを確認します
性能問題
残念ながら自分の環境では満足な性能は得られませんでした。解像度は320x240
、フレームレートは10fps
程度が限界のように思います。Motionは結構CPUパワーが必要なようで、Pi 3やPi4ではもう少し性能がでると思いますが、Raspberry Pi Zeroでは実用には耐えれないようです
1件の返信
[…] 前回 Raspberry Pi ZeroにMotionを導入した際は性能不足で実用に耐えなかったですが、今回はハードウェアエンコードを有効化することで問題を解決します […]