OpenCV tutorial for beginners | FULL COURSE in 3 hours with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey my name is Felipe and welcome to this three hour course on opencv with python this is going to be an amazing course we are going to start talking about what exactly are images in opencv then I'm going to show you how to read an image or a video from your computer then I'm going to talk about some basic operations like groping and resizing and then I'm going to walk you through the most important functions and filters like color spaces blurring threshold Edge detection drawing contour and I have also prepared a bonus lesson a surprise lesson which is something super and absolutely unique and original and you're not going to see it anywhere else on YouTube and I have also prepared two projects in which we are going to apply absolutely everything we learned in this course the first project is about color detection and the other project is about building a face anonymizer so this is going to be an amazing course this course is ideal for beginners in opencv and computer vision and now let's get started so the first thing we need to discuss in this course is what are images because if we're going to be working with opencv then we're going to be working with images all the time and we definitely need to know what our images in order to work with images right so let's talk about images the first thing we need to say about images in opencv is that images are numpy arise if you're familiar with python then it's very likely you will be familiar with numpy numpy it's a very very popular python package in order to do different type of mathematical operations and numerical calculations so numpy is a very popular pylon package and is the first thing we need to say about what our images in opencv images are numpy arise and you can see this is an example this is only a few lines of code a couple of lines of code and what I'm doing in this example is reading an image and then I'm printing these type right I am printing the image type and you can see that the output is that this thing much is an ampere right so this is an example you can do yourself also and you are going to see exactly the same output so now let's continue and now that we know that the images are numpy arise let's see what happens when we ask for the image shape right as images are numpy arise we can definitely print what's the image shape right this is something we can do with absolutely any nmpri and this is the output we add for example in this particular example in this particular image I am loading over here in this example and this is the how we can make sense of this information right when we get something like this the first element will be the head the second element will be the width and the third element will be the number of channels of that specific image right so we have hate with a number of channels right those are the dimensions of our image now let's see it with a very very specific example for example in this case this is the image of of a squirrel and let's assume this is the image I am loading over here and I got this output for its shape 720 1280 and three channels and this is exactly what this means right so we have a height of 720 a width of 1280 and then three channels right and in this case we have an image which is in BGR so the three channels are going to be the blue component the green component and the red components right so this is just like a very very specific example of how you can make sense of these hate with a number of channels information of a random image of an image you load using embroid so let's continue now let's talk about the composition of how an image is composed and we are going to say an image is made by pixels right and this is an example with another image in this case we have an image which is much much much much smaller and we we have the hate for this image is 7 the wood E15 and you can see that this image is comprised of many many different blocks right this is something we can definitely see because in this case we have a much much much smaller image and you can see that for example we have seven blocks in this com coordinate and we have 15 blocks in this order coordinate right and each one of these blocks in the entire image is a pixel this is how we are going to call to the basic component of an image right a pixel this is where the information will be stored in an image right absolutely every single image will be comprised will be composed of many different pixels and this is exactly the how the the pixels look like right for example in this very specific example each one of these blocks is a pixel right that's how they are called old and now let's talk about the range for these pixels right these pixels are going to have many different values let's talk about what are the values the possible values for the pixels and we're going to say that in most cases this value the pixel value will range from 0 to 255 right it will be an integral value will which will range from 0 to 255. that's very important and that's going to happen in most cases and we are not really defining what exactly is most cases but let's just keep in mind that this is what's going to happen in most cases in many cases now there will be some situations in which we will have an over value an old range or another different type of values for the pixels and a very specific example is what happens with binary images in binary images absolutely every single Pixel will be either black or white and its value will be either 0 for black pixels or one for white pixels and in some situations you can also see it like this which is either 0 or 255 right so in binary images we will not have a range from 0 to 255 but we will have absolutely for absolutely every single Pixel one of these two values either 0 or 1 or in some more cases either 0 or 255 and in another example is images which are made of 16 bits pixels right where absolutely every single Pixel is saved the information in that pixel is saved with 16 bits or also known as two bytes right I will say in most cases in most images you are going to be working with the images will be 8 Bits right absolutely every single Pixel will be only one byte but in some cases there are also images of 16 bits and in this case the values the possible values for absolutely every single Pixel go from 0 to 65 535 that's very important that's another thing we need to know about the images now let's see a few examples about this Branch for the different pixels and this is the same image I showed you a few minutes ago about this squirrel and you can see that now I'm considering only this very small square we have over here this very small white square and in this Square this is the information the pixel information for all the three channels right remember this image is comprised of three different channels blue green and red and if I look at those three channels in these very specific Square which let's assume it's something three by three right it's three columns and three rows these are the values for all the different pixels in this Square you can see that we have many different values in different many different values which are ranging from I think the the smaller one of all of these numbers is 15 and the larger one is something like 117 right so we are here in this case absolutely every single Pixel will range from 0 to 255 so this is an example of this case of what happens in most cases this is what's going to happen with all BGR images and then this is another example and in this case we are going to be working with a binary image so we are here right binary image pixel value is in 0 1 or 0 255 so in this case this image will be encoded absolutely every single Pixel will be either one or a zero right you can see that absolutely all white pixels are one an absolutely old black pixels are zero right so this is uh this is the last thing I'm going to discuss regarding what our image in opencv so remember an image is a numpyri we can ask for the image shape and the first element will be the hate the second one the width and the third one the number of channels then absolutely every single image is made by pixels and the value for this pixel is going to range from 0 to 255 in most cases but there will be other situations in which we will have a different range for example for a binary image absolutely every single Pixel will be either 0 or 1 or 0 255 so that's going to be all for this lesson and now let's move to the next lesson so let's see how we can input an output data using opencv and let's start with images so let's see how we can read an image from our computer from our hard drive how we can write an image back to our computer back to our file system and how we can visualize an image and in order to do that I am going to define a new variable which is called image path and in order to Define image path I'm going to import OS which is another library and this is how I am going to do the image I'm going to use in order to show you how to work with images in opencv it's located in the current directory within another directory which is called Data so this is how I'm going to Define image path I'm going to do the final like OS dot pad dot join the current directory data and the image is called build.jpg okay now I am going to define a new another variable which is called IMG and IMG it's equal to CV2 dot imrith which is the function we are going to use in order to read images using opencv remember this function this is the most important function in this chapter cbq Dot inbreed and then I'm going to specify the image path and that's all we need in order to read an image from our computer from our hard drive now let's see how we can do exactly the opposite process now let's assume let's suppose we have an image in our memory in our program in our script and we want to write this image into our computer this is how we're going to do we are going to call another CB2 function which is called IM right you can see this function it follows exactly the same structure as this one this is in real and this is in right so it's exactly the same a structure as in the previous case and we need to input the location in which we want to save this image so let's say this will be exactly the same location but this will be burnt out dot jpg right and let's just save exactly the same image back to our computer this is what we are going to do right and it's exactly the same image but we are changing the name we are same in this image with another name which is build out dot jpg right so I'm just going to execute this script and I'm going to show you how this looks like you can see these are the three files I have in this directory in this data directory bird is the image I have read from my file system from my computer and then this is build out this is the image I have saved into my computer this is the image I have saved using IMG right right so I am using in read to read this image and then I am using IMG right to write this image right it's exactly the same image we are not doing absolutely anything with it so we are just taking it from our computer and we are saving it under a different name that's exactly what we're doing and that's absolutely all we are doing for now so we have completed these two steps which are reading an image from our computer and then grading an image back to our computer now let's see how we can do in order to visualize images right we are just going to take this image we have red and we are going to visualize it using opencv and for this we are going to call CB2 IM show which is the other function we are going to see now and we need to Define two arguments one of them will be the name of the window in which we are going to visualize the image and we're just going to call this window frame or let's just call this email this window image because we are going to visualize an image and then I am just going to input IMG which is the image we are reading from our file system and then another thing we need to specify is this over function which is weight key and long story short the only thing we're doing with this function is telling opencv to wait to wait until I press a key so opencv is just going to open a window with my frame with my image on it and then it's just going to wait with this window on it and definitely it's going to wait forever until I press a key that's basically what I'm doing with this sentence and for now just remember that this sentence is very important when you are visualizing an image it's very important and I'm going to show you what happens if you forget to write it right but for now let's just execute this function as it is so you can see that now we are visualizing exactly the same image I show you in my local file system right this image we are reading and this is the image we are visualizing exactly exactly the same image and I told you that this weight key function is very very important so now I'm going to show you what happens if I forget to do it right now I'm just going to call him show and I'm not going to call weight key immediately after and this is what happens you can see I'm running the code everything has been executed successfully and there's no desolation visualization whatsoever so what's going on right now is we are visualizing the image but opencv just opens the window and just immediately close it right that's why we are not seeing it so long story short long story short shows remember to specify this function and this number we are writing here it's the number of milliseconds you want opencv to keep this window open take a look what happens if I specify 5000 5000 means five thousand milliseconds which means 5 seconds so this is what happens I'm just going to press play we are seeing the image we are visualizing the image and now after five seconds everything is completed and I didn't press any key right so always remember to specify weight key and if you do it with the number zero means e open CB issue is going to wait with the window open in different forever right so these are the three steps we should cover how to read an image from our computer how to write an image back to our computer and how to visualize an image using em show and now let's see how we can do exactly the same process but with a video videos are other very important type of data we usually work with in computer vision and with opencv so we definitely need to know how to do exactly the same process with a video but this is going to be a somehow simplified process because the only thing we're going to do is reading the video from our computer and then we're just going to visualize the video we are not going to write the video back to our computer for for now because that's a slightly more complex process and for now let's just focus on these two items so the process will be very similar I'm just going to import OS and I am going to specify a video pad which is something like this it's the video is located in exactly the same location as the image we just used and this is something like data and then monkey dot MP4 and then we need to call CV2 dot video capture that's the function we are going to use and we need to specify the video path and let's call it video right so this is exactly how we are going to read a video from our file system from our computer right so this is exactly how we need to do now let's move on to the next step which is visualizing this video and in order to visualize it I am going to Define a variable which is red and I am going to initialize red as true and you're going to see why in just a minute now I'm going to define a while and I'm going to say while red and this is how we are going to do I'm going to read frames from this video by calling video.rev and video.read will return two variables one of them will be red and the other one let's call it frame right and the way we are going to make sense of these two variables is that frame will be the actual frame we are reading from this video and read it's a variable which is it's a Boolean variable which means if the frame was read successfully or not because remember we are reading a video and as long as we have frames left everything will be okay and red will be true but once we have reached the end of the video we will not have any frames left and red will be false that's how we are going to make sense of the information in red it's a volume value which is true every time we have successfully read a new frame and it will be false if we have not read a new frame successfully so now let's continue what I'm going to do now is calling CB2 in show exactly the same as before now let's call this window frame because we are going to visualize a frame and this is the frame we are going to visualize right now let's call CV2 wait key but now instead of waiting indefinitely instead of waiting and putting a number zero as we did here let's do something different the video I am going to read it's a 25 frames per second so 25 frames per second means that we have one frame every 40 milliseconds right so this is what I'm going to do I'm going to tell opencb to wait 40 milliseconds after it visualize every single frame right I'm going to tell opencv to keep this window open for exactly 40 milliseconds and by doing so we are going to have the impression that we are going to see this video executed in real time because opencb is going to display one frame after the other the only thing it's going to do is display a frame it's going to wait 40 milliseconds it's going to display the next frame and so on but we will have the impression we are seeing a video running on real time right so this value we are going to specify for for the weight key parameter and another thing we're going to do remember that red is a Boolean value which specifies if we were successfully if we are reading a new frame successfully or not so this is how I am going to do I'm going to say if red then visualize the frame and if not then don't do it that's very important because once we have reached the end of the video we will not have any frame left to read and a red will be false and then we will have an error if we don't do this we will have an error so this is very important now let's see what happens I'm going to execute this code and you can see we are running exactly this code and we are visualizing the video I have specified and we are looking at a monkey which is peeling some sort of fruit and it's using absolutely all of its body in order to peel this fruit this is exactly the video we are visualizing and you see what happens right we are visualizing one frame after the order one frame after the other and after we have visualized absolutely all the frames the program just terminates and everything is just completed so this is exactly how you can visualize a video using opencv and something else we should do after everything is completed is calling video dot release and then CB2 destroy all windows this is very important because this is the way opencv is going to release the memory it has been it has allocated for this video for this object so always remember to call these two sentences these two lines after calling a video after doing something like this let's see if we can run exactly the same without any error and everything has been executed just fine so these two lines are very important and please remember to add these two lines at the end of every script where you are doing a similar process where you are loading a video where you are reading a video so this is exactly how you can work with the input and output of a video and now let's move to a webcam let's see how we can do exactly the same process but with our webcam in this case we only need to specify OS and we don't need to specify an image location or a file location because we're going to be working with our webcam and this is how we're going to do I'm going to call CV2 video capture and I'm going to specify the number of the webcam I want to access right in my case I am not going to access numbers 0 but I'm going to access the number two I have more than one webcam attached to my computer and now I'm going to access the number two remember this is the number the ID of the webcam you want to access if you only have one webcam in your computer most likely this will be the number zero but remember you could also specify all values if you have other webcams attached to your computer so in my case I'm just going to call the number two and this will be webcam and now now in order to visualize this webcam what I'm going to do this will be a very very similar process as in the previous case so I'm going to call a while loop but in this case we're going to call a while true because when we are reading frames from our webcam we never reach the end right we always have new frames to read so I'm just going to call while true and this will be something like this a red frame equal to webcam dot reads we are assuming red will always be true that's our assumption because there's not any reason to assume otherwise we are just reading reframes from all webcams so red always should be true and now we're going to do exactly the same as before CV2 in Show I'm going to call this window frame shows us before and I'm going to call I'm going to input frame then I'm going to say CV2 wait key and I'm going to specify 40 as well for now I'm just going to specify 40 and at the end of this chapter at the end of this lesson I'm going to share a few comments regarding this value in the case of a webcam but for now let's just say this is 4D and then you're going to release the memory of my webcam 2 so this will be something like webcam.release and CV2 um destroy all windows also also we need to specify this value which is the the amount of time we have to wait but as we are taking frames for more webcam continuously and where and well through ideally we should have a way to go out of this while true right and this is the way we're going to do it we're going to Define it like this and 0 x f f equal to or Q if this happens then we're going to break the loop right basically what this means is that we are going to wait 40 milliseconds after we are visualizing absolutely every single frame and once the user presses the letter Q we are going to go out of this while true that's exactly what we are saying here right we are going to break this way through when the user presses the letter Q that's what we are saying it's it's a little weird the way this is expressed but this is just the way it works in opencv so let's execute this script and let's see what happens you can see that now we are taking frames from my webcam and everything it's okay everything is perfect and now let's see what happens I'm just going to press random keys from my keyboard I'm going to press the letter P nothing happens I'm going to press the letter L nothing happens the other I nothing happens and now this is what happens if I press letter q i I am moving away and moving out of this while true right I'm breaking the way through I'm just terminating the execution of this program so this is exactly how you can work with a webcam how you can read a webcam and how we can visualize all the frames from a webcam now these are my comments regarding this number so we have input this number which is 40 but the thing is that when you are reading frames from your webcam the process is slightly slightly slower than when you are reading friends from a video you have in your local computer the process is slightly slightly slower so the amount of time you have to wait the way you compute the way you calculate how much time you have to wait is slightly more complex so we are not really going to cover the how to set this number in this chapter in this lesson for now just remember that the amount of time you should wait between different frames it's it depends on the amount of time it takes to the webcam to read a frame and also to the amount of frames per second you are getting from this webcam so the process is slightly more complex as in the previous case so this is going to be all for now so let's talk about some basic operations in opencv let's talk about image resizing and image cropping in this lesson we are going to learn how to resize an image and how to crop an image so let's get started let's start with resizing I am going to import OS and also opencv obviously and now I'm going to Define an image which is called IMG and this will be CV2 in read and in the location of my image which is located in my current directory and is called dogs.jpg now let's see this image size I am going to print image.shape and I'm also going to visualize this image so we know how it looks like we we need to do something like image image right I can create a new window and I am calling this window image now I'm going to call CV2 weight key and I'm going to tell opencv to keep this window open forever or until I press a key and let's see what happens you can see that this is the image I am reading from my hard drive this is the image of two dogs and if we look at the print I can see here my terminal this image size is 960 times 1280. so this is the image size and this means it's 960 pixels height and 1000 and 280 pixels width this is the current size of our image and this is how the image looks like now let's try to resize this image into a different size I am going to call CV2 dot resize that's a function we are going to use in order to resize an image I'm going to input my image and now I'm going to input the new size for this image the size I want this image to be resized too right so I am going to resize this image in half I'm going to to resize the width in half and also the head in half I have to input two numbers the first number will be the new width which I'm going to say this will be 640 and then the second number will be the new height which I will say this is 400 and 80. this is half 480 is half of 960. I could have used a calculator but I think I didn't need to do it so this will be the new size this is a new size of our image and I will call this new image resize image now let's print the size of the new image and also let's plot it okay now I'm going to press play and let's see what happens and this is what we get you can see that this is the original image and this is the resized image and you can see that the height of the original image is twice the height of the resize image right and if I look at the width I see the same proportion the width of the resized image is half the width of the original image so everything seems to be working properly and now let's see at the prints this is a print of the original image remember 960 times 1280 and this is the print for the resize image you can see that the resize image hate it's exactly a hate I have specified over here and the resize image width is exactly with I have specified over here please mind that in here in the resizing we are specifying width and then hey 8 but when we are printing the image shape we are getting hate and then wait please mind this detail because otherwise it could be a little confusing so this is exactly how you can resize an image using opencv and this is going to be all for the image resizing obviously you can choose whatever size you want and also you can make the image larger if you want you can play you can play around with different sizes you can do absolutely anything you want also you don't necessarily have to keep proportions for example if I do something like this 640 640 if I make the image into a square you can see that this is the precise image it doesn't follow the proportions of the original image but opencv just comply with my commands right opencv shows gave me the the resizing I specify although the image is obviously not keeping the proportions of the original image this only to show you that you can specify whatever number you want here so this will be all for the resizing and now let's move to the cropping and let's start by importing OS and also CV2 the same way as we did before and now let's start to by defining IMG the same way we did for the resizing let's do something like this CV2 join corn directory and then dogs.jpg and I have to say this is equal to this okay and now let's work with cropping let's visualize the image again so I can show you something this is the image I showed you before and in order to show you how to crop an image I am going to show you how to crop this image so we only keep the dogs right you can see these are two dogs which are somehow centered into a huge image right so I am going to show you how to group this image so we only get the dogs so let's stop this now and the way we are going to do it let's remember what's the size of this image I am going to print image shape we get 960 times 1380 and the way we are going to Define this crop will be something like this I'm going to define a new variable which is cropped image and we are not going to use an opencv function the way we are going to crop an image is by selecting the intervals we want for the crop so remember that an image in opencv it's an NPR right so the way we are going to do it is by just specifying the intervals we need and if we go back to the plotting we can see that the docs we are going to crop are pretty much in the center so they are pretty much in the let's say in the third part of the hate and in the third part of the width right it's something like third and third so I am going to do something like this and let's see if it works I am going to Define my cropped image like uh this is height first so this will be something like 300 320 and 640 right I'm just going to keep the middle third of this image and then for the win I'm going to do something similar I'm going to specify it from 422 to 800 around 40 something like that it doesn't have to be perfect and I'm going to visualize it let's see if it works it didn't work so let's try a little better let's say this will be something like let's say 220 and 6 740 and 320 and 940. did see now and now it's a little better so you can see that now we are cropping the image now we are getting a new image we are uploading a new image and this image is a crop of the original image if I do something like this you can see that they pretty much much right you can see that we are getting a match and the way I did it if you if we go back to the code is by specifying exactly the interval I'm interested in in the original image you can see that I have specified 220 740 for the hate and if I go back to the original image you can see that 220 740 for the hate we are please follow these numbers which are over here 220 it's around here for the hate and then 740 it's around here right around here and then for the width we have specified 320 940 so 320 we are here and 940 we are around here right so this is exactly the crop we are getting we are grabbing we were cropping exactly those intervals we have specified and this is exactly how you can crop an image using opencv so these are basic operations but these are very important operations in pretty much any computer vision project you work in most likely you will need to do something related to image resizing image cropping or both because these are basic operations but these are very important so this is going to be all for this lesson and this is exactly how you can resize and crop an image using opencv so color spaces this lesson is about how to work with color spaces in opencv and this is one of my favorite topics actually I really really like color spaces I really like all the theory behind color spaces and we could spend hours talking about color spaces but obviously in this lesson it's just like a very introductory course a very introductory lesson about how to work with color spaces but please remember we could go as crazy as we want and as deep as we want because there's a lot to talk about when we talk about color spaces but let's take it one step at a time let's start this lesson by just taking an image reading an image from disk and visualizing this image right so I'm going to import OS I'm going to import CB2 and I am going to Define an image which is called IMG and this is CV2 in read and all start join and the image is called beer.jpg and then I'm just going to visualize it by defining two arguments IMG which is the name of the window in which we are going to visualize this image and then the image I'm going to visualize and this is weight key and the number zero right and if I press play something is not right build.jpg I forgot the extension now everything should be okay so this is the image I have chosen for today's lesson and you can see that this is a build a green bird and there are a few extra colors on this image too so in order to get started with color spaces in order to get started getting some intuition behind color spaces every time you read an image in opencv using imrid this image will be in the BGR color space which means that absolutely every single Pixel in this image is a combination of the blue green and 100 colors right absolutely every single Pixel is just like a combination of these three colors for example if I put my mouse here and please take a look at these values over here if I put my mouse there you can see that it says R 140 G 201 mb61 which means the value for the red color is 140 for the green color is 201 and for the blue color is 61. so this is what it means that absolutely every single Pixel is a combination of these three values and this is how the BGR color space works right in the BGR color space that's the name of this color space BGR absolutely every single Pixel in the image It's a combination of these three colors and remember every time you read an image using opencv this image will be in the BGR color space now let me show you a function which is the function we are going to use in order to convert from all the different color space from macro space for to another color space which is called convert color right this is the opencv function we are going to use in order to convert an image which is in a given colored space to another color space right so we have many different color spaces and we can convert the image from one to another right we are still getting some intuition behind how color spaces work so just uh this day wants to buy a time now let's convert this image from the BTR color space to the RGB color space and this is how we're going to do it right and let's call this new image image RGB now I'm going to plot this image and also image RGB right so I'm taking my image in BGR in the vgr color space and I'm converting this image into the RGB color space which is a different color space if I press play you can see that I'm still getting the same image as before and now I'm getting this new image which is image RGB which it's exactly the same image in a way but the colors look as if a little different right and this is because we have converted the image we have converted the color space from BGR to RGB so as I mentioned before we could definitely go crazy with color spaces and we could talk for hours about color spaces but you can think about all the different color spaces as different ways to represent an image or different ways to express the colors in an image right or different ways to express the information in an image in this case this was the original image in bjr this is the image in RGB and you can see that the the way these two color spaces work pgr means blue green and red RGB means red green and blue so basically what we are doing here is switching the colors we are switching the blue and the red color it will thing that used to be blue now with red until we are run right we are switching the position of these two colors right that's what we are doing so if we visualize the image into using IM show as we have converted from EGR to RGB you can see that everything that used to be read like this peak this feather over here and these three or these stick or these three leaves or something like that now everything looks like bluish right this is because we have switched the position of the blue under red colors but it's very important to notice that we are still looking at the same image in a way right we are still looking at the same information the only thing we are doing is just changing how this information is organized right but that's the only thing we're doing then it's exactly exactly the same image that's I think it's the most important intuition the behind color spaces and that's what I want you to take from this lesson all the different color spaces which are many many they are only different ways to express the information they are different ways to organize the information right another very popular use case of converting between color spaces is converting to grayscale so I'm going to copy and paste this line and I'm going to say from BGR to Gray and I'm going to call this gray now I'm going to convert my image my video or image into grayscale and this is how it looks like sorry copy paste and this should be all okay now I'm going to visualize all of them the BGR the RGB and also the grayscale and this is the RGB we don't really need the the RGB anymore so we are going to I'm going to show you the degree scale the the idea the intuition behind this color conversion from BGR to grayscale is having the information from three channels into only one channel right before our information was among the blue green and red Channel which are three channels so we have a lot of information for because for absolutely every single Pixel we have a value for blue for red and for green that's a lot of information and we are somehow condensing all this information into only one channel right we are creating an image which is a grayscale image so we only have one channel and we have different variations of the same color right we only have one color and we have different variations and that's how a grayscale image looks like you can see that now if we look at here we have only a value which is says L 111 and if we go here it says 153 and then 159 and so on right we only have one value we only have one channel now let me show you another color space which is very very popular remember there are many many many many color spaces so we can definitely spend hours looking at all of them it's not the idea for today's lesson I'm just going to show you a few of them and I'm going to show you the HSB color space which is another color space which is very very popular in computer vision and it has some very very popular use cases I'm going to show you how it looks like so now I'm going to plot and just going to plot the original image and the HSV so I'm going to do something like this so you can see that this is the original image and this is the HSV image which looks like super super crazy right this doesn't really look like a viewer this doesn't really look real this doesn't really look like anything we have seen before and it's exactly the case right this is the HSB representation of this image this is the image the original image converted into the HSV color space and this image in HSV in the HSB color space has some very very very important applications some very very important use cases for example if we will be trying to detect color we will definitely use the HSV color space but if we look at the HSV color space we are going to see something that AWS it doesn't make any sense whatsoever this is only to show you that all the different color spaces some of them or actually all of them they are very useful for their use case for their purpose but some of them may not have any sense whatsoever when we are looking at an image in that color space for example in this case looking at an image in the HTTP color space may be completely meaningless for for us for a human right when we are looking at an image we are looking at this image with our eyes with our human vision and our human Vision it's trained to look at a BGR RGB right to look at the images as a combination of the green red and blue colors so if we look at something in HSB it looks very crazy but remember this is another color space which is very very popular and very popular in computer vision you're going to find many many projects many applications of um the HSV color space and one of the most popular applications is color detection and that's pretty much the one of the most popular color spaces in computer vision and if I show you a list if I show you this list you can see that these are all the possible values we have for these constant right the here we used color BGR to gray here here we use color bdr2 RGB color BGR to HSV in order to do different color conversions and these are all the values for that parameter for that argument for that constant these are all the different values we could choose right because remember there are many many many color spaces and we could perfectly be converting from any color space to any other color space so that value that constant could take different values right you can see that this is a super super super long list and this is only to give you an idea of all the different possibilities you have when you are converting between color spaces so this is pretty much all for now this only to give you some ideas some intuition behind color spaces so this is something like the tip of the iceberg of color spaces obviously we could spend hours talking about all the different color spaces and there's a lot of mathematics behind color spaces so trust me we could go crazy talking about color spaces and and talking hours and hours probably we could create an entire course many entire courses about Corner spaces so we could go as deep as we want this was only like the tip of the eyster and please remember that there are many many different color spaces all of these color spaces are different ways to express the information of an image to express the colors of an image under different use cases for which you want to use a color space or another one and also remember this function which is called CBT color and this is a function you are going to use in order to convert from one color space to another color space remember we could go as crazy as we want with this topic but now is not the time so this is going to be all for this lesson and let's move to the next lesson in this lesson we are going to work with image blurring I am going to show you different opencv functions you can use in order to apply a blur on top of your images image blurring is very very important you definitely need to become familiar with this technique it has many different use cases and maybe one of the most popular and one of the most common use cases for blurring an image is to remove noise remember every time you work with images or with data you will be dealing with noise and it's very important to become familiar with different techniques in order to remove the noise on these images so let me show you all the different functions from opencv you can use in order to apply a blur this is from the opencv official documentation and you can see that we have these four functions in order to apply a blur these four functions are basically the same in terms that you will always be blurring your images but the it will be different the way you apply this floor right for now just remember that you have many different options different functions which you can apply in order to blur your images and before starting with the coding before showing you these functions and how they work I want to give you like a very very high level intuition on how blurring works every time you are going to be blurring an image I want you to think about averages right every time you will be applying a blur you will be averaging your images you will be Computing averages so you will be replacing absolutely every single Pixel by the average of all the other pixels which are around it right by the average of all the pixels in a neighborhood around that given pixel right that's like the high level intuition of how blurring works of of how all these different blurring functions work and that's like the intuition I want you to keep in mind right every time you apply a blur you are computing averages and you are replacing every pixel by the average value of all the pixels in a neighborhood around that given pixel right and the way you compute that average and the way you define this neighborhood right the size of this neighborhood the way you you define those two things is going to depend on the specific blurring function you are using and it's going to depend on the parameters you input into that function so that's something that's like a very high level intuition I want you to keep in mind before starting with this tutorial and now let's go to pycharm this is a patreon project I agree for this lesson you can see that this is only some boilerplate code the only thing I'm doing here is reading an image and then I am showing display in this image so the image I'm I'm loading it's called freelancer.jpg and let me show you exactly how it looks like this is the image we are going to be using in this lesson you can see that this is a freelancer which is having a coffee and it's checking his mail or something like that it's doing something in the computer we don't know exactly what and this is the image we are going to be using in order to show you how these different learning functions work so in order to get started let's start with the classical blur which is CV2 dot blur right this is the way you are going to call this function we need to input the image we want to blur and also we need to input a kernel size or actually we need to input these two parameters remember that I told you that this this high level intuition about how blurring works you are always taking averages and it all depends on the size of the neighborhood you are taking in order to compute this average so we are going to input two parameters which are going to Define exactly what's the size of this neighborhood and I'm going to make this a neighborhood a square and I'm going to say case size will be equal to 7 right so I'm going to say something like this so this is how I Define in this neighborhood is how I'm defining like the proximity of the of absolutely every pixel and this is a parameter you need to input when you are applying a blur you can apply different values I am applying exactly the same in both coordinates but you can just apply different values and you and you definitely need to do some 3DS and I have chosen k equals 7 just because obviously you can apply our values the larger this number the stronger the blur right or the bigger the region you will be taking in order to compute this average so I'm going to show you a few examples with a very low value of K and with a very high value of K so you can see a difference but just remember the general structure for this function right you need to input an image and then the size of the neighborhood you want to take you want to consider in order to compute these averages and this functions is the classical blur and this will be most likely this will be more than enough in most of your projects most of the times you want to apply a blurring function this is the classical blur and this will be more than enough I'm also going to show you two additional functions but the this one which is the classical one is the one which is most commonly used when applying blurs and this will be just fine in most of your applications in most of your projects so I'm going to call this image blur and that's pretty much all so now I'm going to display the original image and also the blur image and let's see how it looks like so this is the image we input this is the original image of our freelancer who is having a coffee and this is the blur image you can see that is exactly the same image exactly the same information but now it looks like uh like blurred right because we are losing detail in this image if you always keep in mind how blurring works is that it involves taking averages that's going to help you to understand why the image looks the way it does right so I'm going to show you what happens if I put like a larger number something like 70. let me show you what happens and you can see that now we are still I mean it's still the same image but now everything is like completely like completely we are losing a lot of information basically that that's because we are taking like a huge neighborhood in order to compute these averages and now we are just like averaging a lot of information to wear and I'll just yeah we are just losing a lot of information that's basically why this is going on uh you can see that still the same image we have something here so we have something here all those regions which are very large regions from the same color like for example the T-shirt remain like the same color the same all these the same about this part over here you can see it remains pretty much the same but all the other regions which are not large enough for example the person's face which there are many colors and many different objects near to it then we are just losing the the all the information and now it doesn't make any sense at all so this is only to keep in mind that the size is the size for this neighborhood it's going to affect the way the image looks like or the way the exploring looks like so I'm just going to return to a lower value of 7 which I think is going to be okay right we want to lose some detail but we don't really want to lose that much detail as before so something like this will be just fine and now let's move to other functions which you can also use in order to apply a blur on your images from the opencv documentation you can see that there are four functions but in this lesson I'm only going to show you three because this one is a little more advanced and I I think it's it's a better strategy to focus on these three functions from now for now so this was the classical blur and now let me show you how the garage looks like I'm going to call cv2.gaussian blur I need to input the image and then I need to input this value the same value as before and now I'm going to input an additional parameter which I'm going to set in three and this is needed by by the way the option blur works so I am going to call this image gaussian blur and this will be enough if I'm not mistaken and I'm going to press play so you can see that this was the original image this is the classical blur and this is the gaussian blur you can see that pretty much it looks the exactly the same as the other one I mean it could look a little different maybe but it's pretty much the same you can play around with these values you can make this instead of a three we can make it a five this has to do with the way this blurring is compute but you can see that it's pretty much the same and so on you can also make it a larger region and maybe there we I mean if you if you look at the images super super in a super detailed way you're going to see they look different I mean in both cases we have an image which is being blurred but the way this blurred is this blurring is is done it's makes the images slightly slightly different so anyway this is how the gaussian blue looks like for this image and now let me show you the medium blur which is going to be the older blue we are going to do today medium blur will be CB2 Dot median blur and this receives the image and only case size we don't need to input two values but only one uh because in the case of median we're always taking a square so I'm going to press play I'm also going to visualize it and you can see that this definitely looks different as the all the other ones I'm going to I have too many windows so let's see if I can put all of them in the same in my screen let's see if we can look at all of them at once I'm going to organize them something like this okay so this was the original image this is the classical blur this is the GAO Chambler and now this is a medium lure and you can tell this one definitely looks different as the other two definitely looks a little different you can still see that it's the same idea we are taking averages we are applying a blur we are losing detail but the way this blurring is done the way this function is being applied you can see that the image looks different as in the other case so this is a nor blur you can also use in your projects and depending on your use case you may want to use one function or the other so this is pretty much all in order to show you the different functions and how to use all these different functions and now I'm going to show you a very specific example of how to apply a blur in order to remove the noise of an image which is one of the most popular applications of applying pliers so I am going to use another function another image which is called cow saltpaper.png and before applying all these blowers I am going to show you how the image looks like so this is now we are only going to see the image we are not going to see all the blurs and this is the image I am going to show you how to how to work with this is the image I am going to show you in this example you can see a decent image of a cow this is a grayscale image of a cow and if you look closely you will see it has like a lot of noise in the background right this this is an image I created myself I added noise myself I added noise intentionally to this image because I wanted to make it look the way it does this is like a very very specific type of noise and now let's see if we can remove this noise applying all these blurring functions applying all these filters using opencv all these blurring filters so I am going to uncomment these three lines and let's see what happens so now these are the three uh sorry these are the three images let's these these ones are a little bigger and a little larger so let's display it once one at a time this is the original image and this is the classical blur you can see that nah we are not really seeing anything too impressive we are still seeing some noise we are still seeing the cow I mean we are not releasing anything too too impressive let's move to the other two functions let's see how they look like this is the gaussian lure and you can see that now I mean if we are playing this floor in order to remove the noise all this noise in the background now I mean I'm still I mean I'm not convinced I've seen a lot of noise over here the image doesn't really look nice I mean I yeah I I don't really like it now let's move to the medium lure and in this case you can see that the image appears to be completely and absolutely perfect I mean it it does it's not a 100 perfect it's not like we have recovered absolutely all the detail and absolutely it makes it like an absolutely perfect image no but we have removed the noise in the background completely you can see that in this image there's a lot of noise over here and in this image there's not noise whatsoever there's not any noise I mean all these black dots we can see over here we can no longer see them over here so this is only to show you a very specific example of how you can use one of these filters one of these blurs which is the medium blur in order to remove this type of noise and obviously that depending on the type of noise you have on your images you may want to apply a blur function or another and you may want to play with the different parameters and so on in order to achieve a better result removing your noise but anyway this was only an example and this is pretty much all for this lesson these are all the opencv functions or actually these are three of the four opencv functions you can use in order to apply a blur on top of your images and it is going to be all for now so let's see how we can apply an image thresholding using opencv in opencv there are two different types of image thresholding there is a simple thresholding and an Adaptive thresholding so in this lesson I'm going to show you how to use each one of them so let's go to pycharm to the pie charm project I created for this lesson and you can see I created some boilerplate code for this lesson so the only thing I'm doing is reading an image and displaying this image so in order to move one step at a time let's see how this image looks like I'm just going to press play and you can see that this is the image of a bear and this is an image I'm going to show you in this lesson in order to to show you how to work with thresholding and in order to to start getting some intuition behind thresholding remember that the idea will be to take or image or image which will have many many different colors and we're going to convert this image into a binary image that's pretty much the idea or that's pretty much one of the most common use cases of using a threshold an image thresholding if I show you the documentation the opencv documentation for image thresholding you will see that there are different type of thresholdings right there are different types of there are different ways of thresholding an image and in some of these ways you will not be creating a binary image right but I will say that creating a binary image is the most common use cases or the most common use case or maybe is one of the most common use cases so that's why that's the application that's the project we are going to create today so that's the type of thresholding we will be making today into this tutorial using opencv and let me show you how to do it the first step will be converting this image which is in BGR into grayscale so I'm going to call cp2 convert color I'm going to input the image and I'm also going to call CV2 color BGR to Gray and this will be image Gray let's see if everything works properly now I'm going to display I'm going to display only the image gray let's see how it looks like everything seems to be working properly this is our image into grayscale and now I'm going to call CV2 dot threshold and let's do our Trace well let's work on our threshold so I'm going to input the image Gray and this is where we need to input a value which will be or Trace called right I am going to input the value 80 because I haven't been I have been doing some tests already and I know this 80 is a good number and basically this will be your threshold and this will be the simple threshold this will be the global threshold so this means that absolutely all the pixels in this image which will be under below the value of 80 will be taken to 0 and all values which are above 80 will be taking 2 255 which is the value we are going to specify here so this is basically how thresholding works this is basically how this result works you have to input an image you have to input the threshold and you have to input the value at which you want to take all the values which are above the given threshold and this will be the trace binary right remember I show you there are different types of thresholdings and we are going to apply the binary so this returns two values which are red and risk and this should be all now let's see how this looks like now I'm going to display trisk and in order to make it more clear what is considered below and above the threshold I'm going to display the original image right so let's see how it looks like and you can see I'm getting something right so this is the threshold of the original image so truth holding is used for many different use cases in many different situations and one of the most common situations were actually a situation in which I have used threshold in many many projects is to use it is to create a semantic segmentation algorithm right is to segment your image into different regions for example in this case you have a verb which is in front of a grass in front of something which is green and you can see super super clearly that the bear is darker than everything else right so if we apply a trace world as we are doing right now we have successfully at some extent what I will say successfully we have keep the where we have kept the where we have segmented the bear from the background and we can see that we have the work the Bear in in dark pretty much and then the grass or the background in a white right it's not perfect obviously it's not perfect and we are not going to have a perfect result using only a CV2 function and opencv function but I would say it's a very very good result also you can see there is some noise in the background so we could perfectly improve this result by using CV2 dot blur I'm going to input trisk and I am going to make it a value of maybe 10 times 10 and this will be Trace again and after that I'm just going to call trisk again in order to make it a binary image again and let's see what happens let's see what happens you can see now I have removed the background also this has a main this has made some changes to the Burr I was detecting now I don't have so many details but you can see that if my idea was creating something like an image segmentation like segmenting the word from the background I will say this is an okay result remember we could be making different type of eye creams if we will be making an object detector for example this is perfect in order to detect the better right we will say okay now give me all the pixels with a value of 0 and that will be exactly the location of the verb I'm just naming a few uh very common use cases of using animal stress holding but for now just keep in mind that this is the way you can apply a threshold and this is also the way in which you can improve the results in case things don't really look perfect from the first try right from the first attempt so this is basically how you can apply a simple threshold a global threshold and remember how it works you are setting one threshold a global threshold and all values in your image which are above that threshold will be white and all values in that image will be which are below the threshold will be black that's exactly how the global threshold the simple threshold works but that's not really enough in some cases right for example I'm going to show you another image let's move to another script I have prepared and I'm going to show you this image for which if we create a global threshold if we apply a global threshold there's not one single threshold there's not one single value we can use a threshold that will give us a better image right that's it's it's that's going to help us in order to make it a better image a better looking image in order to remove all the shades and so on right so in a situation like this we could perfectly use a thresholding in order to make this image nicer in order to remove all the all the shades in order to keep only the text we could perfectly do something like that but we could not apply the global threshold in this case we will have to use the Adaptive threshold and in order to show you how this image will look like or how this binary image will look like under a standard threshold under a simple threshold let's use a simple threshold to see how it looks like I am going to I'm just going to copy and paste the previous close because it's exactly the same I'm going to convert it into grayskill and then I'm going to call CV2 threshold and then I'm just going to display threshold right and you can see that this is how it looks like with a value of 80. now let's change the threshold and let's make it a value of 127 for example so you can see that this is how it looks like for a value of 127 so obviously we cannot use this now let's make it a little uh maybe 100 instead of 127 let's see how it looks like this is not perfect either right we still have many errors we definitely cannot input this into an OCR technology so I could continue trying with different values let's try with a value of 60 only to show you we could do this forever and you can see that in this case it's a little better we don't we no longer have those huge black sections but now there are huge parts of the image which are completely gone so there is a lot of text we are just missing by using this threshold so long story short there is not one single threshold we can apply to the entire image so this is how we are going to use the Adaptive threshold we are going to call CV2 adaptive threshold and I am going to input the image I am not going to input the threshold anymore because the idea with this function is that opencv will figure the threshold out by itself right this will be something like magic absolutely every single section in this image will have its own threshold and this will be a computed for on itself right we are not going to input the threshold but we do need to input the value at which we want to take all the values which are higher than the given threshold which will be found by itself and then we need to input some additional parameters I'm just going to call this stress and let me go back to the documentation so we can see it more clear and how the recommendation looks like I'm just going to copy and paste I'm going to copy and paste this one and I'm just going to change it with some more values okay going to do something like this okay so uh let's go once the right time I'm going to change this by 20 if I'm not mistaken and this by 30 if I'm not mistaken so this is basically what I have done I have a copy and pasted some value some parameters so you can see this one is uh a parameter which is it's uh there are there are different ways to apply this adaptive threshold you can apply it in two different ways so there are two different values you can set here one of them is adapted trust gaussian and the other one is adapted trace mean so basically it shows remember there are two different ways to apply this adaptive threshold then the the thresholding type which will be exactly the same as in the previous case there is called binary binary and then these two constants which has to do also with the way the Adaptive threshold works now remember we will not have only one threshold for the entire image but we will have many many many many many different thresholds because we will apply something like a sliding window we will move through the image we will create very small sections in this image and for each one of these sections we will compute a threshold we will compute the ideal threshold for that specific region right so we will have many thresholds each one for each one of these small regions and this has to do with that this is the size of the region if I remember correctly this has to be an odd number and this is another number which is used through this calculation through this how they threshold is compute so this is basically how the Adaptive threshold works and now let's see what happens if I press play by using the All update threshold you can see that I have made a mistake obviously and let's see why because I am not taking image grade now I am using image gray so everything should be okay and I have another error look PSI divided by 2 is 1. oh I made a mistake this one we can make it 30 for example but this one should be odd okay let's see now and this is the result by applying an Adaptive threshold you can see that now we are getting exactly all the text in this image and we are getting only a text and we are not getting all those extreme situations as we were having before right we are not having like those uh huge sections in the image which were completely black or those over sections which were completely missing now we are getting the text and only the text and if we were using something like an OCR technology if we input something like this it's much much better for the algorithm much much better in order to be more clear more clear on the difference between the Adaptive threshold and the simple threshold let's just play one next to the other I'm just going to the other script I'm going to copy and paste this value and this will be adaptive risk and this is simple to risk and it should be all and now let's do something like this and let's just plot one next to the order and so we can see a difference right so this is the original image this is the let's see oh I see it I named these two windows the same way now it should work okay so this is the original image this is the Adaptive threshold result and this is the simple threshold result you can see that it makes the difference it definitely makes the difference so this is going to be all for this lesson and this is how you can apply an image thresholding using opencv so let's see how we can Implement an edge detector using opencv there are three different algorithms you can use in opencv in order to implement an edge detector one of them is the solo operator the other one is Stella plus and operator and a laplacian edge detector or you can also Implement akani H detector and this is exactly the type of waste detector we are going to do in today's lesson so let's get started so this is a function project really for today's lesson you can see the only thing I'm doing is reading an image and then displaying this image using image row and let me show you exactly the image we are going to use in this lesson and this is a basketball player so we're going to use this image of a basketball player in order to show you how to detect all the edges in this image so the way to use the Kanye detector is very very straightforward and it's like this you have to call CB2 Kani you have to input your image and you also have to input two numbers which I'm going to set in 100 and 200 I'm going to explain exactly what are these numbers in a couple of minutes but for now let's just complete this Edge detector so this will be my image Edge and now I'm going to visualize the image the same way as I was doing before and I'm also going to visualize image Edge and let's see how it looks like I think I made a mistake this is candy with a couple Capital C and now you can see that we are getting the edges for this image and you can see how well this performs I will say this Edge detection is working perfectly and you can see that the only thing I did is just calling cb2.kani and I input these parameters so this is how easy it is to set up an edge detector using opencv and now let me show you let me share a few details regarding how to set this constants how to set these parameters these two values are related to how the Kanye detector works there's something called hysteresis thresholding and this is this is related to how it works so if you are curious to know exactly like all the details of how these Edge detector works I invite you to take a look at this tutorial on the Kani H detector which is like the official opencv tutorial on the Kanye detector and you're going to have a lot of details regarding how to set the these two variables and what exactly do they mean right but if I will have to be honest the way I set these variables this is the way I set these two constants for When I Was preparing this lesson it's just by trying an error right just try a few numbers until you are satisfied with the results that's my recommendation for you I will say that that's the that's a very healthy approach because also I noticed that this Edge detector is very robust and it's not really that sensitive to different values for example if I do it between 50 and 200 and I press play you can see that we are still getting a very good Edge detection the age detection maybe it's not as good as the previous one because now we have a lot of extra details but I will say it's a very good Edge detection anyway and if I go back to 100 maybe if I can do it between 200 and 500 something like that now you can see that we are breaking the attention a little now we are getting less details so if we go back between 200 and 300 we will get something like this which is also a very good detection and you get the idea only by trading an error you are going to find the values for which you want to set your acetector in my case I'm going to say it back in one between 100 and 200 but please remember to play around with different numbers until you are satisfied with the results that's a very good method in order to set these constants and now let me show you a couple of additional functions which could be super super helpful in order to visualize this Edge detection these functions are not really related to Edge detection these are the these are two functions which are more related to more full of morphological transformations in opencv so this is not strictly related to age detection but I think it is a very good time to talk about these two functions and these are CV2 delayed and CV2 errors so I'm going to do it one step at a time so let's start with CV2 delayed and I'm going to call this image Edge delayed or I'm just going to see it with a with a d c video delete I'm going to input the image and the image I'm going to delete is the The Edge detection right so I'm going to input this image and then I need to input an ampere right so this will be something like MP once and this will be something like 5 5 and the type uh MP int 8 if I'm not mistaken let's see if it works yeah it works and now I'm going to visualize it so I'm going to create a new sentence and this will be image HD on image HD okay so I'm just going to show you how it looks like and then I'm going to explain what exactly we are doing here so this is the original image this is The Edge detection and this is the result from the the dilation right from using cv2.dilate so this is exactly what we are getting and you can see that it looks like if we were drawing a new line around all these edges right it looks like if we were making everything to look thicker right and that's exactly how dialect work we are making all the borders thicker this is a function which is used in order to make everything in order to dilate all of your images all of your white Borders or all of your white Contours right so this is exactly how it looks like and let me show you another example from the opencv documentation on how dilate works for example if you input an machine like this you are going to get an image like this which is exactly the same image but it's like thicker right and this is a function which you can definitely use if you want to encase your visualization after you detect all the edges in an image for example in this case we are making a better visualization in all these um in all these edges maybe it's it's too it's too thick so what I can do instead is reducing the thickness by using a numpy or why which is three by three as you can see that now it's basically the same idea we are still making everything thicker but now it's not so thick as before so basically that's how you can use CB2 dilate and now let me show you a road basically the idea with a rose is to do the opposite function the opposite operation as with dilate so I'm just going to copy all of these I'm going to copy this and I'm going to paste it here and I'm going to input this image the one we got from the dilate method and this will be a rose okay ah now let me show you how it looks like now we will have many many images but that's okay we are doing the opposite operation from delayed so if you if I show you the image we got with a road the image we got with the dilate and the edge detection you can see that this one which is the one we got with a road looks pretty similar in a way to the original lips detection image right we are getting the borders over here which are pretty much the same thickness as the original image this section over here doesn't really look like this one because it was super super clever so yeah we didn't really apply like exactly the opposite function but you get the idea that it's pretty much the the opposite right it's pretty much the if we dilate we are making everything thicker with a road we are making everything thinner right that's pretty much an idea and if I show you the opencv documentation for a row you can see that this is an example if we input an image like this and we erode this image we are going to get an image like this which is exactly the same image but with the borders eroded right so that's pretty much how you can use dilate any rules in order to help you with your projects and that's going to be all for this lesson in this lesson we covered how to implement an acetector using Kani using a Kani as detector and this is going to be all for today so let's do some image drawing let's see what are all the available functions in opencv you can use in order to draw on top of your images in order to do image drawing these are the four functions I'm going to cover in this tutorial we are going to learn how to make a line using opencv how to make a rectangle how to make a circle and how to add text on top of an image so these are the four functions we are going to use in this tutorial there are many other functions in opencv you can also use in order to do a drawing in order to draw on top of an image but I would say these are the four functions which are the most popular functions right these are the four functions I use the most in my projects so this is definitely a very good place to start with image drawing now let me show you the image I have chosen for today's tutorial this is the image we are going to use in order to draw on top in order to do all of our drawings in order to draw all of our figures which is the image of a whiteboard a completely empty whiteboard so this is the image I have chosen for today's tutorial I think I think it's very appropriate if we are going to do some drawings we do some these drawings on top of our whiteboard right I don't know what you think but I think this is a very good option in order to use ASA as an image for this tutorial so this is the image we're going we're going to use and now let me show you how to make each one of these figures each one of these functions the first function we're going to use is cv2.line this is the function we are going to use in order to do the lines on top of an image and this is going to be CV2 line the first argument is the image we're going to use in order to do all the drawings on top and then we need to specify two points which are the starting point of this line and the end point of this line right we need to specify two points and the line will be the line between these two points so let me show you so I'm just going to choose two random coordinates remember that we are specifying two points so we are specifying the x coordinate and the y-coordinate right I'm going to specify the x coordinate in 100 and the y coordinate in 150 then for the second point I'm going to say something like 300 and then uh 450 and I'm going to make this line in the color green so this is 0 and 255 and 0. and then the thickness we need to specify a value for thickness for this line and I'm going to say that the thickness is three okay and let's see what happens let's execute execute this code and you can see that we have drawn a line on top of the Whiteboard right this is what we have done this is what we have made so you can see that the first point which is the starting point of this line is if you look at these values over here it's 100 and 150 so it's exactly what we have specified then the end point is 300 and 450 which is exactly what we have specified as well so this is exactly how you can draw a line on top of an image something I'm going to do because this is going to be very very helpful I'm going to print the image shape because remember we are going to do a lot a lot of drawings and we need to specify many points in all the different functions we are going to use today so we definitely want to specify points which are within the shape of this image so let's just print the shape I'm going to close this window and I'm just going to stop the execution and this is exactly the shape of the image we are using in order to draw on top so this is very important is something we need to remember because we are going to specify many many many many many different points so let's move to the rectangle let's see how we can draw our rectangle using opencv and I'm going to call cbq Dot rectangle I'm going to input the image and then I'm going to input two points just like in the previous case the first point is the upper left corner of this rectangle and the second point will be the bottom right corner right so these are the two points we need to specify and I'm just going to choose two random values I'm going to say the first one is something like 200 and 350 something like that and then the second point will be 450 and then 600 right remember we need to be always within the shape of our image so something like this will be okay now we need to specify a color for this rectangle and I'm going to make it red so red is BGR so 0 0 255 and then a value for thickness and the value for thickness will be 5. let's see what happens you can see that this is a rectangle we have drawn and the first coordinate the upper left coordinate is 200 and 350 and the bottom right coordinate is 450 and 600 right this is the rectangle we have just drawn and now let me see what happens if I choose different values for this thickness right we specify a value of 5 and that's exactly the rectangle we had let's see what happens if I say this thickness value is 10. you can see that we get an even thicker rectangle right and let's see what happens if I say thickness is equal to something like 30 let's see what happens now we add like a super super super thick rectangle right so choosing different values for thickness will give you different values for the thickness of the figure you are drawing right uh selecting different values you will be able to adjust the thickness of your figures and now let me show you what happens if I select the value of -1 right every time we choose a positive integer you already know this what happens right if we put like a like a larger number if we increase the value thickness the figure starts increasing its thickness but let's see what happens if I if I specify a value of minus one you can see that in this case we get a completely filled figure right in this case the rectangle is completely red inside right we don't get a value of thickness actually but we are specifying opencv to draw this rectangle as a field rectangle right so the color we have specified is a coloring which opencv is going to fill this sphere so this is pretty much all for the rectangle I'm just going to leave it in -1 now let's move to the circle in this case we need to specify a function which is CB2 Circle and we are going to call the image as always this is all this is always going to be the first parameter and then we need to specify the point the center of this circle which I'm going to say this is something like 500 and 550 this is the center of this circle I'm going to draw and remember how this works this is the width of our image and this is the height of our image so this is how much space we have in the horizontal coordinate in the x coordinate and this is how much space do we have in the vertical coordinate and remember how this works over here this is the x coordinate and this is the y coordinate right it's like if it's like in the inverse order right so this is height and width and this is X and Y so it's like in the opposite order but it should just remember how it works so if specifying a value of 500 it's okay because this is how much we have 1000 and specifying a value of 550 will be just fine because this is how much we have 671. so now let's specify the radius for this circle remember if we are drawing a circle the two parameters we need are the center under radius so I'm going to say something like 15. this will be the radius of my circle and now obviously we need to specify a color and I'm going to select green why not so this will be green is 0 255 and 0. we already made the lining green so I'm going to make it blue okay and now as always we need to specify a thickness value so I'm going to say thickness is equal to 10 and let's see what happens okay you can see that this is the circle we have drawn a very very small circle let's see what happens if we make it larger so I'm going to say the the radius is equal to let's say 150 and let's see what happens you can see that now we have drawn a bigger circular and much much larger Circle and it's it's so big that now it goes outside of the frame it goes outside of the image right so this is what happens if the figure goes outside the image nothing happens you're not going to wait an error you're not going to get any any warning you're not going to have anything the only thing that happens is that opencv is just going to draw this figure within the image and everything that's like outside of the image nothing happens just um there's no drawing whatsoever I know error whatsoever either so this is pretty much all let's do something different let's do the circle over here because I have a lot of empty space over here so this is something like x equal to maybe 800 and y equal to 100 right so if I say something like these I think the circle will be in the empty region yeah the circulates in the empty region so perfect because we have all of this empty space and we need to do some figures on it and let's make it a little smaller so everything is within the white board right we have a whiteboard for a reason we want all of our figures solo for drawing on inside the Whiteboard so this is pretty much all for the line the rectangle and the circle and now let's continue to the text the text is the last function we are going to see in this tutorial so let's see how we can add text on top of an image we have to call CB2 put text we need to specify the image as always and then we need to specify what the text we want to write something like hey you and we need to specify exactly where we want this text to be located and let's say we want it in the um in the other section of this whiteboard let's say this will be x800 and then why something like 450 let's say order it's it will be in the lower right section of the Whiteboard if I'm gonna stay in and now we need to specify the font which we want to use in order to draw this text and there are many available fonts in opencv let me show you if I go to opencv official augmentation about drawing let me show you something if I go here you can see that these are all the different fonts you can use in opencv right these are all the different fonts so I'm just going to choose any of these I'm going to choose the first one and I'm going to show you how it looks like with the first font in this list so let's go back to pycharm and I'm going to say this is something like this okay now as always we need to specify a color and I'm going to say this color is maybe 255 at 255 on zero this is a mix of blue and green so this will be something like purple maybe let's see what what color is this and then we need to specify the thickness value for the text which I'm going to say it's something like you I also need to specify the size of the text and if I'm not mistaking the size goes here so I'm going to say that the size is 2 as well right let's see what happens let's see if we have an error everything is okay you can see that we are printing the text and by the way I was wrong blue and green it's not purple it's this color which is something like a light blue or something like that so everything seems to be working properly but let's make the text over here right let's make it more in the in the center because otherwise it goes outside the frame so let's do it between let's say 700 and let's see if that's enough let's make it 600 and that's okay so you can see that now we are uploading the entire text within the the Whiteboard so everything it's okay and you can adjust the different values for example let me show you what happens if I make the text larger if I increase the size the text size I'm going to say a value of 5 and let me show you what happens you can see that now the text is increased it's much much larger and now let me show you what happens I'm going to make it back to chew if I choose another value for the thickness if I say this is something like 10 right you can see that we have increased the thickness of the text so everything seems to be working properly so this is going to deal for this tutorial this is exactly how you can use some opencv functions in order to do some drawings on top of your images this is how you can draw a line a rectangle a circle and how you can put text on top of your images so this is going to be all for this lesson in this lesson we are going to work with Contours and let me show you the image we are going to use in this tutorial this is the image I have chosen for this tutorial and you can see that these are birds these are a lot of beers which are just flying in the sky and this is something like a picture of many many different beers and I'm going to show you how to use Contours in opencv in order to build something like an object detector to detect these type of objects this is a very very good image in order to show you how to do it because you can see that the birds the objects we are going to detect are much much darker than the background right the background is pretty much white and all the different birds are pretty much black right this is like a somehow binary image and this is like exactly perfect the type of image we need in order to work on this tutorial we could work with absolutely any other image but the results are going to be much much better if we use as animals like this so let me show you how to do it so I'm going to call CV2 threshold I'm going to input the image and then I'm going to input the threshold the threshold value at which I want to transfold this image and then I'm going to input the number at which I want to take all the numbers all the values which are higher than this threshold but actually we are going to do it the other way around remember from our lesson on image thresholding there were many different types of thresholdings right in that lesson we used cv2's risk binary but in this lesson we are going to use CV2 Trace binary imp because we want to take everything that's lower than 127 to 255 and we want to take everything that's higher than 127 to 0 right that's exactly what we want to do in this lesson and I'm going to call this red dress and let's see how it looks like so I'm going to plot the original image and I'm also going to plot the thresholdered image I press play and you can see that this is what we get this is the thresholders image and this is exactly the type of image we need in order to work with Contours when we are working with Contours we are going to detect The Contours the borders of absolutely all isolated white regions in the image right that's very important we definitely need a binary image within an image in which absolutely all pixels are either black or white and if we want to detect these beers if we want to attack this object then we need to make these objects white so this this is why we applied an inverse threshold an inverse binary threshold because if we will have applied like a traditional threshold and not in burst one we will have had something that's very similar to this original image where we have all the viewers in black and the background in Wise and if we want to work with Contours we need the the opposite right we need to apply an inverse threshold because we want all of our objects to be white all the objects we are going to take we need we need these objects to be white so now let's continue now let's call the function we're going to use in order to find all the Contours in this image and in order to do that let me show you the opencb documentation the opencv official domain Edition for Contours and this is the function we are going to use CV2 fine Contours and let me give you like a super quick note about this function that depending on the opencv version you are currently using you could have three values you could have three return values from fine Contours or you could have only two right let me show you in my case I am using this version of opencb I'm using 4.7.0.68 and in this version of opencv we are going to add only two values at the return of fine Contours right that's very important because depending on the opencv version you are currently using you could have two or you could have three so this may change for example in this tutorial in this opencv tutorial you can see that we have three but in our case we have two or actually in my case I have two in your case it depends on your opencv version so let's continue these are the constants we are going to specify for this function and now let's see how we continue Contours are all the Contours we are detecting and ideally if we go back to the image let's see what happens oh I see what happens we need to do an additional step I'm used to doing my thresholds on the original image but in this case we need to apply a CV2 convert color because we need to apply the threshold on a grayscale image so we need to call the image and then CV2 um color BGR to Gray and this will be image Gray this is the image we need to input into threshold and from here everything should be just fine so let me show you three images Gray and threshold right let's play it again shows um one second so I can show you so let me show you the original image this is the image we are loading from my computer then this is the grayscale image the image we are converting to grayscale it looks very very similar but actually this is grayscale and now this is the thresholdered image which is being applied on the grayscale image now right it looks pretty pretty much the same it looks exactly the same but we need to do it like this because because of the way it works right we need to input a one-dimensional image here we need to input an image with only one channel and we need to make things this way right convert to grayscale first then applying threshold and then we need to take this thresholder image into this function so this is how we need to do it and now let me show you this object we have um we have over here which is Contours this represents and this contains all the Contours in our so let's go back here ideally Contours will be something like a list where each object each element in this list is one of these regions right with Contours we are going to have all the Contours all the Border all the borders of all the isolated regions in our image so every single one of these Bears every single one of these white regions we are going to have a contour for each one of them right in this case for example we may have only one for all the three of them because they're they are not isolated right in the case of all the other beers for example this one this one this one this one you can see that they are completely isolated they are completely surrounded of black pixels if you want to see it like that but in this case all these three beers overlap they have some overlapping so we are going to have only one contour for the three of them so you get the idea right like when calling Contours we are going to have a contour a border for each one of the isolated regions in your image right each one of the white regions each one of the isolated white regions in our image so this is exactly how Contour works so ideally each one of the Contours in this list should be one of these regions so let's see if that's the case what I'm going to do now I'm not going to plot the image gray anymore and I'm going to do something like this for CNT in Contours now let's iterate in this object over here and what I'm going to do is I'm going to print CB2 Contour area right I'm calling this function which is going to Output the area of a contour so I'm going to input this value now I'm going to press play and we don't really care about the images anymore so let's take a look at the areas of all these Contours and you can see that we have many many different values we have a very huge values very large values like this one and then we have some very small values I will say that this is pretty much how it always works right because if we go back to the image you can see that although we are looking at something which is like a collection of many many different white isolated regions actually there will be a lot of noise on this image as well so when we are calling something like CB2 fine Contours we are going to get a lot of noise too right many of these Contours will will actually be something like noise and we don't really care about those Contours so what we will do is we are going to get only the Contours which area it's big enough right we are going to get only this type of areas and we are just going to dismiss all these very small values right so let's do something like for CNT in Contours if these let's say if this is greater than let's say 200 and see what happens then we are going to continue right because we are going to do some things with our Contours now and we are going to do it only if the area it's larger is greater than a given threshold because we want to remove all the noise so what I'm going to do now is let's move one step at a time I'm just going to draw all these Contours we have found on top of the image and let's see what happens so I'm going to call CV2 draw Contours through Contour and I'm going to input the Contour and also the image which I'm going to draw on top of the original image and then I need to specify a value for The Coloring which color I want to draw this Contour I'm going to say this will be green and the thickness value will be something like 1. let's see what happens CB2 doesn't have row Contour oh I see what's the error we need to call draw Contours with an S at the end and also there is an additional parameter which we need to input here which is -1 long story short just remember to add this minus one over here and everything will be just fine so now let's see what happens I'm going to press play again and now you can see that this is the thresholder image this is where we are where where we are Computing in order for Contours but we are drawing the Contours on the original image so this is where we are doing our drawings and you can see that we are drawing all the Contours all the borders of all the different viewers right you can see that we are just drawing I Contour on top of absolutely all the beers and if we go here you can see it is exactly as I told you this was going to be we are detecting only one region so that's why we are drawing a contour which is around all of them right so please stay with me we are Computing The Contours here on the thresholded image but I am showing you this drawing on the original image right and now let's continue I told you when we were stored in this lesson that we were going to build an object detector to detect these objects these beers and this is how we are going to do it we are not going to draw the Contours anymore this is only to show you this function and how you can use this function in order to draw all the Contours on your image but now let's call a different a different function which is um bounding rigged if I'm not mistaken CNT let's see what happens if I press play That's okay yeah that's exactly how this function is called and this will be X1 y1 X2 Y2 on this value okay and now I am going to call CV2 rectangle I am going to input the original image and I'm going to input these values because with this function we are getting the bounding box around this given Contour right we are getting a bound inbox the values for the abounding box of these given Contours so this is exactly what we need in order to draw a rectangle and to detect this object to build this object detector so so I'm just going to input X1 y1 and X2 Y2 now I need to input the color which I'm going to say this is green to and then the thickness value which I'm just going to say 2. let's see what happens and let's see well something is obviously not right right let's go back to the function I think I made a mistake and actually this is X1 y1 wait I hate we are getting the information for the bounding box but we are getting in in this format if I'm not mistaken so let's see now I'm going to say this is X1 plus width this is y1 Plus 8. okay okay now everything is okay and you can see that we have built an object detector to detect all these objects on the original image right remember the parameters remember how I use these functions we are calling bound indirect and we are putting the Contour as input and we are getting a bounding box from this function this founding box is expressed as X Y coordinates of the upper left corner and then the width and the height of the bounding box and then we just have to call rectangle I'm going to input the X Y coordinates of the top left corner and the X Y coordinates of the bottom right a corner which is exactly this one so this is pretty much how it works and now if I show you the image again this is exactly the object detector we have built and if you ask me I think this is amazing I think it's amazing we can build an object detector with only a few lines of code and that's amazing that we can just set this up and running in only a few 19 in only a few minutes this is only to show you that if you want to build an option detector sometimes sometimes in some cases you don't really need to use something so sophisticated and so still the art like YOLO or like detectron or something that sometimes a very very simple image processing technique like the one we are doing just now it's just more than enough to to build your object detector right it's more than enough to detect absolutely all the objects you want to detect on your image so yeah never underestimate the power of image processing functions sometimes you can build this powerful Technologies this powerful object detector with only a few lines of code so this is pretty much all for this tutorial and this is how you can use Contours in your images using opencv and now let's talk about the war news lesson let's see what we are going to be discussing here in this lesson this is something I will say it's not absolutely needed if you're going to work with opencv or computer vision it's not absolutely needed you can definitely take projects and working projects without knowing or without knowing anything about what we are going to discuss in this lesson but I would say this is definitely a plus this is definitely a bonus this Sunday is something you can use in order to leverage or enhance your knowledge about opencv but it's not absolutely new and actually this is something that I ask myself a few years ago when I was working in a computer vision project and this is related to color spaces right some time ago I noticed that absolutely every single color space or let's say all the most popular color spaces they are of Dimension 3 right so for example bjr RGB HSV lab and we have many many other recorded spaces and they are all Dimension 3. so I realized that if we want to encode all the information of an image we definitely need to use a color space of Dimension 3 if we want to encode all the color in an image we need to use a color space of Dimension 3. so I ask myself why exactly all the information in an image can always be encoded into a space of Dimension 3 why this number Y3 right so this is something I asked myself a few years ago one while working with computer vision why we always need three channels if we don't want to lose any information regarding or images right Focus for example if you take an image from a for example in vgr and you take it to grayscale then yeah you can perfectly do it but you are losing a lot of information and if you want to go back if you want to take your image in grayscale and convert it into vdr you will not have the same result as you originally had right you are going to lose a lot of information so why exactly all the information on your image is encoded into a space of Dimension three three that's very very important that's what I asked myself and I'm going to tell you what I found out so this has to do with our vision system right with the way or Vision Works with the way our eyes work with the way or the the information from the eyes is processed and let me let me tell you exactly how it works for example here we have like a very very high level schematics about how this Vision system works obviously we are not going to talk about biology or anatomy in this lesson obviously that's not the approach we are going to take but this is only to keep in mind how a very very high level schematics of this system looks like right so bottom line we have light we have eyes and we have rain that's all the information I make from this image right we have these three components light eyes and a brain now let's continue and let's see why exactly all the information from an image is encoded in a space of Dimension three and this is exactly why so the human eye or eyes has three different types of photoreceptor cells for color and these cells are called cones right and in this picture you have the way these cones these different type of cells respond to light right so in the background you can see that this is all the light spectrum and you can see that these curves have to do with the way these different type of cells responds to the light right and I will say that I'm not really going to focus on the curves themselves but I think the answer is basically here is exactly here absolutely all the information of an image were absolutely all information Mission which is ready to color in an image is encoded into a space of Dimension 3 because we have three different type of cells in order to capture color right so it's like having three different type of sensors in order to capture color so at every given time at every single time we are capturing information from three different sensors right that's exactly why absolutely all the information which is ready to color it's encoded into a space of Dimension 3 Because at every single time at every given time we are capturing information from three different sensors that's all the color information we capture with our eyes so it's only natural from there to express exactly the same behavior into an image in opencv right so I think this is very important because if you're going to work with computer vision yeah you have to mind opencv Python and everything that happens like within the computer but you also have to remember that you are also something like a machine and you are capturing the world with your eyes and your processing everything with your brain so that's going to affect the way you are going to assign some more advanced systems right I will say this is not going to make the difference into any beginner type of project most likely this will not make the difference into any beginner project but as you gain more experience in opencv and computer vision and you start making more advanced projects then this will make a difference right this is something that will be important maybe in some cases right if you're going to work in a super super Advanced project then it will be very important to know exactly what's going on for for you like right I mean how your computer your machine is processing all the information and from there you are going to build everything else so that I think this was very important and it was once I I understood this once I understood everything I mentioned in this lesson it made things much more clearer and I understood much much better everything that was related to color spaces and everything that was related to opencv and computer vision so this is why I think this is so important and also the last thing I'm going to say this is a very very quick note that these three three chromacy which is exactly this having three different type of photoreceptor cells it's something that it's common to humans and some animals right but there are few animals which don't really have the same way to a function right which may have more types of photoreceptor souls right or they may have less right they may have one or they may have two but in some cases there are some animals with maybe four maybe five or even more than that and I'm going to show you an example of the this animal which is the mantis shrimp and this animal has more than 16 types of photo receptor cells and obviously I do not know if they use all the 16 types of photoreceptor soles in order to capture color in order to sense or color or if they do different things right maybe they have a different function but let's assume only for a minute that they use all the 16 types of photoreceptor cells in order to capture color so if they have 16 cells and we have only three this means that they can capture way more information and way more colors and this means that if these type of animals evolve in the future and they make computers they build computers and they build something like Python and they build something like open CV then their images in opencv will have 16 channels right this is what it means so this is only a very very quick note in order to complete this lesson and this is going to be all for this lesson hey my name is Felipe and welcome to my channel in this video we are going to work with color detection let me show you super quickly what exactly we are going to be coding in today's tutorial this is my webcam so hi this is me currently we are detecting all yellow objects in my webcam so this is what happens when I am holding a lemon you can see that we are getting a 100 perfect and very accurate detection we are getting at real time detection this is working on real time I am running this code on my local computer in a CPU so this is an absolutely 100 perfect detection and it's obviously going to work with absolutely every other yellow object I input into my webcam for example this is another example with a banana with it which is also a yellow object so this is exactly what we are going to encoding in today's tutorial and most importantly we are going to work 100 with python and opencv we are not going to use YOLO or detector and 2 we are not going to use a tensorflow Keras or any of those super super complex deep learning Technologies so following the steps of this tutorial you are going to learn how to build a color detection using Python and opencv in only a few lines of code so let's get started so let's see the requirements for this project these are the three libraries we are going to use in today's tutorial we're going to use opencv numpy and pillow as always if you want to install these dependencies you will need to go to the terminal and type something like PP install dinosaur requirements in my case I have already installed these requirements so nothing is going to happen on my computer but please remember to install all these dependencies before starting this tutorial let me show you a file I have created and that's going to be super super useful for today's tutorial I have created this utils file it's called util.pi and it contains a function only one function and this function is going to be super so super super useful later on on this tutorial you are going to see exactly why as this is a tutorial about how to detect color using Python and opencv you can see that the input of this function is exactly that is exactly a color so this is going to take care of some parts of our process which is going to make everything much much simpler and much easier for us but you are going to see exactly why in a few minutes now let's start with this tutorial and everything I'm going to do is to load or my webcam because we are going to work a lot with the webcam today so I'm going to create a white room and I'm going to say something like I need to import cp2 then I'm going to create an object which is cap and this is going to be CV2 video capture and I need to specify what's the webcam I want to load using opencv and I'm going to specify it's number two this is going to depend on how many webcams you have connected to your computer it may be the number two one zero who knows if you if you only have one webcam it's most likely the number zero but in my case I'm going to use number two so while true I'm going to read frames for from my webcam so this is going to be something like app.reads and then I'm going to visualize this Frame so I am calling to him show frame and I'm going to call this window frame so so that's pretty much all then I need to close this window and I'm going to do it once I press the dead or cube that's going to be all and then I'm going to release memory and CV2 destroy sorry destroy all windows okay this is the most basic structure we need in order to do nothing in order to open our webcam and just visualize our webcam stream and I am going to press play to see exactly how this looks like and you can see that we so far we have absolutely nothing we are only visualizing my webcam okay so let's continue what I'm going to do now do we have built or most basic structure in order to visualize our webcam now let's let's handle all the color detection which is what we are going to do in today's tutorial and before starting the coding in the sentences all the code we need in order to detect color I'm going to give you like a very quick instruction in in the HSP color space when we work with images it's most likely we are thinking about these images as if they were in BGR color space or RGB color space which means that every single Pixel in our image is a combination of the blue green and red colors absolutely every single Pixel absolutely every single color in our image can be expressed as a combination of these three colors blue green and red but in some cases it's going to be very convenient to convert a representation to convert or image or color space from BGR which is the original representation or the original color space into a more convenient color space depending on our use case depending on what we want to do with our images and if we talk about detecting colors we are going to work with the HSV color space this is an image of how the HSP color space looks like or this is like a model for the HSB color space and you can see that we have something that looks like a cylinder and we have three different channels or three different components in this cylinder they're called Hue saturation and value and yeah I'm not going to give it a like a very comprehensive description of this color space because we don't really need to know super super in detail what exactly is the value what exactly the saturation we don't really need to know like super super comprehensively about these things but we are going to work mostly with the Hue Channel and if we look at the huge Channel this is where we are going to have our information related to the color of our image to the color of all the different pixels in our image now we are looking at a cylinder which contains information for Hue saturation value but let me show you how this cylinder looks like if we look it from above if we look at this cylinder from above we are going to see something like this so this is what I want you to think about when we think about the Hue channel that we are going to have something like a circle and as we go through this circle we are going to be changing like through different colors so different colors are going to have different values in this Hue Channel that's pretty much the intuition behind working with the HSB color space the information related to the image color to the pixels colors is going to be encoded in the Hue channel right we are going to use this information in order to detect an image color pixels colors and we are going to tell python we are going to tell our program or software to detect all pixels from a given color so for example uh well I I have already showed you that we are going to take the color yellow so we are going to tell this software to give us all the pixels within this region which is the yellow region now we cannot ask this program to give us only one value for the yellow color because you can see that this is like an entire region right there is like an entire region of the Hue component where we have yellow so what we are going to tell our program and what we are going to be coding today is defining something like an interval something like a region something like like this where we are going to Define what's the color we are interested in and for example in our case we are going to work with yellow and if you think about this representation if this is something that if this is our model for the color for the HSP color space and for the Hue Channel you can see that in order to specify the yellow in order to tell python to give us all the yellow pixels we will need to specify something like two inter like an interval something like these two values we are going to tell our program to give us all the pixels that are within this interval we're going to specify two values and then we're going to tell the software we are going to code today to give us absolutely all the pixels that are within these two values that's pretty much the idea what we are going to do in today's tutorial we are going to use the HSP color space we are going to take colors using the Hue component the H component the Hue channel of this HSB color space and then we are going to Define two values and we are going to ask our software to give us all the pixels Within These two values that's pretty much the idea so let's see how we are going to delete that so the first thing we need to do is to convert our image from the BGR color space to the RGB color space so we are going to do something like frame then CV2 color BGR to HSP so with this sentence we are converting our input image from the original VCR color space into HSP and this is going to be something like HSV image and that's pretty much it so this is our HSB representation of our image and now the what we are going to do now is we are going to call a new opencv function a function we have never used before which is called Rin range and this is a function we are going to use in order to get a mask from all the pixels that belong to the color we want to detect so we are going to use this function and the return from this function is going to be a mask is going to be exactly the location of all the pixels containing the information we want and the way we are going to call this function is that we are going to input HSV image and then we are going to input two values which are going to be exactly these two numbers exactly the interval for which we are going to tell opencv to give us all the pixels in this image that are in between these two values so we are going to input two values and the way we are going to um it defined these two values the way we are going to find these two values is by calling the function I have defined in the YouTube file so this is exactly why I have defined this function by calling this function it's going to be very very simple to get exactly what is the interval we need in order to get the color we want and you can see that for this function the way we are going to use it is that we are going to input a color and the output is going to be exactly two values which are named lower limit and upper limit the we were going to use CDs I'm going to import so I'm going to say something like from util Imports get limits and then as you can see here we are we need to input a color so I am going to define the color we are going to detect today which is going to be called yellow and it's going to be 0 255 to 55. this is yellow in RGB color space right if we want to Define yellow this is exactly the value for yellow in b g r color space so the way we are going to input this function is by calling get limits and then we specify the color we can do it like this so it's more clear and this is going to be lower limit and upper limit you can see that we need to specify all colors in BGR because this function take takes care of converting this color to HSV and then it just continues to do some additional processing but we definitely need to input this color in BTR and then this function will convert the color into HSB and we'll do some additional stuff and now the only thing we need to do is to copy these two numbers and to paste them here so we are taking our HSV image and these two limits and we are calling the CV2 in range function now let me show you how this mask looks like before continue with your process maybe it's a good idea to show you how this mask looks like so I'm just going to press play you can see this is a mask this is exactly how the mask looks like so we are looking at a completely dark completely empty image but let's see what happens when I input a yellow object something that's completely yellow and you see what happens so when we talk about this mask this is exactly how this mask looks like we are getting all the pixels within our image that are from a given color we have specified yellow and this is exactly what happens we are getting all the yellow pixels in our image okay so now let's continue and you can see that that we are very pretty much there we are we have almost completed this tutorial because the only thing we will need to do now is to draw a bounding box in our image so yeah this is uh this is going to be a very short tutorial uh you can see that we have only written a few sentences and we are pretty much there we have already our mask with the location of four of all Fallout 4 yellow pixels but now we need to draw the bonding box and not only that but we need to detect exactly what's the bounding box for all of these pixels and this is where I am going to use the pillow Library so I'm going to from pill I'm going to import image which is the function we are going to use from the pillow library and this is what I am going to do I am going to define a new variable which is going to be called mask underscore and this is going to be image uh from array and I'm going to input my mask so this is a new variable which is called mask underscore and the way we have created our image is taking our mask and then calling image Dot from array so this is basically the the only thing we are doing is converting our image from being a numpyri which is opencv representation 4 or image and we are just converting this image into pillow that's the only thing we are doing we are keeping exactly the same information but in a slightly different format and now the reason we are making this conversion is because we are going to call a function from this new variable which is going to be gate bounding box and that is it if we want the bonding box of Fallout 4 yellow pixels if we want the bonding box of the Mask I have just showed you a few minutes ago this is the only function we need and this is how easy it is to get the Bounty box we need for our object this is exactly why we are using pillow in order to get the bounding box because it's going to be super super easy to do it okay and now another thing I can show you is how bounding box looks like when we have detected an object and when we have not detected absolutely any object so I am going to print bounding box and I am going to press play again you can see now there's not any yellow object in our image so nothing happens unbounding box is none and let's see what happens when there is a an object you can see now we are getting some numbers and these numbers are exactly the abounding box and if there's not any object I get none so what I am going to do is if bounding box is not known then I am going to get the locations I'm going to anger up my bounding box which is going to be something like this and then I am going to draw a rectangle with this bounding box on the rectangle will be something like this is let's draw the rectangle on the original frame then we need to specify the upper left corner which is X1 y1 then the bottom right which is X2 Y2 then the color which let's draw this funny box in green and then the thickness which I'm going to specify five and this is going to be equal to our frame and that is it that is all if I press play now we are going to see our image we are going to see the the the stream from our webcam and we are going to see the bonding box drawn on top of this webcam so for example here I'm still drawing The Mask I'm going to draw the frame and I'm going to press play again and you can see this is me there's not any yellow object so nothing is going on and this is what happens when I have a lemon or which is a yellow object you can see we have at the exact location the exact bounding box for this object so yeah this is pretty much all this is pretty much the idea for today's tutorial and I'm not sure how many frames per seconds we are getting but you can see that this is pretty pretty real time I mean we are getting a lot of prints per second we are definitely getting a very very good prediction a very fast detection and we are getting a very good resolution and most importantly we are not using a GPU we are running this script on a CPU and you can see how good of a detection we have also if I use another yellow object for example a banana you can see that we are getting a very good detection as well we are detecting exactly where the banana is located and yeah so we are detecting the yellow color very very accurately we are doing it very very fast and we are not using a GPU so this is amazing this is a very very good object detector hey my name is Philippe and welcome to my channel in this video we are going to make a face anonymizer face and organization is a very important area of researching computer vision and it involves to take a person's face and to make it completely Anonymous so in this video I am going to show you a very simple but very effective computer vision Ivory to completely anonymize all the faces in an image or a video using 100 Python and opencity this is an ideal project for beginners in computer vision I am going to show you how to take an image a video or a webcam container person how to locate exactly where the face of that person is located and how to apply a very simple computer vision technique in order to completely anonymize that person's face yeah exactly like this so following the steps of this tutorial you will be able to build a face anonymizer using Python and offensively so let's get started and this is exactly the process in which we are going to be working today you can see that this is a four steps process so in only four steps we are going to have our face anonymizer up and running and you can also see how simple this process will be the first step will be to read an image to read the image we are going to anonymize then we are going to detect all the faces in this image then we are going to blur all these faces we still don't know what blurry means but what I'm going to show you in a few minutes and then we are going to save this image back to our disk back to back to our computer so this is exactly the process in which we are going to be working today and the first thing we are going to do is to set up this process for an individual image to set this system this pipeline this face anonymizer for only one individual levers and then at the end of this tutorial I'm going to show you how to take the same process to an entire video or how to apply the same process to a webcam so let's start with it and the first thing I'm going to show you is the requirements we are going to use in this project you can see that we are going to use only two libraries we're going to use opencv and media pipe we are going to use media pipe face detector in order to detect all the faces in our image so these are the two libraries we are going to use as always please remember to install these dependencies before starting with this tutorial and now let's get started so I'm going to start with the first step which is reading an image in order to that I am going to need CV2 so I'm just going to import CV2 and then as always I am going to Define uh an image which is called image EMG IMG and this will be CV2 in read and then my image part which I haven't defined but I'm going to Define it now image but will be I'm going to use an image which is in this location data test image okay so this is exactly the image I am going to show you how to uh how to detect all the faces and how to anonymize all the faces in this image actually we only have one face but you know what I mean I'm going to use this image as an example in this stage and this is the image I have just loaded using opencv let's go one step at a time and let's see if everything works probably I'm just going to execute this code as it is everything works properly okay so we can continue and we have completed the first step in our four steps process so we are one step closer to reach our goal that's a very good news for us now let's continue with the next step and this is where we are going to detect all the faces in this image so what I'm going to do is to import the other Library I'm going to import media pipe SMP and this is how we are going to do it the first step will be to create the object which we're going to use I'm going to call this object something like MP face detection and this will be MP Solutions face detection okay and then and what I'm going to do is with MP face detection dot face detection and I need to input two parameters one of them is the mean detection confidence and the other one is the model selection I'm going to set the model selection in 0 and I'm going to set the mean detection confidence in something like 0.5 and I'm going to I'm going to explain exactly what these parameters mean in a couple of minutes but for now let me continue I am going to open this object as fake detection and then I'm just going to do nothing for now okay so what we have done here I'm going to change the order because it's going to be more clear I have created this new object which is the object we are going to use in order to detect all the faces in our images and this object requires two parameters one of them is called Mall selection and the other one is mean detection confidence multi selection refers to uh when we are using the phase detector for media pipe there are two different models we can use we can set this value in 0 or 1 and if we use 0 means that we are going to detect faces in how to say it in phases which are very close to the camera in faces which are within 2 meters from the camera this is the situation where this model performs the weather and this e and then if we selecting one is because we are going to attack spaces which are farther than 5 meters away from the camera right so this is why I have set this value in 0 if I show you the image we are going to use in this stage of this process you can see that this is an image which was taking very near the camera so we definitely need to use this value for model selection and then for mean detection confidence I think 50 is a good value we could put it in a higher value we could make like uh we could use like a higher confidence value but I think 50 is going to be fine for now so this is why I have selected these two values and now we have an object which is the object we are going to use in order to detect all the faces in our image and this is how we are going to do it I am going to convert my input image from BGR to RGB because the phase detector we are going to use it needs to detect faces or an RGB image so I am going to call CV2 convert color and then I'm going to input image and then CV to color VCR to RGB okay and now that I have created this object what I'm going to do is to call face detection process and I'm going to input my my image my RGB image and I am going to call this object out so out will be the output from processing this image and now in order to go one step at a time I'm going to show you exactly how out looks like so I am going to print out but I'm going to print all detections which are under the detections attribute of the output of this processing right if I show you this object this attribute is how it looks like you can see that now we have a lot of information and this definitely looks like a bounding box this definitely looks like the face we are detecting because you can see we have four different values this is the X Y and then the width and the height of the bounding box or four face or that's what it seems that's what it seems this is and also we have additional information which are something like key points we are not really going to use these key points so this is are the these are or face landmarks and we are not going to use it in this tutorial but this is in order to show you what's all the data we have in this output in these detections also please notice that we also have the score the confidence value for this detection and you can see that this is a very very high confidence value it's like 96 so this is a very very high confidence so this is basically how this object looks like and what I'm going to do now is to extract this information from this object and the best way to do it will be to do something like 4 detection in out Dot detections and then if I show you back the how this object looks like we will have to get this member which is location data so this will be something like detection dots application data I'm going to call this location data and then we have to call relative bonding box so this will be a bonding box and this is location data relative bonding box okay and then the X mean y mean quit and hate are these members from the relative bonding box so I'm going to do it like this X1 y1 width and height will be bonding box dot x mean bondingbox dot y mean bonding box dot width and then bonding box dot hate let's see if this works only to see only to make sure we don't have any error we don't have any error so everything is okay so okay so we have unwrapped the bounding box from the face we have detected in this object so everything goes super super well now before we continue and before we go to the next step in where we are going to take this phase and we are going to anonymize it we are going to blur it before before we continue let me show you what happens if the image we have a input doesn't contain any face because that's a normal situation that will perfectly happen if we run this process in an image that doesn't contain any face we will face with an error and let me show you exactly why so what I'm going to do is I am going to print this object to see how it looks like not only in this case where we are detecting a phase where we are taking an image with a face but let's see what happens when we input an image with absolutely no face and in this experiment I'm going to show you how what happens when I input this image of a giraffe write this is an image I'm going to show you what happens when I input this image into this script so the I'm going to change the image now it's going to be test no face obviously that the image I have just show you and the one I'm going to show you now in this test it does contain a face but it's not a human face it's such a rough face so it will be a very good example in order to see what happens when we input an image with absolutely no face so now I'm going to run the same process again and you can see that now we have a we have found an error and we have found an error here where we started our iteration and you can see that the print we have made or for object or for detections is none so in the case of uh taking an image containing a giraffe you know I'm just kidding so in the case of taking an image containing absolutely no faces container or type of objects then this is what's going to happen we are going to get a known as or detections so we before starting this iteration we need to make sure that out detections is not known right so I'm going to say something like um if of detections this will be enough but let's do more clear and let's say if out detection is not none so let's do this right and now everything is going to be okay if we have found at least one phase if we have found at least one detection then iterate in all the detections you have found in order to do everything we are going to do right we are going to anonymize absolutely all the faces we have found in this image but in order to do that we need to make sure we have found at least one face otherwise he is going to find a huge error we will have a huge error as the one we just had okay now let's continue and let's uh continue with our bounding box in order to show you more clear that this is actually the phase we have found in this image I am going to do something else I am going to extract these values which are the height and the width of our image and I'm going to say this is image dot shape and now I am going to rename X1 as the integer of X1 times width this W this this capital W the the 104 the one representing our image width and then I'm going to do exactly the same for all the other values because remember the uh the bounding box we have unwrapped its relative bounding box the values are relative values so we definitely need to convert it into an integer and we definitely need to convert it into these values in order to use it later on on this script so this is how we are going to do it this will be this will be a y1 w H and now I need to adjust this by an H on this main H2 now in order to show you more uh properly how this works I am going to draw a rectangle around this face so I'm going to input my image and then these two values will be X1 y1 and then X1 plus W and y1 plus 8. I'm going to draw a green bounding box a green rectangle and then the thickness will be something like I don't know 10. and this will be our image and that's pretty much all and now what I'm going to do is to um to visualize it and in order to do that I'm going to call CV2 aim show image and this will be my image okay and then I'm just going to hold it with weight key and assume okay okay so let's see what happens let's see if we have detected the face we should be detecting and okay so we are still uploading the giraffe if because I haven't edited this image part so I'm going to do it now this will be testimage.png and that's pretty much all lets you know and you can see that we are detecting exactly the person's face we are detecting exactly the face of this guy okay so let's continue okay so we have detected this person's face and now the next step is to anonymize this step now we are here and we are going to blur all the faces um and in order to do that I'm not I don't need to draw the rectangle anymore and what I'm going to do this actually should be here right because this is where we are going to do it I am going to call as an opencv function which we have never used it so far in this channel which is called blur and blur is going to receive two parameters one of them is the image we are going to blur and then it's the kernel size this measures somehow the strength of the blur we are going to do on our image if it's going to be a very intense blur or if it's going to be a more soft blur right so I am going to input a very random value I'm going to input this in 10 times 10 and we can adjust it later on so this will be your image and let's see and the first thing I'm going to do is to blur the entire image so we can get more familiar with the blurring so we can see how it works and then we are going to blur all the face but for now let's do it in the entire image and let's see what happens and I realize now I don't know what happened outside but it's like super super uh cloudy or something so the lighting is like a super low but you can you can see me as long as you can see me everything is going to be okay so this is the image we are blurring you can see that now it looks completely different as the as how it looked uh before we apply the blur and you can see everything looks like super super blurred right in order to show you more clear more more clear I'm going to apply the camera aggressive blur and I'm going to do something like this and now let's see what happens you can see and now we have like a more more intense blur okay this is only to show you and to so we we can become like more familiar with how this flooring works and now we want to apply exactly the same blur but only on the face and this is how we are going to do it we are going to apply this in image y1 and then y1 Plus 8 and then and yeah and then X1 and X1 plus width and the three channels okay and this is our face yeah these are the coordinates of our face and it's we should do something like this so this is what we are doing we are taking the face in rematch we are blurry in the face and then we are replacing the previous phase with the new face with the Blurred face which is exactly what we want to do and now I am going to press play and let's see what happens and you can see that we are blurring the face and we are not learning everything else but it's not very very clear because I have reduced the kernel size to 10 by 10. I'm going to do it in 30 times 30 and let's see what happens and you can see that now it's much more clear that we are Brewing this guy's face now I will say everything is pretty much ready for this step of this process because you can see that this guy's face is completely anonymized we have successfully anonymized this person's face someone who knows this person can't know who he is because now he is completely blurred now he's completely anonymized so we are definitely one step closer to reach our goal and now let's continue the next step we should do is to save this image because it is the last step in our process so this is how I'm going to do it and going back here um no I'm going to do it here because yeah I'm going to do it here and what I'm going to do and don't really need to visualize it anymore so I'm going to delete these two lines and the only thing I'm going to do is CB2 aim right and I'm going to input the image I'm going to save the the images will be the second parameter and then I'm going to input the location I'm going to save this image into so this will be all spot join and this will be an output directory which I haven't defined there yet output directory and then the name of this output will be anything I'm just going to call it output.png anything will be okay we could name this image with exactly the same name as the image we have read from our file we could name it exactly the same we could extract the name and apply exactly the same name but in the similarities just have very dummy example so let's just do it like this so I need to Define these two values or why this is not going to work so I need to import OS and then I'm going to Define this output directory which I'm going to do it here before we start with the entire process this will be the current directory on it will be something like output and then I'm going to say something like output directory if not if the directory doesn't exist I'm going to create it so us make the ears output there okay and if it does exist I'm not going to do anything with this directory okay so we can continue I see that the light is back I see that the cloud or whatever has gone so yeah you can see me again perfect now let's continue so we are grading the image into this uh directory into this location and this should be it and this is going to be all if this works means that we have successfully applied absolutely all the steps in this process all four steps in this process and we have anonymized absolutely all the faces in our input image this means that everything is perfect and everything is ready and everything is completed let's see what happens when I execute this script so I'm going to press play Let's see if we don't have an error we don't have an error and now if I go to Output to my output directory you can see that now I have this output which is exactly the image I have input but with the face which is completely blurred so yeah so everything works properly and now we have completed this process for the case of an individual image but at the beginning of this tutorial when we start this tutorial I told you that after applying this process to an individual image we were going to make it work on an entire video and on a webcam so this is how we are going to do it this is just going to take us a few more minutes but you can see that we have solved this problem we have definitely solved this problem so we should be super super happy with us with ourselves because we have successfully solved this problem what we have to do next it's only a detail and it's going to take a few more minutes but it's only like uh it's meaningless next to what we have just achieved which is a completely succeeding anonymizing all the faces in an image now we only have to iterate in all the frames in a video and that should be enough so let's see how we can do that what I'm going to do which is going to make things much much simpler in order to continue this process and applying all the all this pipeline all these face anonymization to an entire video is to grab some parts of this process into function I'm going to define a function which is div process image and this will receive an image and also a face detection object okay and this will be all of these right we are going to read the image and then everything that's after reading the image will be inside or function now this will be immature GB this is fake detection so everything is pretty pretty well um actually something I can do instead of doing this color conversion here I'm going to do it inside of this image processing and now I can show the input or now I can choose the inputs my image okay okay now everything is okay yeah we are going to input the image and this face detection object and then we are going to do all the image processing inside this function I'm going to return the image return image and that should be all so what I'm going to do now is to call this function here this will be process image and image unfair detection and that's pretty much all okay so we have grabbed the entire functionality everything we have made in order to process this image in order to detect all the faces in order to blur absolutely all the faces in this image into an individual function and what I'm going to do now is to make a few very very small edits in order to fit this into different use cases into different purposes because we can we want to make this script functional on an individual image but also on a video and on a webcam so this is how we are going to do it I am going to import another Library which is Arc parse and I'm going to define a few additional objects I'm going to say something like art Parks um argument parser I'm going to create this objects and this will be arcs and then I'm going to say something like arcs add argument and I'm going to Define an argument which will be mode right we will have an argument we will have a value which the user can set in different modes so if the user wants to um wants to input an image the the user will specify a value of image in mode and if he wants to detect all the faces anonymize all the faces in a video the user will select video and if he wants to do every the same when a webcam the user will type webcam so mode will contain the mode selection of the user depending where the user wants to run this model wants to run this script and then I'm going to add another argument which will be the file path right because if we are going to be working with an image or with a video we are going to read a file from our disk so we definitely need an argument in order to do that and this will be I'm going to test how it performs with an individual image and then I'm going to do it with a video or without webcam and I need to copy these file path this file location into this new argument because now we are going to specify or image location or image pad in one of these arguments I'm going to just delete this value and another change I'm going to do is that I am going to change the location we are where we are reading or image and I'm going to do it like this I'm going to put it here and I will ask what it what's the value of the one of these are one of these arguments what's the value of mold and if arcs.mode in image then I am going to read the image and I am going to do everything else and now I have to redefine image but which will be arcs dot um file Bots and obviously I almost forget but I also need to say something like this arcs Parks arcs okay okay now everything should be okay I know the only thing I need to do is to adjust this process and I'm going to put the image here and then I'm going to save it here right because if we are in an image then we have to do exactly the same process we have we were doing before but everything should be within this if okay now let's see if this works properly now I am going to run exactly the same process I am going to run again exactly the same image and let's see if we can produce exactly the same output so I'm just going to delete the image we saved a couple of minutes ago I'm going to press play and let's see if we can generate exactly the same image again okay so everything works properly everything continue working properly for the case of an individual image and now let's see how we can fit this process into a in order to make it work with a video now a leaf arcs.mode in video we are going to do exactly the same but instead of reading an image we should be reading a video so we will do we will say something like CV2 video capture and this will be args file path because now file path will be a video will be the location of a video and once we do that I am going to read the first frame of this video and this will be something like cap.reath I'm going to reduce the memory before I forget and then the only thing we need to do is to define a well true because we want to iterate in absolutely all the frames we want to read a new frame in in every iteration and this will be something like this cup.read and before we read the new frame what I'm going to do is to apply the versus function we have defined so I'm going to do something like this I'm just going to copy exactly the same sentence and I'm going to replace image for frame right and that's pretty much all we need to do in order to make this process in order to make it work on a video okay so we are applying the process to an entire video on what we need to do now is to save the video because we want to repeat exactly the same process and in order to save the video I am going to create a new object which is output output video and output video will be CV2 video writer if I'm not mistaken maybe I'm mistaken so I'm going to adjust it in a couple of minutes if it doesn't work and I need to specify a few arguments the first one is the location where I'm going to save this video and this will be part join output here and then output MP4 right and the same applies for the what I mentioned about the image we could apply exactly the same name as the video we are reading as input but let's just make it simpler and it's just I'm just going to Define it as output MP4 so then I need to specify another value which is the codec we will be using which is this one and then I need to specify the frame rate how many frames per second and I'm going to specify this in 25 and then I am going to specify the width and the height of this video which will be the width and the height of the image we are reading in this video so I'm going to create the object here so I can access frame and this is something like frame shape one and frame shape zero okay and and if we want to make it even better the difference per second we I have hard coded in 25 if we want to make it super super super nicely we should be accessing what's the frames per second in the video we are reading and we should be specifying exactly the same but let's make it simpler and I'm just going to hardcode it into a device but if we would want to make it super super properly then we will have to specify exactly the same frame rate as here and that's pretty much all okay I'm just going to run this process to make sure everything works properly and everything that's wrong properly okay so I'm going to continue everything runs properly but we are CD executing individual image so I'm going to say something like video and the video I'm going to use as an example will be this video which is a person which is talking in front of a camera and we can definitely see her face in the entire video so this is going to be a very good video to use as an example in this script so what I'm going to do now is video on the data will be test video what was the name Test video dot MP4 okay and now let's see if it runs properly okay it doesn't run properly because let's see where we have an error okay okay right what we need to do is to take this value and put it here okay let's do it before everything else so it's a little cleaner but yeah it's the same idea let's see now everything seems to be working properly let's just wait no but it doesn't work properly and wait through process image frame face detection we are reading this Frame that's okay and then image.shape but frame is known um let's see why frame oh this is not quite true this is white red I don't know why it was way through it seems I it seems like I know I I wanted an infinite Loop for some reason but now we want to specify while red uh okay so everything runs smoothly everything is executing just fine so we can continue so the only thing I'm going to do now is to say output video dot right and then I'm going to specify the frame I'm going to write right but obviously I need to write this Frame before reading the new frame because otherwise I'm just going to read exactly I'm just going to save the exactly same frame I am reading from my video but if I do it here everything is going to be just fine and that's pretty much all now and great in this uh frames and everything should be ready so the only thing I have to do now is to release a memory the memory from this new object and release and that should be all now I'm going to execute the code again and now we should be saving absolutely all the frames and creating a new video which is blurring the face of the person I show you in this input video and let's see what happens if I go to Output and I open this file this is exactly how this file should look like it's the person exactly the same person I was showing you before but now it's with her face completely blurry and you can see how well this performs it you can see that we are not missing a absolutely any frame we are capturing the person's face in absolutely all the frames in this video and we are uploading exactly the location of the face so it works super super super properly and super super smoothly and now let's continue because remember we want to make this work on the webcam we want the user to be able to anonymize all the frames in the webcam so I am going to add a new model which is if arcs dot mode in webcam what will happen now is that we are going to run a process which is very similar to this one but now I'm going to open the webcam which will be remember we you need to specify a number in most cases it will be in the number zero because this in case we only have one webcam connected to your computer you are going to use number zero but in my case I'm going to use number two because I'm going to access another webcam I have more than one more than one webcam attached to my computer and then I'm going to release memory at the end although I'm not completely sure if it is absolutely needed in the case of the webcam but I'm going to do it anyway it's always a good practice when you are creating memory when you are creating an object and this object occupies memory to release the memory this object is taking this object is occupying so this is what I'm going to do I'm going to release the memory and then the only thing I need to do is a process which is very similar to this one in this case I'm not going to save the frames I'm just going to visualize it and I'm going to process it and I'm going to read it but uh something like this Frame frame and then I'm not going to write it but what I'm going to do is to call cv2.m show because I am going to visualize this Frame so we can see exactly how it looks like I'm going to read frames from my webcam and I'm going to visualize it and that's all and then I'm going to wait wait key I'm going to wait for 25 milliseconds so it looks continuous so it looks real time and yeah and that's pretty much all let's see if I have forgotten something so webcam reading frames processing frames visualizing frames and then read a new frame and then release in memory yeah everything looks proper and now what I'm going to do is to set this new mode which is webcam and now I will this it doesn't matter because we don't we're not going to use it but I'm just going to set it in none and let's see what happens I have already I have this webcam right here something is not working properly let's see what is it let's see if I made this proper you cop video capture reframes copyright white Reds process image aim show frame 25 milliseconds everything seems it's okay uh can't open camera by index okay maybe I made a mistake let's ah because I haven't connected my webcam that's the reason why okay so I have connected my webcam that took way longer than expected but it doesn't matter now we are ready to continue so I'm going to press play and let's see how it performs or let's see if it works with my webcam and you can see that it works perfectly now we are detecting exactly my webcam we are detecting my face and we are blurring exactly the location of my face so the face anonymizer is working super super perfectly on my webcam you can see that we are getting a real-time detection everything is working in real time and everything is working super super properly congratulations you have completed this course about opencv in Python so my congratulations to you and if you enjoy this course I am going to invite you to click the lay button and I'm also going to invite you to tell me what you think about this course in the comments below my name is Felipe I'm a computer Regional engineer and in this channel I make tutorials cooling tutorial wheels and I also make courses exactly like this one where we talk about different things related to computer vision and machine learning so if these are the type of videos you're into I invite you to subscribe to my channel this is going to be all for today and see you on the next video
Info
Channel: Computer vision engineer
Views: 23,179
Rating: undefined out of 5
Keywords:
Id: eDIj5LuIL4A
Channel Id: undefined
Length: 191min 10sec (11470 seconds)
Published: Mon Feb 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.