wrong way driving detection | yolov8 wrong way vehicle detection | yolov8 traffic management

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 use YOLO V8 and we are going to detect wrong way vehicle we are going to detect the wrong way vehicle then we are going to track the vehicle we are going to create the counter for wrong W vehicle 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 mention today we are going to detect track and count wrong way vehicle for that simply open your Tony python ID then go to the tools manage packages search for open CV hyphen python just hit enter the first package click on install and it will install open CV hyphen python then we need Ultra litics ultral litics our yolow we package so ultral litics click on install it will install ultral litics then we need a CV Zone package hit enter first package CV Zone click on install and it will install CV Zone on our Windows 11 machine this is Windows 11 machine of course you can use your Windows 10 machine also so for today's session I have create a new repository I will mention the link copy paste the link inside the browser wrong wave vehicle detection ulate go to the code click on down down zip and it will download our ZIP format repository so as you can see now it's downloading so friends as you can see we successfully download our repository now minimize the browser minimize Tony and open our file manager downloads and this is what our repository right click then wi RAR extract the folder name and then we have our folder again same folder and inside that we have all our files so so let's just first watch our video file so wrong way. MP4 just open the video file and then here it is as you can see this is what our scenario uh we have here CCTV video recording and this is the one way the way as you can see only for this side the vehicle is allowed to go only this side here it is as you can see the arrow on the road allowed to go only this side not for this side let's just say this is the upside this is the downside so if the vehicle is going downside we can simply say the vehicle is going basically wrong way so this particular vehicle as you can see it's basically going wrong way as you can see it's going wrong way so our goal and here it is the next vehicle so our goal is to detect a wrong W vehicle we want to detect our wrong vehicle also we want to track our vehicle and and when we track the vehicle of course we can create a counter so this is what our scenario as you can see simple so let's get started for that I mentioned the basic code with uh object detection because I have explained the yolow V object detection for open CV and python in several videos I have created a playlist so if you don't know how to use YOLO vet with open CV and python watch the playlist you will get idea simple so I have mentioned the basic code where we have our object detection as you can see I have mentioned the wrong way. mp4 file so we have here a basic object detection with our tracker because I have explained in several previous videos how to use a Tracker also so I'm not going to explain all this things again because I have explained several times so this is what our object detection here it is as you can see X1 y1 X2 and Y2 so we have object detection then we are we have our classes then of course we want to detect only car class so if there is a car so we are going to create a list so if we want to use our tracker we want to send the rectangle coordinates inside the tracker so for that we create a list inside that we pass X1 y1 X2 Y2 that's it then we are basically calling our tracker inside that we are passing the list then we have our tracker as you can see then with the help of tracker now we will again track that object so of course we will get the new rectangle coordinates along with the ID for each car so X3 Y3 X4 y4 and the ID is equal to this B box simple and then simply we can draw a rectangle also we can put our text on rectangle top left corner let's just first run the basic code so first it will download our pre-trend model which is Yolo vs. PT as you can see I have mentioned the yellow V at s. PT the pre-rain yellow V at uh model pre-rain model so it will download and then we we have our detection so friends as you can see we have now over detection and it's detecting the car also we have already mentioned our tracker so there is a ID for each car as you can see this car we have zero this car we have another ID this car we have 11 and this car we have 21 13 10 12 so we are basically detecting our car we are tracking our car because there is a ID for each car so it means object detection and tracking already done see simple so let's just what we are going to do only one change here this CV2 do rectangle we are going to mention first and then we are going to mention our put text method only Small Change that's it now let's just freeze our frame save the code and if I run the code so now as you can see for this car we have two the two ID means our tracker is perfectly tracking so let's just watch for this car this car is basically passing from this area to this area and the ID is two so now the ID is still same here it is as you can see the ID is still same it means our tracker is perfectly working so now our goal is to detect and track which we already complete now we want to Simply say the car is basically going wrong way so for that we want region of Interest area so for region of Interest area here it is as you can see I have mentioned two area this is the area one which I have draw with the help of CV2 do polines method and this is what our second area same way I have draw using co2. poly lines method so how to draw the poly lens for that we have here our mouse event function here it is as you can see mouse event function so when you move your mouse on the frame you will get the XY coordinate inside the shell so if you want to draw the area with the help of C2 do polyline so simply you need to start from here let's just say this is whatever area one coordinate so 594 228 so simply you need to mention area one like this way this is the the four coordinates 1 2 3 4 so this is what our polyline coordinates and then this is the area one then this area one I have mentioned in C2 do poly lines method as you can see the area one because I have explained this topic several times so I'm not going to explain in deep way so this is how you simply draw a poly lens so you can simply say this is how we can draw our region of Interest area simple so same way I have draw the area two also now we have our detection we have we are tracking we are assigning the ID then we have now our region of Interest area now what we want here let's just start again our code we have done with detection tracking we also mention our region of Interest area so now we want to mention our condition because we want to detect the wrong wrong way vehicle so direction is what vehicle is coming from this way and then it's go this way so this is the direction this is basically Direction coming and going out so what we can say here the rectangle our object rectangle bottom left corner this is the top left corner of our object rectangle this is the bottom left corner so when object bottom left corner this as you can see I have highlighted with blue color circle here it is as you can see you will see let me show you here because this is the four let's just mention eight then it will increase the radius so here it is as you can see so this blue circle is basically our rectangle object rectangle bottom left corner bottom left corner so how do we get the bottom left corner CX and Cy coordinates simply we have here X3 Y3 X4 and y4 so CX is basically X3 and Cy is basically y4 this X3 and y4 is basically our rectangle bottom left corner coordinates so if you want to highlight that particular area as a circle we want to mention CX and Cy X3 and y4 and then we can simply draw the circle C2 Circle method CX and Cy the radius color and minus one for because we have we want to fill our Circle so this is how you can highlight your circle uh we can say we can highlight the center point which we use as our condition simple so we highlight our Center Point now we can simply create the condition when our rectangle this point the bottom left corner if it is inside our area one only then we can detect our vehicle otherwise we don't want to detect all other area of vehicle we only want to detect the vehicle which basically coming from this direction from this Direction Only We want to detect and track because only this vehicle in this particular scenario only this vehicle this area vehicle is basically going wrong side simple so for that we want to create our condition if the vehicle point which we highlight if it is inside our area one then only we are going to draw detect and track our vehicle for that we have CV2 do Point polygon test method so simply here result is just a variable result is equal to C2 do Point P Capital poly p Capital poly gon T Capital test CV2 do Point polygon test and as I mentioned what is our condition we have our area and if our Center Point means if our CX and Cy if it is inside our area then only we can use a C2 Point polygon test method so first we want our area so our area is this this method so just copy it first we want to mention our area one so area one is this np. array so just copy and just paste as it is that's it just paste it just a minute we want to paste copy it from np. array to np. in32 copy it and just paste as it is that's it now after that what is our next condition we want to mention our CX and Cy because whenever CX and Cy if it is inside our mention area then only we have our result so CX and Cy and after that mention false simple now if we print our result if you save the code if we run the code the result is in between one or minus one so when the center point if it is inside our area we have one here it is as you can see let me show you here it is as you can see the one means our Center Point the highlighted area CX and Cy is now inside our area and if outside our area then we have minus one so we can simply create here a condition if if our result if it is greater than is equal to zero then only we are going to draw the circle then only we are going to draw the rectangle then only we are going to put our text on our rectangle top left corner so now other area of vehicle will not be detected only when the vehicle in our area one and in that particular time we have detection here it is as you can see our Vehicle area bottom left corner inside our area there is a detection but now the car cross over area one now there is no detection as you can see there is no detection nothing car just pass our area now when our car inside our area one in that time we want to save their ID and their current position we want to save their ID and their current position for that I have mentioned here a dictionary W app is just a VAR varable and inside this dictionary we are going to save their ID and their current position when our car inside our area one so this is what our condition after that we can simply say we want to call our dictionary which is W up this one W up so w up and then we want to mention the vehicle so what is our vehicle our vehicle is basically the ID because we assign the ID for each vehicle so if the ID is there means if that particular vehicle we are going to saving so w up ID is equal to their current position their current position so their current position is basically CX and Cy simple now if you print here print our dictionary which is W up so when our vehicle inside our area in that time we have their ID and their current position we are basically saving so here it is as you can see for this car we have two is the ID so two ID inside dictionary and their current position as you can see so now this car is basically crossover area but we save now we know that this particular car is already crossover area one simple because we save their ID and their current position now when there is a next vehicle so now here it is as you can see this car is basically coming and when it cross our area one right now we only save one car the car ID and their current position so now for this car when this car bottom left corner bottom left corner cxn c y inside our area 1 and as you can see now as I mentioned object rectangle bottom left corner inside our area one in that time we are saving their ID the 21 is ID for this card and their current position so basically we are saving the ID means we are saving the car and their current position whenever car inside our area one now we know that our car is already pass our area one so if the same ID if it is inside our area two that's it now we can simply say the car is basically going wrong way simple so after that here we can simply say because now we have each car ID inside our dictionary so we want to call that ID so if the ID if ID in W up which is our dictionary then the same ID same car now inside our area two so same condition this is the condition Point polygon test same condition copy it paste it only we want to change here area and variable so because we mention here already result so this will become Result One and area one will become area two and all other things are same and then the same condition if the result if it is greater than is equal to0 is equal to Z then only finally we are going to draw the circle we are going to draw the rectangle we are going to put our text on the frame so now if we save the code now we can simply start our while loop save it run it and now watch this this car crossover area to and just a minute is there there is something issue oh here it is as you can see I have mentioned the result here we want to mention this variable which is result one result one now the car when cross over area one there will not rectangle and nothing when the same car cross our area two in that time we have detection because we mentioned we don't want to draw the rectangle and all other things only we want to draw a rectangle and all other things if the same car crossing our area too so watch for this car it will cross our area one then we are saving their ID here you will see the next ID here it is as you can see 21 and then the same car inside of area two in that time we have detection simple now we we have the ID so now simply here I have create a wrong way empty lead so simply we can say here after we have all condition match we can create the counter means we want to append the list so what is the counter list name wrong way so wrong way wrong way do upend and we want to append the ID and because there is some time for crossing area one to area two so meanwhile it will check same idea again and again it will count same idea again and again for that simply we want to mention if wrongway dot count is equal to equal to Z means please don't count same ID again and again again and wrong way do append now if we print here now we are saving the ID inside the list so length of list is basically our counter length of list is basically our counter so now simply we can create here let's just say W uh W is equal to our counter list which is wrong way wrong way and the list length is basically our counter simple now just copy this is on putut text method and mention after polyline and then here we are going to mention our counter on the frame so here F ID we are going to mention our W because our W is counter and then variable let's just say wrong way car wrong way car simple wrong way car and now here we want to mention the coordinates so let's say 60 and 100 we can make changes for coordinates so let's just say 600 okay so now it's good now only we are going to increase the rectangle size and the font size 2 two save the code run the code now we have the counter how many car basically go wrong side so right now there is a zero counter and uh there is something issue because our counter is not increased what is the issue let me wrong way print print wrong way and let's just comment this I think there is a issue in this condition oh we we forget to mention our ID because if the wrong way do count what we are going to Counting we need to mention here ID that's why the list is empty if wrong way. count we want to mention here ID because we are saying that please don't count same ID and we not mention the ID that's why there is nothing that's why there is nothing okay now it will work now it will work just a minute again I want to mention W length of our counter W is equal to length of our counter which is wrong wrong way now it will work save the code run the code now zero and as you can see the counter is increased so we count one car which go wrong way which go wrong way now for this car the core cross our area one we are saving their ID their current position and if the same ID same car cross over area to then we are going to count the vehicle is basically going wrong way so we count two car go wrong side I hope you will learn something from this video we'll meet on next video till then thank you take care and bye-bye
Info
Channel: FREEDOM TECH
Views: 1,006
Rating: undefined out of 5
Keywords:
Id: 1HBF5ytGKcg
Channel Id: undefined
Length: 25min 0sec (1500 seconds)
Published: Fri Nov 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.