Tracking Objects | OpenCV Python Tutorials for Beginners 2020

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to learn object tracking we will use our mouse to select an object and track it using different methods that OpenCV has to offer this is a fairly simple tutorial so it should be easy to follow I upload videos on a weekly basis so don't forget to Like share and subscribe and hit that notification bell so you don't miss out on any of the future content so let's get started [Music] so first we are going to import our package which is open CV so we'll go to file we will go to settings and then in our project they are going to add open CV so here we will type in total CV and we will click on it we will install the latest one so once it is done we are going to import CV - now what we are going to do we are going to use a webcam to actually do our tracking so for that we need to write the code for running a webcam and detecting the capturing the frames so we are going to create an objects so we will write CB two dots video capture and then we will write the the number of our camera so in my case I have an external camera so it will be one so here we will introduce our while loop so we will write while true and then we will write success success and then image is equals to cap dot read so this should give us our frame and then we are going to show this frame so we will say c v2 dot i am show and we will write for example marking and then we will write IMG and at the end we will write if see v2 dot wait key 1 and so this is to see if we have pressed the Q key and if we have we are going to break and finish the program that's about it so if I run this now it should run so I will right click here and press run let me check my camera so let me run again and there we go so now we are getting the feed from my camera and the next thing we can do is we can we can check for the the frames per second so we are going to write down our fps so we can write here timer is equals to C B two dots cats tick account and then before we show we are going to write our FPS so we'll say FPS is equals to C V two dots cats take frequency thank rate 1 C and then we are going to divide it will see V 2 dot get stick count get stick count - Tyler so that we get our frames per second so we can display this on our main screen so we can write C v2 dot put text and we are going to put it on the image and we are going to convert it into string before we put it there F beep s and then we have to define where we want to put it so we will put it let's say 75 and let's say 50 and then we are going to write C v2 dot fonts we will pick any fonts and then we will write down our scale at 0.7 and then we will write a color let's say it is what you want to put let's put any color 0 0 and then 2 5 5 and at the end we will define the thickness as - so let's run that and there we go so we are getting our frames per second but it is in decimals as well we will convert it into integer so there you go so we are getting the frames per second chilly this is too thick let's change this to bonds let's make it simple a little bit better okay so now that we have the frames per second what we need to do is we need to introduce our tracking so now we have different types of trackers that open CB provided by default we have boosting we have KCF we have median flow we have moves we have a lot of them and for each one of them we have to initiate the tracker so for this example let's start we will change the trackers later on but let's start with the Moose tracker so we will write tracker is equals to C v2 dot tracker and then we have to write moles underscore create and then brackets now if I run this now you will see something weird now it says that C v2 dot c v2 has no attribute tracker moves create now this is because we have to install another package in order for the trackers to run so we will go to file settings and then add and then we will go to open CV - cone trib by yes there you go so this is the package that contains all the trackers so we are going to install that too and there you go so once that is done now if we run it again it should not give that better so that is well and good now in order to run the tracker we are going to first initialize it to be the bounding box we are going to say that we want to track this part or this object and to initialize that we are going to detect the first frame so what I mean by that is we are going to take a frame from our webcam before we start the while loop and then we will drag a bounding box around it so that we can initialize our tracker so as we have done before here we are going to take our frame and then we are going to using this image that we have taken we are going to initialize our bounding box so our bounding box is equals to now cv2 has a function that allows you to create a bounding box around an image so that is select ry-ry and then we will define where we want to find we are going to define the window name which is tracking and then we are going to define our image and then we are going to say false now next we are going to initialize our tracker using this bounding box so we will say a tracker dot initialize frame frame image and then bounding box so if we run that and now if we click on our image we should be able to drag and when we press Enter it will start our while loop so that is it for the initialization next we are going to run our tracker and actually that is fairly simple to run all we have to do is we have to update our tracker value so what we need to do will write success and then we are going to write our bounding box is equals to tracker dot update and we will finish we will put our main image so this way it will get the bounding box and now all we have to do is we have to draw this bounding box so if we are actually getting we are finding the box we need to initialize it we need to draw it if you are not getting it we will say for example the object is lost or something like that so we will write here if success success is true then we are going to draw our box and if it's not then we are going to put the text somewhere saying lost so this sorry this here is your box function that you would write here draw a box and we will define and for now we will just try to pass and over here we are going to write CV two dots okay let's just copy because we have already written it will copy this here and we put it there so this is our text and this time around if it doesn't detect anything we are going to say for example lost and we should put it a little bit down let's keep it at the same point but little bit down let's say 35 so let's run that and we will let's let's do this and now right now it's saying it is lost so that is good and if we don't do that if we select a good objects that it will not say lost because it's able to detect it but now we did not put the bounding box so we cannot see it so we will go back and in the draw box we are going to write the function to draw our box so it is very simple all we have to do is we have to get our image we want to draw on and we have to get our bounding box that has all the values of it now one thing to note here is that okay we can send in our image and we can send it our bounding box but one thing to note here the bounding box is not a list so let me show you what I mean by that so we can prints and we can write the type and we can write bounding box so if we run that what happened okay so we need to write a pass so let's say you have this and you can see it's not it's basically a tuple so what we have to do is we have to get each of these values we cannot directly use them in the bounding box we have to convert it into integers so we can say that so if you're not familiar the bounding box has four values inside it actually let me just print it out so you can see so again the same mistake to write the pass so let's put that and there you go so basically this is the X and this is the Y of the initial origin and this is the width and this is the height of the bounding box so this means we have X Y our initial positions and then we have the width and the height which is our bounding box so we are going to get it from our bounding box a tuple so we are going to write here integer because we want to convert it into integer and our bounding box at number 0 so at number 0 will be X so the same way at number one will be why then we'll write two more times and at number one will be why at number two will be wit and number three will be height so now we have the individual values now all we need to do is we need to create a bounding box so we will say a rectangle and we want to draw it on this image and our initial position are x and y and then we want x and y plus the width and height so we will say the initial one is X plus wait and then y plus height so these are and the ones acting and and then we are going to write the color so let's define any color to five five let's say zero and then two five five again and then we can write three and then one so this is your thickness and your line type so if we run that should we run it or let's let's put four your tracking or not because we are putting whenever it's disappears we are putting lost so whenever it's there we should put track so we can put copy this and we can paste it here and instead of lost we can say tracking and we can change the color to green so let me run that again so let's select this press enter and there you go so we have our fps we are tracking right now and we can see our object if we move it around you can see it is tracking well so what we can do next is we can try a different body called trackers so right now we used the Moose create and then we can use tractor is equals to one of the ones that I really like is CSRT so we are going to use that tractor and we will say C rst and we will say underscore to create now this one is basically it is a bit slower but it's more accurate than the moose so most of the times you are basically talking about a trade-off between speed and accuracy so for example in moose the speed is really high but the accuracy is lower in C rst sorry C s party c-- s artie the the accuracy is high but the speed is low so we can see here the frame rate will drop let's run that so you can see the frame rate is about eight nine you can see there is a little bit of lag but this will give you better results in terms of tracking so I'm going to add all these different trackers on the main code that you can find on github so you can try all of these different types and you can choose which is the best one for your project and I also recommend reading about these trackers there is a good blog by someone named Lazar you can find it here I will put the link in the description and this guy he has written a brief description for each of these trackers that OpenCV has to offer and he does a fairly good comparison of their pros and cons their speed accuracy and he goes briefly into a little bit of detail of how each one of these work so in order to decide you can read about these in this blog post so this is it for today's video I hope you have learned something new if you have any specific topic you want to learn you can post it in the comments and don't forget to share like and subscribe and I will see you in the next video
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 77,881
Rating: undefined out of 5
Keywords: opencv python, opencv tutorials for beginners, beginner python, object tracking, tracking opencv, opencv object tracking, Tracking Objects, python (programming language), opencv tutorial, opencv pycharm, opencv 2020, OpenCV Python Tutorials for Beginners 2020, opencv tutorial 2020, cv2 tracking, computer vision, python tutorials for beginners, opencv tutorials, python tutorials, beginner opencv, opencv python tutorials for beginners, easy pyhton, image processing
Id: 1FJWXOO1SRI
Channel Id: undefined
Length: 18min 27sec (1107 seconds)
Published: Sun Mar 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.