For motion detection the developer has to keep in mind a range of parameters with a key role:
• Robustness: the conditions change from a place to another, so developers have to keep in mind these situations to generate a software that is able to adapt to different variations of light and background;
• speed: processing communication in real time, it must be granted one video streaming performing enough to use this type of devices for all the elaboration;
• cost: after the values discount of hardware devices, it is now possible to delight with new types of interaction, because the systems house dedicated processors and code optimization for compression and image processing interfaces.
Evaluations and Considerations
The application of these algorithms is very effective for intercepting any change on the observed scene, but it is misleading as it can lead to false positives, such as changing the brightness in the environment or even moving the workstation location on which the webcam is housed or even a simple passage of a pet in front of the webcam for examples.
In this case the solution is to adopt the machine learning and computer vision.
OpenCV: library for Computer Vision
OpenCV is an open-source software developed for Intel architectures.
Machine vision is having a great success thanks to the low cost of image capture devices and high performance.
Some examples of its research areas are:
• motion tracking,
• human-machine interaction,
• identification and recognition of objects,
• face recognition,
• robotics algorithms.
OpenCV developed algorithms concern the filtering, the binarization, feature tracking, shape analysis, 3D reconstruction, camera calibration techniques, object recognition with histograms. Originally, the library was written only in C language; subsequently wrappers were written for other languages such as C#, Python, Ruby and Java.
Through the use of these new interfaces, the number of code lines necessary to call desired procedures is reduced, and common programming errors regarding the memory leak phenomenon are reduced, through allocations and deallocation of data structures, that led to a very easy approach for programmers.
To avoid false positives we must train the computer to face recognition.
We need a training phase to memorize the characteristics of the face that is recognized based on the positions and distances of 128 well-defined points.
Create Background Subtractor MOG2 function
OpenCV contains implementations of Background Segmentation. One of those is the class called BackgroundSubtractorMOG2, that is a Gaussian Mixture-based Background Segmentation algorithm, and it consists in fixing the background pixels and allocates a Gaussian Distribution to every pixel. The weight of this distribution is the number of time while the colors remain in the scene. The reasoning behind it is to identify the background using the information from the Gaussian mixture, ie more the color of the background remains the same
and more probability the observed scene is unmodified.
When between frame t-1 e frame t there is no movement, a completely black image is obtained, for the low-pass filter, on the contrary the movement will be detected by calculating the absolute difference between the two frames, obtaining white areas.
The graph of a Gaussian is a characteristic symmetric “bell curve” shape, a blurred image is obtained, to decrease the noise generated by the camera and it is salient when you want to drop the resolution of a frame. The Gaussian filter is of course the most convenient in which the new pixel value is the weighted average of the values in its neighborhood. These weights are distributed by Gaussian function, so: wider is the bell and then greater will be the effect of equalization or smooting.
Creates MOG2 Background Subtractor.
Parameters
History Length of the history.
varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model. This parameter does not affect the background update.
detectShadows If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.
The Mahalanobis distance is a measure of the distance between a point P and a distribution D, introduced by P. C. Mahalanobis in 1936. It is a multi-dimensional generalization of the idea of measuring how many standard deviations away P is from the mean of D.
Python code for motion detection
import numpy as np
import os
import time
import cv2
cap = cv2.VideoCapture(0)
# Gaussian Mixture-based Background/Foreground Segmentation
fgbg = cv2.createBackgroundSubtractorMOG2(50,200,True)
frameCount = 0
snapCount = 0
while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
break
frameCount += 1
resizedFrame = cv2.resize(frame,(0,0),fx=0.5,fy=0.5)
fgmask = fgbg.apply(resizedFrame)
count = np.count_nonzero(fgmask)
if(frameCount > 30 and count > 1000 ):
print(‘Motion detected’)
print(‘Frame: %d, Pixel Count: %d’ % (frameCount, count)
if(snapCount > 5):
cv2.imwrite(‘snap.jpeg’,frame)
snapCount = 0
os.system(“sendemailc.exe”)
else
snapCount += 1
cv2.imshow(‘frame’, resizedFrame)
cv2.imshow(‘mask’, fgmask)
if cv2.waitKey(33) >= 0:
break
cap.release()
cv2.destroyAllWindows()