People Counter using YOLOv8 and Object Tracking |People Counting (Entering & Leaving)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video tutorial we will see how we can create a people counter using YOLO V8 so as you can see in this video tutorial as the people crosses this line like this one is going up so we in the green signal we have up counter and these persons are coming down so you can see that that uh here in the red we have a down counter so two persons were drowned and the counter says two persons are going down and here we can see that the four persons are going up so the counter is incrementing in that way so we will be Implement creating this people counter in this video tutorial so don't skip any part of the video and do watch the complete video so let's get started so let me explain you first in the first step the complete process flow so let me open the slide presentation which I have created for this so in front of you I have actually in the front of you here is the complete process flow of this project how we will implement it so first of all uh we will implementing the yellow V8 object detection so using a yellow V8 object detection we will detect the persons okay so you can see that uh we have a bounding box around the person and we have a label the person and here we have the unique ID so each of the detected object will be assigned a unique ID so you can see that here we have a 8 32 33 46 and I think this is hidden so it will have some different ID so basically uh we assign each of the object a unique ID like you can see here we have four or five and they are not generated in a like in a in a straight way like after one two three four five it is generated as randomly like for example this object can have a eight and here this object is assigned a 32 ID so it is basically the object ID is assigned randomly okay so in the first step we need to process the detections and as a result objections we get the bounding box around the detected object and the label Okay so after getting the bonding box and the label so we need to find the center coordinate of the body directed object okay so this is the center coordinate of the detected object and the bounding box so in the first step we detect object assign a label and in the second step we find the center coordinate of the detected object or you can say that the center coordinate of the bounding box so here we have the bounding box and this is the center coordinate of the bounding box so after finding the center coordinate we draw the center coordinate using CV2 dot Circle so here I have drawn the center quadrate as well okay so after finding a center coordinate we do the object tracking so we will be using deep sort for object tracking so what does dim sort object tracking does is or any other object tracking algorithm does is it assigns a unique ID to each of the detected object so you can see that here the person is detected and here 46 this is the unique ID and here a this is a unique ID so this unique ID is being signed by object tracking algorithm which can be any so here in this project I am using deep sort while you can use sort or any other algorithm as well so using object tracking we assign a unique ID to each of the detected object okay then after assigning unique ID to each object detected object then in the next step we draw lines okay so here you can see that we have created our first line like you can see over here and this line is for the purpose when the person crosses this line we get the up count and here we have the second line so when the person crosses this line we have the down count so how this all happens okay when we we say that when this Center coordinate this Center coordinate of each of the detected object passes this line then we we then we have created a list then uh after that detected object Center coordinate passes this line the unique ID is added to the list okay the object for example here is the A8 is a unique ID so when this first person uh when this sun crosses this line a this unique ID like this person unique ID is added to the list okay so I have created an empty list whenever a object person or any other object crosses this line the unique ID of that project is added to the list okay so we add the unique IDs uh of each of the object that crosses this line okay so all these unique IDs are added to the list and then we find the length of the list so when we find the length of the list we get the total count like and two three four five so in this way this count gets incremented okay similarly for this when the person over here watches this line in the down count we also add them the unique IDs into the list for example this person crosses this line so we add this unique ID of the person into the list empty list which we have created and in the end we find the length of the list so what does it say is so example basically in this uh KSP are only focusing on the center coordinate so when this Center coordinate of top detected object you can say that or the bounding box center coordinate of the bounding box crosses this line this line which we have created in the green okay so when this red bonding box crosses this line we add this unique ID of the object into the empty list which we have created and so in the end of that video when all the objects have passed through this line we find the length of the list and we say that that this is the total count of the uh the objects which have passed through this line so basically in a video we process the detections frame by frame so after each frame we get here the increment like the total count uh in the current frame of the object going up or or going down so for example this uh this basically this you can say that this counter is incremented after each processing after uh each of the frames so as you process the current frame uh this counter get the result so if any object has crossed this line so we can say that the down counter gets incremented and if any object has crossed this line we can say that the up counter will be incremented okay so simple concept whenever this Center coordinate or you can say this Center coordinate over here crosses this line then we say that we then we basically save the object unique ID into the list and in the end we find the length of the list what is the length of the list and this process is repeated after each frame like we we calculate the total objects in the current frame that have crossed this line and this process is attributed after doing the processing on each frame okay so let's see the code okay let me go towards the code okay just give me a minute so here we have the code I think you can see the code okay so for this project I will be using deep sort object tracking let me open the GitHub let me open deep sort object tracking over here I think I need to add the link I just just give me a minute let me copy the link [Music] so I'm just adding the link over here just give me a minute okay so so for this project I will be using this GitHub repo over here so this GitHub wrapper yellow V8 deep sort object tracking will be used for this project and in order to get the collab file you just need to click over here open a new tab so you will have the collab file in front of you so so you just need to open this whole app file over here and here we have the complete script over here so so just give me a second many session so let me terminate the previous sessions and let me try to explain you all of this so for example here you will go to runtime and select the runtime as GPU okay so in the first step you need to select the runtime as GPU and then click on connect okay then we need to clone this GitHub wrap over here okay run anyway okay so now we are just loading the GitHub repo okay then we need to set this uh repo as our current directory then we need to install all the dependencies this will install all the required libraries required to run this project successfully so it might take few seconds so let's see okay so this is basically installing all the required libraries which are which is required to run this project okay so if we you skip this step in later stage on when you run the training prediction or validation script uh if you have not installed the dependencies then you will face the error that following library is not installed or some Hydra library mac.it library is not installed so it's necessary that before that doing the training or prediction install all the dependencies required to run the script okay so as we are doing detection so I will go to the detection folder and which is over here you will owe me Aid detect so I will just go towards this folder and just SEC set this folder as my current path so just setting this folder as my current path and Ctrl V now as we are implementing object tracking using deep sort so I will be downloading a deep sort files over here okay so let me open the predict.5 script I think the Deep sort files are downloaded over here if we see we have the Deep sort folder over here so we will be using this deep sort folder to implement the object tracking okay so I will open the predict.pi file so here I will make the changes basically necessary to run this project as per our requirements so basically we don't need a trails in this place so I will first so I will first remove all that fails from here okay [Music] so just remove all the trails from here which is over here okay so first we will remove all the trails and then we don't need this data DQ ID over here okay so just remove this as well remove this as well okay and we need that to calculate the IDS and just remove this as well okay so good okay so basically we first need to create two lines over here okay so first of all uh let me open the uh my input video on which I want to run this script and then first of all I will see I need to create two lines so let me first download this video input video over here so just me just I have doing the implementation previously over this script so here I have the video file path so let me just click over here and just go to my Quran folder okay so just give me a minute okay so here we have our transcript file so just write we will not be using this time of video we'll be using this demo video for this project so just uh right over here okay so and just download this video so it might take few seconds or okay so we have the video to over here so we can see that if you see video two dot MP4 so just download this video over here and let me make some changes over here okay so it might take few seconds okay so I'm just downloading the video okay over here okay so the video is downloaded let me just go over there let me just give me a minute please Okay so let me save a photo from this video okay that's good so I have let me share my screen so guys I have saved this photo from the video okay so basically now we need to create the two lines for the account and put a down count so first of all we need to find the coordinates for these lines so first of all I will just go over here and select the color as and just give me a minute okay so I have selected the color as red so you can first of all I will create a line over here okay so this is line four is for the up count okay so when the person uh process this line we have the unique ID saved in the list and then we can play the length of the unique IDs and we can say that uh these number of people have crossed this line and we have this number of total account okay so first we find the point coordinates for this line so here we have 1 14 209 and here we have 307 173 so how do I find the coordinates basically uh just focus on this where my arrow is currently please Focus like in Windows above it basically Focus over here so these are the coordinates for this line these are the X1 y1 coordinates and these are the X2 Y2 coordinates okay similarly we will create a line put a down count over here as well okay so these are the coordinates please Focus over here okay so these are the coordinates for the X1 y1 for the down count and these are the coordinates for the X2 Y2 for the down count okay so let me get back to the script okay so now now I'm back to the script okay so first of all I need to Define this coordinates for this line okay so just remove this we don't need this as well okay so I have done already this let me just follow the steps over here okay so for example uh this for the video too I have found this coordinates okay so just copy these coordinates from here and just click over here and just Ctrl V over here okay oh no I am not copied correctly okay so just control C and just control it okay so basically these are the X1 y1 coordinates and these are the X2 Y2 coordinates for the line okay and these are the X1 y1 coordinates for the downline and these are the X2 Y2 coordinates for the down line okay so in the next step what we'll do is in the next step we need to create an empty list okay so just copy this empty list over here which I have already created so whenever the unique ID or object crosses this line which we have created so this this is a list where we store the unique ID of the object which crosses this line okay so let me explain this concept from this okay so uh I think okay so whenever this object crosses this line like this person crosses this line okay so this person will be assigned a unique ID okay so this person the unique ID will be stored in this list which we have created now so when the person crosses this line down person so this person recording will be stored in a separate list okay so just going back towards the code again okay so we have created two lists total account up total countdown okay so when each of the persons process this line when the up person crosses the upline so we can say that we still store the unique ID into total counter place when the person going down crosses the down line so we will store the ID in the total countdown list okay so in the next step what we will do is okay so we don't need this draw Dash border function up you can have this as well okay so in the next step we need to calculate the width and height of the bounding box okay so let me add this okay let me update the Define UI box complete function over here okay so we don't need to have okay so let me just update this all function I think after this I don't need to change anything so before video I've already implemented this script so in order to save the time instead of doing all the implementation over here Okay so just go here and just replace this all with this updated script and just click on Ctrl s okay let me explain you function over here okay uh here we are just finding the you can say that X1 or and y1 coordinates let me tell you what C1 and C2 is okay just give me a minute let me open the or let me open the PowerPoint slides so X1 y1 are basically these two points this top left corner points I think this is not the way I choose just give me a minute [Music] okay let me just share my screen okay I think you can see my screen so basically uh the point over here let me just write this okay so this this corner point please focus on this corner point where I am highlighting okay so these are my X1 y1 point or you can say that this is the C1 point which we have represented and this two point is my C two point which is represented by X2 and why do okay so this is my C1 point which is presented by X1 y1 like this point okay this corner Point like you can say the top left corner Point uh is my X1 y one point okay and that uh bottom right corner point which is over here this is my X2 Y2 coordinates okay similarly for here these are my X1 y1 coordinates and similarly for here this is for my X2 Y2 coordinates which is represented by C2 and C1 represent the X1 and y1 coordinates so let's get back to the code okay so here we have just defined our C1 and C2 coordinates and here we have the c x and c y okay so what is the c x c y basically let me explain this to you as well let me open the image okay so now I have opened it on image I think you can see damage okay so like c x and c y are the center coordinates okay c x and c y represent the center coordinate okay so c x represent the uh uh value on the x-axis and c y represent the value on the y axis so please Focus over here if you please focus in the start above the start tab okay here so here you can say that the first point 287 is the CX and 316 is the Cy okay so c x and c y represent the center coordinates so let's go back to the code so here we have found the c x and C Y which are represent the center coordinates of the bounding box or of the bounding box and here using Circle we have drawn a circle so let me show you again I think you have understood but if someone has missed so I think we should know Okay so as you can see over here using CV2 dot Circle we have created a circle over here using the center coordinates using the center coordinates we have created a circle using CBA 2 dot Circle over here okay okay you can see this okay let's go back to the code and explain the rest of the code and here we are creating a bounding which using cb2. rectangle I am creating a bounding box around the detected object so let me explain this as well so you can see this bounding box around the detected object so using CV2 dot rectangle I am creating this bounding box okay okay let me go back to record again okay so in the next phase if we have the label like if we have the person detected or any other object detected then using CV dot two dot rectangle basically I am just creating let me just go over here okay so using CV2 dot rectangle I am creating a rectangle for the label okay so let me go back and explain this to you as well so using CB to dot rectangle below I am creating this rectangle okay so this rounded rectangle you can see that so basically here I have used uh rounded rectangle function for to create a simple rectangle we use CV2 dot rectangle so this will create a rectangle for uh the label where we put the text so this rectangle like this rectangle is created using CV2 dot rectangle okay so if you want to create this rectangle you can use CV2 dot rectangle to create this rectangle okay let me go back to the code and explain you the rest okay so now to put text into the rectangle I've been using CV to dot put text so using CV2 do not put text I will put the ID of each of the object and plus I will add the label of the object into the rectangle which we have created of the above so here you can see that using CV would output text I have added this idle ID unique ID 46 and the label over here which is person okay similarly unique ID is 94 and the label over here is person okay so using cb2. output text I have added the ID and the label as well okay let me get back to the code again okay so now basically we are saying that use in the c x and c y are basically the center coordinates okay so what we are saying that if the CX value okay let me explain this to you choosing let me explain this to you using the image okay let me go back to the image so just opening the image here okay so I can just remove all these things from here okay so what we are saying that if the center coordinate over here is this Center coordinate crosses this line okay then we will have the unique ID of this object like this is the unique ID will be stored in the empty list which we have created above okay so like this when this coordinate crosses this line then we'll have this unique ID stored in the empty list which we have created above so let me get back the code okay so this is what we are saying that if this Center coordinate crosses this line then this unique ID will be saved dot append ID so you need this unique ID will be saved in the total count of list which we have created above like this is the total count up list uh which we have created above for the up count okay and we can say that when this uh plus here we are adding minus 50 and plus 50 so let me explain this concept to you as well okay so let me go over here okay so you can see the image over here as well so we can say that uh like this Center coordinate might go above this line okay or for example this Center coordinate process above this line which we have created like this line which we have created so this Center coordinate might cross is above this line okay might pass above through this line so here uh we are saying that we will accept plus 50 or minus 50. so if the uh you can say that if the center coordinate is above plus 50 or below 50 uh in the height in the range then we will append the ID into this total count up list okay and we are here we are defining CO2 dot line so when this Center coordinate passes through the line our line color changes to Green already it is green so it changes to Green when the center coordinate crosses the line okay so here we are just uh when the center coordinate crosses the line we are saving those IDs unique IDs of the object into the total count up list similarly when the so let me just open here so similarly when this object when this object coming from here crosses when the center coordinate crosses this line okay so then we will save this unique ID into the total countdown list okay so let me go back to the port so okay so when the person going down Center coordinate crosses the line then we will save the unique ID into the total countdown list okay and then we're here using civil dot Circle we are creating these two circles over here I think here I don't have the created a circle let me go over here okay so using CPU dot Circle I have created these two circles over here and I am just putting the total count of the persons going up and going down so green represent the persons going up and two represents the persons going down okay so we are just saving the total count of the persons going up and down into so the total count of person going up is represented by the green uh plate over here green circle over here and the total count of the person going down if it is represented by the red circle over here okay so what we will do next is uh write me so this is the total count of the persons going up we have created and here this is the total count of the persons going down and we are just finding the length of the list uh that basically we append the unique IDs in the list and then we find the length of the list and we can say that these number of persons are going up and these numbers of persons are going down okay so this is our final script and we just need to run this script and see what results do we get okay so let me see if I have missed anything okay so basically I need to Define okay I have just missed one thing let me just go over here so basically we need to define the global total count total down variable and we need to create a line so using cb2.9 we will create the line so just going back over here okay I think uh it's not allowing to save let me open this okay just give me a minute okay I will just okay so I need to change the shapes need to save the Chinese drive so just copy to drive click on copy to drive okay so let me open over here okay I think I need to run the script over here just terminate the previous sessions now okay so this is a small mistake I made I should have saved it at the start okay so just give me a minute uh let me the session is restarting okay my sessions okay terminate the previous session and let's just run this GitHub repo again over here so just running a cell over here installing all the dependencies downloading deep sort files okay please make sure that the runtime is stacked as GPU which is selected okay so this might take few seconds okay so here we have um and just open it and let's make this changes okay so just go over here and basically I have just showcase so let me copy all this I have as I've already explained you the core concept all this over here so just paste it over here okay I will add I will provide you this predict.wifi as well okay so I've just missed one thing which was I was not able to explain so here we have to find the total count up and total count down as the global variable which we which are the empty list which we have initialized above and using CV2 dot lines we are basically okay let me explain this to here using cb2.2 lines CB2 dot line I am just defining these lines I am just adding these lines into my video okay so just click over here and just go back and let's Okay so this all these things are fine okay here it's also fine so just run the script so let me download the the video for this script which is over here so just give me a minute so I can download the video to run this script which is over here and just go over here so just update this and just run this node okay so just missed one thing okay so just go over here and here we will download the sample video from the Google Drive so just close this now for now so here just Ctrl V and now we are downloading the sample video from the Google Drive one which we want to test okay so video two and just add the path over here video two and now learn the prediction dot Pi scripts so if there is any error we will fix it or if there is no error then it's good and the script will run and then we will validate the results so it might take few seconds for to execute okay so the predictions have started okay so our basically the whole video is divided into 632 frames and it is uh doing the processing on each frame one by one so this might take some time okay okay so it's 183 192 19 205 217 227 237 245 252 260 to 68 to 72 so it will take few seconds so let's wait and see what results do we get okay so our results will be saved in runs detect train and Radio 2 dot MP4 so we'll just copy the path over here and as the complete the execution I will display the demo video over here and we will see what results do we get from here okay so this might take few seconds more so let's wait and see what results do we get okay so 599 594 618 there 6 20 6 26 632 so let's run this now so it might take some time but it will complete quite soon okay so okay so our script is running and it is on the go so video two is our output video file name here we have and let's see what results do we get okay just pausing the video as we have the results I will be back so guys our output video is here and our script was done successfully and here we have the output video Let's download it and see what results do we got okay so just downloading the video and let me open that video just shifting my screen just give me a minute so here is our output video you can see that when this person crosses this line uh we have the account and when this person will cross this line this we have the two and these two persons have crossed this line so you can say that we have two over here and here we have like one two three four and here two persons have also crossed this line so here we also have the two count down count and here we have the up count like four persons have gone above so this is all from this video tutorial I will share the script file in the description of the video so if you like this video do subscribe the channel and give it a thumbs up thank you for watching see you all in the next video tutorial till then bye bye
Info
Channel: Muhammad Moin
Views: 13,197
Rating: undefined out of 5
Keywords: yolo, yolov8, objectdetection, objecttracking, computervision, pytorch, opencv, opencvpython, machinelearning, peoplecounting, peoplecounter
Id: xwW3tBkMAmI
Channel Id: undefined
Length: 35min 28sec (2128 seconds)
Published: Tue Feb 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.