Parking Space Counter using OpenCV Python | Computer Vision

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to create this amazing parking space finder and uh you can say it is kind of a counter as well so it is telling you how many free spaces do we have and how many uh spaces overall do we have so here you can see we have 69 spaces for parking and out of which 14 are available and now you can see this car is leaving this one is leaving as well so now it becomes 15. so all of the green spots are available to park and the red ones are not available now the best thing about this approach is that it is based on completely image processing techniques so we are not using any deep learning methods or machine learning it is a very simple method and it is quite robust as you can see so here for example you can see that there is a person moving and even when the person is moving the parking space is still showing that it is available so it is not affected by a person moving uh through the space so it has to be a big object like a car so if there is something uh that is as big as a car then it will detect it as uh occupied otherwise it is detecting as available space and you can also see we have also neglected a few of the spaces and you can see this region actually let me show it you over here so this region is not the same as this region and the reason is because it has this line in between so it's it's not like you wrote a for loop and then you shifted it over here uh with another uh what you call for loop so it's basically we have accounted for all these different changes and here you can see we have an extra space as well and here you can see we are neglecting the space as well so all of these varieties and all of these uh different approaches we have added in our code and believe it or not it is very simple and it is a very good project if you are starting your image processing and computer vision journey so without further ado let's go ahead and start creating this project because it is quite a lot of fun and it is quite useful as well as long as the camera is not moving this technique should work very well so let's go ahead and start working so here we are in our pycharm project and you can see that we have named its parking spaces and we also have carpark.mp4 and carpark img so these are the two files that we will be needing throughout this project so the first thing we will do we will go to file settings and then we will look for the project interpreter and over here we are going to add our interpreter and this will be from the existing environment that we already have and we will click ok and that will add our uh what we call packages here so here we will install cv zone and we will click on install package do remember that this is version 1.5.3 so if there's a later version you can try that if it doesn't work out well you can always go back to this version and try the exact same code and it should work fine so we will hit ok and it seems that the opencv numpy and the cv zone packages are already installed so what we can do is we can start off with our file now we are going to create two files one of them will be for the space or the parking space picker so we will call it parking space uh picker and the other one is going to be our main code which we will run and locate all the parking spaces and see if they are vacant or not so these two files by the way you can download from our website and they will be available for free to download all you have to do is sign in to your account and then you can download these files so here in the parking space picker what we are going to do we are going to first of all open up our image and here in the image you can see that we have a lot of parking spaces and what we need to do is we need to mark each one of these as our region of interest now one way to do this is using a for loop so what i can do is i can start from this point and i can say that okay for x in range for example 15 keep going down and creating these boxes but the issue is with that is that these boxes are not exactly of the same size because the image is not perfectly bird eye view so it's not taken from the exact top it is taken from a little bit of an angle so it's not very clear to see so therefore uh we cannot use the for loop because while it starts at the correct position when it starts to accumulate the error becomes quite big and we will not be able to get our correct values so what we can do is we can manually put these region of interest and another reason why we want to do it manually is because some of the spaces they are not for parking for example this is this place is for the trolleys so we don't want to check that and another thing to note here that this parking space is similar to this parking space but it is not similar to the third one the third one has a solid uh what you call pavement in between so you can see here this is not present in these two so if we wanted to replicate it three times we cannot we can replicate this and this but even then we will have to neglect these two and then we cannot uh replicate it here plus you can see this is a space which is not available but over here this space is available so there's a lot of irregularities within our regions of interest so it will be easier to do it manually plus we don't expect the camera to move at all so it will be fixed so you only have to do this manual process one time and then it will stay like that as long as you do not move the camera so this is totally worth going through the manual process rather than automating it every single time or trying to find these lines by default so if you use a contours method to find these lines that's just too much overhead that is not required in this project we can do it simply by some basic image processing and that is why this is a good project because we are not using any complicated methods we are not using yolo version 4 or 5 or mobile net ssd to actually detect the cars we are simply going to use basic image processing techniques to find the correct car uh not to find the correct car to find whether the car is present or not so this is a very good way to understand that you don't always need a very complicated method to solve uh such problems so we have this image and then we have this video that will uh by the way the video the camera actually moves a little bit i think the video is shot on a drone so it is moving a little bit but um it's it should still work fine as long as it's not a very big change so in reality if you are doing it on a cctv camera then you don't expect it to move you should have a fixed camera in a fixed position so in that case it should be fine so what exactly are we going to start off with we are going to start off by creating a file uh creating a program that will allow us to select and deselect these parking spaces we will put them all in a list and then we will take it to our main code so one file is for selecting and the other one is for running so once we select we are going to save it in a file as well so the first thing we will do we will import cv2 and then we will import pickle uh pickle is uh the package that we will be using to save the to save all the places or the positions of the parking spaces and then bring it to our main code so that is what uh we have the pickle for and then we are simply going to import our image so here we are going to write cv2 dot i am read and then we are going to read our image which is parking space picker uh no it's not that it's car car park image dot png and we are going to save it in image so that's simple as that and then we are going to create a while loop we will say while true and then we are going to say cv2 dot im show and we will show our image and we will call its img and then we will write cv2.whitekey and then we will give a one millisecond delay so if we run this this should open up our image there you go this is our image it is called image at the top here with the title and yeah so now what we need to do is we need to create a rectangle around it so we need to create a rectangle at that exact position so just for testing not not for actual values so what we can do is we can write cv2 dot uh rectangle and then we will put it on our image let's say we put it on 100 and let's say 100 and then the second point is let's say 150 and the height or maybe the width will be more what is the width it's about 100 so it should be let's say 200 and then this should be around 50 so let's say 150. then i believe we have the color yeah so the color let's put it at two five five zero two five five that will give us purple color and the thickness let's put it as two so let's run that there we go so what we are trying to do basically is to try to see what exactly is the size of our rectangle so once we have that size then we can fix the width and the height then all we need is the initial position so the x and x1 and y1 this is what we need and then we will automatically generate x2 and y2 so this is the basic idea so let's try to find this i believe i believe we can do it maybe here so this is hundred so maybe 200 something so let's say this is 230 and this is a little bit less so let's say 80 okay so this is 230 uh it should be around here so this should be around here so it's much less than that let's do one of them at the time one time so let's go back 50 i think 50 is good so the width is good now we need to push down the height so the height right now is 230 let's make it 217. oh sorry this is the one that is being okay um okay we need to add something here so let's say this is 500 for now let's just put it a big value so that it doesn't interfere there you go so yeah we have already crossed that point so we were trying to get this one i believe so let's say 200 so maybe 20 less 180 no 195 uh yeah so a little bit less than that 192. yeah so let's say it is and now we need to know the width uh the width let's say right now is 50 and 200 so it's 150. so let's say we put it at 100 and 20 125 that's less so we will put it at 100 and 170 little bit less 160 yeah so maybe a little bit less 157 let's say yeah i think that's that's good enough and then we have this uh the heights the height let's say is around this is 192. let's say 230 a little bit more to 40 yeah so i think this is a perfect rectangle that represents one parking space so 157 minus 50 is our width so we have width and we have height equals to uh so 157 minus 50 is 107 and then our height is 192 and 40 is 48 so let's see if that is fine so this is the width and the height that we have and then later on we can now just put one single place one single circle or dot and that will create this width and height uh rectangle based on the values that we have put here okay so let's keep this let's keep this let's not remove it now what we will do is we will put a position list we will call it position list is empty and whatever positions we have we are going to put here so every position will have an x and a y so just the starting point and then later on we will add this width and height to it so for displaying purpose and we will do it on in our original code as well the main code so what do we do next the first thing we will do is we need to detect the mouse click so to detect the mouse click what we have to do is we have to write cv2 dot mouse callback set mouse callback and then we have to give in the exact same name of the image that we are using so let's say this is called image so we will copy that here image and then we have to give in a function which we will call if we click on something so this time we will call it mouse let's say click so this is the function that we are going to call so when mouse click happens we will write it here let's call it mouse click there you go so or let's keep it small because um in in python they want you to have what you call they want you to have small names nothing capital but i like to add capital because i'm not using underscores that's why they give this error anyway so we can right mouse click here and inside the mouse click what we have to do is we have to call in uh we have to add in some parameters the first one will be events so it will automatically give us all these events then we will have x and y and then some flags and some other parameters so we don't care about these other parameters but we care about the events x and y so what will happen is that we will check if the events is equal to cv2 dot event underscore left button down so if we are pressing the left button it's called events okay so left button if we are pressing that then we want to append to our list so what do we want to append we want to append so we will write position list dot append what exactly do we want to append we want to append the x and y position so this is the basic idea of what we will do and what we can do next is we can display what we have created so what rectangles we have created so we can say for position for position in position list we are going to draw them and we can just copy this part because we are lazy so we will write that down and then here we will change the value so the initial value will be position and the later value will be position at 0 plus width and position at height plus height position at one plus height so that's the basic idea and if we run this if we run this and if we click on something there you go so now the rectangle has been created but it's not been saved per se but it has been created and you can see it fits perfectly so this is our first step that we wanted to do now sometimes we press on the wrong place so we want to delete as well we should be able to delete because we don't want to reset every time we make a mistake that will not be a very good idea so what we can do is we can say that if our event is basically right click button so we will right click to delete then we are going to check if our position point if our click point is in between one of these uh positions that we already have then we will delete it so we will check that for position in position list we are going to check what happened there for position is position list we are going to get the let's say uh x1 and y1 is equals to position we are going to check if our current x is in between x1 and x1 plus width if it's in between that box and our y is in between uh y1 and y1 plus our height so if it's in between that then it means we need to delete this so how can we delete um we need to know the iteration number so we can enumerate a new in new merit what is happening a new merit yeah i don't know what happened before uh enumerate and that will give us the iteration number i so here we will say that we want to delete so we will say position list position list dot pop and we want to pop at i simple as that so we will delete the i value whatever is in the i value so if i press in the left mouse button it will create and if i right click oops it did not delete so it should delete but it's not deleting why is that the concept is fine cv2 dot white key okay that that's fine uh if events is cv to write button down for i and position enumerate lists oh okay i know why it's not deleting because our image is imported once and whatever is drawn on it it will remain the same but webcams they don't work like this webcam will generate a new image every time or a video will generate a new frame every single time so this is a static image we need to import it again and again within the loop then it will get deleted so in the video we will not face this issue because it is automatically generating new frames so here if i click click click oops i made a mistake boom gone boom all of them empty so this is the basic idea of how we can create these rectangles and how we can delete them but right now we are not actually saving them we are just creating them so to save them what we need to do is we need to store it in pickle uh not store it in pickle but store it using a pickle object so how can we do that now the first thing we want to do is to see if there is a pickle object or not or or let's do it later let's do it later because that might confuse you a little bit so let's do that later on and what we will do is every time we append and every time we delete after that we are going to add to our pickle object so what we will do is we are going to write here with open and then we are going to write the file name here we are going to call it let's say car car park positions let's say and then we are going to give in the read and write permission so we will write wb and we are going to write as f and then within that we are going to write pickle dot dump and what exactly do we want to dump we want to dump the position list in the f in the file so this is the basic idea so what we can do is we can run this and if i click here now you will see there will be a new object that is created here a new file if we stop you will see there is a car park position so this is our pickle object now if i run it again what it will do is it will overwrite the previous one but we don't want that what we want is that it keeps the old one and then if we want to add or edit or delete whatever we can do it from there so to do that we will copy this part we will copy this part and in the beginning of the code we are going to check if there is a position if there is an object or not so we are going to write with open all of that but with rb as f and then we are going to check if we have that then we are going to put our file in position list rather than creating a new list we will say position list is equals to pickle dot load the file f so whatever is in file f put it in position list so we are going to try to do this we will try to find it if we don't find it then we will write accept we are going to create a new um what do you call empty list so that's the basic idea so now if i run this you will see the previous drawings are already there the previous markings are there so we can delete and only one is remaining now and i can put another one here so now we have two remember that so if i stop this and i run it again now i will have only two so that's perfect so if i wanted to keep adding more i can add here and then if i made a mistake if i uh went to my code and tried it out it's not working well i need to change a little bit then i can run this again and i can see okay this one is not good i will delete it and i will click on it again wherever i want so this way it is much easier and it is more efficient than actually uh doing it again and again from the beginning so for example here you can see i i added a gap here so i can delete it and then i can start again so now there is no gap so this is the way we can sort out all our boxes and once we have these boxes we can go to our main code and this main code is going to use this car position park so now that we have our parking space picker what we can do is we can create a new file here and we will call it let's call it main.pipe so here we are going to import cv2 we are going to import pickle and we are going to import cv zone and we will also import numpy as np numpy as np so these are some of the libraries that we will be needing later on and i will explain why do we need each one of these so the first thing we have to do is to open up our video so here we are going to write that this is our video feed so the video feed is over here so we can not not here this is carpark.mp4 so we can write cap is equals to cv2 dot video capture and then we can simply write carpark dot mp4 dot mp4 so this will add our video and then we can write here while true then we will get the frame from our video so we will write here that the success and the image is equal to uh cap dot read and then we are going to write cv2 dot im show and we will write image and image and then we are going to write cv2 dots weight key is one millisecond delay so this should be enough to run our main code and there you go now you can see the video is running the cars are moving they are moving quite fast uh because the the computer is able to process it uh with more than 30 frames per second the image the video is 30 frames per second but it can process it faster so it finishes faster this whole video is about 28 29 seconds uh but you can see it took about three four seconds to do the whole thing so that is quite uh a lot yeah maybe five seconds anyways so the first thing we need to do we can slow down the video we can simply right here for example 10 and that will slow down the video but what we need to do really is to loop this video because we don't want to keep testing by pressing the run button again and again so we want to loop it so to loop it what we can do is we can check its frame so we will write here if cap dot set uh not set cap dot get and we will write cv2 dot frame uh cap prop frame count i think that is the gap prop position frame yeah this is the one so this will give us the total frames that we have in the video and then we are going to check the current frame to check the current frame uh we are going to check cv2 now actually this one this is the current frame and now we are going to check the total frames so it will be the same cap dot gets and then cv2 dot prop underscore frame count is it frame count uh prop frame count yeah it's the same so this here is giving us the current position and this here is giving us the total number of frames that are present in the video if that is the case then we are going to write cap.set and we will write cv2 dot cap underscore prop underscore position frames as zero so we are resetting the frames if they reach the total amount of frames that the video has so if we run that actually let's put it at one again so it's faster so we can tell if it's going to shut down or not so it is running running running and there you go it started again so you can see here when the person actually uh throws the trolley here there you go then the video ends so you can see that happens quite a few times so it is repeating so we can put 10 here back so it will slow it down a little bit so that is what we need and now we are good to go and ready to run our code so the first thing we can do is we can import all the positions that we have so in the parking space picker so what we can do is we can use the same method of loading so we this time around we do not need to try because we know there is a file that has all this information so we can write here with open car position this and this position list is equals to this so now within our position list we can display all the what do you call positions by copying this part here using our rectangle method and there you go and the width and the height we need to declare again so the width and the height we can copy and we can paste here so if we run this now uh actually let's put it on one side here no put it back there okay so now if we run that uh we should get it with our image where is the problem image is not defined really main oh okay so we need to put this over here my bad so let's run that there you go so these are all the position points that we are getting from our position list uh from our picker and now they are over here so this is what we can easily do so this is all well and good now what we need to do is we need to crop all of these regions so [Music] actually what we can do is we can put it in a function let's just call it uh check parking space and we will put the checking of the parking space in this code in this function so here we can remove this part and we can put it here and we can just call this function over here and that should be good to go yeah same effect okay so here what we are going to do we are going to first of all we are loading all these uh rectangles now what we need to do is we need to crop our images one by one and then we need to check how many um we need to find a way to find if there is a car inside that or not so first of all let's just crop so here we are going to write so let's call this x and y is equals to position and then to crop it we can simply say that our image crop is equals to image and then we are going to give in the initial position so the initial y it starts from y so it will be y and then y plus height so height come on something is wrong so this is the height so this is the one we are talking about then we will say that our x and then x plus width so this will crop it and we can write cv2 dot i am show and then we can show the image cropped corrupt and we can write here image crop but because it will do it for like a lot of them so we can do it after the for loop so it will just show us one of them at the very end or if you want to see all of them we can do that too we can write here um we can write here string and then x so x will be a different value each time so or no it will not be so x let's say x multiplied by y let's say this is the name of the image so we need to have a unique name of the image so that it doesn't overwrite so it will show us all the different images that we have so there you go so these are the cars that we are getting so each car is a separate image and it's a good point that we have seen here each of these should be drawn later on so not before you crop this drawing should be done after the crop so that it doesn't have that purple region around it so if we run that wait it still has it why does it still have it oh it's not having it's maybe of the ah it's of the one above it so maybe maybe we should draw it at the very end like in a different loop so we can write here after doing that for position in position list and then we draw wait what happened oh i pasted the wrong thing let me go back cancel paste for position in position list list we write it like that so if we run this now yeah now you will see there is nothing no lines on the sides so now what we need to do is we need to find for each one of these we need to tell whether this region has a car present in it or not so how can we do that we can do that by looking at its pixel count so we need to convert this image into a binary image based on its uh edges and corners and then from there we can say that if it doesn't have a lot of edges or corners then if it's a plain image then it means there is no car but if it has then it means there is a car so how can we do that first of all we have to do some thresholding so to do some thresholding what we will do is after we get our image we are going to convert it into grayscale so we will write here image gray is equals to cv2 dot cvt color cvt color and then we will write that we want to convert our image and we want to convert it into uh color underscore bgr to gray so we wanted to convert it into gray and after the gray we can also add some blur so we will write here image blur is equals to cv2 dot uh haussian blur and here we are going to add the value of our image gray and then you have to given the dimension of the kernel so the one that i found is useful is three by three and here we give the sigma x as one so these are the values that i played around with and i found that this is a good value feel free to play around with these values and you can find your own and hopefully it will be better so we can check the result of it we can copy this and paste it here we will write here image blur and we will write here image blur so if we run that there you go this is our image now where is it there we go this is our image it is gray and now it is a little bit blur so it's it's a little bit hard to see but definitely there is a little bit of blur so that's good after the blur we are going to convert it into uh what you call a binary image and we are going to use threshold we are going to use adaptive thresholding so we will write cv2 dot adaptive threshold and we are going to give in our image blur and we will give in the maximum value as 255 the method as adaptive cv2 dots adaptive threshold gaussian c and then we are going to add a binary inverse so cv2 dot threshold binary inverse and then we have to give in the block size again these are the values that i found to be useful but again you can have different values the ones i found were 25 and 16. so these are the values again you can play around with these values you can even add track bars i will add the code for track bars later on and it will be on the website you can download it from there you can also add drag bars and you can move these track bars to check which value is the best so if you want to play around with that so let's check out the image threshold how does it look like so image thresh let's run that and there you go so this is what we are getting okay so if we have a closer look here you can see wherever there is a car for example let me write it here so for example here you can see there's a lot of white pixels there are some pixels there you can see here there are some pixels but when there is no car you can see there's a very few pixels here you can see the car is leaving and the pixels are almost negligible so the white the number of white pixels the same thing over here you can see the number of pixels are reducing here you can see there is no car this whole region we are neglecting so there is nothing there so that's a different story but other than that you can see here as well here as well so this is a good indication of whether there is a car present or not again how did i find this by changing the values by changing the gaussian value by changing the uh what do you call the adaptive threshold so you have only two values here and you have one more value you can add uh which is basically the median blur so there is a little bit of you call what you call um salt and pepper noise you can say little bits of dots here and there so to remove these you can use median blur so we can write here image threshold or let's say image median is equals to cv2 dot median blur and then we have to give in our image threshold and we have to give in the value for it so the kernel size the bigger it is the more it will effect so the kernel size that i found useful was five so let's run that actually we need to display the median now there you go so now you can see a lot of the dots they are removed they are well they are less than before now sometimes these pixels these values they might be a little bit thin so what we can do is we can make it a little bit thicker so that it's easier to differentiate between empty spaces and when there is a car so to do that we can use dilation so we will use the morphology function and we will write here image dilates dilate is equals to cv2 dot dilates so it's not dilated it's dilated so image dilate and i didn't put the g image dilate and then we are going to given our image so it will be image median and then we have to give in the i believe the kernel so the kernel you can't just give in the size you have to give in a kernel that's why we had to import numpy so we will use that to write it down here so here we will write kernel is equals to numpy dot once and the ones will be let's say three by three so this is the value that i found was good and we will write numpy dot integer of 8 bits so this is the idea actually you have to write unsigned integers of 8 bits and here you have to give in the kernel and then you have to given the iteration number so iterations is equals to one you can give more iterations but uh it might get a little bit too thick so you have to be careful so there you go so all these have a little bit of effect but together they really make up the whole algorithm and it makes it work quite nicely so now is the moment of truth we need to take this image and we will crop these uh this image one by one so instead of cropping what you call actually we should have an image here let's say img uh image um let's say processed let's say pro image processed so we will send in our image dilation as our image processed and then the image processed is what we are going to crop so if we run that now you will see all these are the ones so now we need to count the pixels how many pixels each of these uh car spaces has if it's a lot it means there is a car if there is not a lot of them it means there is no car so how can we count so we can count simply by writing uh count is equals to cb2 dot non uh count non-zero pixels and we have to give in our image so it will be image crop and that's it so this will give us the count number so now what we can do is we can write this count number in front of the box so we can write here cv2 dot uh put text or you know what yeah let's do one thing let's use a cv zone instead so we will write cv zone no not cv2 series or cv zone dot put text rect and we will put it on our main image and then we will give in the text the text will be basically our count so string and count and then the position will be let's say the starting position is x and the ending position is y plus width y plus height minus let's say 10. so let's run that and there you go so now you are getting the numbers but they are really big so we need to scale them down so scale scale is equals to 0.2 okay that's too small [Music] let's put scale is equals to one better better but i believe there is an offset and there is also a thickness thickness is equals to one and the offset is equals to zero let's try that yeah so now it's much better uh maybe we can increase the thickness a little bit uh thickness is two let's say and the scale is let's say 1.5 and we can push it a little bit further so let's say minus 5 instead of -10 yeah so now it is showing us the value of each of the cars and you can see that 1500 1700 uh actually let's make it a little bit smaller it's too big let's put it as one and let's make it minus three yeah now it's better so yeah so you can see here that these are all filled up and the empty one you can see the value is very low the difference is huge so here you can see it's 1 700 2 000 1 300 and the empty space is 400 500 or 400 200. so there's a big gap so what we can do now is we can put all of them and then we can check what is the minimum value what is the maximum value and then we can find something in between so let's go back to our parking space picker and here we are going to select all the parking spaces so let's remove this one and put it back again again it doesn't have to be perfect just an estimate so whenever you make a mistake you can reset i think this whole area is done yeah one of them is done let's do it for this one i think the the the height might be a little bit bigger than the actual size you can change the height later on as well from the main code it's not a big deal so all of this is done as well let's go here okay so this is done now we can simply close it and go to the main and run it and all of them should automatic oh you can see there's a lot of images being created actually we should delete that we should why is it so far away so as i mentioned the camera is actually moving a little bit so by the end of the video it's almost to this point and by the start of the video it's a little bit shifted again you can play around with these values but it should still work so let's stop this and first of all we will remove this i am show because that's putting up a lot of strain and we can also remove these two images and let's run that there you go so now you can see that um so this is an empty spot and the value is 600 this is an empty spot this this so 600 is the maximum till now then at this point is 400 something 100 something 200 something 200 so 600 is the maximum we can see here but to be on the safe side we can put it as 800 anything above 800 because we can see that the ones that have cars in them they are above a thousand two hundred three hundred so this is one thousand two hundred this is the minimum one uh where else this is 1100 so this is the minimum this is 1020 so we should not definitely go above 1000 for sure but i think 800 is a good good place so it's in between so we can say that if it's below 800 there's no car if it's above 800 there's a car so we can where is the count so this is the count so we can say if the count is less than 800 then and then if else this so if the count is less than 800 first of all we are going to change the color uh of the display so if we are displaying with purple uh let's make it green and red so if it is available it's green if it's not available it's red so color should be zero two five five and zero and we can copy this and we can paste it here and we will say two five five no uh zero zero zero two five five so this is red so if it's less than eight hundred means there is no car then green otherwise it's red and we can also change the thickness thickness if the car is present we want it to be like if the space is available we want it to be visible more visible so let's put the thickness as five and if it's not available so then we can put it as let's say why is there's an extra comma here okay we'll put it as two so it's easily visible um okay so now we need to display so instead of displaying it again at the end we need to display there so after the processing is done and the count is done then we can do that so we can bring it here actually we need to remove it from here so here we are going to run that and instead of the color we are going to put the variable color and the thickness we will put it as this so if we run that now you can see that the red regions are the ones that are not available and the green ones are the ones that are available and it is pretty accurate and as you can see when the car leaves it becomes available so let's see that oh there is a problem here it's going to 800 plus so either we need to change the values a little bit or we can make it 900 let's say so it shouldn't effect there you go so here the car is leaving so now it's empty space here the car is leaving now it's empty space so that's pretty good and as you can see here this is a very good part when the person is moving in between here oh it's still flickering a little bit of flicker is fine okay this is around 900 so that's not good ah the placement is bad okay so i think we need to the the problem is the camera is moving a little bit so that's why it's a little bit difficult so what we can do is we can go to back to our parking spaces this one we can push it a little bit back all of them so that it doesn't have a big effect like even the camera movement it should not have that big of an effect but again the best part will be to actually stop the camera for moving that will be better than doing all of this and i believe this one as well it was moving a lot so let's run that again with the main and yeah now it's not flickering this one is flickering because the car is leaving that's fine but here you will see the interesting part when the person is coming inside it's not being detected as a car it's still space is available so that's good so it means if there is a big object if there is a big car then only it will detect so here you can see it's leaving so it becomes green here as well it's about to leave so it will become green so that is pretty good but i don't like that this is purple let's make it red as well so the cv zone um where did we put it oh okay it's in the main so here we can change the color we can change the color of the rectangle rectangle and we can make it red so 0 0 and 2 five five so now it is red so you can see so it's red and green so the only thing left now to do is to check uh how many spaces do we have and uh how many total are there so because we didn't actually count them so what we can do we can put it after the for loop yeah so every time it counts we are going to um we are going to add to our space counter uh space counter is equals to zero and in the beginning and every time it is less than 900 we will say space counter plus equals one and at the end of the day we are going to displace so we will put again let's put the text rect we are going to put it outside the loop outside the for loop and this time we will write a space counter and let's put it at let's say 100 and the height as 50. the scale we can put it as let's say 5 thickness as 5 offset as 20 uh color we can keep it purple so let's just keep it that way or let's put it green better i think green will be better scale is too big let's put it at three and we will write here color uh rectangle is 0 uh 200 and 0. this 255 will be too bright of a green yeah there you go so yeah 13 spaces 14 now because it's 14 because left and what else yeah this one is leaving so 15. but let's let's write let's write something with it so here let's make it an f string and we will write here what do we write here let's say free these are the free spaces out of the length of our so out of total which will be the length of our position count our position list so no matter how many we have 69 we have 69 spaces so 13 out of 69 are available so this one left so now it's 14. uh this one left as well so now it's 15. so these are the free spaces that we have so yeah so this is the basic idea of this project and to be honest it's quite simple and you can see that a very simple approach can be powerful as well as long as you are using the correct method it should be robust enough and it should give you some good results actually let's change the color of this as well um [Music] so we are putting the text no we should put it after so we should put it here let's say this is for the count yeah so we should put the rectangle first and then the count and we can use the same color scheme that we had there so the color is this and the thickness okay so the thickness we don't care about so the green ones will be green as well so the number of theirs will be green so i think yeah that looks better yeah so this is the basic idea of this project i hope you have learned something new if you like the video give it a thumbs up and share it with your friends if you found it useful then give it a comment below and let me know what did you like what did you did not like and i will try to come up with more of such examples so this is it for today and i will see you in the next one
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 618,549
Rating: undefined out of 5
Keywords:
Id: caKnQlCMIYI
Channel Id: undefined
Length: 66min 42sec (4002 seconds)
Published: Sun Dec 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.