Vous êtes sur la page 1sur 9

100 Rue Duquet,

Sainte-Thérèse (Québec) J7E 3G6

Nom : ________________________________ GR : ______

Coé : ________________________________ Date : ___________________

LABORATOIRE 2
Vidéo analogique et transmission NTSC
Webcam avec Raspberry pi
Durée : 2 heures.

Objectifs :

 Installer une caméra sur Raspberry Pi


 Capturer des images
 Enregistrer des vidéos
 Installer la Webcam
 Accéder à la Webcam à distance

Matériels utilisés :
 Ordinateur
 Kit Raspberry Pi
 Caméra

Rédaction : mcossette, 13 septembre 2021 2019


Mise à jour : mbenmimoune, 13 septembre 2021
Laboratoire 2 Webcam avec Raspberry pi

Étape #1 Installation de caméra sur Raspberry Pi

Caméra pour Raspberry pi – Caractéristiques :


 Sony IMX219
 Résolution : 8 Megapixels
 Modes video : 1080p30, 720p60 and 640 × 480p60/90
 Intégration Linux: V4L2 driver
 Résolution : 3280 × 2464 pixels
 Zone d'image : 3.68 x 2.76 mm (4.6 mm diagonal)
 DSI (Display Serial Interface)

Interface de caméra : MIPI-Alliance (Mobile Industry Processor Interface)


CSI-2 (Camera Serial Interface Type 2)
Permet de connecter la caméra au Processeur Broadcom BCM2837

MIPI CSI-2 version 1.01 :


 Supporte 4 lignes de données mais seulement 2 sont disponibles pour le RaPi
 1 Gbps par ligne pour un maximum de 4 Gbps
 Lignes unidirectionnelles de la caméra vers le processeur

Mouncef Benmimoune Page 2 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

1 Ground
2 CAM1_DN0 Data lane Negative 0
3 CAM1_DP0 Data lane Positive 0
4 Ground
5 CAM1_DN1 Data lane Negative 1
6 CAM1_DP1 Data lane Positive 1
7 Ground
8 CAM1_CN Clock Negative – MCP (MIPI Clock Negative) broche de caméra
9 CAM1_CP Clock Positive – MCN (MIPI Clock Positive) broche de caméra
10 Ground
11 CAM_GPIO
12 CAM_CLK
13 SCL0 I²C Bus – Serial Clock Line
14 SDA0 I²C Bus – Serial Data line
15 +3.3 V Power Supply

Installation :

Câble :

pi@raspberrypi#sudo apt-get update


pi@raspberrypi#sudo apt-get upgrade
pi@raspberrypi#sudo raspi-config // Pour activer la caméra

Choisir : 5 Interfacing Options

Mouncef Benmimoune Page 3 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

Choisir : P1 Camera

Choisir : 5 Enable Camera

Choisir : Oui

Ensuite faire "Finish" et "Reboot"


pi@raspberrypi#sudo reboot

A ce moment-ci, vérifier que la caméra est bel et bien détectée:

root@raspberrypi:/home/pi# vcgencmd get_camera


supported=1 detected=1

Trois applications en ligne de commande sont maintenant disponibles:


 raspistill et raspistillyuv pour la capture des images
 raspivid pour la capture vidéo

Capture d'image :
 Capturer une image avec la commande en ligne raspistill
 Visualiser cette image dans le Raspberry pi. La capture est enregistrée dans le répertoire
/home/pi

pi@raspberrypi# raspistill // Commande pour prendre un snapshoot


pi@raspberrypi# raspistill -o image.jpg // Pour prendre un snapshoot, sauvegardé dans
/home/pi/image.jpg
pi@raspberrypi# cd
pi@raspberrypi# mkdir Camera
pi@raspberrypi# raspistill -o ~/Camera/image.jpg -q 100 // La photo sera sauvé dans le
répertoire /Camera

Mouncef Benmimoune Page 4 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

usage: raspistill [options]

Image parameter commands

