Video suveillance sous Bodhi Linux

Prérequis

Bien sûr nous avons besoin d’une ou plusieurs caméra (USB ou Wifi).
- Ma webcam est une Logitech QuickCam for Notebooks (la Logitech QuickCam Chat refuse de focntionner chez moi !).
- Je n’avais pas de caméra Wifi sous la main, aussi j’ai installé IP Webcam sur mon téléphone Wildfire S / Android.

Pour mon P4 2GHz (1.2 Go de RAM et 40 Go de disque dur) j’ai choisi la distribution Bodhi Linux :
- Bodhi Linux est une distribution GNU/Linux basée sur Ubuntu LTS et utilisant le gestionnaire Enlightenment ;
- C’est donc une distribution légère demandant peu de ressources (128 Mo de RAM, 1.5 Go d’espace disque et un processeur cadencé à 300MHz suffisent) mais très « eye candy ».
- A noter, que Bodhi Linux 2.2 bien que basée sur Ubuntu Precise Pangolin (12.04 LTS), dont les logiciels deviennent rapidement désués, veille à fournir les dernières versions stables des logiciels.
- L’idée de Bodhi Linux est donc de proposer un système de base avec un bureau simple (e17), un gestionnaire de fichier (EFM), un navigateur internet (Midori), un terminal (Terminology) et de laisser le choix des applications aux utilisateurs. Tous ce qui nous faut ;-)

Installation

Nous allons utiliser motion, un programme spécifiquement conçu pour détecter les mouvements en analysant les flux d’images.

Nous auront aussi besoin de sendemail et wput pour nos besoins de notification.

# aptitude install motion sendemail wput

Webcam USB

La configuration de base (pour une webcam) passe par le fichier motion.conf ; Ce fichier contient une palanquée d’options, j’ai listé ci-dessous celles que j’ai changé pour ma webcam USB (le fichier est très bien documenté, je vous laisse tester les autres réglages...) :

# vim /etc/motion/motion.conf

# Videodevice to be used for capturing  (default /dev/video0)
# for FreeBSD default is /dev/bktr0
videodevice /dev/video0

# The video input to be used (default: 8)
# Should normally be set to 0 or 1 for video/TV cards, and 8 for USB cameras
input 8

# Image width (pixels). Valid range: Camera dependent, default: 352
width 320

# Image height (pixels). Valid range: Camera dependent, default: 288
height 240

# Maximum number of frames to be captured per second.  
# Valid range: 2-100. Default: 100 (almost no limit).  
framerate 2

# Threshold for number of changed pixels in an image that
# triggers motion detection (default: 1500)
threshold 1500

# Number of frames to capture after motion is no longer detected (default: 0)  
post_capture 5

# Gap is the seconds of no motion detection that triggers the end of an event
# An event is defined as a series of motion images taken within a short timeframe.
# Recommended value is 60 seconds (Default). The value 0 is allowed and disables
# events causing all Motion to be written to one single mpeg file and no pre_capture.
gap 5

# Use ffmpeg to encode mpeg movies in realtime (default: off)  
ffmpeg_cap_new on

# Use ffmpeg to make movies with only the pixels moving
# object (ghost images) (default: off)
ffmpeg_cap_motion off

# Codec to used by ffmpeg for the video compression.
# Timelapse mpegs are always made in mpeg1 format independent from this option.
# Supported formats are: mpeg1 (ffmpeg-0.4.8 only), mpeg4 (default), and msmpeg4.
# mpeg1 - gives you files with extension .mpg
# mpeg4 or msmpeg4 - gives you files with extension .avi
# msmpeg4 is recommended for use with Windows Media Player because
# it requires no installation of codec on the Windows client.
# swf - gives you a flash film with extension .swf
# flv - gives you a flash video with extension .flv
# ffv1 - FF video codec 1 for Lossless Encoding ( experimental )
# mov - QuickTime ( testing )
ffmpeg_video_codec msmpeg4

# Locate and draw a box around the moving object.
# Valid values: on, off and preview (default: off)
# Set to 'preview' will only draw a box in preview_shot pictures.
locate on

# Draws the timestamp using same options as C function strftime(3)
# Default: %Y-%m-%d\n%T = date in ISO format and time in 24 hour clock
# Text is placed in lower right corner
text_right %Y-%m-%d\n%T-%q

# Draw a user defined text on the images using same options as C function strftime(3)
# Default: Not defined = no text
# Text is placed in lower left corner
text_left Camera 1

# Target base directory for pictures and films  
# Recommended to use absolute path. (Default: current working directory)  
target_dir /tmp/motion

# File path for snapshots (jpeg or ppm) relative to target_dir
# Default: %v-%Y%m%d%H%M%S-snapshot
# Default value is equivalent to legacy oldlayout option
# For Motion 3.0 compatible mode choose: %Y/%m/%d/%H/%M/%S-snapshot
# File extension .jpg or .ppm is automatically added so do not include this.
# Note: A symbolic link called lastsnap.jpg created in the target_dir will always
# point to the latest snapshot, unless snapshot_filename is exactly 'lastsnap'
snapshot_filename %t-%v-%Y%m%dT%H%M%S-snapshot

# File path for motion triggered images (jpeg or ppm) relative to target_dir
# Default: %v-%Y%m%d%H%M%S-%q
# Default value is equivalent to legacy oldlayout option
# For Motion 3.0 compatible mode choose: %Y/%m/%d/%H/%M/%S-%q
# File extension .jpg or .ppm is automatically added so do not include this
# Set to 'preview' together with best-preview feature enables special naming
# convention for preview shots. See motion guide for details
jpeg_filename %t-%v-%Y%m%dT%H%M%S-%q

