Building Surveillance System Using USB Camera and Wireless-Connected Raspberry Pi
Read this post to learn how to build a surveillance system using a USB camera plugged into Raspberry Pi (RPi) which is connected a PC using its wireless interface.
3. Capturing Images using PyGame
The “fswebcam” package is useful for quickly testing whether the camera is working well or not. After making sure it is functioning well, we can start building a Python script that accesses the camera to capture images continuously using the PyGame library. The following code uses PyGame for capturing a single image, opens a window for displaying that image, and finally save such image.
Assume that the above code is saved in a Python file named “im_cap.py”. To execute such code, we can issue the following command from the terminal:
Here is the window displayed after executing such file.
We can modify the previous code to capture more than one image. For example, we can use a for loop to capture a number of previously specified images. We can also use a while loop that is not limited to a number of images. Here is the modified code that captures 2,000 images using a for loop.
Here are 8 captured images. Note that the camera position is changed a bit.
4. Building the Background Model
Up to this point, we successfully built a simple surveillance system in which a camera captures images which are saved in the SD card of RPi. We can extend that to automatically detect changes to the scene. This is done by building a background model for the scene. Any change to such a model will indicate a change. For example, if someone is passing through the scene will cause a change to the background.
The background model can be simply created by averaging multiple captured images to the scene background without any object in them. Because we are interested in the color information, the images will be converted into binary. Here is the Python code used to build the background model.
Here is the background model in both gray and binary after averaging 500 images.
5. Detecting Changes to the Background Model
After building the background model, we can test a new image to check if there is a change to the background or not. This is done simply by converting the new image into binary. Then the number of white pixels is compared in both images. If the number exceeds a given threshold, this indicates a change from the background. The threshold changes from scene to scene. Here is the code used for testing a new image.
Here is a test image in both color, gray, and binary in which there is a change from the background due to the appearance of an object (person) in the scene.
6. Building a Simple Circuit that Lights a Led When a Change Occurs
As an indication of a change to the background model, we can build a simple circuit in which a led lights when a change occurs. This circuit will be connected to the GPIO (General Purpose Input Output) bins of the RPi. The circuit needs the following components:
- One breadboard.
- One led.
- One resistor (more than or equal to 100 ohms). I use a 178.8 ohms resistor.
- Two male/male jumper wires.
- Two male/female jumper wires.
It is recommended to test the circuit before connecting it to the GPIO pins. This is because if the resistor value was not selected properly, this might lead to not only burning the led but also damaging the GPIO pins. To do the test, we need a battery for supplying the breadboard by power. Here is the circuit after connecting all components correctly.
After that, we can remove the battery and connect the breadboard the GPIO pins of RPi. Based on the breadboard numbering of the GPIO pins, the ground is connected to bin number 20 and the high voltage is connected to the output bin number 22. The following figure illustrates the connections between the breadboard and the RPi. The RPi is also connected to both the charger and the USB camera.
The output GPIO bin is controlled using the Python script given below. Its default state is LOW meaning the led is turned off. When there is a change to the background, the state will be changed to HIGH meaning the led is turned on. The led remains on for 0.1 seconds then its state returns back to off. When another input image is different from the background, the led returns back to on for another 0.1 seconds.
The following figure is shown one input image which is different from the background image due to the existence of a person. As a result, the led will be turned on for 0.1 seconds.
The following video (https://youtu.be/WOUG-vjg3A4) shows the led state for multiple frames captured using the camera. The led is turned on when the input image is different from the background model according to the used threshold.
For More Details
Ahmed Fawzy Gad, “Building an Image Classifier Running on Raspberry Pi”, September 2018, https://www.linkedin.com/pulse/building-image-classifier-running-raspberry-pi-ahmed-gad
For Contacting the Author
- E-mail: firstname.lastname@example.org
- LinkedIn: https://linkedin.com/in/ahmedfgad/
- KDnuggets: https://kdnuggets.com/author/ahmed-gad
- YouTube: http://youtube.com/AhmedGadFCIT
- TowardsDataScience: https://towardsdatascience.com/@ahmedfgad
Bio: Ahmed Gad received his B.Sc. degree with excellent with honors in information technology from the Faculty of Computers and Information (FCI), Menoufia University, Egypt, in July 2015. For being ranked first in his faculty, he was recommended to work as a teaching assistant in one of the Egyptian institutes in 2015 and then in 2016 to work as a teaching assistant and a researcher in his faculty. His current research interests include deep learning, machine learning, artificial intelligence, digital signal processing, and computer vision.
Original. Reposted with permission.
- Intuitive Ensemble Learning Guide with Gradient Boosting
- Building an Image Classifier Running on Raspberry Pi
- Genetic Algorithm Implementation in Python