Detecting color with Python and OpenCV using HSV colorspace | Computer vision tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 a 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 into this tutorial and most importantly we are going to work 100 with python and opencv we are not going to use YOLO or detector and chew 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 are 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 minus r 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 super were 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 CB2 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 the number two this is going to depend on 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 numbers here 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 crap.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 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 Q 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 or 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 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 or 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 HSB 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 detect colors using the Hue components the age 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 do 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 something like frame then CV2 color BTR 2 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 opencb function a function we have never used before which is called Rin range and the easy 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 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 opencb to give us all the pixels in this image that are in in between these two values so we are going to input two values and the way we are going to um Define 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 255. this is yellow in RGB color space right if we want to Define yellow this is exactly the value for yellow in BGR color space so the way we are going to input this function is by calling that 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 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 you can see that we have already written a few sentences and we are pretty much there we have already our mask with the location of four of all follow four yellow pixels but now we need to draw the bonding box and not only that but we need to detect exactly what's the bonding 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 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 bounding box of all four 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 bombing 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 bonding box and I am going to press play again you can see now there's not any yellow object in or 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 bonding box is not known then I am going to get the locations I'm going to unwrap 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 bounding 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 addiction 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 so this is going to be all for this tutorial and this is exactly how you can detect color using Python and opencv please let me know in the comments below what do you think about this video and please let me know if you have any idea of some very cool and awesome project we could work on this channel so if you enjoyed this video I invite you to click the like button and I also invite you to subscribe to my channel my name is Felipe I'm a computer vision developer and in this channel I make tutorials exactly like this one where I show you different applications and different use cases of computer vision and I also share my experience and I share my resources as a computer vision developer so if it also are the type of videos you are 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: 37,221
Rating: undefined out of 5
Keywords:
Id: aFNDh5k3SjU
Channel Id: undefined
Length: 20min 0sec (1200 seconds)
Published: Thu Oct 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.