-?, --help : This help information


-w, --width : Set image width <size>
-h, --height : Set image height <size>
-q, --quality : Set jpeg quality <0 to 100>
-r, --raw : Add raw bayer data to jpeg metadata
-o, --output : Output filename <filename> (to write to stdout, use '-o -'). No file saved if not specified
-l, --latest : Link latest complete image to filename <filename>
-v, --verbose : Output verbose information during run
-t, --timeout : Time (in ms) before takes picture and shuts down (if not specified, set to 5s)
-th, --thumb : Set thumbnail parameters (x:y:quality) or none
-d, --demo : Run a demo mode (cycle through range of camera options, no capture)
-e, --encoding : Encoding to use for output file (jpg, bmp, gif, png)
-x, --exif : EXIF tag to apply to captures (format as 'key=value') or none
-tl, --timelapse : Timelapse mode. Takes a picture every <t>ms. %d == frame number (Try: -o img_%04d.jpg)
-k, --keypress : Wait between captures for a ENTER, X then ENTER to exit
-s, --signal : Wait between captures for a SIGUSR1 or SIGUSR2 from another process
-g, --gl : Draw preview to texture instead of using video render component
-gc, --glcapture : Capture the GL frame-buffer instead of the camera image
-set, --settings : Retrieve camera settings and write to stdout
-cs, --camselect : Select camera <number>. Default 0
-bm, --burst : Enable 'burst capture mode'
-md, --mode : Force sensor mode. 0=auto. See docs for other modes available
Mode Size Aspect RatioFrame rates FOV Binning
0 sélection automatique
1 1920x1080 16:9 0.1-30fps Partial None
2 3280x2464 4:3 0.1-15fps Full None
3 3280x2464 4:3 0.1-15fps Full None
4 1640x1232 4:3 0.1-40fps Full 2x2
5 1640x922 16:9 0.1-40fps Full 2x2
6 1280x720 16:9 40-90fps Partial 2x2
7 640x480 4:3 40-90fps Partial 2x2
-dt, --datetime : Replace output pattern (%d) with DateTime (MonthDayHourMinSec)
-ts, --timestamp : Replace output pattern (%d) with unix timestamp (seconds since 1970)
-fs, --framestart : Starting frame number in output pattern(%d)
-rs, --restart : JPEG Restart interval (default of 0 for none)

Image parameter commands

-sh, --sharpness : Set image sharpness (-100 to 100)


-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness : Set image brightness (0 to 100)
-sa, --saturation : Set image saturation (-100 to 100)
-ISO, --ISO : Set capture ISO
-vs, --vstab : Turn on video stabilisation
-ev, --ev : Set EV compensation - steps of 1/6 stop
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb : Set AWB mode (see Notes)
-cfx, --colfx : Set colour effect (U:V)
-rot, --rotation : Set image rotation (0-359)
-hf, --hflip : Set horizontal flip
-vf, --vflip : Set vertical flip
-roi, --roi : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
-ss, --shutter : Set shutter speed in microseconds

Preview parameter commands

-p, --preview : Preview window settings <'x,y,w,h'>


-f, --fullscreen : Fullscreen preview mode
-op, --opacity : Preview window opacity (0-255)
-n, --nopreview : Do not display a preview window

Exemples :

raspistill -t 2000 -o image.jpg //capture image en résolution max par défaut après 2 sec
raspistill -t 2000 -o image.jpg -w 640 -h 480 //capture dans une autre résolution
raspistill -t 2000 -o image.jpg -q 5 //réduit la qualité de l'image
raspistill -t 2000 -o image.jpg -p 100,100,300,200 //affiche un préview
raspistill -t 2000 -o image.jpg –n //désactive l'affichage du préview
raspistill -t 2000 -o image.png –e png //sauve l'image en png

Mouncef Benmimoune Page 5 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

Capture vidéo :
 Capturer une vidéo avec la commande en ligne raspivid
 Visualiser cette vidéo. La capture est enregistrée dans le répertoire /home/pi

