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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Implement Canny Edge Detector in Python using OpenCV
- Sentiment Detector GUI using Tkinter - Python
- Face Detection using Python and OpenCV with webcam
- Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
- Detect the RGB color from a webcam using Python - OpenCV
- Live Webcam Drawing using OpenCV
- Saving Operated Video from a webcam using OpenCV
- OpenCV | Motion Blur in Python
- Project Idea | Motion detection using Background Subtraction Techniques
- Puzzle | Car and Helium Balloon motion
- PYGLET – On Text Motion Select Event
- PYGLET – On Text Motion Event
- PYGLET – On Mouse Motion Event
- PYGLET – Key Motion String
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
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.