MacFeeling Blog

マックな感じ …

Raspberry Piセットアップ(motion編)Stretch版

公開 : | 0件のコメント

この記事は最終更新日から 約2年が経過しているので情報が古い可能性があります。

Raspberry PiにUSBカメラを接続して、motionで 動体検知対応のカメラとして機能させるまでの備忘録
今回はmotion編

(ターミナルのログは所々Jessieの時のものを使っています...)

環境は
USBカメラとの相性で、OS「Raspbian」はちょっと古いですがStretch版を利用
macOS Monterey 12.1
すべてマックのターミナル.appを使っての操作です。

利用するUSB接続のウェブカメラはロジクールのHD Webcam C270

Raspberry Piに利用するウェブカメラをUSB接続します。

Raspberry Piを起動したら、Macのターミナルを起動してssh接続をします。

ウェブカメラが認識されているか確認します。(入力するのは「」の中)
lsusb」と入力してリターンキー

Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
のように表示されていればOKです。

$ lsusb
Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
Bus 001 Device 004: ID 0781:5571 SanDisk Corp. Cruzer Fit
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. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

パッケージのリストを更新
sudo apt-get update」と入力してリターンキー

パッケージの更新
sudo apt-get upgrade」と入力してリターンキー

途中で、Do you want to continue? [Y/n] と聞かれたら
Y」と入力してリターンキー

環境によっては、終了するまで時間がかかります。

motionのインストール
sudo apt-get install motion」と入力してリターンキー

途中で、Do you want to continue? [Y/n] と聞かれたら
Y」と入力してリターンキー

インストールが終了したら、設定ファイルの編集です。
まずはブラウザで表示できるかどうかを確認するための必要最低限の設定を行います。

sudo nano /etc/motion/motion.conf」と入力してリターンキー

とりあえず以下の5箇所を右の値に変更

norm 0 → 1
width 320 → 640(ご利用のウェブカムによります)
height 240 → 480(ご利用のウェブカムによります)
stream_maxrate 1 → 30(ストリーミング配信のフレームレート設定。ご利用のウェブカムによります)
stream_localhost on → off (ブラウザで表示するため)

問題なければ、「controlキー + o
File Name to Writeと下に表示されたらリターンキー
controlキー + x」で編集を終了します。

motionを起動
sudo motion -n」と入力してリターンキー

ブラウザ(Safari)で、以下の形式でアクセスできます。
http://{RaspberryPiのIPアドレス}:8081
例の場合だと
http://192.168.0.123:8081

確認できたら、motionを終了します。
controlキー + c」で終了します

[1] [NTC] [STR] stream_stop: Closing motion-stream listen socket & active motion-stream sockets
[1] [NTC] [STR] stream_stop: Closed motion-stream listen socket & active motion-stream sockets
[1] [NTC] [VID] vid_close: Closing video device /dev/video0
[0] [NTC] [ALL] main: Threads finished
[0] [NTC] [ALL] main: Motion terminating

あとは、期待通りに動作するまで、設定ファイルの編集 » motionの起動 » ブラウザで確認 » motion停止の繰り返しになります。

設定した項目

###########################################################
# Capture device options
############################################################
norm 1
width 640
height 480

フレームレート、1秒間に撮影する枚数(これはストリーミングのではない)
framerate 4

############################################################
# Motion Detection Settings:
############################################################
検知の閾値(動体検知のしきい値。このピクセル数に変化が生じた場合撮影を行う)
threshold 500

検知する範囲を指定するマスク画像を作成して指定
mask_file /home/pi/Pictures/motion_mask.pgm

動体検知

動体検知の閾値を色々と変えてみたけど、なかなか上手くいかない
検知する範囲を指定できないものかと、設定ファイルを眺めて見ると

area_detectとmask_fileがなんとなく該当する感じ

area_detectは
1 2 3
4 5 6
7 8 9
とエリアを9分割で指定するタイプ
簡単だけど、これ以上は細かく設定はできない

mask_fileは検知する範囲と、除外する範囲のマスク画像を作成して指定するもの

これが良さそうなので、早速、動体検知で作成された画像の1枚をAffinity Photoで開いて、
検知する範囲を白、除外する範囲を黒に塗りつぶして保存(ここではmotion_mask.jpg という名前に)

ただし、設定する画像はPGM形式が必要ということで、変換するにはどうするのかとググってみたところ、MAC PGM 変換。オンライン フリー — Convertioのページにて、オンラインでPGMに変換できるということで、このサービスを利用。

出来上がったPGM形式(ここでは motion_mask.pgm という名前)のファイルをRaspberry Piにアップ。
すでに公開鍵認証でssh接続の設定をしているので

Raspberry Piセットアップ(公開鍵認証編) | MacFeeling Blog

今回は/home/pi/Picturesにアップするので
無い場合は事前に、Raspberry Piにssh接続してPicturesディレクトを作成しておく必要があります。
念のため
「cd」と入力してリターンキー
「ls」と入力してリターンキー

Picturesが無ければ
「mkdir Pictures」と入力してリターンキー

「ls」と入力してリターンキー
Picturesが作成されていることを確認する

形式は
sup -P [ポート番号] -i [秘密鍵id_rsaのフルパス] [アップするmotion_mask.pgmのフルパス] [pi@Raspberry PiのIPアドレス:アップデート先のフルパス]

