Count Vehicles on images | with Opencv and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we do not only detect the vehicles but we are detecting also this car right here that it's not even that's so visible this like tiny car right there also this red car far far away uh it's working extremely well hi welcome to this new video tutorial my name is sergio and i help company students influencers to ease and efficiently build visual recognition projects we're going to see today an object counting project more specifically on a vehicle counting project from images let me show you something i have a folder where i have a lot of images where not a lot i have maybe 10 or 20 i don't know but i have a few images and i want to automatically count how many vehicles are on each image and then also the total of the vehicles of the entire folder um i'm just going to open some random image just to give you a look how the vehicles look like so for example we have one two three four four cars in this one then let's go to the next image with two cars one two okay this is uh one two three four and so on so how do we proceed we will make first of all an audio detection project where we will be able to detect and surround with a box uh all the vehicles on each single image and then by using this object detection script we will also loop through all the images in in real time we will make accounting of all the vehicles but before starting i want to let you know that there is a contest regarding vehicle counting from a drone footage this contest is organized by murtaza's workshop and i will give you more information at the end of the video so keep till the end keep keep watching till the end and let's start oh if you're new to computer vision don't worry everything will be simple to follow just remember that you need the library opencv that you can install with this command so first of all let's open the terminal cmd and this will work similar on ubuntu and mac as well for the installation of the library pip install opencv dash python and you press enter i'm not going to do that because i have it already then second you need to download some files i will put the link to download the files on the description below because i have created these files to make things more simple and these right here are the files also there are the images that i'm using if you want to use my images but i recommend to use your own images so for all these files there will be a link below we have dnn model which will include the files for the deep learning model to detect the vehicles vehicle detector is a file i've created i can show you just to make things really simple to extract uh the coordinates of the vehicles there is no need that now you know this it will be really simple just follow along with what i am doing there will be vehicle counting which is the script that i'm calling right now so let's start first of all we're going to import cv2 which is the opencv library that we will use uh to load the images and to extract some information now let's start by loading just one image we have a lot of images but to make things simple let's start with only one so i'm going first of all so to define emg equals cv2 dot in read to read the image and then images now let's take only one image i take this one so we're pretty much putting just the path of the image that we want to download so this is the name of the image and then jpg how do we make sure that we're loading the image correctly there is no better way to show the image cv2 dot image show uh we can go this whatever we want the cars it's the name of the window and then what do we want to display we want to display emg now this will be enough to load the image to show it but we need to remember to use a weight key event to keep the image on hold otherwise it will open the image and then the the code finish the execution and the image gets close so we need a weight key event for that reason weight c2.weight keep zero to keep the image on hold and now let's run this one if we see this it's correct if we don't see there is some error whatsoever okay here we have uh the image with the cars now what is the first goal the first goal is to detect the the vehicles and surround them with a box for that specific reason we are going to import the file vehicle detector which is a python file which is on the same folder so we can do from vehicle detector import of vehicle detector now let's hear load vehicle detector so we imported the file but we need to load the object to use it later so we can call this vd which stands abrasion of vehicle detector equals vehicle detector and that's pretty much what we have to do now how do we detect the vehicles that are on the image it's so simple that you have no idea we can just do um we can say vehicles vehicle boxes equals vd dot detect vehicles where do we want to detect the vehicles we want to detect them on emg so i'm going to copy emg right here that's it that's all we have to do for vehicle detection now of course let's run this one and later i will show you something so i'm going to run this just to make sure that everything runs smoothly and that we don't have any error and we know the image we don't have any error now how do we know that we are detecting correctly the vehicles well uh we have these vehicle boxes which from which we you can understand that here we have some bugs about vehicles so the box will be the coordinates which indicates the position of the vehicle so let's print vehicles boxes and now we should get at least two boxes which are the biggest vehicles on the screen i'm not sure if there is anything behind them behind these cars but probably we have one box two boxes three so we have a few of them probably okay also because we have vehicles behind these cars which are somehow somewhat visible and they can be detected uh what you see here is an array inside an array we use an empire uh the only thing that you need to understand and here we have coordinates these coordinates define the position of each car we have the x and y position the first two numbers so x and y is the top left point so for example if this will be of this uh black car it will be this top left point right here than we have with 660 so width and then height so let's take this information and let's use it so we're going to loop through them for box in vehicle boxes now let's extract the information x y width and height from box now that we have these coordinates we can of course display them cv dot rectangle because the box is just a rectangle we want to draw the rectangle uh we want to draw the rectangle on where do you want to draw the rectangle on emg uh by the way these are all simple operations if you're not familiar with opencv it might seem all difficult but it takes just a few lessons to learn all of this so i recommend that you you check on my youtube channel i have a lot of other videos about open cv and also i keep uploading new videos so uh if you want to stay updated follow the channel rectangle ctrl rectangle emg now what do we need about the rectangle we know of course to know the coordinates where we are going to put the rectangle and we open cv we need two coordinates the first one is the top left point so if it's of this car we will need this point top left top left x and y and then we need the right bottom point the right bottom point will be the up the opposite extreme point so we will have x plus width and then y plus height now another thing that we need to define is the color what color do we want to show this box oh it doesn't really matter we can put like some red color how do we decide a color we it's the mixture of the three primary colors first one is blue from zero to 2055 how much blue do we want let's put around 25 how much green do we want from 0 to 255 we don't want any green mustard we want red let's put 180 of red and finally the thickness how thick do we want rectangle let's make it three pixels thick and now let's run this one and as you see here we have the detection which is working like in extremely well we do not only detect the vehicles but we are detecting also this car right here that it's not even that's so visible this like tiny car right there also this red car far far away uh it's working extremely extremely well this uh vehicle detection now we have okay we are detecting the vehicles and that's the first step but my project is about object counting so how do we count the object well let's that's even more simple than what we're doing right now it's to count the objects we can just say uh vehicle equal count equals is the length of vehicle boxes so to know the number we can just let's display this on the on the screen simple.put text let's put on emg let's put the text on let's put the number so vehicles are they calls and here we put plus the string of what halfway called count as simple as that now we need some information about the text where do we want to put the text let's put the text on the top left so we say just around uh 20 pixels from the left and let's say 50 pixels from the top then save it to um now we need to define the font i'm going to um to use zero so we have different type of font if you put zero you have the first font one the second three and so on uh i don't really care about the phone now we want just to display some information as simply as possible and as soon as possible size of the text let's say two color of the text let's make it let's make it blue or green let's put both of them so 100 of blue okay probably it will not be well visible okay whatever 200 and 0 and thickness of the text let's say 3. and let's check what we're getting right now with this okay not so bad so the vehicles count right here at the top we see eight one two three four five six seven and eight working really well and this is the first part of the project that we're detecting the vehicles on an image now how do with the uh how do we do this for multiple images on one folder not a big deal we take the folder uh simply we load the images from all the images from the folder in a loop follow carefully we have where the images on the folder images so load images from a folder so this is the images folder we have the library which is called globe import globe which will allow us to load all the path of all the files in a specific folder a glob dot glob so let me lab dot glob now we define the path images what do we want load from images we want to know all the files so we we say all with asterisks dot uh we can say dot jpg so all the files in jpg format you could say load all the files like dot asterisk everything if you have all select different formats formats like jpgs pngs and so on but be aware that if you have also other file like text files it might be a problem so i i recommend always to just select the specific images type and now let's print images folder and let's check if we're loading this correctly okay we see images we see the first image then we see the path of the second image third image and so on once we have this it's uh okay let me put the vehicle detector at the beginning not to mess things around once with the image what do we do we do for emg path in images folder we put everything that that's here below on the loop like this and now instead of loading just the image with this specific path we can load all the images so let's print for example emg path and then emg path i'm going to run this so i can show you what we see to understand the idea for example this is the first image that we have so emg path we have images and then this is the image so we use this emg path on in read in g path right here and now let's load this again and let's see uh like how we get the first image and what we display uh here we have an image we have vehicles for so we have one two three and four working really well even with really small vehicles as i showed before we're using a weight key event which is freezing the first image that we see so if i press a space bar now we see the second image we see now the third image okay this is quite big there is no space to feed the image on the screen but there are two vehicles so you can trust me there are two vehicles let's go on the next one we have 10 vehicles right here and so on we're almost at the end of the project if we wanted just to count all the vehicles that are on the folder we will do this operation uh uh right here on the loop so here we'll loop through all the images here we're processing the images one by one with the vehicle count of each single image we can make for example uh image um vehicles folder count and we say zero so at the beginning we of course don't know how many vehicles we had before looking through all the images so we set right here we start from zero each time that we find an image with new vehicles we're going to increase this number and that's how it will work so we're vehicles for the account we look through the first image and then we right here we get vehicle count and here we can update the total account so update total count uh once we updated the account vehicles vehicles folder count plus equals vehicle count and let's also show in in real time let's print this dot account print total current count and we see vehicles folder count oh let's run this one right now and let's see what we get so we have total current count with four because here we have four images we go to the next image which is which we are processing right now so four plus six we have total current count is ten we go to the next image vehicle to they are there even if we don't see there are vehicles on this image somewhere down below then we have total plus 10 we have total current count we have 22. now one thing that i'm going to do is we don't need to display each image like if we want just the total count at the end we i can for example put a comment on cv2 in show or even better we can use cv2.weightkey1 so instead of freezing the image we loop through the next one or but if you want you can just remove this or put a comment you don't need to display the boxes or the images oh i'm displaying them so that we know at which point we are at and we know that this is working so i'm not touching anything now it's sloping image after image and it takes one or two seconds depending on the speed of the computer to process the image if you're just using a normal computer if you're using the graphic card it will be really fast it will be at least 10 times faster i don't know what happened but i got some error which at some point i don't know i guess at some point it was trying to load an image that doesn't exist probably um i didn't get to the end for some reason but that's uh i will now solve the problem i don't know what it is but that's how it needs to be done okay i guess i found out the error already uh for what i see there is some accent on this letter and i guess that opencv can't read the image with this accent which i don't know from which language this is but i simply removed the accent so instead of being a with the accent now it's just a normal a and in the problem should be now solved i haven't tested this yet so we're going to check together in real time if that was the error that wasn't allowing us to load the image correctly now let's wait till the end there are not many images so i guess it will take only a few more seconds to complete this operation okay it was this one it wasn't able to load and now it's loading the image so that was the error the accent so be aware where we have some letters with the accent that you might encounter some of these errors it never happened to me before by the way you can see that is really precise even we when there are a lot of cars and it's hard to detect the the deep learning model is doing an awesome job even when the car is not complete when there is only half of the car we have the detection and we process the entire folder and you can see we have total current count we have 91 cars this is all for this project now let me remind you about the contest there will be a contest about vehicle counting from a drone footage the contest is organized and made by the famous channels murtaza's workshop and i will have the honor to be a judge we've also uh there will be another judge which is ritesh from augmented startup so we'll have the honor to uh take part with the two most popular channels on youtube about computer vision for the contest i'm going to leave you the link below you can submit your own project and there will be special prizes for the winners this is all for this tutorial i want to remember you that i will be uploading new content soon so i i suggest that you keep in touch keep updated with the channel so see you on the next video
Info
Channel: Pysource
Views: 19,698
Rating: undefined out of 5
Keywords:
Id: h1XhllUAA6c
Channel Id: undefined
Length: 24min 15sec (1455 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.