Simple Color recognition with Opencv and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now in real time the object which is crossing the center point we have this circle has the color detector so for example we have now this orange which is between yellow and orange you can see all this hi welcome to this new video my name is sergio i'm a computer vision developer and consultant and i help companies freelancers and students to ease and efficiently build computer vision projects today we will see color recognition so how to recognize a specific color in real time from a webcam and we're going to see this in three steps at the beginning of the first step you are going to learn the difference between the hsv color format and the bgr color format on the second step you are going to learn how to define the ranges to detect different colors so if it's orange color if it's blue if it's gray and other colors that i have somewhere here around like most of the primary secondary colors and then on the third step you're going to learn how to apply these in real time from your webcam so you will be passing an object in front of the webcam and the program will tell you what color is that specific object so let's start we're going to use the opencv library and we're going to write the code in python so if you are new to complete vision and you don't have opencv you need to install the opencv wii the command so first of all you open the table whether you are on linux mac or windows it works the same way you just type pip install opencv dash python and then you press enter if you have ubuntu most likely if you're using python3 you need to install using pip3 instead of just pip all the code that i'm using right now and more information are on the blog post below so if you want to download it's for free you click on the blog post below and you will find the download button and all the text we've written the information of the article about this video and now let's start importing opencv so import cv2 to understand the concept of the colors we're going now to load some samples images so emg equals equals cv2.in read so we're going to load some image blue dot jpg so let me make sure that i have this image here somewhere blue background dot jpg blue background dot jpg and then cv2 dot im show let's show now this image emg and then emg cv two dot weight key we need a wet key vent to keep the image open on whole otherwise the code executes and it closes so now we can just run this one and we have now a blue image that's just a blue background uh what color format uses opencv and how does this work it's very simple this image which is size i'm not sure it should be 1000 by 1000 pixels is draw drawn with the bgr color format so each single pixels has three values if i print this image print emg of course now we have uh we cannot display all the input but we will have 1000 uh columns if i will print everything so 1000 pixels on the width so on the x-axis and then 1000 on the y so 1000 columns and 1000 rows and it uses the bgr format so the first one is the blue color the second is the green and the third is the red and the scale goes from zero that is the absence of that color to 255 which is the maximum of the color and it's the same for each of them so different colors are the mixture of the three primary colors so it's pretty similar to what we have in real life if you mix if you mix the the blue with the red you have one color if you mix the red with the with the green and so on or if you mix three all of them at the same time with different gradients in this case you will see that this is blue because we have from the scale 0 to 20 55 we have 154 of blue we have no green and we have only one of red which is almost like we have no red to prove you this let's uh try with a different image i have also some other colors save i have orange oh i have red so let's load red.jpg and you can see this is red and we have 33 of blue 26 of green and 237 of red then i have i have also orange or somewhere orange background.web on dot webvp let me load this one stop and run so i have already i i may [Music] okay i did some mistake we've we the with the title so i didn't get the path correctly orange background.web so this should be correct the orange in this case the mixture of red so almost the maximum of red and sum of green so some only 94 of green from zero to 255. of course these are images where each pixel has the same value uh we can have of course in real life that's it doesn't work that way if i take this orange of course each single pixel of this orange is different and so uh just to give you an idea let's take some other image i have orange through oranges fruit oranges fruit dot jpg so if i load if i load this one you will see now that each single pixel has a different values of course they are really similar together because most of the value of this on this image are orange maybe some is darker some is lighter and so on now this is the bgr format when working in a real life scenarios the bgr format doesn't work well and let me give you an example and to understand why we can't use the bgr format in real life i'm going to use the color picker from so you don't need this so this is the the free software free image manipulation software and i'm using this because it has a nice color picker which can explain easily how this works here we have rgb and hsv at the same time so rgb is exactly the same that i just displayed now let's focus only on rgb in opencv is bgr but it's the same it's just the order is different but it's exactly the same three channels red green and blue this is the color that we have right now okay sorry this is the color so we have current color this one now it's black because we have absence we have zero zero zero we have zero of each color if i want red here i can put different gradients of red so it's with a low value it will be a dark red with a higher value to be a bright red till the maximum where is 2055 and that's that is what we get the same is for the green dark and bright and the same is for the blue now what is the problem when we want to pick a specific color using the rgb format the problem is that each single color is the mixture of these three primary colors and so sometimes when there is just some lightening change uh let me pick some let me pick this violet right here now we have almost no green only 18 of green 163 of red and 173 of blue if i change a bit this color all of them are changing all the primary colors do change if i focus on these specific colors it's similar but all of them are changing the ranges and so if you work with this in real life also just some lightening change might affect the program and you might miss completely the uh the recognition that's why there is a second corner format which suits better our goal today we and this is the hsv color format h with hsv which stands for hue saturation and value hue is the color that we want so on you already we can pick different colors now to make things simple let's do this hue has a value in this case from 0 to 360. here if we have the red it will be at the beginning of the scale then we have right away orange yellow green xian um like blue darker blue violet pink and red so already the hue gives us the different color without doing much mixture of the rest while the other two values are the saturation is the quantity of that color so 100 is the maximum and zero of course there is nothing so zero will be white you can see and then the value is the brightness so how bright or how dark is the color where 1 is the maximum of the color and this is the minimum of the color i hope that you are following because that's really important to understand to make things simple on opencv i also created a code i did it's a very simple code that you can download below it's called uh when you download the code from the link below there will be a zip file where it's everything included inside and this specific file it's called hsv color picker so you run this code hsv color picker dot pi and that's what we have we have three values hsv and we can play around with the track bar with these three values on the edge we change the color as i am doing right now and on the saturation okay on this s we change saturation in here we change the value so with this color picker we can understand the ranges that we can use to understand the colors in real life with opencv and keep in mind that opencv has different ranges so each program uses different ranges for example the uses from 0 to 360 from 0 to 100 0 100 here we have from 0 to 1 79 0 to 255 0 to 255 don't get caught up stuck in the in the ranges it's just a different scale like on the grades some some country uses from zero to ten some use the letter some from zero to one hundred it doesn't matter uh we have minimum and maximum then different gradients on the middle that was the first step necessary to understand how colors work in computer vision especially opencv now let's create our color picker in real time how do we do that let's code everything from scratch what we are going to do right now is we are going to take a frame from the webcam and so that we can test in real time our code we import cv2 now let's load the camera cup equals cv2 dot video capture zero zero is for the first webcam one for the second so if you have multiple webcams you can just change the index to change the webcam now let's get a frame underscore frame equals cab dot read once we get the frame we need to show it on the screen if you want to see it so cv2 that in show frame and then frame so what do we want to display we want to display frame like this and now await key event to keep the image on hold cb2.weight keep zero and let's run this one let's stop and rerun right now we got a frame so we are already at the beginning a good step now let's check from oh let's put this in real time of course because we're working with the camera there is no point just to have a stable frame and let's do that so we get the frame here we're just getting one free what is a video what is the difference from an image and a video a video is nothing more than image after image so if we want to get the stream in real time we simply can put frame in a loop so while true and everything that it's here in a loop so it takes the frame we show the frame we display the frame and then we go to the next frame and so on now we don't want to freeze this anymore so let's put wait key one so that it doesn't wait for us to press a key but it waits one millisecond and if we don't press a key it goes to the next one if we press a key we will have an output here when we press a key here there will be a value for key if the key is 27 27 will be the s key on the keyboard we want to break so if we press the s key it quits once we quit we want to release the camera cap dot release and also we want to close all the windows so cv2 dot destroy all windows and now let's let's run this one okay now ideally my idea is to make things simple we cannot take the entire image that will make uh some complexity and that's not what we want right now instead i want to take only one single pixel and i want to display the value of that single pixel and the pixel uh will be exactly at the center of the screen somewhere here will be the pixel also i want to increase the increase the what's called the size of the frame or the resolution because it's too small like let's put at least hd so cap.set cv2.cap prop frame width and this is the weave we're going to put hd so hd is 1280 of width and it will be 720 of height so this is hd standard sizes we have full hd will be which would be 1080 by um 1920 4k and so on that's hd for now it's more than enough so 1 to 12 1280 and 720 and let's now take the center we want to take a specific center point of the screen so we will focus only on that one how do we get the center we can divide the width divided by two we have the center x height divided by two we have the center y first of all let's get in real time the width and the height so height width underscore equals frame dot shape the height let's get center x equals width divided by 2 and center y equals height divided by 2. let's make sure that there are integers so we don't get any error because the pixels in opencv they always need to be integers oh you'll you will see now here on the left left bottom left the output of the specific pixel right there so when i put this orange ideally and we see that it has a lot of red some green because here it's the mixture of red and green and it has a little bit of blue when i put this blue you can see that on the values the blue has 255 close to the maximum then we have a bit of green and we have almost no red and this is the beginning so we're just using our dbgr format for this which is not optimal we need to work as explained before and explain also why with the hsv color format and so we cannot just take the frame rate away but we need to convert the frame into hsv hsv frame equals cv2 dot cbt color we want to convert what we want to convert the frame and then cv2.color underscore now we need to say which color format we want to convert this from color bgr to hsv by the way this seems difficult but it's not so this is our these are basic standard functions of opencv and once you know this everything will be easy you need just to master these few basic things of opencv if you want to know more more about this i have a full computer vision course where explain all the basics step by step still creating more complex projects also for beginners so if you have an idea what this is if you are just starting you don't know where to start that's the best course for you to understand everything and build your own projects i will put also some links down below on the description if you want to know more about that now let's continue with this we are going we are converting the hsv frame uh from bgr to hsv now instead of taking the pixel value from the frame we're going to take the pixel value from hsv frame because that's what matter for us it will be exactly the same now i mean it will be exactly the same process like we will be printing now the values but instead of printing the values in vgr we're going to print them into hsv and now let's only focus on the value at the left side which is the h which we have 22 and this is one of that we are close to to the end i will say because from now on it will be very easy how can we now select the different colors to select different colors we can just define color ranges so where does a color begin where does a specific color ends and so and for this that's why i created the color picker to make things very simple i will put just few basic colors and then you can play around with this and you can put your own colors and let's run this one run hsv color pickers so that we can understand the ranges now oh we are going to use only the hue now we don't care about the saturation or the value because the u is what is defining for us the colors of course we can use also them but now it's something very basic which will do a good job already let's now define the colors from zero to six from zero two i don't know two five four from zero to four we have the red let's say this if let's check this carefully on color recognition we are taking the pixel center pixel sender has three value hsv let's now take only the h q pixel u venue let's just take u value equals pixel center zero so from the output that we had before let me show you the output if i can just okay i don't uh right here so this is the hue saturation value we're going to take only the first value here so that's why we put index 0. now how do we define the color we can say if hue value is less than 4 or let's say less or less than 5 because 4 is included then the color is let's say red so we start with undefined color color is undefined then color is red now let's see look at the picker if from five two [Music] from five to [Music] 282 from 5 to 21 is orange if your value elif q value is less than 22 color will be orange alif q value is less than let's see which is the next from 22 to 37 here is like a is this yellow green like i can't really define i guess the the color are never objective it might be a yellow or like some kind of green let's say until 33 and 32 is yellow and then it's green so 33 is color equals yellow l if you value is less than okay i don't want to take a lot of time just taking this i'm going to pause for a couple of minutes i will put all the ranges and then we're going to test it it won't make more sense than just thing like few minutes just checking this and here we have it we have a few colors red orange yellow green blue violet and then red again because here the ranges uh starts with red but also at the end we have the red and probably let's increase a bit this one yeah it's better and now let's test it so i'm going to display in real time the color above this circle let's say cv2 dot draw no dot put text and we want to put the text where on the frame let's put the text just on the top left on the screen and let's now put the color so what do we want to put we want to put the color uh where do we want to put the text let's say i just said the top left of the screen so it starts with position zero zero of course i'm i i need to give some space from the top so let's say position 10 and 50. now the font we don't really care about which font i use so i'm going to do fast go fast with this basic so uh if you change zero one so you have different type of fonts i use zero which is the first font and then the size of the text one and the color let's make it neutral color let's make this let's make it blue and zero and zero and two well ideally we can use the same color we could use the same color of the object that would be nice so let's use the same color instead of putting everything blue let's let's get the color pixel center the uh the bgr color of the object pixel center vgr equals frame and let's say i'm not sure if it works uh we probably need to extract the color i i i'm complicating this program with unnecessary steps really with this color like i could make this more simple but anyway it would be more nice and maybe worth it to to make it this way okay my mistake yeah i'm picking the the pixel on the wrong way to give the color so let's use now blue and later i will just change that okay so let's first make sure first that it works and then we can make it nicer not the opposite so priority first and okay now we have red which can make sense there is some my skin is the closer the closer color between the one we have to my skin is the red now let's check the orange and wow it's already working well we are between orange and yellow which makes sense because with the lightning so part of this is orange part is yellow i have some yellow object on the floor um yellow okay nice let's check the blue wow it's working great and i don't have any other color with mirror now to try i don't have anything green but i guess it works now let's just make it look nicer so red just so i wanted to put the color that we are using color red [Music] pixel center bgr let's extract b g r equals pixel center zero pixels center bgr one pixel center bjerg so so we need to extract the values and then use them right here b g r and we can do the same for also the circle that we are drawing and let's say integer for each of them in int and then int and let's use this where we're going to use this right here so on the text b g r and let's put this neutral let's put it some somehow black or even completely black or just almost black 25 25 25 maybe let's increase a bit the size of the text 1.5 and the position to make it fit so increasing the size so we need to make some space and let's run this one and it work it's working really nice nicely you can see how the color of the text above is adapting to this and we have yellow orange and it's also understanding exactly what color is that let's use now the blue you see nice um and i hope that you enjoyed this tutorial this was some really basic color picker recognition of course you can pick only one specific pixel but if you want a more advanced recognition using a bigger area you should use more advanced methods which will use the hsv format plus some machine learning with knn or convolutional network using deep learning and if you want to know more about color recognition let me know in the comments and i might bring a more advanced video regarding color recognition using neural networks this is all for this video make sure that you subscribe to stay updated with new things that i'm going to release soon and if you want more advanced stuff you will find everything all the links down below to access to my more advanced and previous premium sources this is all for today see you on the next video
Info
Channel: Pysource
Views: 66,790
Rating: undefined out of 5
Keywords:
Id: t71sQ6WY7L4
Channel Id: undefined
Length: 32min 47sec (1967 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.