Friday, March 9, 2018

DIY Smart Surveillance Camera Using Raspberry Pi Camera and C++

Let us make a smart surveillance camera using a Raspberry Pi single board computer. Normally, it records the videos at 5x speed analyzing continuously and producing video files daily or hourly. Once human bodies are detected in the captured images, it will highlight them with green rectangles and change the recording speed to normal. A passive infrared sensor (PIR sensor) is also used to detect human bodies.

The example program discussed here is just to show the feasibility and it can be improved further. A few examples are sending email to your email address if something happened, recognition of faces of family members, recording videos at different speeds depending on time.

Figure. Connecting a PIR sensor.


You can buy a cheap HC-SR505 mini PIR motion sensor at an online store such as Aliexpress. It can be operated at either 3.3~V or 5~V. Since we are using it with Raspberry Pi, 3.3~V is used and output pin of the sensor is connected to GPIO23 as shown in the following figure.

Body detection is performed using Haar feature-based cascade classifiers as discussed in my previous post
Real-time Face Detection
Therefore, the only thing required to do is to change the file name as haarcascade_fullbody.xml. When the digital input for PIR sensor is active, red color text 'PIR DETECTED' will appear on the image. The example program SmartCam.cpp can be seen at the following link.

https://github.com/yan9a/rpi/tree/master/SmartCam

OpenCV in required to use the program. Installing and using OpenCV was discussed at
OpenCV on Linux using g++, CMake, Qt, Code::Blocks

Using RaspiCam with C++

You need to setup an additional library to use Raspberry Pi Camera Module with C++ [Sal17]. After downloading raspicam-0.1.6.zip, you can uncompress and build it using the following commands.
$ unzip raspicam-0.1.6.zip
$ cd raspicam-0.1.6
$ mkdir build
$ cd build
$ cmake ..
If the OpenCV module is found, it will show as
-- CREATE OPENCV MODULE=1

Figure. Setting up RaspiCam


Thereafter, it can be built, installed and ldconfig can be updated as follows.
$ make
$ sudo make install
$ sudo ldconfig
With OpenCV, there will be an example program called 'raspicam_cv_test' in 'utils' folder. You can test it using the following commands. The output of the program and resulting images are shown in the following figure.
$ cd utils
$ ./raspicam_cv_test

Figure. Testing RaspiCam.


To use RaspiCam in C++, you need to include its header as
#include < raspicam/raspicam_cv.h >
RaspiCam_Cv class can be used for Raspberry Pi camera module as follow.
raspicam::RaspiCam_Cv cap;
Grab and retrieve methods are used to capture an image. A simple OpenCV program using RaspiCam can be seen at the following link.
https://github.com/yan9a/rpi/blob/master/raspicamcv/raspicamcv.cpp

Properties of the camera can be set and their examples can be found in raspicam_cv_test.cpp which is in utils folder. One thing to note is that, after altering the properties, you need to release and open the camera again. The example program can be built and run as follows.
$ g++ raspicamcv.cpp `pkg-config --cflags --libs opencv` -o  raspicamcv \
 -I/usr/local/include/ -lraspicam -lraspicam_cv 
$ ./raspicamcv


Build and Run SmartCam

If you use X264 to record the video, the resulting file size is quite small. But the CPU utilization of the Raspberry Pi is very high and its performance is slow and laggy. For MJPG format, file size is big but the performance is faster. The program requires c++11 standard and lraspicam library must also be included when you build the program.
$ g++ SmartCam.cpp ce_io.cpp `pkg-config --cflags --libs opencv` -std=c++11 -o SmartCam \
 -I/usr/local/include/ -lraspicam -lraspicam_cv
$ gksudo ./SmartCam


Related Posts



အကိုးအကားများ

[Sal17] Rafael Munoz Salinas. RaspiCam: C++ API for using Raspberry camera with/without OpenCv. 2017.
url: https://www.uco.es/investiga/grupos/ava/node/40.

No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.