Yolov8 object tracking 100% native | Object detection with Python | Computer vision tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey my name is Felipe and welcome to my channel  in this video we are going to do object tracking   using YOLO V8 so we are going to detect objects  and track objects on a video exactly like that   using 100 % yolo V8 entirely we are not going to  use any additional object tracking Library like   sort or deep sort but we are going to solve  the entire problem using only YOLO V8 and now   let's get started. And now let's go to pycharm and  let's create a new python project so I am going   to file new project and I am going to select the  directory where I am going to create this project   which in my case is somewhere around here I'm  going to press OK and I'm going to create this   new project using a virtual environment with  python 3.8 I click on Create and then create   from existing sources now the project has been  created and now let's install the requirements   we need in order to work on this tutorial I'm  going to click on file settings then project   project interpreter I'm going to click this  plus button over here and I'm going to search   for ultralytics that's the package we need in order  to work on today's tutorial; ultralytics, install   package and that's pretty much all this is going  to take a few minutes but in the meanwhile we can   continue working on this tutorial so I'm going  to click on OK remember we are still installing   this package ultralytics this is going to take a  few minutes but in the meanwhile let's continue   I'm going to create a new file so I'm going to  file new python file and I'm going to call this   file main.py and this is where we are going to  work... this is where we are going to code the   entire pipeline of this project and the first  thing I'm going to do is to write all the steps   we are going to take in order to complete this  project so the first one is to load the YOLO V8  model we need in order to detect objects  in order to track objects right in order to   do the object detection and the object tracking  then we are going to load the video we are going   to use in order to test this pipeline right and  then we are going to read frames from the video   then we are going to apply the object  detection we are going to detect objects   we are going to track objects then we are going to plot the results and then  we are going to visualize right so these   are the one two three four five six seven steps  we will take in order to complete this project   now let's get started over here I am going to  start importing ultralytics so from ultralytics   import YOLO and now I am going to start with the  first step in this process which is loading the   yolo V8 model we will use in order to do the  object detection and the object tracking and   this is how I'm going to do I'm going to define a  new variable which is model and model will be YOLO   and I'm going to use the yolo V8 Nano model  so that's pretty much all now we have defined   the model we will be using today now let's load  the video and let me show you the video we will   be using in order to test this project so I am  going to the directory where I have created this   project which is over here and this is the model...  this is the video we will be using right this   is the same video I showed you during the intro  right this is the video we will be using in order   to test this object detection and the object tracking  you can see we have two dogs and a person and we   will track these two dogs and the person through  all the frames of this video as we are going to   use a pre-trained model from YOLO V8 these are  all the classes we are able to detect with this   model right we are going to use a model which was  trained on the coco dataset and these are all the   classes we can detect we can detect person bicycle  car motorcycle airplane bus and so on we can   detect up to 80 different classes with the model we  are going to use and you can see one of these...   classes one of the objects we are able to detect  with this model is person and other class is... dog   right and if I show you the video again we have  exactly two dogs and a person and nothing else   right these are the only objects we have in this  video so this pre-trained model which was trained   on the coco dataset is going to be just fine in  order to detect and track these objects now let's   continue... so loading the video will be something  like this I'm going to Define a variable which is   video path and this will be the current directory  and it's called test.mp4 right I am loading this   video over here which is called test.mp4 then I  am going to import from... I'm going to import cv2   and I'm going to load the video  like this cv2 video capture   and then video path and that's pretty much all now  let's start reading the frames from the video and   I'm going to Define another variable which is ret  and I'm going to initialize ret as true now I am   going to do something like this while ret I am  going to read a new frame from the video like   this ret frame equal to... this is going to be equal  to cap... cap equal to cv2 video capture and the video   path and this is going to be cap dot read right  so this is how we are going to read a new frame   from the video we are going to call Cap which  is this object we have defined over here right   this is the video we are loading and we are going  to call this function this method which is called   read and this is going to return a new frame  from the video and it's also going to return   this variable which is a Boolean variable which  is going to be true if we were able to read a new   frame successfully and it's going to be false in  any other case so that's exactly why we are doing   a while ret because once we reach the end of this  video once we have reached the last frame from   this video then this is going to be false and we  are just going to get out of this while ret right   this is exactly why we are doing it like this now let's  continue now we have read a new frame and now   let's continue to the next step which is detecting  objects and the way this works the way yolo V8   works in order to detect objects in order to track  objects we are going to do these two steps at once   right we are going to detect objects and track  objects with only one sentence and let me show   you so we are going to do these two steps at  once and we are going to call model dot track   and we are going to input the frame and we're  also going to input another variable which is   persist... persist equal true right because we want  yolo to be able to remember all the objects   it has seen through all the frames in the video  so it's very important we define this persist   equal true this is very very very important and  we are almost there this is going to be results   and now we can continue to the next step which is  plotting the results you can see how fast we are   going you can see how straightforward this process  is this is amazing and now let's move to the next   step which is plot results this is where we are  going to take the frame and we are going to draw   a bounding box in all the objects we have detected  and also this is where we are going to draw all   the results we got from the object tracking and  this is how we are going to do that I'm going to   do... I'm going to define another variable which is  frame_ and this is going to be results  0 because we are detecting all the objects and  we are doing all the object tracking in only one   frame so we are going to access the first element  from the results and then we are going to call   plot and that's it that's the only thing we  have to do in order to do this plotting, now   let's continue to the next step which is the  visualization later on in a few minutes I'm   going to show you another way in which  we could also do this plotting but for   now let's just continue because this is a  very very easy and a very straightforward   way to the plotting now let's continue to the  visualization and this is how we are going to do   we're going to call CV2 imshow and we are going  to define this window something like frame and   then we are going to plot frame_  right and then we are going to call cv2 waitKey and this is going to be something  like 25 milliseconds we are going to wait   for 25 milliseconds and we are going to exit we  are going to quit this visualization by pressing   the letter q and this is how we need to write it  something like... this is going to be equal to ord q   right and this is going to be  break and this is obviously an if   okay if this situation then break so basically  this is how we are going to tell this software   that if we press the letter q we want to  quit right we want to exit this Loop so   this is pretty much all and now the only thing  I'm going to do is to press play and let's see   what happens I'm going to press... I'm going  to run main okay we got an error because   this is not yolo v8n dot py but this is dot  pt right I made a mistake now let's try again   okay and now everything seems to be working  just fine you can see that we are detecting all   the objects in this video and we're also tracking  the objects right in this case this is the object   with the ID number one this is the ID number two  this is the ID number three and you can see that   across all the frames in this video this object  remains as the ID number one this other one as the   ID number two this other one as the ID number three so  that means the object tracking is working just   fine it's working perfectly so this is going to  be pretty much all, I'm going to quit by pressing   the letter q and that's pretty much all the only  thing we could do in order to make it even better   is to do something like this I'm going to do...  here after we read the frame I'm going to do   if ret then continue right because remember we  are reading frames from the video and once we have   reached the end once we have absolutely no frames  left this is going to be false and this is going   to be None right so we definitely do not  want to continue if this is false so this is   how we are going to do it now remember I told you  there was another way in which we could do the plotting   and this other way involves using cv2.rectangle  and cv2.putText, by using these two functions   you should be able to achieve exactly the same  visualization as we got over here and if you want   to know exactly how you can use these functions  in order to draw bonding boxes on top of a frame   and in order to draw text on top of a frame I  invite you to watch other of my previous videos   where I showed you a fully comprehensive course  of opencv with python and one of the lessons   in this course was about drawing so by watching  that lesson you should be able to use these two   functions in order to achieve exactly the same  visualization we got over here so this is going   to be all for today this is exactly how you can do  object detection and object tracking using yolov8   entirely using 100% yolo V8 and this is going  to be all for today my name is Felipe I'm a   computer vision engineer and these are exactly the  type of videos and the type of tutorials I make in   this channel if you enjoyed this video I invite  you to click the like button and I also invite   you to subscribe to my channel this is going to  be all for today and see you on my next video
Info
Channel: Computer vision engineer
Views: 19,865
Rating: undefined out of 5
Keywords:
Id: uMzOcCNKr5A
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Mon Aug 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.