live counting vehicles using yolov8 | tracking & counting using yolov8 | yolov8 object detection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends and welcome to YouTube channel Freedom Tech and in this session what we are going to learn in this session we are going to create a counter for live CCTV camera stream we are going to use your yate object detection method and our tracking method and with the help of yate and our tracker we are going to count a car class for both Lane for going downside for coming upside so in this session we are not going to use here any pre-downloaded video we are going to use a Straight live video stream where we have live traffic and we are going to create a counter means we are going to count a vehicle frequency so before we move to our practical friends if you learn something from our videos please consider to subscribe our Channel okay friends thank you so much and let's get started so friends as I mentioned we are going to create our own vehicle counter with the help of YOLO object detection and tracking using live stream so if you don't know how to use YOLO and tracker I have create a several video I have create a playlist so watch the playlist and you will get the idea how to use yo8 and tracker with the help of open CV and python on Windows machine so let's get started first we need a packages so open Tony python ID go to the tools manage packages and just search for open CV H python hit enter it will search for open CV hyen python open CV hen python first package click on install and it will install open CV hen python on our Windows 11 machine you can use your Windows 10 machine also then we need our ultral litics package ultral litics for Yow so just search for ultral litics first package click on install and that's it it will install ultral litic on our Windows machine then we need CV zone for Ping our text on the frame so for that we need here CV zone so CV Zone just search click on install and that's it it will install CV Zone on our Windows machine that's it we have done with package installation now simply for today's session I have create a repository live CCTV vehicle counting yo I will mention the link copy paste the link inside d browser then go to the code click on download zip and it will download our ZIP format repository so friends uh there are two more packages we need for this session so just open tools and search for vid gear vid gear v i d g e a r vid gear just hit enter and as you can see withd gear just click on and as you can see I already installed that's why it say uninstalled so search for wi gear and then because we are going to use here our YT live stream CCTV traffic so for that we need YT hyund DLP package YT hyund DLP just search for YT DLP and the first package YT hyen DLP just click on install and it will install YT DLP on our Windows machine that's it done now simply we download our repository so our repository inside our file manager open it live CCTV as you can see the first right click and vrar and extract with the folder name it will extract our folder name which is here live CCTV vehicle accounting yellow at hund Main just click on again same folder and then as as you can see I have mentioned the coco. txt file where we have all the classes and then we have our tracker. PI and this is the test.py so let's just open test.py with the help of our Tony p on ID so import CV2 pandas as PD numai as NP ultral litics import YOLO CV Zone with gear. gears import cam gear and tracker import all the things from tracker file simple so this is the basic code which I have explained in several videos so I'm not going to explain all these things again and this is what the video URL this is what the live CCTV URL which we are going to use for creating our counter simple then we have here our object detection method as you can see the rectangle and then this is what our class simple and we are going to as I mentioned we are going to count here a car class so if there is a car in our detection then we are going to Simply append a list and inside that list we are going to append our object coordinates which is the rectangle coordinates X1 y1 X2 and Y2 for our tracker so as I mentioned for this session first you need to watch the basic video how we can use the tracker how we can use the yate object detection then you will get the idea so if we want to use our tracker simply we need to send our object coordinates means our rectangle coordinates inside our tracker so here it is as you can see when we detect a car then we are going to append our rectangle coordinates so this rectangle coordinates it will app inside this list and then we are going to call our tracker tracker. update method because we create here a Tracker as a variable and this variable represent our tracker class which we have inside our tracker. PI file if I open the tracker. pi as you can see the t is capital so this is what the class which we are going to use here and then from this tracker. Pi we have update function so we need to call our tracker so of course we need to call like this way tracker. our update function and inside that as I mentioned we want want to send our rectangle list so this is the list here it is as you can see we are saving the list we are appending the rectangle list if there is car so this is basically car rectangle coordinates we are appending inside our list and then we are updating our tracker with the help of this list now our tracker will track the object so again we have new rectangle coordinates so X3 Y3 X4 y4 and our tracker will assign the ID for each car so we have id1 all these things is basically our B box simple and then for tracking we need our object center point so Center Point CX in X3 + X4 ided 2 we will get our CX and Cy is basically Y3 + y4 ided 2 this is how we will get our CX and Cy then simply we can draw the circle then we have rectangle and then with the help of CV Zone we are going to put our ID on rectangle top left corner simple so this is the basic code so let's just run our basic code first it will download our YOLO vs. PT a pre-trend model and then we have object detection also we we have the ID on each car so it will open our frame so friends here it is as you can see as I mentioned this is what our live CCTV stream the date is here and the current time is here and as I mentioned it only detect a car because we create our condition if there is a car then only draw the rectangle and then only assign the ID for each car so right now as you can see there is no car so so here it is as you can see watch this car is detected there is a ID 54 is still there means our tracker is perfectly working for this car we have 78 ID for this car we have here 73 ID now there is 93 simple it's tracking 85 for this car so our object detection oviate method also our tracker is perfectly working we are tracking our car also we we detect our car now done now we we have our tracker ready now simply with the help of this ID we can create a counter but first here it is as you can see we don't want to track also we don't want to detect our car for all these all these areas okay all these areas all on the road we only let's just say we only want to detect our car our car class only this area as you can see for this area only this area we want to detect our object we want to create our object so for that we want to select our region of Interest area so let's just uh freeze our frame so for that simply mention here a zero let's just freeze our frame so I have explained the tracker. pi so let me close the tracker. pi so now I have start the test. pi and it will freeze our frame where we have object detection and our tracker we are assigned the ID with the help of our tracker so it will start our frame so as you can see this is what our frame so as I mentioned we don't want to detect our car class all other areas we only want to detect the car class like this in this particular area simple so for that we want our region of Interest area so region of Interest area for here we want to create a polyline simple so how to draw the poly line for that we need a coordinates so simple I have create here Mouse move event function as you can see C2 do event Mouse move function and then we create our RGB window and then we call our Mouse call back simple so of course now when you move your cursor on the frame inside shell as you can see we have XY coordinates so now it is really easy to get the coordinates for drawing the poly line on our frame so simply let's just say I want to start my poly line from here here so I just want to move my cursor over here simple and inside shell as you can see for this for this point we have 752 x coordinate and 263 Y coordinates so 752 and 263 752 and 263 752 and 263 this is what our first coordinate now so this is what our first coordinate so let's just say this one is basically our second coordinate so 414 4 384 414 414 and 384 this is what our second coordinate so here now let's just say uh over here this is what our third coordinate 468 41 0 468 410 this is what our third coordinate XY coordinates and this is what our last last coordinate so let's just say over here here 772 272 772 272 let's just first draw this coordinate then we will get the idea how our area look like if we want to make some changes then after we can simply change these numbers let's just first draw these points so here simply we are going to mention this as our area 1 Area 1 is equal to in uh inside the square bracket so mention the square bracket now we want to mention our coordinates in simple bracket so this XY is our first coordinate then this one is our second coordinate the XY and this one is basically our third coordinate the XY coordinate and this one is our last coordinate now we have our coordinates ready now Simply Save the code and stop the code now we want to use this area and we are going to draw the poly line on our frame so here let's just say CV2 do poly CV2 do poly lines this is this is the function for drawing the polyline C2 do poly lines and inside that where we are going to draw our poly lines simple on the frame and then we want to mention all these things as a NP do array NP do array and then inside that bracket we are going to mention our area one area one this is what our area one which we want to draw on our frame this is the points so area one and comma we want to mention NP do in32 NP do in32 then after square bracket again comma mention the true then and the color so let's just say uh this is what our area one so area one we are going to draw as a green color so 0 uh 0255 0 and thickness let's just say two so just save the code and run the code now we have the poly line on our frame so it will start our stream the live CCTV stream so here it is as you can see friends we have the poly lines but we want to make some changes okay so this area is look small and this area really look big so let's just say here I just want to make these points over here so 437 396 437 396 so this one is basically our third coordinate so uh uh again let just check over here 437 396 4 37 396 if we 396 it's uh 396 save the code run the code now let's just check and now as you can see friends now it's perfect now it's good so as I mentioned first draw the polyline with the help of the points and then if you want to make some changes you can simply make the changes because now you have the polyline on your frame so we draw the polyline so this is our area one in green color so we draw the poly line now simply we can say because we have here A Center Point for each car we have the center point we have the ID now simply we can say only detect my car when the car rectangle Center Point if it is inside the area which we draw which we draw on our frame means if the car center point if it is inside this green color poly line then on only we are going to detect the object then only we are going to track the object then only we are going to assign the ID for each object so how to do this simple for that we have CV2 Point polygon test method simple so here we have CX and Cy so let's just say result is just a variable result is equal to CV2 dot Point P capital polyon t Capital test C2 do po polygon test this is the function and inside that as I mentioned what is our goal our goal is to if our car center point if it is inside our area so what is our area our area is basically this this is what our area which we draw with the help of poly lines so from np. array to NP do n in32 is basically our area so just copy as it is just copy as it is and just paste inside here where we mention our s2. Point polygon test just paste it that's it so this is what our area now the next thing next condition we have our area now we mention if the car center point if it is inside this area so where is our Center Point our Center Point is basically CX and Cy so let's just mention it comma inle format CX and Cy that's it and then simply mention false false okay now simply we are going to print our result if we print our result save the code run the code so here it is we print our result so as you can see result is now minus10 because there is no set center point of our car inside our area our car center point is outside our area and if it is outside our area we have minus1 minus one so let's just watch for this car when the car will inside our area and then in that time we have one as you can see for this car we have one here it is as you can see one it means we can see simply create our condition let's just say if if our result if it is greater than is equal to zero then only I want to draw the circle I want to draw the rectangle I want to assign the ID now save the code run the code I have freeze the frame so we want to skip the frame one by one so friends as you can see what is our condition so we mentioned the condition only detect only track only assign the ID for car when the car is inside our area which we mention so now there is no car inside our area so there is no rectangle there is no ID there is nothing now just skip the frame watch for this car watch for this car when the Car Center Point inside our area and as you can see we have a rectangle and we have the ID we have Center points it means we successfully create our condition so now if I start a live while loop so we have live detection in particular our area if the object in particular area means our region of Interest area then only we have detection so it will start a live stream so friends as you can see now there is a signal people is basically crossing the road because there is a signal so traffic is basically down traffic is not moving basically so so there is no detection nothing and it's complete Live friends so now watch and as you can see the car detecting for both Lane for going down for coming up only if the car in our area in our region of Interest area then only we have our object detection our tracker and our ID is there now we create our first condition now we want to create a counter for both side L we create our first area so for creating the counter uh for going downside or going upside we want to create a second area so let's just create our second area first so stop the code and mention zero for freezing our frame again we are going to use our Mouse move function and we are are going to draw the second poly line for second area so it will start our frame and it will freeze our frame because we mentioned zero over here so our frame is started so same way let's just start from over here this is what our second area so let's just say here 7275 tri7 275 triple 727 5 7275 so this is what our first point so this one is basically second Point 445 43 445 43 445 43 and let's just say here 45416 45 416 and then the last coordinate over here 7 91278 791 278 so this is what our second area as I mentioned if you want if you want to make some changes first let's just draw this point then we can mention our changes so we will get the idea so this is basically our area 2 area 2 same way is equal to in square bracket and then we want to mention this as a point so this one is basically first point XY coordinate the second Point XY coordinate the third Point XY coordinate and the last one which is the fourth point for drawing poly line simple so oh 275 and I forgot to mention here this one bracket that's it so area two and now same way using polyline we are going to draw a poly line just copy it and paste it and only we want to mention here area two so this is what our area one so this one is area two so let's just make uh let's just make in blue blue color simple so save the code run the code now we will have our second area as a polyline on our live frame so friends as you can see but but as I mentioned we want to make some changes because it is really small so let's just increase the area let's just say this is this is good but this one is really small so let's just say I want to draw here 796 279 796 279 this one 7 96 uh 9 6 and 279 also so let's just increase uh this no there is no need to increase this area okay so we change the coordinate XY coordinate the last XY coordinate let's just watch how it look like if you want to make some changes again we can make a changes in our area so so now it's good it's good so now this is what our area one and and the blue color is basically our area to so we create our first condition where we are detecting our car only if the car center point if it is inside our area one which is the green color area now we want to create the counter for Lane let's just say for this is the going upside or this is going downside or we can say this is going downside or this is going upside so let's just assume that this is going downside okay this is going downside so if the car want to go down simply first it will cross this area which is green color and then the same car will cross our second area which is the blue color so we want to create here new condition when the car inside our area one which is the green color area in that time we are going to save their ID the unique ID and their current position inside the dictionary simple so let's just first do this thing so here here we have our condition if there is a result means if the car center point if it is inside Area then we are going to then we are going to save their ID and their current position inside the dictionary so let's just say down car so down car so down car um down car and what we want to mention we want to mention their ID so down car ID 1 is equal to what is the current position when we have our car inside our area the current position is basically CX and Cy simple so mention CX and Cy and then here we want to create a dictionary let's just create here a dictionary and the dictionary name is down car because we mention here down car id1 CX and Cy so down car is equal to dictionary now if we print here here if you print our dictionary which is the downard down car down car save the code and if we run the code so friends as you can see now there is nothing because we create the condition when our car if it is inside our area one which is the green color area but we create a new condition when our car inside our area we want to save their ID and their current position so I'm simply skipping the frame one by one so friends now as you can see the traffic is is moving so watch for both side car when the car inside our area and as you can see inside shell we have the ID for this card which we detected inside our area we have the ID and we have their current position means CX and Cy so it will detect both side cars it will detect both side car now as you can see for this car we have 28 ID and their current position which we are saving inside our dictionary so we printing that dictionary and inside that dictionary we have all our details so we successfully done this process now what is our goal we want to create here a counter for this side a downside so we we are saving when the car inside our area one we are saving the idea and their current position now watch for this car this same car will cross first our area one then same car will cross our area two so because of we are saving their ID and their current position simply we can say that we have the ID and the if the same ID card cross our area two then only count this is the count or we can say then only draw the object then only track the object then only assign the ID for object simple so we are saving their ID inside the dictionary so simply here here uh this is whatever this is where we are saving their ID so simply after that we want to create a new condition if if the ID one means if our ID one if it is inside if id1 in our down carard dictionary which we create over here this is what our dictionary so we are simply calling the ID if it is inside our downard dictionary only those ID is valid we say here only those ID is valid for detecting the object tracking the object and assign the ID for that we simply create the condition if the ID one if it is inside our downar dictionary then the same car will cross our area two so crossing the area we have our Point polygon test method means the same car Center Point cross our area two so this is for crossing our area one so let's just copy the condition after where we have if ID in down carard only we are going to just a minute just copy it all the way and paste it that's it and make it is in line so this is here and because we already mentioned the result so we only want to change the variable name so this one will become result one and now the same car will cross our area two so here we want to mention area two and then the same way if the car inside Car Center Point if it is inside our area two then same way we are going to mention if the result if the result one if it is greater than is equal to zero then only we are going to finally draw the circle so make it is in if condition then only we are going to draw the rectangle and then only we are going to put our text on our car simple so save the code run the code now what will happen when the car want to go downside first it will part of our area one then in same car part of our area two then only we are going to draw a rectangle and then only we are going to count our vehicle so now watch for this car watch for this car this car is basically going upside simple so watch for this car first it will inside our area two which is the blue color and then same car basically inside our area one because if the car inside Area one there is a ID in our down car but but this car is not detected because as you can see there is no rectangle and nothing because we create the condition I just want to draw the rectangle and all other things if this side of car moving not for this side going upside only for going downside so watch for this car now watch for this car this side car going upside car there is no rectangle there is nothing simple so now watch for this car when the car is inside our area 1 as you can see for this car we have ID 20 and current position now the same car if it is inside our area two this is the condition which we create then only on this car it will draw the rectangle and assign the ID watch and as you can see there is a same ID inside our dictionary we have 20 ID for this car and the same ID is inside our area two and then we have a detection we have rectangle and we have the ID simple as you can see so this is how friends we can create a counter simple now because we have this side of object detection tracking method so we can simply create the counter for going downside so let's just say we have the ID so when our car completed our boat condition we have the ID the so simply we can say here uh this is the downside so down down car counter downar counter down car counter down car counter dot append and inside that we want to append simple what we want to append we want to append our id1 because id1 is the unique identification for the car simple so downar counter. append and inside that we want to append the ID one and here we want to create down car counter downar counter is equal to empty list down car counter which we mention over there now simply if you print here downar counter now inside this we have the ID this is the list simple so let's just start our frame let just start our code basically so now there is no traffic no traffic in the sense uh there is basically signal so people is moving no traffic is moving so now friends as you can see the traffic is now moving so watch for watch for this side car this car will cross our both area then inside our counter we have the ID as you can see as soon as the car completed our boat condition there is a ID 20 and inside our inside our counter down list we have the ID but here it is as you can see it's counting same ID again and again so simply for that we want to create here a condition here we want to create a condition if downar downar counter this is whatever list downar counter. count and then then what we are counting we are counting the ID 1 is equal to is equal to Z means don't count same ID again and again and then after we are going to create the counter list now it will count ID same ID at only once now this is the list this is the list so length of list is basically the number length of list is basically number so now simply we we are going to start our live stream so I have mentioned the while loop so friends we have started our frame and as you can see we counted one car for downside we counted one car simple for downside we count one car so now there is a signal as you can see the traffic is not moving people are moving because there is a red color signal so now friends as you can see traffic is moving for downside we already count one car now second car this is what our third car fourth fifth one and sixth one so as you can see friends it means we successfully create a car counter for live CCTV stream for going downside now for going upside same way only we want to reverse our condition means here for downside we say that the car will part of our area one first then the same car part of our area to so here for going upside we are saying that the car will first part of our area to then we are going to save their ID and their current position inside the dictionary and then we are going to call the ID from the dictionary and we can simply create the condition that the same ID car is crossing already our area two now the same ID cross same ID is basically Crossing Area 1 for going upside simple so let's just save the code and here let's just create a second part like this way and we can simply mention from here from here result to copy all these things and check it is in line here and just paste it from here because we have result in this line so we want to start from this line just copy paste okay so we copy paste from our result to all the way for for where we have our counter so just copy paste and just paste in where we start our result line here here it is as you can see I have copy in this line result simple so now here we mention result one so we only want to change our variables so this will we want to mention result two and this the counter for going upside so first it will part of our area two so here we want to mention our area two and then if the result it is greater than a is equal to zero so here we want to mention this result to variable and this is not a downside this is what our upside so upar and we want to create here a new dictionary which is upar is equal to empty dictionary and after that we want our uh counter so up car counter is equal to empty list inside that we are going to save the ID for going upside simple so we mention here up car dictionary inside that we are going to save the ID and CX and Cy when the car center point if it is inside our area two because the car wants to go upside so for going upside Car Center Point first it will inside our area two which is is the blue color then the same ID will inside our area one going upside simple so up car here we want to mention instead of down car we want to mention up car then this result two so this result is basically result three and here we want to mention area one because first area two for going upside center point of car first inside our area two then the same car because we may mention here a condition if id1 is if it is inside our upcard dictionary then the same ID carard is inside our area one then here we want to mention result three and then only we are going to draw rectangle and all these things then this is what our upar counter upar counter and then here we want to mention upar counter. upend simple we mention here upar upar oh I mistly mention here A D upar counter now it's perfect upar counter now if we print here copy paste if we print our upar counter list and the length of the list is basically our counter up car and now I have started the while loop is there one is there so now we create a counter for both Lane for going downside for going upside for car class simple so right now the traffic is not moving now the traffic started to move so this car is basically going up so for going up we have one then going downside we count one two then this one is third so we count three car which already cross our area and go down so going upside we count now two car so it means our counter is perfectly working for live CCTV frame now simply we are going to use our C on putut text method and we are going to show our counter on the frame so so let's just say this one is basically car D and this one is basically car D is equal to and now there is no need to mention bracket here and this one is basically car U for car up car U is equal to remove the bracket and then we are going to use our CV Zone put text method copy it paste it and then we want to mention the coordinate so for downside let's just say 50 60 50 60 and here we are going to mention car D and the variable car let's just say uh down side Lane okay downside lane and then we have the counter downside Lane and same way copy it just paste it and here we are going to mention car U for upside and upside lane and then we want to mention the coordinates so let's just first start our frame because uh we want the coordinates for this side for right side because the car up counter we are going to put on the frame at the right side here so this one is here so let's just say over here 84659 84659 8 4 6 and 59 that's it now save the code and let's just cross check our uh our counter is perfectly fixed on our frame or if you want to make some changes in coordinates so let's just first try okay it's good so this is what our upside Lane counter and this is what our downside Lane counter so now simply we are going to start our live stream we mention here one so now we have a counter for live okay so right now there is a signal so friends as you can see signal is over and traffic is moving now watch the counter for upside we count one vehicle means we only create here a counter for car class only car class simple so three downside we count this car as a one so for upside the fourth one upside fifth one five counter two three this side we for upside we counted six car the car class so this one is seven and this one for downside this one is four as you can see the seven and four so friends we successfully create a counter with the help of yolow and our tracker for live CCTV traffic so I hope you will learn something from this video we'll meet our next video till then thank you take care and bye-bye
Info
Channel: FREEDOM TECH
Views: 5,615
Rating: undefined out of 5
Keywords: Real-time vehicle tracking, YOLOv8 vehicle counter, Vehicle counting with YOLOv8, YOLOv8 for vehicle traffic analysis, Real-time vehicle counting using YOLOv8, YOLOv8-based vehicle flow analysis, YOLOv8 vehicle tracking and counting, YOLOv8 vehicle counting tutorial, YOLOv8 for traffic monitoring and counting, Video-based vehicle counting with YOLOv8, YOLOv8 vehicle counting accuracy, YOLOv8 vehicle density estimation, YOLOv8 vehicle counting system
Id: 3tQ5Sz9emo0
Channel Id: undefined
Length: 48min 5sec (2885 seconds)
Published: Wed Oct 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.