Motion Filtering with OpenCV in Python
Today we are looking at a way to extract and visualize the moving parts of a video, using computer vision principles in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🖥️ My Coding Setup 🖥️
⌨️ Keyboard: http://hyperurl.co/neuralkeyboard
🖱️ Mouse: http://hyperurl.co/neuralmouse
🖥️ Monitor: http://hyperurl.co/neuralmonitor
🎙️ Microphone: http://hyperurl.co/neuralmicrophone
✏️ Drawing Tablet: http://hyperurl.co/neuraldraw
🎵 Outro Music From: https://www.bensound.com/
What You'll Learn
This video demonstrates motion filtering with OpenCV in Python, allowing users to extract and visualize moving parts of a video using computer vision principles.
Full Transcript
what is going on guys welcome to this video in today's episode we're going to use background subtraction in order to filter out the emotions or the changing objects of videos so we're going to take a video like this one here where people are walking around a place and what we're interested in is which objects of this video are moving and where we want to filter out the movements and ignore the rest so we're going to end up with something like this one where we basically just get the changing objects the moving objects and their edges so this what we're going to do today so let us get into the code so as always the first thing that you want to do is you want to make sure that you have installed the libraries that we're going to need so we're going to run CMD here activate the Conda environment if we're using one and then just type it install OpenCV - python this is the library that we're going to need for today's video in my case it says already satisfied otherwise it's going to install it so the first thing we do is we import CV - I like to give it an alias off CV just in case you know they publish CV 3 so I can just change the to two or three and I have the same code so this is the import and then the next thing that you want to do is you want to get the video file link descript so what you do is you take a video you can make this video yourself with the camera you can just download it from Google or YouTube or whatever and you want to take this file and put it into the same direction as your script and once you have done this what you do is you type video equals CV dot video capture and here your path of the filename so in my case people dot mp4 now what you could also do is you could go ahead and use your camera as the video data so in order to do that you just say a video equals CV down video captured zero or one or two depending on how many cameras you got if you have one camera it's going to be 0 and if you have multiple it's 0 1 2 3 and so on I'm not able to do this in this video since I'm recording with this camera right now but if I wasn't recording I could just use 0 and we could apply the motion filtering or the background subtraction onto the camera you do this at home if you have a camera but I'm not able to do this in this video but usually you want to use the camera data since you know this is very useful for surveillance systems and so on so it doesn't make a lot of sense to use it on videos that are already that are that are representing past data so you want to have a life motion filtering but as I said I'm not able to do this in this video however the next thing you want to do is you wanted to find a subtractor object so this object is the object of this going to do all the work for us very simple since we're using a library here we don't have to do anything on our own so we just say subtractor equals and then we say cv create background a background subtractor M og to this is what we're going to use and here we pass two parameters the first one is the history so how long back or how far back should I look for changes so basically if this is a very small value it is only going to visualize the parts that are moving very armed and in a very high frequency and if this is a larger value it's going to look farther back so basically let's take 20 here and the threshold is basically just a threshold that you know we accept so 50 in this case you can as always tweak around these values and play around with them to see if you get better results and then what we do is we want to visualize the data so what we're going to do is we're going to run an endless loop and we're going to visualize the video frame by frame with the subtractor applied already so we're going to say while true and then we're going to use the read function of the video and the read function of the video returns two values the first one is just a return value and the second one is the current frame of the video so we're going to say video dot read and now what we're going to do is we're going to say if return value so if the return value exists if it's there we're going to process it we're going to visualize this video and otherwise we're going to do something else so we're going to say mask equals and we're going to say subtractor apply we want to apply the subtractor object that we just defined onto the frame that we currently have for visualization and then we're just going to say CV dot M show mask mask and then that's basically it the only thing we need to do now is we're going to say if CV dot wait key with a delay of five if this equals the key X we're just going to break the loop so basically what this does is as soon as we press the key X of the video or the whole script terminates because we break out of this loop and that's it it's just so we have a way to terminate it because what happens if we don't have a return value if the video is done because you know the bill the video is like eight seconds or something and if we have seen the whole video it's going to actually terminate the script because we don't have a return value but if we don't have a return value what we're going to do is we're just going to reset the video so we're going to save the video equals CV video capture people dot mp4 so basically we're running it in a loop over and over again with the camera you don't need to do this so because you know the camera is running all the time however that's actually the only thing that we need to do now is we're going to say CV destroy on Windows and our video dot release so just closing everything up and that's it let's run it it should work perfectly and as you can see it works it's pretty easy very short code impressive result and we can now go ahead and for example increase to the history 290 so I think we're going to see a little bit more white here yeah as you can see you can see some lines in the bottom or on the floor and you know you can just go ahead and say I don't know 300 or something to see even more changes okay see more of the shadows it's much brighter if you like it that way because what you see here is that the old body is white instead of just the edges so you might want to you might prefer this one otherwise let's just go ahead and increase this to 200 see what happens yeah doesn't change it too much however okay I hope the recording is not lagging too much right now however that's how you do it is just a simple background subtraction of course the process behind it is it's much more complicated but since we're using just this object here and not coding anything ourselves it's very easy to apply so that's basically how I do it so that's it for today's video it was a very simple video a very short video but I think the output is quite impressive for such a little amount of code so if you enjoyed it and if you learned something hit the like button and let me know in the comments also feel free to ask questions and give feedback in the comments and if you haven't subscribed to this channel yet go and do so to see more future videos for free other than that thank you very much for watching and see you in the next video bye [Music] you
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 53 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
▶
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: CV Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
When the Camera Becomes an Exam Proctor: Building an AI-Powered Exam Monitoring System with…
Medium · Python
When the Camera Becomes an Exam Proctor: Building an AI-Powered Exam Monitoring System with…
Medium · Deep Learning
Your Face Is About to Become Your Phone Number
Dev.to AI
ETH Zurich’s bidirectional pixel could turn screens into cameras
The Next Web AI
🎓
Tutor Explanation
DeepCamp AI