Tracking and Counting vehicles using YOLOv8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is arohi and welcome to my channel so guys in my today's video I'll show you how to detect track and count the number of vles that cross the defined line you can check this demo video over here you can see that we are only counting the vles that cross this defined line okay so by the end of today's video you'll be able to create your own vcle counting project where you're going to do detection tracking and counting all right so this is a step by step video so I hope this video will help you out to create your own project so uh guys today's task is divided into two parts first part is we will detect and track the vehicles and once we detect and track the vehicles in each frame of the video then we will send those frame to the second part where we will draw the line and whatever objects whatever cards or trucks or Vehicles Crossing that line we will count them okay so let's start so guys for today's video uh let's let's talk about the environment first you need Python and on that python you need ultral litics package and you can install the ultral litics package by just running pip install ultral litics and you can see by this command you can install it and after that I'm just importing ultral litics package and showing you the version which I am using and then we are also using the supervision module so this is the super um Vision module version I'm using and my torch version is this and it is compiled with Cuda 11.7 and the GPU on which I'm working is Nvidia GeForce RTX 3090 okay now first I'll show you how to detect the vehicles so guys uh generally when ever we work with any AI model so we prepare the data set first and then we train our model on that data set right and then we do predictions but for our today's class we are working on a vles class right so this YOLO V8 model is trained on Coco data set which have classes like bus truck bike car so there is no need to create your custom data set what we are going to do we will use that pre-train model only which is trained on Coco data set okay so this is the model pretrain model so this model is already trained on different type of vehicles which I've told you like bus truck car and bikes these kind of things right and then once you uh here we are loading the model pre-train model now this is my video on which I want to perform the detection so guys in this cell I'm just showing you how detection will work and in the next cell I'll show you how detection and tracking will work together and after that I'll show you how detection tracking and the counting will work okay so by writing this model do predict just provide the source so my video is in testing folder okay let me show you the video first okay so you can see this testing folder in this folder I have this video let me open this video and show you so this is the video on which I want to perform the detections okay okay so now save true means I want to save the results in a runs folder and this is the confidence score I'm using I'm I'm just saying that I only want those detections where the confidence score is um more than 0.5 okay and this is the image size now when you'll run it you just run it and you will get a folder like this okay let's run it so it is processing so now the processing is done once the processing done see now the processing is done you will get a predict two folder let's open that predict two folder now you will get runs under that detect and then this predict 2 let's open it and this is the output video you can see we are getting a class name and the confidence cod with it so this is how detection work now we want to perform tracking means we want to assign a unique ID to each detected object so for that so guys for that this is the cell I'm showing you so guys generally when we uh use the tracking algorithm how we work we we have a track tracking algorithm and we have a separate code of that tracking algorithm and we combine that code with the detection code and then uh we work right but ultral lric package comes with two different type of trackers okay let me show you that as well first so you can see this is the ultral litics documentation page and they have two type of trackers B sort and by track and the default tracker is Bo sort okay and to use this tracker is very simple so we are using the default one you only need to write model do track when you write this dot track the tracking will work okay and this is the video on which I want to work and this is a confidence score and these are the different other parameters if you want to Define save true will save your output in the runs folder all right so now let's run it so now processing is done and you can see we we have a track two folder inside this runs and over there our results are stored let's open that so here inside this track to let's see so you can see every car is assigned with different ID unique ID so here we are doing detection and tracking right so the steps are very simple with just one line of code you are doing detection and tracking okay so guys now right now we are using the default tracker which is B sort in this case in case you want to use this bite track tracker okay so what you can do just write this over here save true and then command write this and this is how you can use the use the other tracker okay so once tracking done now what we want is we want to uh draw one line on each frame and whatever car or vehicle cross that line then we want to count that object now let's see how to draw that line okay so guys now we want to draw the lines we need the exact coordinates where you want to draw the lines so for that you can use this link and over here you have to drag your image so in my case I have that image over here inside this testing folder see I have this image let me open this image and show you first this is one of the from the video which I have shown you on which we want to perform the testing so what I'm doing is I'm just dragging and dropping it over here okay now you have your image over here here we will I want to draw the line from here to here and whatever car whichever car will cross that line we want to count it okay so just enter here so here you can see we have the coordinates right now click again so now you got these coordinates just copy these values from this is the starting point first two coordinates are X and Y values of starting point and these two values are X and Y values of end point of the line okay just copy this line from here and here you can see here you can see that this SV do point this is the starting coordinate of the line and these are the and coordinates of the line okay so this is how you can get the uh coordinates of the line now what I'm doing is so for drawing the line and to counting the object we need open CV module also so that's why I've imported that these are the things we are importing and the this is the um model we are using for detection right and this is the video on which I want to perform the testing and this is the starting point of the line and this is the end point of the line and over here so this trackcore history so this is a dictionary to store the tracking history of object and then we have this crossed objects right so this crossed object is a dictionary to keep track of object that have crossed the specified line and guys then we have these two lines right so these two lines means we want to create a video sync for the out output video with this name so this is the video for this output video we want to create the video syn so that's what we are doing and here we are reading all the frames one by one and then we are performing the detection and tracking on each frame and these are the classes on which I'm specifically want to work these are the classes of the different Vehicles like car truck bus okay so I'm defining those classes over here and here so these four lines are um extracting the bounding boxes tracking idas and uh visualizing the detection results okay and over here in this Loop over here you can see that we are getting uh tracking history we are keeping the track of tracking history and then we are we are basically updating the tracking history and we are then checking if that object has crossed the line or not over here the specified line which we have drawn using that starting end point uh starting point and the end point coordinates right and then over here here you can see we are drawing that line and then we are putting the text on each frame with the count okay and then we are saving that output video now let's run this code okay let's run this code and see the output you can see the process has started and you will get a output video with this name once this processing is done so let's go back to the hair the processing is still going on once it done you'll get a video over here yeah so it's done now let's open and see it you can see here the object crossed is one now this object is crossing is two right so this is how it work with single object so you can still see so that counter is increasing uh on the basis of the cars or the vehicles which are crossing that line we are only uh you know counting those objects so this is how you can work with single uh line now let's say you want to count the cars which are crossing uh which are going this way okay so let's we will draw another line okay now let's draw two lines one is this one another line will draw over here okay and then then we will count how many cars are crossing this green line and how many cars are crossing this red line okay so for that the code will be same guys we only need this is for single line the same way we need starting and end points for the second line and we need these uh this crossed object one more dictionary which will keep the track of the objects which are crossing the other line This is for one line now we are going to have two lines so we need uh another dictionary which will keep track uh for the other line also okay so that's what I'm doing over here you can see this is again our model this is the video on which I want to test and it is in testing folder and this is the uh line coordinates for first line these are the line coordinates for second line and now you can see this this and this we have two dictionaries this will store the uh one dictionary is responsible for one line the objects which are crossing the one line okay and then here you can see earlier we were having only this code now we have added this part as well because we have two lines so this process will be done two times and then here we are uh adding the front line and here we are adding the side line to the frame and this is how we are counting and showing the results on frames now the output video name is this when we once we execute this code you will get the output video with this name with two lines and Counting the objects Crossing those two lines let's do that okay so the error is d. MP4 so that we have a output uh this testing video and testing folder so now let's run it so code is executed let's go back to the folder here and here this is the video it is uh in processing right now so once it done you'll get output video here so here let's see now you can see we have two lines one is green line and one is red line the number of count which is in red color shows you the count Crossing this red line and the green line count will show you the uh which is cross crossing the green line right so you can see the results here so guys this is how you can make your vcle counting uh project right I hope this video is helpful and guys if you like my videos please uh subscribe my channel and like this video thank you for watching
Info
Channel: Code With Aarohi
Views: 3,518
Rating: undefined out of 5
Keywords: objecttracking, computervision, objectdetection, ai, deeplearning, yolov8
Id: Z3uquMElyzI
Channel Id: undefined
Length: 15min 21sec (921 seconds)
Published: Mon Nov 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.