A motion-activated video camera implemented using openCV-python.
Used to investigate my housemates' suspicions that a cat has been sneaking into our house in the early morning via our front door mail slot.
Constantly displays the camera feed and two 'delta' feeds, both being grayscale representations of movement in the environment. The first delta feed's pixels are given by the absolute difference in intensity between each frame, so faster movement gives higher intensity pixels. The second is that feed converted to black and white by thresholding with the THRESH
parameter (default 8).
When motion is detected for FRAME_COUNT
(default 5) consecutive frames, it begins saving the three video feeds. These are saved until PATIENCE
seconds (default 30) have passed without movement. The videos are saved to timestamped folders, and the camera feed video is annotated with the time and the status (whether motion is detected at each given moment).
To safely exit the program, press the escape key while one of the windows is selected.
git clone https://github.com/danitamm/motion-detection.git
cd motion-detection
virtualenv -p python3 env
source env/bin/activate
pip install -r requirements.txt
python motiondetector.py
Runs smoothly on a ubuntu machine with built in camera. To run on another machine, it may be necessary to change the following constants in the MotionDetector object initialization:
CAM_IDX
to your system's camera indexESC
to your system's 'esc' key numberFPS
(default None) to the frame rate of your camera. If None, it is calculated and printed byGET_FPS
, but adding it directly as input to the constructor saves you 1 second at the beginning of each run.
THRESH
(default 8): the minimum change in pixel intensity required to be considered motionPATIENCE
(default 30): the number of seconds to continue recording after the most recent motionFRAME_COUNT
(default 5): the number of consecutive frames that must contain motion in order to begin recording. Note that consistent motion forFRAME_COUNT
frames is equivalent to consistent motion forFRAME_COUNT / FPS
secondsannotate
(default False): whether or not to place a bounding box around the motion in the main video feed. Interesting but can make the video feel cluttered
Motion is detected by background subtraction, in which the difference between each consecutive frame is calculated and studied. The frames are converted to grayscale, blurred with a Gaussian filter, and the element-wise absolute difference between frames is computed. The threshold is applied to the result to map each value to 0 or 255. The resultant image is dilated for display purposes. If any of the resultant pixels are 255 then movement is detected. When movement is detected while not yet recording, each consecutive frame with movement is appended to memory
. Once there are at least FRAME_COUNT
consecutive frames in memory, each of these frames is recorded and we begin real-time recording. If there is a frame without motion before FRAME_COUNT
is reached, the memory is cleared and the process repeats.
- This
memory
andFRAME_COUNT
functionality were introduced to decrease the number of false positives that arised from flickering lights. Note that this functionality relies on the assumption that we are only interested in motions that begin with a consistent period of motion of at leastFRAME_COUNT / FPS
seconds.
The use of Gaussian blurring for denoising and of dilation for display purposes were inpsired by this blog post.