Home security system version 2
In order to improve my previous home security system, I bought the Raspberry Pi Camera (with no IR filter), with a goal of creating a system which captures images of movement when I am not home. Since I don't want to have to remember to turn it on and off, it needs to determine my presence automatically.
The Raspberry Pi and the camera module (with a USB Wi-Fi adapter)
My original plan was to use Python and OpenCV to create my own motion-detection system, as opposed to before where I used a premade package (Motion). I captured a still image, subtracted it from the previous image, and called this a difference image. Any pixels which were non-zero (light) here were pixels which were different between frames. The magnitude and number of these pixels gave information on the extend of movement in the frame. I also added an average over multiple frames to try and decrease the sensitivity to outlier events. This was heavily based off of Home surveillance and motion detection with the Raspberry Pi, Python, OpenCV, and Dropbox.
Early attempt at feature detection (note this image captures dark regions greater than a certain size, not motion)
Motion vector estimates
After reading the picamera documentation, I found out that the camera is capable of outputting the motion vector estimates that the camera’s H.264 encoder calculates while generating compressed video. Instead of capturing single still images, I switched to capturing a video stream. An added benefit was the increase of framerate from about 2 frames per second to more than 30.
Rendered motion vector data
Based on the number and value of the motion vector data pixels, a counter was incremented if a threshold was met, and decremented if not. The decrement value was significantly smaller than the increment value. Actual motion caused a quick ramp up which triggered the motion-detected routine, followed by a gradual decay once motion was not detected. Once the counter descended past a threshold, actual motion was considered over. If during that decay new motion was detected, the counter shot back up and the counter began decaying again.
Graph of the counter value
Similar to the previous version of my system, my router is checked for my phone's MAC address to see if I'm home. If my phone is connected to the Wi-Fi, it ignores all motion.
Uploading to the cloud
While (threshold-exceeding) motion is detected, the video stream is written to a file on the Raspberry Pi's SD card. Once motion is finished, the file is uploaded to the cloud (Amazon S3) using the Boto 3 API. Amazon SNS notifications were set up so that an email is sent to me when a new file (new video) appears in the S3 bucket.
One drawback to this system is that the upload only starts when the motion event is finished. It would be preferable to upload as the stream is captured, as an adversary can just disconnect and remove my setup as is (before the video is uploaded).
Example output video
Future improvements might include using infrared LEDs to provide active illumination, since the camera does not include an IR filter (this causes washed out colors in daylight). In addition, there is a slight lag after the camera video stream starts. This could be remedied by constantly saving a video loop and splitting into that loop when motion occurs.
Originally I thought about using OpenCV for face recognition, as a replacement for checking my router. I abandoned this path due to (perceived) limited available processing power, as well as satisfaction with current method.
My code can be found at https://github.com/colonoh/ceilingcat2.Home