This python program will allow you to detect motion and also store the time interval of the motion.
Main Logic : Videos can be treated as stack of pictures called frames. Here i am comparing different frames(pictures) to the first frame which should be static(No movements initially). We compare two images by comparing the intensity value of each pixels. In python we can do it easily as you can see in following code:
Analysis of all windows
After running the code there 4 new window will appear on screen. Let’s analyse it one by one:
- Gray Frame : In Gray frame the image is a bit blur and in grayscale we did so because, In gray pictures there is only one intensity value whereas in RGB(Red, Green and Blue) image thre are three intensity values. So it would be easy to calculate the intensity difference in grayscale.
- Difference Frame : Difference frame shows the difference of intensities of first frame to the current frame.
- Threshold Frame : If the intensity difference for a particular pixel is more than 30(in my case) then that pixel will be white and if the difference is less than 30 that pixel will be black
- Color Frame : In this frame you can see the color images in color frame along with green contour around the moving objects
Time Record of movements
The Time_of_movements file will be stored in the folder where your code file is stored. This file will be in csv extension. In this file the start time of motion and the end time of motion will be recorded. As you can see in picture:
- Face Detection using Python and OpenCV with webcam
- OpenCV | Motion Blur in Python
- Saving Operated Video from a webcam using OpenCV
- Puzzle | Car and Helium Balloon motion
- Project Idea | Motion detection using Background Subtraction Techniques
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet