【アドベントカレンダー】 Raspberry pi + Webカメラで自宅警備
はじめに
10月アドベントカレンダーの5日目に記事を書くことになりました。小林です。
前回のアドベントカレンダーの記事で書いた Raspberry pi[Ubuntu]でファイル鯖[Samba]を構築 と関連したものになります。
本記事だけでも実際に運用する分には特に問題はありませんが、他のサービスと連携することでより一層の便利なものになります。
ぜひとも、前回の記事を一読していただいてから本記事を読むことを推奨します。
目次
- はじめに
- 対象者
- 構成
- 構築
- Samba と連携
- さいごに
- 参照
対象者
- お手軽に自宅警備をしたい
- Samba で共有するファイルがなくて困ってる
構成
- Raspberry pi 3 B+ (Ubuntu 20.01)
- SDカード (32GB)
- USBカメラ
構築
必要なパッケージのインストール
> apt install autoconf automake autopoint build-essential pkgconf libtool libzip-dev libjpeg-dev git libavformat-dev \
libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev libwebp-dev gettext libmicrohttpd-dev
ユーザ追加
> useradd -m motion-user
パッケージのダウンロード
> cd motion-user
> git clone https://github.com/Motion-Project/motion.git
インストール
> cd motion
> autoreconf -fiv
> ./configure
> make && make install
バージョン確認
> motion -h
motion Version 4.3.1+git20210818-d670829, Copyright 2000-2021 Jeroen Vreeken/Folkert van Heusden/Kenneth Lavrsen/Motion-Project maintainers
設定ファイル編集
今回インストールしたバージョンで用意されている motion-dist.conf は、設定項目の大半が省略されているため使用しません。
motion.conf を作成し、以下のデフォルト設定を張り付けます。
# /usr/local/etc/motion/motion.conf
#
# This config file was generated by motion 4.3.1+git20210818-d670829
# at 2021-09-23T18:11:28
############################################################
# System control configuration parameters
############################################################
# Start in daemon (background) mode and release terminal.
daemon off
# Start in Setup-Mode, daemon disabled.
setup_mode off
# File to store the process ID.
; pid_file value
# File to write logs messages into. If not defined stderr and syslog is used.
; log_file value
# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6
# Filter to log messages by type (COR, STR, ENC, NET, DBL, EVT, TRK, VID, ALL).
log_type ALL
# Do not sound beeps when detecting motion
quiet on
# Native language support.
native_language on
# Watchdog timeout.
watchdog_tmo 30
# Watchdog kill.
watchdog_kill 10
# User defined name for the camera.
; camera_name value
# Numeric identifier for the camera.
camera_id 0
# Target directory for pictures, snapshots and movies
target_dir .
# Video device (e.g. /dev/video0) to be used for capturing.
video_device /dev/video0
# Parameters to control video device. See motion_guide.html
; video_params value
# The Motion method to use to change the brightness/exposure on video device.
auto_brightness 0
# Device name (e.g. /dev/tuner0) to be used for capturing when using tuner as source
; tuner_device value
# Number of frames to capture in each roundrobin step
roundrobin_frames 1
# Number of frames to skip before each roundrobin step
roundrobin_skip 1
# Try to filter out noise generated by roundrobin
roundrobin_switchfilter off
# The full URL of the network camera stream.
; netcam_url value
# The parameters for the network camera.
; netcam_params value
# Optional high resolution URL for rtsp/rtmp cameras only.
; netcam_high_url value
# The parameters for the high resolution network camera.
; netcam_high_params value
# Username and password for network camera. Syntax username:password
; netcam_userpass value
# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value
# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_params value
############################################################
# Image Processing configuration parameters
############################################################
# Image width in pixels.
width 640
# Image height in pixels.
height 480
# Maximum number of frames to be captured per second.
framerate 15
# Minimum time in seconds between capturing picture frames from the camera.
minimum_frame_time 0
# Number of degrees to rotate image.
rotate 0
# Flip image over a given axis
flip_axis none
# Draw a locate box around the moving object.
locate_motion_mode off
# Set the look and style of the locate box.
locate_motion_style box
# Text to be overlayed in the lower left corner of images
text_left CAMERA1
# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q
# Overlay number of changed pixels in upper right corner of images.
text_changes off
# Scale factor for text overlayed on images.
text_scale 1
# The special event conversion specifier %C
text_event %Y%m%d%H%M%S
############################################################
# Motion detection configuration parameters
############################################################
# Always save pictures and movies even if there was no motion.
emulate_motion off
# pause motion detection.
pause off
# Threshold for number of changed pixels that triggers motion.
threshold 1500
# The maximum threshold for number of changed pixels that triggers motion.
threshold_maximum 0
# Enable tuning of the threshold down if possible.
threshold_tune off
# Noise threshold for the motion detection.
noise_level 32
# Automatically tune the noise threshold
noise_tune on
# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl
# Area number used to trigger the on_area_detected script.
; area_detect value
# Full path and file name for motion detection mask PGM file.
; mask_file value
# Full path and file name for privacy mask PGM file.
; mask_privacy value
# The value defining how slow or fast the smart motion mask created and used.
smart_mask_speed 0
# Percentage of image that triggers a lightswitch detected.
lightswitch_percent 0
# When lightswitch is detected, ignore this many frames
lightswitch_frames 5
# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1
# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 60
# The number of pre-captured (buffered) pictures from before motion.
pre_capture 3
# Number of frames to capture after motion is no longer detected.
post_capture 0
############################################################
# Script execution configuration parameters
############################################################
# Command to be executed when an event starts.
; on_event_start value
# Command to be executed when an event ends.
; on_event_end value
# Command to be executed when a picture is saved.
; on_picture_save value
# Command to be executed when motion in a predefined area is detected
; on_area_detected value
# Command to be executed when motion is detected
; on_motion_detected value
# Command to be executed when a movie file is created.
; on_movie_start value
# Command to be executed when a movie file is closed.
; on_movie_end value
# Command to be executed when a camera can't be opened or if it is lost
; on_camera_lost value
# Command to be executed when a camera that was lost has been found.
; on_camera_found value
############################################################
# Picture output configuration parameters
############################################################
# Output pictures when motion is detected
picture_output off
# Output pictures with only the pixels moving object (ghost images)
picture_output_motion off
# Format for the output pictures.
picture_type jpeg
# The quality (in percent) to be used in the picture compression
picture_quality 75
# Text to include in a JPEG EXIF comment
; picture_exif value
# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q
############################################################
# Snapshot output configuration parameters
############################################################
# Make automated snapshot every N seconds
snapshot_interval 0
# File name(without extension) for snapshots relative to target directory
snapshot_filename %v-%Y%m%d%H%M%S-snapshot
############################################################
# Movie output configuration parameters
############################################################
# Create movies of motion events.
movie_output on
# Create movies of moving pixels of motion events.
movie_output_motion off
# Maximum length of movie in seconds.
movie_max_time 60
# The fixed bitrate to be used by the movie encoder. Ignore quality setting
movie_bps 400000
# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45
# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv
# Duplicate frames to achieve "framerate" fps.
movie_duplicate_frames off
# Pass through from the camera to the movie without decode/encoding.
movie_passthrough off
# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S
# Use pipe and external encoder for creating movies.
movie_extpipe_use off
# Full path and options for external encoder of movies from raw images
; movie_extpipe value
############################################################
# Timelapse output configuration parameters
############################################################
# Interval in seconds between timelapse captures.
timelapse_interval 0
# Timelapse file rollover mode. See motion_guide.html for options and uses.
timelapse_mode daily
# Frame rate for timelapse playback
timelapse_fps 30
# Container/Codec for timelapse movie.
timelapse_codec mpg
# File name(without extension) for timelapse movies relative to target directory
timelapse_filename %Y%m%d-timelapse
############################################################
# Loopback pipe configuration parameters
############################################################
# v4l2 loopback device to receive normal images
; video_pipe value
# v4l2 loopback device to receive motion images
; video_pipe_motion value
############################################################
# Webcontrol configuration parameters
############################################################
# Port number used for the webcontrol.
webcontrol_port 8080
# Enable IPv6 addresses.
webcontrol_ipv6 off
# Restrict webcontrol connections to the localhost.
webcontrol_localhost on
# Type of configuration options to allow via the webcontrol.
webcontrol_parms 0
# Method that webcontrol should use for interface with user.
webcontrol_interface 0
# The authentication method for the webcontrol
webcontrol_auth_method 0
# Authentication string for the webcontrol. Syntax username:password
; webcontrol_authentication value
# Use ssl / tls for the webcontrol
webcontrol_tls off
# Full path and file name of the certificate file for tls
; webcontrol_cert value
# Full path and file name of the key file for tls
; webcontrol_key value
# The header parameters for webcontrol
; webcontrol_header_params value
# The number of minutes to lock out a IP for failed authentication attempts
webcontrol_lock_minutes 5
# The number failed authentication attempts before locking out a IP address
webcontrol_lock_attempts 5
# The maximum number of IPs to track at once for locking out.
webcontrol_lock_max_ips 25
############################################################
# Live stream configuration parameters
############################################################
# The port number for the live stream.
stream_port 8081
# Restrict stream connections to the localhost.
stream_localhost on
# Authentication method for live stream.
stream_auth_method 0
# The authentication string for the stream. Syntax username:password
; stream_authentication value
# Use ssl / tls for stream.
stream_tls off
# The header parameters for the stream
; stream_header_params value
# Percentage to scale the stream image on the webcontrol.
stream_preview_scale 25
# Have the stream image start on a new line of the webcontrol
stream_preview_newline off
# Method for showing stream on webcontrol.
stream_preview_method 0
# Quality of the jpeg images produced for stream.
stream_quality 50
# Provide the stream images in black and white
stream_grey off
# Output frames at 1 fps when no motion is detected.
stream_motion off
# Maximum framerate of images provided for stream
stream_maxrate 1
# Limit the number of images per connection
stream_limit 0
############################################################
# Database and SQL Configuration parameters
############################################################
# The type of database being used if any.
; database_type value
# Database name to use. For sqlite3, the full path and name.
; database_dbname value
# The host on which the database is located
database_host localhost
# Port used by the database.
database_port 0
# User account name for database.
; database_user value
# User password for database.
; database_password value
# Database wait for unlock time
database_busy_timeout 0
# Log to the database when creating motion triggered image file
sql_log_picture off
# Log to the database when creating a snapshot image file
sql_log_snapshot off
# Log to the database when creating motion triggered movie file
sql_log_movie off
# Log to the database when creating timelapse movie file
sql_log_timelapse off
# SQL query at event start. See motion_guide.html
; sql_query_start value
# SQL query at event stop. See motion_guide.html
; sql_query_stop value
# SQL query string that is sent to the database. See motion_guide.html
; sql_query value
############################################################
# Tracking configuration parameters
############################################################
# Method used by tracking camera. See motion_guide.html
track_type 0
# Enable auto tracking
track_auto off
# Serial port of motor
; track_port value
# Motor number for x-axis
track_motorx 0
# Set motorx reverse
track_motorx_reverse off
# Motor number for y-axis
track_motory 0
# Set motory reverse
track_motory_reverse off
# Maximum value on x-axis
track_maxx 0
# Minimum value on x-axis
track_minx 0
# Maximum value on y-axis
track_maxy 0
# Minimum value on y-axis
track_miny 0
# Center value on x-axis
track_homex 128
# Center value on y-axis
track_homey 128
# ID of an iomojo camera if used
track_iomojo_id 0
# Angle in degrees the camera moves per step on the X-axis with auto-track
track_step_angle_x 10
# Angle in degrees the camera moves per step on the Y-axis with auto-track.
track_step_angle_y 10
# Delay to wait for after tracking movement as number of picture frames.
track_move_wait 10
# Speed to set the motor to (stepper motor option)
track_speed 255
# Number of steps to make (stepper motor option)
track_stepsize 40
# Command to execute to move a camera in generic tracking mode
; track_generic_move value
##############################################################
# Camera config files - One for each camera.
##############################################################
; camera /usr/local/etc/motion/camera1.conf
##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/local/etc/motion/conf.d
今回の設定変更項目以外は省略します。設定項目を削除するというわけではありません。
(/usr/local/etc/motion/motion.conf)
# /usr/local/etc/motion/motion.conf
#
# This config file was generated by motion 4.3.1+git20210818-d670829
# at 2021-09-23T18:11:28
############################################################
# System control configuration parameters
############################################################
### バックグラウンドで起動することを許可する設定
# Start in daemon (background) mode and release terminal.
daemon on
### 動作を検知した際に保存するデータのディレクトリパスを設定
# Target directory for pictures, snapshots and movies
; target_dir .
############################################################
# Picture output configuration parameters
############################################################
### 動作を検知した際に画像を保存する設定
# Output pictures when motion is detected
picture_output on
############################################################
# Movie output configuration parameters
############################################################
### 動作を検知した際に動画を保存する設定
# Create movies of motion events.
movie_output off
############################################################
# Live stream configuration parameters
############################################################
### ライブ映像を外部から閲覧する許可の設定
# Restrict stream connections to the localhost.
stream_localhost off
Samba と連携
以前の記事で書いた Raspberry pi[Ubuntu]でファイル鯖[Samba]を構築 の内容が完了できていることが前提になります。
motion で保存したデータを Samba でファイル共有することで外部から確認するために、データを保存するディレクトリパスを変更します。
今回は例として /home/share01 の直下に保存していますが、実際の運用ではディレクトリを分割するのが好ましいです。
(/usr/local/etc/motion/motion.conf)
# /usr/local/etc/motion/motion.conf
#
# This config file was generated by motion 4.3.1+git20210818-d670829
# at 2021-09-23T18:11:28
############################################################
# System control configuration parameters
############################################################
### 動作を検知した際に保存するデータのディレクトリパスを設定
# Target directory for pictures, snapshots and movies
target_dir /home/share01
さいごに
今回は、自宅の監視をするために motion を使用して画像を保存し、Samba で外部から確認できるようにしました。
しかし、保存されるデータはサービスが起動している間は蓄積を続けるため容量がいつか限界を迎えます。
そのため各自で保存したデータを管理する工夫が必要になります。かと言って、手で毎回消去したりするのは面倒です。そんな時は、スクリプトで容量管理することが最も綺麗で簡単です。スクリプトを書くことが必要になるので、少し難しいかもしれませんが、頑張ってみてください。
それでは、次のアドベントカレンダーでお会いしましょう!
ディスカッション
コメント一覧
まだ、コメントがありません