Real time shape detection - Opencv with Python 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone to this new video tutorial we're going to Serie now shape detection L in a live video so for example I have few objects like this one we want to detect the shape of this object for example this one is a rectangle this second object a circle this object kind of rectangle but not exactly we're going to do this in three simple steps first one we need to detect the object removing the background so we will we will have a mask second one when we have the masks we find the contours finally third one from the contours we're going to detect the shapes just as that let's start and let's see first we start detecting the object so we remove the background we import CV - we import numpy us and Pete let's load the video from the camera cup is equal to cv - that video cut sure 1 1 it's my second webcam if you have only one web communy to use 0 0 for the first one for the second and so on for the other webcams while true now we load the frame from the webcam underscore frame is equal to cab dot read let's show the frame cv through the team show frame and then frame what key went key is equal to cv - that white key 1 so if we press any key if he is equal to 27 which is the S key on the keyboard we break the loop so it means we want to stop the video once we back the loop we release the camera cap dot release and finally CV to destroy all knows let's run the script if everything is correct we get the video from our webcam and we get it so it's fine first as you notice I took objects we only read color to find a mask there are different methods we're going to use the HSV color space if you don't know what is this you can check on my other video tutorial so we're and it's about detection based on the color of the object we're going to apply that method right now so let's stop this one we can press esque on the keyboard and we will stop the video let's apply the HSV color space so as it is HSV we need to convert the frame which is BG our color format so it stands for three channels blue green red in HSV HSV is equal to C V so that CV T color we convert the frame to see v2 dot color underscore bheegi bheegi R to HSV once we have the HSV we need to define a range as we cannot detect only one specific red as we can have more red some based on the light in the red can be in some part brighter in some part darker so we need to define the range we have lower red and we need to create an array and P dot array and let's define now let's put zero zero zero for the moment later we will change this and then moment upper red which is 2 n P dot array 180 255 and 255 and then we use this range the mask is equals to see it so that in range of the HSV lower red and upper red and now see if it suited him shop mask and then mask and let's run the video and you can see oh nothing changes as we need to set properly the parameters of the range for example the red let's try to change the first one minimum of 19 so it's HSV color space and we are defining the lower the lowest age hue it means hue as for saturation and v4 value the same for you these are the upper value so that we can define a range we can find the proper one by testing it many times now I've found properly the red almost we can see some detection of the red to work properly we'll should have only the red shall be white in the mask and all the rest should be black we can adjust it trying many times but it's better to create a track bar so it will be much faster so let's do this way to create a track bar first we need to create the window so CV to that name it wind off let's call it frame and it's the window where we're going to put all the chart bars after that we create six track bars because we have six values values that we want to change in real time first one CV to dot create track bar and then track bar name let's call it lower H and then on the window which is frame and then the value of this one 0 standard value and the maximum value 180 and then nothing function this function we're going to create it here just called death equals nothing X and then pass short explanation of this each time we use the track bar we can pass some function here so if we want to do any operation here we can do it as we don't need to use this function we just put function nothing and we just do nothing but we have the first one let's create six of them I copy this one five more times one two three four five we are going to work with lower and upper edge three the first three will be for the lower so lower H lower s lower V and then upper edge upper s and up for V and here will give all the maximum values so the lower it's zero 20:55 the maximum 255 180 studies this the starting value of the function or the track bar as here is the maximum we put alt the maximum 255 and 255 and it should be okay and now we let's run the script and let's see if we have the window with the track bar and at it's a mistake I put the track bar on the frame and I want to put it on the track bar window so we can also keep it on the frame but it will be on the same frame of our image for a matter of being more clean we it's ready to use a different window so track bars let's copy this name in all of them this way so we have specific window we can change the values and nothing changes on our mask you can see this part nothing is really changing right now nothing is really changing because we need to call it later on here in these values that we have here we need to call this function each timing we can change the value real time so let's go inside the loop we are inside the loop here so how did it work we work we take first frame we're converting in HSV then we define the ranges we create the mask and we show the frame and this is made each time so that frames it the video it's just the sequence of these images it depends on the camera 25 times per second or 30 times per second or 6 it depends on your webcam in this case it shall be in my case 30 times per second so let's now define these values that you can that we can change then dynamically first one lower H is equal to C v2 dot get track bar position the track bar name is lower H and the window name is track bars we copy this one here let's test this first one i run the script and you can see that it's changing something in the mask as I'm changing this value in real time for all the others nothing worked because I haven't defined them yet let's do the same for all of the six values lower as it's equal to trackbar lower ass then lower VI we copy them doing it faster lower V over V track bars and upper h4h and an upper H upper s and up V so keep in mind that we are getting the position of each stock bar so we wanted to get the position of the first one so the track bar name and the windham name must be exactly the same first one second one and so on for all the rest now that we are getting the values let's copy them in the right position all the lower lower values here and all the upper here and I think I did a mistake as it should be probably lower age zero to 20 55 and the last one should be 180 I'm not sure right now I'm it doesn't matter that much from the moment and 255 255 and 180 and 180 everything should work correctly let's run the script let's now find the mask for the red color no it was for me right before us the mask with the all the ranges allow it it shall be completely white so it was correct as it was before so this one was 255 255 and this 180 and 180 255 and 180 and it should be correct I need sunlight okay this way now let's define the mask for the red only the red must be allowed okay should be okay it's I will define this distant mask that we can use to have this standardized I'm going to copy these values on the track bar so lower H will be 68 lower V 154 so we don't have to change them each time let's keep this as standard civil cop 68 154 68 154 and then 180 255 into 1 and 43 243 we have the mask and we were done with the first step from this mask let's now create a truck start let's find the contours from this mask so let's go ahead counter the Tachyon let's put it this way and we have the function in OpenCV see if it will not find contours the function returns has three values the first one we don't need it so we put underscore we need just the contours which is the second one so we define contours and the first value we don't need it so we put under scopes to skip it and see if it so that find contours of the mask the mode seaweed so that red trip and method sieve it will chain aprox simple now that we have the contours we can draw them for contour let's called for CNT in contours let's draw see v2 dot draw contours we're going to draw the contours on this frame let's put it inside of an array C and T so inside the brackets this value I don't remember exactly what what this tent first will honest 0 we'll check it later we can check it and then the color of the contour let's make it black zero zero and zero and let's run the script and here we have contours let's make it thicker so let's change the thickness let's put it to five let's run the script again and we can see that we have the detection off not only the right object also some part of me as I don't have a really proper proper mask but anyway we will we will clean this part on the video when we have not so clear mask as we want to define only the object we can do a couple of things first we can using morphological transformation remove some noise we've seen in some tutorial the erosion metal so if this area is the pixel it's like bigger than 5 / 5 pixels for example this small dot if it's bigger than 5 or 5 we can say okay delete all of them let's do this way let's apply the erosion method on the mask so a mask it will be equal to C V 2 dot erode we want to wrote the mask and we define the colonel Colonel is basically a square where we define okay take this small square and then delete it so karna is equal to NP dot once 5 5 and then NP dot you int 8 this is just an umpire method to create a black square in fact if we show it suited him show Colonel it's nothing else that just this window you can see this black small square nothing more and let's close this Colonel we erode the mask and we're already using this mask this erosion method so you can see how much cleaner is now the mask so there are not all that small dots as the word before it's a good result so let's move on we have so first we need the first we did the first two step first we detected the object by the color second we detected the contours now it's the final step to detect the shape of the contours first thing let's do one thing we can see really clearly that this for us it looked like a square this red part but it's not for the code as you can see it's not perfect because a square rectangle so units square it has to be four sides here there are four sides but the line is not completely straight so it it looks like if in this one line there are four exam ten lines you can see one two three it's like more connection of many dots so we need to approximate these contours to make it only a square we have the approximation method of the contours using a function of OpenCV and let's do that right now draw contours of CNT we have aprox procs it's equals to c v2 dot aprox poly DP we want to approximate the contour C and T then we define the ratio we can change this value value later let's see what works best for us let's use now 0.01 per arc for as CV two that are our length of CNT and closed so the the polygons are all of them close we're going to work with closed polygons and true also at the end closed polygons again and let's draw the contours with the approximation of Prague's and let's see what we get now instead you can see that now the contours are much more approximate you can see more straight lines and we can see the difference by printing something on the screen for example we print the length of the contour so it's a it tells us how many sides that count ourselves so print the length of C and T so we are printing the length of the original contours without the approximation and you can see this is the length of the countess let's print the length of the contours approximated aprox you can see how the number gets numbers get much smaller as we are going to approximate the sides we have this contour so now really easily we define okay if the length of the sides if 4 is 4 so we can say okay that's a rectangle if the length is 3 so if it has three sides it's a triangle and install on for all the figures so let's do this this way we can say if the length of aprox is equal to 4 so we can say it's rectangle print correct print a test it's a rectangle let's see now we don't get probably somewhere it's a technique for four of them we can see now it's a rectangle it's detecting this one let's do better this way mmm it was printing the rectangle already before there was the red rectangle because probably it was in this small part of the mask there was some really small rectangle and was detecting that let's improve this code ok I have an idea let's improve this this way we can see for example that here there is some noise and we are detecting some contours we don't want that how can we remove the noise well we can see that these are really small parts so we can say for example if the area of the mask is smaller than some number then we can skip it if it's higher so if we have a big object only in that case we can detect the object and find the contours let's do this way let's then find the area again instead of the contour so area is equal an area is equal to C v2 dot contour area of C and T and is equal to okay we will we draw the contours only if there is bigger let's say than 400 pixel pixels if area is greater than 400 only then we draw the contours and only then we can find the detection so let's do this this way let's put it inside this let's test it again now we can see that we have the mask we have some white noise but we don't have the contours because it's really small let's try with our big object let's see if something changed and we can see only now the detection it's a rectangle I will say it's good work before going and using other objects I have an idea let's put the text in real-time so we can put the text somewhere here telling us that this is a rectangle let's do this way if the Lang is equals to 4 then we put a taxi v2 dot put text on the frame the text let's call it rectangle and our I don't remember now this venue of the text okay okay yeah this is the position so x and y we will check that later the font the size and the color the color let's use again black color so let's say zero zero and zero size one okay let's define the font before the while loop we could define the font here but as the font it's only one it will use always the same there is no point to define it each time inside the loop it's only a waste of resources so somewhere here after track bars for example we can say font is equals to seaweed so that font Hershey complex you can use whatever font you want I will take the first one and here we have the the rectangle let's for the moment put position 10 and 10 and later we will find the right position so there is nothing on the screen there is no rectangle I use the object that the rectangle is detecting we can see in the top left on the screen that we get written rectangle now we shall take the position of the contours so that the text can be specifically on the rectangle so we can do it this way we can take from the contours X its approach that level so we are extracting the array of aprox aprox contains all the all the points of the polygon so we'll take just the first point I will put the text on that point Prague's dot Ravel first one is zero and why it's one so we can just put x and y and let's try again we can see where rectangle and the text is now following the rectangle which is exactly what we want what if we have different objects like circle which doesn't get even detected probably the red it's not like the red we had before so in this case we need also to change the mask for example like it's now we have different masks the red in this case we can define the red based on this on the number of the sides as we're approximating the circle usually the circle with this approximation can have between 10 to 2 twin sizes sides so we can count for example 1 2 3 4 5 6 7 8 so we can say okay if it's between 10 and 20 it's a circle let's add this one if lengths aprox now let's see if 10 is less than the length of aprox and that length of the proxy less than 20 then we can say see it without put text we copy this one and instead of rectangle it would be a circle let's try this one I need to adjust the mask to the tag correctly the circle 134 and it's taken correctly to see because the one in the middle and the one outside so let's use this new mask 666 134 let's see this mask work properly also with this one it's attacking some secret angle so there must be some improvement to make for sure but anyway 66 and 134 now let's add for the triangle so if the length if length of aprox is equals to 3 then see v2 dot put text triangle also one more thing to fix if an L if if because if it finds the first it doesn't go further if it's a triangle it cannot be a rectangle so it will stop the loop and it will move on if we put if in each one it will try to find again also if it's a triangle it look for the rectangle and it's a waste of resources it's so it's bad practice to just leave if-if-if and let's try now the attack see if it gets correctly detected somehow it gets triangle and circle somewhere so there is some noise to remove we can still improve the code a little bit let's make some more improvement for example let's change the ratio of the approximation set of 0.01 let's try 0.02 so we will have less sides triangle this looks works much better rectangle and let's see circle circles so so the mask is not working one percent fine also there is still some noise in the mask which is the technique so we can change the set the size of the the area for example to still improve the shape detection and you can do you cannot of course more you cannot depend are gonna fit it fit has five sides and you can improve of course much more this script for the moment that's all and thanks for watching
Info
Channel: Pysource
Views: 62,960
Rating: undefined out of 5
Keywords: shape detection, video, real time, python 3, opencv, tutorial
Id: AMFhjir4WgQ
Channel Id: undefined
Length: 35min 59sec (2159 seconds)
Published: Sat Dec 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.