例では
ポート番号は55555
秘密鍵id_rsaのフルパス:/Users/ユーザ名/.ssh/id_rsa ユーザ名を仮にhogehogeとする
アップするmotion_mask.pgmのフルパス(ピクチャフォルダにあるとすると):/Users/ユーザ名/Pictures/motion_mask.pgm
Raspberry Piの固定したIPアドレスは192.168.0.123
アップデート先のフルパス:/home/pi/Pictures/

とすると

scp -P 55555 -i /Users/hogehoge/.ssh/id_rsa /Users/hogehoge/Pictures/motion_mask.pgm pi@192.168.0.123:/home/pi/Pictures」と入力してリターンキー

motion_mask.pgm 100% 46KB 45.8KB/s 00:00

となればファイルのアップロードは完了

検知した後の動作を停止する秒数?らしい
event_gap 3

############################################################
# Image File Output
############################################################

動きが検出された場合の静止画像の保存をする
output_pictures on

############################################################
# FFMPEG related options
# Film (movies) file output, and deinterlacing of the video input
# The options movie_filename and timelapse_filename are also used
# by the ffmpeg feature
############################################################

# Use ffmpeg to encode movies in realtime (default: off)
ffmpeg_output_movies off

############################################################
# Text Display
# %Y = year, %m = month, %d = date,
# %H = hour, %M = minute, %S = second, %T = HH:MM:SS,
# %v = event, %q = frame number, %t = thread (camera) number,
# %D = changed pixels, %N = noise level, \n = new line,
# %i and %J = width and height of motion area,
# %K and %L = X and Y coordinates of motion center
# %C = value defined by text_event - do not use with text_event!
# You can put quotation marks around the text to allow
# leading spaces
############################################################
動体検知した物体を四角で囲む
locate_motion_mode on

表示されるタイムコードの文字を大きくする
text_double on

############################################################
# Target Directories and filenames For Images And Films
# For the options snapshot_, picture_, movie_ and timelapse_filename
# you can use conversion specifiers
# %Y = year, %m = month, %d = date,
# %H = hour, %M = minute, %S = second,
# %v = event, %q = frame number, %t = thread (camera) number,
# %D = changed pixels, %N = noise level,
# %i and %J = width and height of motion area,
# %K and %L = X and Y coordinates of motion center
# %C = value defined by text_event
# Quotation marks round string are allowed.
############################################################

静止画の保存場所
target_dir /mnt/usb1/motion

事前に外付けUSBメモリのマウント先/mnt/usb1/にmotionディレクトリを作成しておく

/mnt/usb1/に移動
cd /mnt/usb1/」と入力してリターンキー

motionディレクトリを作成
sudo mkdir motion」と入力してリターンキー

所有者、所属グループの変更
sudo chown pi:pi motion/」と入力してリターンキー

静止画の保存ファイル名
picture_filename %Y%m%d%H%M%S-%q

############################################################
# Live Stream Server
############################################################
ストリーミング配信のフレームレート
stream_maxrate 30

ストリーミング配信画面へのアクセスを許可
stream_localhost off

############################################################
# HTTP Based Control
############################################################
設定画面へのアクセスを許可
webcontrol_localhost off

ある程度固まったら、本格運用を開始します。
設定ファイルの編集
sudo nano /etc/motion/motion.conf」と入力してリターンキー

デーモンでの起動をオンにします。
以下の箇所を右の値に変更
daemon off → on

############################################################
# Daemon
############################################################

# Start in daemon (background) mode and release terminal (default: off)
daemon on

問題なければ、「controlキー + o
File Name to Writeと下に表示されたらリターンキー
controlキー + x」で編集を終了します。

/etc/default/motionを編集
sudo nano /etc/default/motion」と入力してリターンキー

start_motion_daemon=no → yes に変更。

# set to 'yes' to enable the motion daemon
start_motion_daemon=yes

問題なければ、「controlキー + o
File Name to Writeと下に表示されたらリターンキー
controlキー + x」で編集を終了します。

/etc/init.d/motionを編集
sudo nano /etc/init.d/motion」と入力してリターンキー

「--chuid motion」の部分を削除

if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then

の部分を

if start-stop-daemon --start --oknodo --exec $DAEMON -b ; then

に変更

問題なければ、「controlキー + o
File Name to Writeと下に表示されたらリターンキー
controlキー + x」で編集を終了します。

起動時に自動的にサービスを開始するために
sudo insserv motion」と入力してリターンキー

insservがない場合はインストールする
sudo apt-get install insserv」と入力してリターンキー

motionを起動
sudo motion -c /etc/motion/motion.conf」と入力してリターンキー

$ sudo motion -c /etc/motion/motion.conf 
[0] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0] [ALR] [ALL] conf_cmdparse: Unknown config option "sdl_threadnr"
[0] [NTC] [ALL] motion_startup: Motion 3.2.12+git20140228 Started
[0] [NTC] [ALL] motion_startup: Logging to syslog
[0] [NTC] [ALL] motion_startup: Using log type (ALL) log level (NTC)
[0] [NTC] [ALL] become_daemon: Motion going to daemon mode

補足
motionの起動(止まっている場合)
sudo motion -c /etc/motion/motion.conf

motionの再起動(動いている場合)
sudo service motion restart

motionの停止
sudo service motion stop

もし上記コマンドを実行して
Warning: Unit file of motion.service changed on disk, 'systemctl daemon-reload' recommended.

と言われたら

sudo systemctl daemon-reload

を実行した後に、もう一度やり直してください。

Raspberry Piの終了について
sudo shutdown -h now」と入力してリターンキー

緑色のランプが何度か点滅して、赤色のランプのみの点灯になれば、コンセントから抜いて大丈夫です。

コメントを残す

必須欄は * がついています




日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)