Motion Detection in Python
Tejaswi Kumar
Data Analyst | Python | SQL | Statistics | Research Analyst | 99th percentile in SAT
Introduction:
OpenCV or Open-source Computer Vision is a great Library for Python which can be used effectively for motion detection.
Machine Learning can be applied after Motion Detection to obtain insights from visual data such as classification of activities being performed by people. Further applications which arise from integration with other such programs and Machine Learning algorithms have been discussed later in the article.
Attributes of video for motion detection:
Finding a good video for motion detection program testing is not easy. I decided to use the STS-129 HD Launch video by NASA. A good video for this should contain many types of motion and also parts of the video should have no moving objects. For example, straight line motion (liftoff), fast motion (motion of rocket outside atmosphere), slow motion (rotation of rocket from camera’s perspective), irregular motion (smoke), different sized objects moving in different directions (people), particle like motion (sparks), objects whose different parts move with different velocities; somewhat like fluids (flags), seemingly immediate motion (launch countdown display), relative motion of close objects very similar in color (tree leaves), multiple matter states containing ‘object’ motion (rocket booster separation) etc.
Motion Detection algorithm Step 1:
First we process the individual frames of the video by blurring them a little (to reduce noise) and then we measure the difference between successive frames with the use of absolute difference method. The method basically tells us how different the intensities of the frames are at respective locations.
Motion Detection algorithm Step 2:
Then a threshold function is applied to check color intensity difference, wherein we set a threshold, say 50. If the pixel color intensity difference at a point is more than 50 then the point is considered to be in motion and the respective pixel in our threshold frame is made white.
After this we apply another threshold function of say, 2500 on the groups of white pixels in our threshold frame. If a group of pixels having more than 2500 white pixels is in motion then it is classified as a moving object.
In above picture both NASA logo and rocket are crossing the threshold set in our program and hence are classified as moving objects. If the threshold is increased reasonably, the logo will cease to be classified as a moving object and the rocket will then be the only moving object in the frame.
Object motion capture and identification:
We now identify the location of the spaces in the frames occupied by moving objects, and enclose those spaces in rectangles while showing the original color video.
I have enclosed moving objects in red rectangles upon applying motion detection algorithms.
Conclusion and other use cases:
The core concept is hence rather simple. All that has to be done is to compare successive frames and decide whether or not they are substantially different. If they are then motion is happening.
Upon motion detection, face detection and face recognition is also possible. I have explained my method of performing face detection via python using OpenCV in my previous article, Computer Vision for Machine Learning using OpenCV. After this, using specific Machine Learning algorithms such as Convolutional Neural Network (CNN) we can analyse which persons are interacting or have been interacting with various people, objects or animals. Applications can be found in law enforcement, retail, intraorganizational connectivity etc.
The same program may be used for real-life live motion detection via webcams. Hence, OpenCV can be used for motion detection in all kinds of situations.
I think that this is a great library which can be utilized in various applications including but not limited to industrial, security, medical, wild life monitoring, border security surveillance, etc.
Copyright ? 2018 Tejaswi Kumar & Associates, All Rights Reserved
Aviation Management | Civil Aviation | Customer Service | Air India Express (AIX) | CRM | CX
6 年Great Tejaswi. I liked it.