Lightweight Simple CAmera MOtion DETection application.
CAmera MOtion DETection application.
Fast, simple and stable motion detection application that works with any local and remote cameras or video sources.
The existing solutions were too complicated to install and set up, too slow, abandoned or simply didn't work, so I decided there was a need for a simple camera motion detection application as easy to use as any other command line application. camodet is ultra lightweight, portable and fast, it can be run in a raspberry pi or similar.
For Ubuntu based distros use the prepare.sh script, it'll install all dependencies and will compile OpenCV3.
./prepare.sh
For other distros use the script as a guide for the steps. The www.opencv.org documentation on how to compile it for your Operating System, can also be used.
Download and extract the sources, then inside:
mkdir build
cd build
cmake ..
make
done!
docker run --rm -it --network host -v "$(pwd)/output:/output" wojsudol/camodet:latest -i "http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480" -o "/optout/video_name"
For easy access to video source from you local network, use docker --network host
parameter.
To exit running process press ctrl+pq.
To get a list with a description of all parameters use the -h flag:
$ ./camodet -h
Usage: ./camodet [options]
Options:
-h Print this help message.
-i input_video: The source for motion detection.
-o output_name: The name for the output recordings.
A number and extension will be automatically added after it:
e.g. output_name23.avi
-s: Open window showing the input video.
-D: Date and time labelled to video.
-a seconds: Seconds to record after the motion has stopped.
-c number: Counter number to skip using in the output name (Default 0).
-g Generate template image for ROI mask.
-l cam_name: Label camera name on video.
-m mask_image: Mask image to use for ROI motion detection. Black areas are ignored, White areas checked.
-t number: Threshold area (sqare pixels) to trigger detection.
Movements below this area are ignored (Default 600).
-n number: Noise reduction level (Default 21).
-d number: Show intermediate images in a debug window. Number can be:
1: noise reduction | 2: frames difference | 3: threshold | 4:dilated(final)
Detecting motion on the system camera using default parameters without openning window:
$ ./camodet
If a window showing the input is needed:
$ ./camodet -s
For using an IP camera stream as input for the motion detection, the -i flag has to be used:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480
For finding the right values for each parameter a debug window is provided with the -d N flag, with N a number between 1 and 4. The number after -d will tell camodet wich motion detection step to show.
Note that when the correct values for each parameter are found, the -d flag can be omitted.
For example, testing a lower noise reduction for the input stream video:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -d 1 -n 3
If there are objects in movement that stops for a seconds, then move agains, it'd generate two video files. To wait recording after a motion has been triggered during a time, for other motion to be included in the recording, the -s seconds flag can be used:
For example, to wait 10 seconds after motion has stopped:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -n 3 -a 10
When browsing through several video recordings it may be usefull to tag the recordings with time and date, as well as camera names. Some cameras already generate the video with the desired labels. Otherwise in camodet it can be done with the -D (for date/time) and -l camera_name flags.
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -n 3 -D -l "Camera1"
The recordings of the motion detected will be named using a default name and increasing a counter. To specify the name the -o filename flag can be used. If the number in the name is wanted to start counting from a different number than 1 (e.g. to not overwrite existing recordings) the -c number can be used.
For example to generate recordings named Street10.avi onwards:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -n 3 -D -l "Camera1" -c 10 -o "Street"
In some cases, the camera frame will include some areas were continuous movement happens, but we want to detect motion in a smaller area of the image. For example if we have a camera on the back yard and the image includes a little bit of street. We don't want a recording generated everytime a car passes on the street, so we'll use a mask to ignore that area of the video.
The mask is an image with white and black. The black areas will be ignored from the motion detection, and the wite areas will be used to detect motion and trigger a recording. With the -g flag an image is generated so we can draw on top of it and use it as a mask.
So it'd require three steps:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -g
In the generated mask.png paint black the ignored areas. Then white the rest of the image.
Start camodet using this mask image:
$ ./camodet -s -i http://camera1.mairie-brest.fr/mjpg/video.mjpg?resolution=640x480 -n 3 -D -l "Camera1" -m mask.png