pi@raspberrypi# raspivid // Commande de capture vidéo


pi@raspberrypi# raspivid -o video.h264 -t 10000 // Pour prendre une vidéo qui dure 10000 ms
usage: raspivid [options]

Image parameter commands

-?, --help :
This help information
-w, --width :
Set image width <size>. Default 1920
-h, --height :
Set image height <size>. Default 1080
-b, --bitrate :
Set bitrate. Use bits per second (e.g. 10MBits/s would be -b 10000000)
-o, --output :
Output filename <filename> (to write to stdout, use '-o -').
Connect to a remote IPv4 host (e.g. tcp://192.168.1.2:1234, udp://192.168.1.2:1234)
To listen on a TCP port (IPv4) and wait for an incoming connection use -l
(e.g. raspivid -l -o tcp://0.0.0.0:3333 -> bind to all network interfaces, raspivid -l -o
tcp://192.168.1.1:3333 -> bind to a certain local IPv4)
-v, --verbose : Output verbose information during run
-t, --timeout : Time (in ms) to capture for. If not specified, set to 5s. Zero to disable
-d, --demo : Run a demo mode (cycle through range of camera options, no capture)
-fps, --framerate : Specify the frames per second to record
-e, --penc : Display preview image *after* encoding (shows compression artifacts)
-g, --intra : Specify the intra refresh period (key frame rate/GoP size). Zero to produce an initial I-frame
and then just P-frames.
-pf, --profile : Specify H264 profile to use for encoding
-td, --timed : Cycle between capture and pause. -cycle on,off where on is record time and off is pause time in
ms
-s, --signal : Cycle between capture and pause on Signal
-k, --keypress : Cycle between capture and pause on ENTER
-i, --initial : Initial state. Use 'record' or 'pause'. Default 'record'
-sg, --segment : Segment output file in to multiple files at specified interval <ms>
-sp, --split : In wait mode, create new output file for each start event
-c, --circular : Run encoded data through circular buffer until triggered then save
-x, --vectors : Output filename <filename> for inline motion vectors
-cs, --camselect : Select camera <number>. Default 0
-set, --settings : Retrieve camera settings and write to stdout
-md, --mode : Force sensor mode. 0=auto. See docs for other modes available
-pts, --save-pts : Save Timestamps to file for mkvmerge
-cd, --codec : Specify the codec to use - H264 (default) or MJPEG
-lev, --level : Specify H264 level to use for encoding
-r, --raw : Output filename <filename> for raw video
-rf, --raw-format : Specify output format for raw video. Default is yuv
-l, --listen : Listen on a TCP socket

Image parameter commands

-sh, --sharpness : Set image sharpness (-100 to 100)


-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness : Set image brightness (0 to 100)
-sa, --saturation : Set image saturation (-100 to 100)
-ISO, --ISO : Set capture ISO
-rot, --rotation : Set image rotation (0-359)
-hf, --hflip : Set horizontal flip
-vf, --vflip : Set vertical flip
-roi, --roi : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
-ss, --shutter : Set shutter speed in microseconds
-drc, --drc : Set DRC Level (see Notes)
-st, --stats : Force recomputation of statistics on stills capture pass
-a, --annotate : Enable/Set annotate flags or text
-3d, --stereo : Select stereoscopic mode

Exemples :
raspivid -t 5000 -o video.h264 //enregistre vidéo de 5 sec en 1080p30 par défaut
raspivid -t 5000 -o video.h264 -b 3500000 //enregistre vidéo de 5 sec taux de 3.5Mbits/s
raspivid -t 5000 -o video.h264 -fps 5 //enregistre vidéo de 5 sec à un taux de 5 fps
raspivid -t 5000 -o - //vidéo de 5 sec transmis à stdout
raspivid -t 5000 -o - > my_file.h264 //même et enregistre dans my_file.h264

Mouncef Benmimoune Page 6 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

Certains lecteurs vidéo ne sauront pas lire le format .h264. Pour cette raison, on peut installer la
librairie MP4Box qui permet de convertir les vidéos au format MP4. Pour installer MP4Box, il
suffit d'entrer cette commande :

pi@raspberrypi# sudo apt-get install -y gpac

Une fois installé, exécuter ceci pour convertir le fichier :

pi@raspberrypi# MP4Box -add video.h264 video.mp4

Le fichier vidéo est maintenant lisible sur tous les appareils.

Visualisation de la vidéo :

Pour visualiser la vidéo, plusieurs options sont possibles :

Première option : Installer le video viewer VLC

pi@raspberrypi:~ $ sudo apt-get install vlc


Note: on peut démarrer la visualisation de la vidéo avec VLC, mais on peut voir que le
raspberry pi n'est pas assez puissant pour décoder rapidement la vidéo dans le format H264. Ce
qui fait qu'on voit les 2 premières images et ensuite la vidéo reste bloquée sur la deuxième
image.

Deuxième option :
omxplayer -o hdmi video.h264 //Pour visualiser sur la sortie HDMI
omxplayer video.h264 //Pour visualiser sur la sortie composite

Connecter le câble HDMI du raspberry pi à un écran et visualiser la vidéo

Troisième option :

Transmettre video.h264 dans une boite de courriel, et la visualiser avec VLC dans Windows.

Mouncef Benmimoune Page 7 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

Quatrième option :

Cliquer l'icône illustré dans VNC et transférer le fichier vers le bureau de Windows

Visionner dans Windows.

Mouncef Benmimoune Page 8 Collège Lionel-Groulx


Laboratoire 2 Webcam avec Raspberry pi

Étape #2 Installation de Webcam

Installer motion :

pi@raspberrypi:~ $ sudo apt-get install motion


:
The following additional packages will be installed:
ffmpeg libavcodec57 libavdevice57 libavfilter6 libavformat57 libavresample3
libavutil55 libmariadbclient18 libpostproc54 libpq5 libsdl2-2.0-0
:
The following NEW packages will be installed:
ffmpeg libavdevice57 libmariadbclient18 libpq5 libsdl2-2.0-0
libwayland-cursor0 motion mysql-common
The following packages will be upgraded:
libavcodec57 libavfilter6 libavformat57 libavresample3 libavutil55
:
pi@raspberrypi:~ $ cd /etc/motion
pi@raspberrypi:~ $ sudo cp motion.conf motion.conf.bak
pi@raspberrypi:~ $ sudo nano /etc/motion/motion.conf et configurer les paramètres suivants :
daemon on //permet à motion de s'exécuter en tant que service
stream_localhost off //permet la visualisation du stream à partir d'autres ordinateurs
framerate 15
width 640
height 480
target_dir /var/lib/motion

Sauver les changements

pi@raspberrypi:~ $ sudo nano /etc/default/motion


start_motion_daemon=yes
Sauver les changements
pi@raspberrypi:~ $ sudo nano /etc/modules

Ajouter la ligne suivante à la fin du fichier: bcm2835-v4l2 et faire un retour de ligne


Sauver les changements
***** Attention, dans le v4l2 il s'agit bien d'un l (L) et non pas d'un 1 *****
Donner les droits d'écriture dans le répertoire d'enregistrement de la vidéo :
pi@raspberrypi:~ $ sudo chmod 777 /var/lib/motion

Redémarrer le Raspberry pi
pi@raspberrypi:~ $ sudo reboot

Ouvrir la page web dans firefox

http://AdresseIPduRaspberryPi:8081

Pour visualiser l'adresse du raspberry pi au besoin :


pi@raspberrypi:~ $ ip addr show

Si besoin d'arrêter et démarrer le service de webcam


sudo service motion start
sudo service motion stop
sudo service motion restart

Mouncef Benmimoune Page 9 Collège Lionel-Groulx

Vous aimerez peut-être aussi