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.

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.

Figure. Connecting a PIR sensor.


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.

Tuesday, January 23, 2018

UDP/TCP Socket Programming with wxWidgets

  1. Introduction
  2. UDP
  3. TCP
  4. References

Introduction

In this article, UDP and TCP socket programming using wxWidgets is discussed. For that wxWidgets needs to be installed in your machine. Installing wxWidgets on Windows and Linux platforms can be seen at the following link.
Sockets are used to send and receive data on a network such as the Internet. Almost all modern operating systems support socket layer to transfer data using TCP or UDP. But programming sockets on different platforms usually involves platform specific tweaks. wxWidgets support socket classes which allow you to use sockets for transferring data without concerning about the platform. These classes can be used in various ways and some of them are discussed in the following sections.


Thursday, November 23, 2017

OpenCV with wxWidgets

  1. Introduction
  2. Linux
  3. Windows
  4. References

Introduction

In this article, using OpenCV with wxWidgets is discussed with examples. For that, OpenCV and wxWidgets need to be installed in your system. Installing and using them on Linux and Windows platforms can be found at the following links.

Linux

Building an application using OpenCV and wxWidgets in Linux terminal is simple. As an example, a simple program called wxcvsimple.cpp (online) is discussed below. At the start of the program, in OnInit() method of the Application,
wxInitAllImageHandlers();
is called. Then, wxStaticBitmap variables are declared in MyFrame class, a derived class of wxFrame, to display images. The following code can be used to initialize wxStaticBitmap in the constructor of MyFrame class.

Monday, November 20, 2017

OpenCV on Linux using g++, CMake, Qt, Code::Blocks

  1. Introduction
  2. Using GCC ၊ CMake
  3. Using Qt
  4. Using Code::Blocks
  5. References

Introduction

To install OpenCV on your Linux machine, you need to have the following packages as prerequisites [Ope17g].
  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
To install the required packages, you can enter the following commands in terminal.

Monday, October 30, 2017

Cross-platform C++ programming with wxWidgets

  1. Introduction
  2. Windows Setup
  3. Linux Setup
  4. Mac Setup
  5. References

Introduction

wxWidgets is a C++ library for creating cross-platform applications. It enables developers to create GUI code to compile and run on several computer platforms such as Windows, OS X, Linux and UNIX with minimal or no code changes.

wxWidgets is free and open source software which satisfies those who wish to produce for GPL and proprietary software without license costs [wxW98]. On the other hand, Qt is available for free under LGPLv3 license but has some limitations for commercial use unless you pay the license fee [Qt17].

wxWidgets uses Native platform , therefore GUI has more native look and feel [wxW12]. Qt extends the C++ language but wxWidgets does not extend the C++ language which is less surprising to developers expecting standard C++.

wxWidgets produces small and efficient binary applications. Therefore, it is suitable for embedded systems. In term of library size, Qt library is \(\approx 200\) MB where wxWidgets library is \(\approx 30\) MB.

wxWidgets not only works for C++, but also has bindings for python, perl, php, java, lua, lisp, erlang, eiffel, C# (.NET), BASIC, ruby and even javascript [wxW15a]. wxWidgets is one of the most complete and mature GUI toolkits. There are a lot of utility classes also.

wxWidgets is used by a huge range of organisations and individuals all over the world. Some of the better-known organisations who have used wxWidgets include NASA, AMD, Xerox, and Open Source Applications Foundation (OSAF). wxWidgets applications that you may be familiar with include AVG AntiVirus, Audacity, Filezilla, Code::Blocks, CodeLite.

Wednesday, September 13, 2017

Pixy

Pixy (http://cmucam.org/projects/cmucam5) is a vision system that can be used to add computer vision to your Arduino, Raspberry Pi, or BeagleBone project. Once you have taught it the color to detect, it can send you the position and size of the detected objects.

Thursday, August 3, 2017

KiCad Getting Started Guide in Myanmar Language

KiCad is a suite of free software to design PCBs.[KiCad on Wikipedia]. I have been using it to make PCBs and it is OK so far. It is also easy to use. It comes with tools to check design rules and to produce Gerber files for manufacturing. I have written a getting started guide for KiCad in Myanmar language to take note about my experience and methods. And I combined them with English version guide which can be found on its website [Getting Started in KiCad, March 5, 2017]. My Myanmar version getting started guide for KiCad book can be found at

https://yan9a.github.io/KiCad/kicadmm.pdf



Tuesday, June 6, 2017

Flash content protection for LPC824

In this article, I would like to discuss about evaluation of LPC824 low cost 32-bit ARM Cortex-M0 microcontroller using OM13071 LPCXpresso824-MAX Development board. LPC824M201JHI33 is used in the board. Its size is only 5 x 5 x 0.85 mm in HVQFN package.

In order to evaluate it, MCUXpresso Integrated Development Environment (IDE) is downloaded and installed. Other tools for it can be found at this link . After launching MCUXpresso and assigning a workspace folder, example projects can be imported by clicking Quick Start Panel (near bottom left corner) -> Import projects from file system ... -> Browse LPC open resources as shown in the following figure.



Wednesday, May 31, 2017

Programming serial port in C++ with wxWidgets for Windows and Linux

I have developed a class library 'Serial.h' to use serial port (com port) on both Windows and Linux. This cross-platform 'Serial' class is written in C++. A simple example for C++ console program using the class is demonstrated. As an another example, using it with wxWidgets for GUI application is also presented. The source code can be found at

https://github.com/yan9a/serial


Figure. A wxWidgets GUI application using 'Serial' class with Visual Studio 2017

Friday, March 31, 2017

Geometric Center of Myanmar

I have written a program to find the centroid of Myanmar. The centroid is found to be at latitude 20.86 degree and longitude 96.48 degree after compensation for Mercator projection.

Source code: https://github.com/yan9a/Myanmar_Centroid

Document: Section 5.4 of https://yan9a.github.io/OpenCV_Projects/opencv320.pdf