# File path for motion triggered ffmpeg films (mpeg) relative to target_dir
# Default: %v-%Y%m%d%H%M%S
# Default value is equivalent to legacy oldlayout option
# For Motion 3.0 compatible mode choose: %Y/%m/%d/%H%M%S
# File extension .mpg or .avi is automatically added so do not include this
# This option was previously called ffmpeg_filename
movie_filename %t-%v-%Y%m%dT%H%M%S

# File path for timelapse mpegs relative to target_dir
# Default: %Y%m%d-timelapse
# Default value is near equivalent to legacy oldlayout option
# For Motion 3.0 compatible mode choose: %Y/%m/%d-timelapse
# File extension .mpg is automatically added so do not include this
timelapse_filename %Y%m%d-timelapse

# The mini-http server listens to this port for requests (default: 0 = disabled)
webcam_port 8081

Il suffit ensuite de démarrer motion : # motion -c /etc/motion/motion.conf

On peut vérifier que la webcam est bien prise en compte en regardant le flux vidéo sur http://localhost:8081

Ainsi tout mouvement détecté par la webcam produira des fichiers jpeg (ainsi que des vidéos, ici au format avi) dans le dossier /tmp/motion

Concernant les notifications, j’ai pris le parti d’envoyer un email dès lors qu’un mouvement est détecté, d’uploader les images sur un serveur ftp et d’envoyer aussi la vidéo de la caméra (dès qu’elle est prête) par mail :

# Command to be executed when an event starts. (default: none)  
# An event starts at first motion detected after a period of no motion defined by gap  
on_event_start sendemail -f motion@hostname.fr -t klnavarro@gmail.com -u "Motion detected on camera %t" -m "Movement has been detected on:
%Y-%m-%d %H:%M. The pictures have been uploaded to your FTP account."
-s smtp.sfr.fr

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)  
# The filename of the picture is appended as an argument for the command.  
on_picture_save wput --basename /tmp/motion/ %f
ftp://klnavarro:xxxxxxxx@ftpperso.free.fr/motion/

# Command to be executed when a movie file (.mpg|.avi) is created. (default: none)
# To give the filename as an argument to a command append it with %f
on_movie_start sendemail -f motion@compaq.fr -t klnavarro@gmail.com -u "Motion registered on camera %t" -m "Movement has been registered on: %Y-%m-%d %H:%M. The movie has been attached to this mail." -s smtp.sfr.fr -a %f

Caméra IP

Pour utiliser plus d’une caméra, on aura besoin d’utiliser les fichiers de configuration thread1.conf et thread2.conf. Dans le fichier /etc/motion/motion.conf activez les lignes :

# Remember: If you have more than one camera you must have one
# thread file for each camera. E.g. 2 cameras requires 3 files:
# This motion.conf file AND thread1.conf and thread2.conf.
# Only put the options that are unique to each camera in the
# thread config files.
thread /etc/motion/thread1.conf
thread /etc/motion/thread2.conf
; thread /etc/motion/thread3.conf
; thread /etc/motion/thread4.conf

Dans le premier fichier nous reporterons les réglages spécifiques à la Webcam USB et dans le second nos régalges pour la caméra IP :

# vim /etc/motion/thread1.conf

# Videodevice to be used for capturing  (default /dev/video0)
# for FreeBSD default is /dev/bktr0
videodevice /dev/video0

# The video input to be used (default: 8)
# Should normally be set to 1 for video/TV cards, and 8 for USB cameras
input 8

# Draw a user defined text on the images using same options as C function strftime(3)
# Default: Not defined = no text
# Text is placed in lower left corner
text_left Camera 1

# Target base directory for pictures and films
# Recommended to use absolute patch. (Default: current working directory)
target_dir /tmp/motion

... les autres paramètres sont repris du motion.conf.

L’outil IP Webcam fourni le flux vidéo de la caméra du téléphone sur http://192.168.1.67:8080/videofeed :

# vim /etc/motion/thread2.conf

# Videodevice to be used for capturing  (default /dev/video0)
# for FreeBSD default is /dev/bktr0
videodevice /dev/video1

# The video input to be used (default: 8)
# Should normally be set to 1 for video/TV cards, and 8 for USB cameras
input 1

# URL to use if you are using a network camera, size will be autodetected (incl http:// ftp:// or file:///)
# Must be a URL that returns single jpeg pictures or a raw mjpeg stream. Default: Not defined
netcam_url http://192.168.1.67:8080/videofeed

# Username and password for network camera (only if required). Default: not defined
# Syntax is user:password
; netcam_userpass value

# The setting for keep-alive of network socket, should improve performance on compatible net cameras.
# 1.0:         The historical implementation using HTTP/1.0, closing the socket after each http request.
# keep_alive:  Use HTTP/1.0 requests with keep alive header to reuse the same connection.
# 1.1:         Use HTTP/1.1 requests that support keep alive as default.
# Default: 1.0
; netcam_http 1.0

# Target base directory for pictures and films
# Recommended to use absolute patch. (Default: current working directory)
; target_dir /usr/local/apache2/htdocs/cam2
target_dir /tmp/motion

# Draw a user defined text on the images using same options as C function strftime(3)
# Default: Not defined = no text
# Text is placed in lower left corner
text_left Camera 2

Daemon

Par défaut, le daemon motion n’est pas activé / démarré.

Si l’on veut l’activer, il faut mettre à "Yes" la variable start_motion_daemon dans le fichier /etc/default/motion (sinon le placer dans /etc/rc.local).