Color detection with python and OpenCV | #pyGuru

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends it's too dramatic this side in watching my youtube channel Piru friends in today's tutorial we are going to create a color detection script using which we can detect any type of color in the given image okay so see here I'm here with my script so first of all let me just run you run and show you what will be building by the end of this tutorial so let me just run it once and then we will move to the coding part of this particular script so basically it will open a window like this this window has an image and if I click at some particular point it will give me the color of this particular pixel so if I just split here so it's saying that it's color is very million and it's also give means RG and B values so let me just double click here and it's saying it it's color you Roebling so this way you can find any color of any pixel like this okay so this is what we will be building by the end of this tutorial using OpenCV and pandas library so let's next to the coding part of it let me just close this window here I have an empty Python file and see here too there are certain requirements for making this project first of all you will need two libraries the pandas library and the opencv - python library which is OpenCV library okay how to install it it's very easy just you to do pip install pandas and open say we - python like this just run this command in your command prompt and it will install it okay the next thing is check out the video description there's a CSV there's a zip file in the video description you have to download the zip file inside the difference there is this content the main Python file that I have just so we shown you by running then there are few test images that you can try and here's also a CSV file ok we need this CSV file for this project and you can download the CSV file from the chip now from the video description link ok so let's move to the coding part now so first of all after installing these you have to import it now I have already installed it so let me just import it import pandas SPD then the next thing is we have to import the opencv library so how to import it it's imported something like this import CV - okay now see OpenCV library is designed for computer vision and we'll be using it to detect the color at some particular pixel in the image okay before moving further there are a few basic concept that you must know first of all what is an image now see we email and we see an image just like what it looks like to us like an image but how would those computers perceive it basically for computer it's in two dimensional array okay consisting of values in pixels okay two dimensional arrays of pixels so I'll be showing you what I am saying so for this see here I'm going to create two Wavell first the first one will be the IMG path it should it really straw the path of our image so let me just choose any image let me just I have this little pig - dot jpg it's in the same folder where I give my Python script which is changed or py okay and the next variable will be csv path which will store this path of for CSU file so csv particles colors dot csv then the next thing is first of all let me try lead this CSV file so that I can show you what is the content of this CSV file and why we need it okay so how to read a CSV file using panda it's very easy you to create a data frame so DF now you can give this very well name anything DF means data frame that's why I have chosen is what is a data plane you can think it is a simple table okay so inside the PD now which is kind as we have a method called the read CSV we are going to use it and inside the parentheses you have to power the path of the CSV file I have already stole the CSV path in the path of the see she found in the CSV pass variables are just passing it like this now let's try printing the first five rows of the CSV file so you can printed something like this D F dot head five okay so it will print the first five rows of the CSV file on my computer screen so little bit executed and then we will move further now see it's printing the first five rows of the CSV file actually in sprinting 6 because the CSV file does not have any header ok so we'll be adding it in a bit now see what these things and what this five rows are showing this the first one is the color then here the second column is a color name then it's the hex value of the color okay then here the r component of the color here the g component of the color here the B component of the color now see generally what we see color as one thing but actually in computers it is stored in RGB component so the RGB strength of red green blue so this value is the red component this value is the green component and this value is the blue component okay so each color in computer is made up of three components okay each pixel is made up of three components are D and B so here for the Air Force blue are a F this is a color name it's R component value is 93 G component is 138 and B component is 168 one more thing that you should keep in mind that not only the colors are stored in RGB format while in open CV colors are storing BZ our format okay so just leeward of our G B now I don't worry we will be learning it what it means so first of all let add a header here so that we can see what these East things are swimming so how to do add a header it's very easy I'm going to create a list called index here I'm going to do something like this the first value will be color and then second we will be color name and the third column is our hex values I'll pass X here the fourth column is our component the fifth column is G component and the sixth column is B company to me through every year adding it now let's include it in our DF this method leads ES limited so names equals index okay and here you to pass header equals none now if we learn the same thing again it will print the first five rows along with also the header so that we can know what each column is representing this is color column this is called a name column this is hex column this is our component Z component and B component of these five colors okay so now listening further and try printing how many total more rows are there so you can print it like this something like this print length of data frame okay so it will just print the total in the more of rows that are inside this data frame so there are total 865 rows okay so what I wanted I was suppose you want to extract some particular row here suppose you want to extract this particular row so let's see how can we do this okay now why I am telling you all this because we will need it okay so I am just explaining it further before going to the actual program here you could do something like this print D F dot l OC okay and here you to pass first the index of the row C index is this 0 1 2 3 4 in Python we know that index start from zero so suppose you want the second row you have to pass index as one so if I this price 1 here it will just print me the first sorry the second row of the CSV file so the ticket a diluted see yeah it's printing now it's 65 after that it is printing the first row see this is the first row F was blue a USAF so color is this color name is this it's X when you with this it's RGB value are these ok now suppose you want just only the R value from the first row this is the first sorry this is the second row and you want only the R component of this so how can we get X like this it's very easy here here you have to file the column limp so I'm just passing R so now it will print mean the second row and only the AL component of that row so see here it's 0 because we know that in the second row the R component is 0 okay so this is how we can extract values from CSV files or a data frame Union dependence so let me just clear all this and now let's move to the image this thing okay here we are worked with a CSV file now let's move to the how can we read images using open CV so first of all I'm going to play take very well color IMG and inside the cv 2 which is or OpenCV library we have a method call I am read and inside I am reading you to pass the file location or the image location so I will just passing CSV path here sorry IMG path okay and we have to pass the image path here and then the next thing is if I try printing IMG let's see what happens basically it will print array here so little bit executed and let's see what it's printing so see here it's printing an array of numbers now you can see as I said earlier we do we see an email just like an image or how computer see basically they see it is an array of pixels so this is a first pixel this is our second pixel this is our third pixel okay so something like this so here it's a very large menu but thankfully it's not printing all of this ok because we know that it's something like numpy ok so I hope you know what is IMG is doing basically it's reading and IMG in the area format ok so let's move further on now the next thing is how to display it so first of all I'm going to create a window how to create a window it's very easy you to create something like this see v2 dot named window ok and then for a file the window name so you can pass window name as anything ok I'm giving it image ok and the next thing is that you have to do is see v2 dot I am so ok just try it you know this like we did I am read whenever I am so method inside I am so the first thing that you have to pass is the window name or window name is image but I am passing it in a Miss and the next thing is the image that you want to display so the image that we want to display is a mg so I am this passing IMG here the next thing is you to call two methods extra meter that is CV to dot V 8 key here I'm passing 0 and the next meter is CV to not destroy all windows I will be telling you what this means see see v2 dot weight 0 means until and unless we do not click the cross button of the no that will justice and just appear when I run this program we do not have to close the window and when I click the cross button that is just like something like this the the window should get destroy okay by this can see we to don't destroy all windows so let us run this and let's see what happens so basically an image will open here but the size of the image is quite large see it's displaying the image in need it's exercise so we are not able to see the full image so what I'm going to do here see as I said what this weight he means until unless I do not click this cross button it will not open close the window will not be closed okay so this is how you do it how to display any means the next thing that I'm going to do is we are going to resize your image so let's see how can we do it so I am G equals C v2 dot resize and here the first thing that you have to pass is the image that you want to resize so the image that I want to resize is IMG and then the size so I want the size to be 800 by 600 now see after resizing where we are storing it back to the IMG variable okay so I hope you understood this image we are resizing and storing it back to the IMG variable so let's just run the same program again and let's see what happens so yep so here we got an resized image okay so everything looks fine perfect this is our image now we have to find suppose I just double click here I want to note it at which position I am clicking on okay so we want to know the coordinates of our image coordinate of work this mouse okay so I double click here we are you can see I hope you can see the plus of plus cursor so if I double click here I want to know the color that is the RGB value and the you can say the position that is the coordinates x and y coordinates of this particular cursor location okay so let us see how can we do this so for this I am going to create a function called F draw function now you can give this function name now see also this is not some normal function this function is defined for OpenCV okay basically what I am going to do I am going to buying this particular function with our mouse buttons okay so let's see how can I do it so it will take few arguments so first argument will be event then X then Y then flex and then params which is parameters okay here I'm going to put a if condition if event equals what is an event you know if you double-click on the image that is an event if you left-click of your left click on a mouse at some particular position on the image that is an event if you right click that is an event if you middle click that is an event okay so these are even defined in the OpenCV so if even to see a cv2 event l word underscore ell button DB click now what this means if event is left button is clicked doubles okay so L left button double click okay something like this what I have to do I have to just print X comma Y then basically you do not have to do it and do anything when you click doubles when you double click your left button of your mouse it will automatically print the x and y position update okay so we have created a function we have to bind it to our mouse so how can we do it here we have to add one more method called cv2 dot set mouse call back okay set mouse callback now set mouse callback takes two things first that window again our window name is image so I'll pass image here and the next is the function so the function is draw function okay so this now put and remember you do not add post parenthesis here we are just binding it with this function so let us run this program and basically what it will do it will open the image yep so we are getting an image now suppose if I click anywhere on this image it will print here the coordinates of that particular point where I am double-dating okay so say it's printing for 75 90 here its 371 157 so remember you to double click your left button of your mouse okay so yes it's printing perfectly the next thing that you want is the you can say that RGB value of that particular location as ub value a bit particular coordinate now see as I said earlier in the tutorial that open CV gives our stores value in bzr format instead of RGB format okay how to extract it it's very easy there's some thing you can something you can do something like BD r now why busy are because open CV stores where the colors in VAR format instead of RGB format okay IMD which either I'm image okay which is your image and here I'm passing by comma X which is the particular location where I am clicking on okay remember not X comma Y but Y comma X so let's try printing bz r b comma z comma r hope you are able to understand so this time it will bring two things the x and y coordinates along with their pixel value so if i double-click here so the first line is still printing the XY coordinate subject particular location and the second line is printing the B comma C comma R that is blue green red components of the pixel okay so hope you understood it we are able to get the big energy the cover components and the position everything looks good now let's move further and let's move to work program now here I'm going to create few values first clicked equals false which means that we have not clicked anywhere we have not clicked double click the left button yet okay and then I'm going to create few more waves are equal Z comma equals B and then X position and then Y position equals 0 basically what this means that we are creating 5 variables r g b x position Y position and we are initializing it with 0 ok so I hope you are able to understand now the next thing is that I want to do is I want to make all these variables Noble now CY global because we know that if you have known Python if you are trying to change some modifying some more variable value inside this function the change will not appear outside this function so if you want it if you are you're subtracting or doing anything with the variable inside the function and if you want the change to be appeared outside this function here or here what you can do you can make the variable global so how to make it global it's very easy global this is a three word and then the variable that you want to make Noble so click this is a first variable then are the B then X position and then y position then I hope you understood this thing the next thing is what we are going to and actually let us remove this line here I'm going to make click equals true now see we are since we have made global variable global no it will whenever you print this variable outside this function it will print true okay not false now and the next thing is I want to stroll the x coordinate in the x position value okay and y coordinate by paws variable something like this and then we have got the busy our values okay and the next thing is B equals int B now if you try printing B type of see how can you find the type of value of variable storing you can do something like this print type and then variable name okay so B if you want to know what type of value B restoring but you can just do something like this print type B now here if you try doing the same thing here okay now what it will print you know you will find that the value is stored in the numpy value can numpy you int in it but i want to get you into integer so I am just converting it into integer using the int function okay same we have to do it with this so here we are going to print Z and same here we are going to print int I hope you understood that it's very easy now we can just try printing the values of all these things inside outside this function so let's see how can do it print click our value Z value B value X position value and the y position values when this program and let's see what happens basically it will print all these values in a single line so let's just double click it oops you okay so don't worry we'll come out to it later okay and let's run this here let's move further and here what I want to do is everything looks good we're did everything and why was in let me just do this thing little yet executed well it's not printing currently oops sorry actually it's my fault we have to put it in a while loop okay and the current currently the our image this script is running only once okay so it's only printing the these values in easly so and if you want to keep it printing while the program is running you have to put it inside while loop okay don't worry we'll be doing it in a bit so let me just remove these lines actually we can what needed I hope you understood it basically it will read all these things store in this variables now the next thing is I am going to create one more function that is depth get color name okay here it will take three values the RG and B values sorry R comma G comma B values and after that it will give us the color name built on this RGB value so let's see how can we do it again it's very easy so first of all I'm going to create a variable called minimum I will give us some value let's say thousand you can give it any value some high value okay not some low value like zero under something like this let's give it some high value like this and then here I'm going to use a for loop for I in range Len of DF now I have already shown you what this line this basically it will print on all the total number of rows okay length of the apron the total number of rows and then here I am going to create a variable called D okay and see our basic concept is suppose I want I already know this values offer the color value or the color components of a particular pixel how to find what color it is there's a very simple formula we are going to find the absolute difference of each values with the values stored in the CSV file okay so basically we will iterate through all the rows of our CSV file and whenever the distance or the D value is minimum that is absolute difference or the sum is minimum that is our color in them so what I'm saying let me just do it and so you absolute off are - in easier D F dot L you see in this thing I have already shown you I and the column name are okay let's just go let me just copy it paste it here and then again paste it here okay so the second will be G here it will be easy yeah it will be basically we are trying to find out the absolute some oops sorry it will be capital me okay so when the absolute some will be minimum that is our color name so here we are going to put an if condition don't worry I'll be running it and so you know what does it this loose if mean sorry if D less than equal to minimum what you have to do we have to store the minimum equals DS okay and then C name will be that is a color name will be DF dot L or C I and then the color name okay basically what it does first of all we try to find out the minimum difference okay of RGB components with the values stored in the CSV file remember we will do it for all 865 rows and the minimum D when you find the minimum D basically we will store it in the minimum variable and then we will try to find out the color name okay so C name equals D F dot loc here is the room number that is 0 1 2 3 4 and here is a column name okay so this is color name I hope you understood it and after that what here I'm going to do I'm just going to return senior media okay so let me just do this thing actually we do not need it currently so this is try printing this print get color name and pass zero comma zero comma zero now if you know zero comma zero comma zero that is re 0 G 0 p 0 it is actually black so register on this and let's see if it's printing black or not yep it's printing black okay similarly we can pass 255 comma 255 command 255 and 255 so let's see what it's printing it should print fight okay so it's printing white and if I just pass 0 over here and fire 0 here let's see what this is printing it should print red for us because RGB value is our values 255 0 okay so it's plain it's this function is working perfectly if you pass RGB values it will give us the color name okay I hope you understood the working of this function okay same way basically we just try to find out the minimum difference okay between the color the value that we apart and the values that are stored in the CSV file now the next thing is that is our final thing okay so we need this thing and here I'm going to create a while loop now see as I said if you want to keep our program running so we have to use a while loop here we are going to see v2 dot I am so so display our image so how to display it like this image and the image that you want to display this is a window nib this is the image that you want to display now if clicked remember if we double click this click variable will automatically become true so if clicked means if plate equals true okay now this line is same as this thing okay so if plate means if plate equal to true now initially we had made it false okay so this the thing that I am going to write below here we will not work until and unless you you do not double click the left mouse button okay here first of all we are going to create a rectangle so see v2 dot rectangle now the rectangle metric takes two things first of all the image and then the initial position so initial position will be 20 comma 20 this is our top top of our image and then the final position so 600 comma 60 and basically it will I am trying to create a strip so that we can write some text on it and then the b/g are basically we have to pass a color of the rectangle and then we have to buy the thick mix let me just try printing - one passing - one okay and the next thing is yep so we have created a rectangle passing minus 1 as thickness means whatever the color it is it will fill inside the whole rectangle don't worry if you do not understand it basically it will create a rectangle whose initial position initial coordinate in this final coordinate is this so basically it will create a strip on the top of all emails so that we can write some some text on that particular rectangle okay or particular strip so here I'm going to create a variable called text and it has some first of all the text will be we are here calling the get color name function okay so this is a function that we just created so we have to pass the RGB values now C or as even values are stored in the variables r g and b variables so R comma Z comma B now as I have made it global the change will appear outside this function okay so RGB here and the next thing is plus R equals plus R and sorry and then plus Z equals plus G and then what we want plus B equals plus B basically is this a text okay so first of all the color name will be printed then it's our component then it's G component and then it's B component okay so it's it this text will appear on this particular rectangle that we have just created now we have to display this read this text on the top of our rectangle that we have created so how to do it it's very easy see v2 dot put text this is a function that we are going to use and we are trying to pass it write it on the image variable I saw the image and what we want to write the text that is this text what is Tex now first of all the color name and then the RGB components okay and then the initial coordinate so 50 comma 50 let's pass it like this and then the the next thing you to pass is something like this two comma zero point two eight this is a point face I think and then the color so I want to write in it in white color and 255 and then two this is thickness and then the line type which is you have to type it like this see v2 dot line a a I'm sorry CV 2.9 a okay and I hope basically it's a put text function will write and it takes that will pass on the image so image text initial position then the font then the font face then the color name thickness and the line tape also we are going to do one thing more if R + G + B is greater than equal to 600 we are going to just pick the same thing here is a color it is too light we can pass Giove okay so zero comma zero comma zero I want to write it in black color yep so we are nearly done with our program you have to just put one more function here let me just clear this lines we do not need it let's uncomment this and finally we have to do one more thing if CV 2.8 key instead of passing zero here this time I'll pass 20 which means that after 20 seconds if no thing is being done on the image or the window it will automatically flows oh wait a minute episode we are here and hi here I'm going to put an 0 X F F equals 27 now what this means that whether after 20 second if you're not doing and if no movement is being done on the window or if you placed Escape key how to write Escape key something like this and 0xff equals 27 it means that if you are not pressing the if if you press the Escape key we have to just break out of the loop so we are nearly done this is our whole script so let me just try running it and let's see what happens if everything goes fine we should get the window okay so resist double click here here we have some error so let's see can only concatenate a string not integer 2 STR so it's my fault we have to convert this integer values to string okay so that we can concatenate it so STR and then G and then STR and let's turn it yep so we are getting it already the double click it now it's printing see here this is the rectangle that we created okay and - wait means the whole color will be filling inside the rectangle then this is a text the color name then our component D component and B component okay and then we put this text on the top of this window and remember here inside the rectangle will pass bz r okay why would this vigía what it was a pixel we are clicking on that particular color will be this rectangle color will change according to that particular pixel so if i click it single is electric blue here Roblin here dark khaki and Vegas gold per million carry mine pink dark gray vest agree liver clever st. yep so it'll it looking good true blue yep yep so this is how we detect colors in images so you know now you remember you cannot close this window by clicking this okay if you try this it will again get open to close this you to press Escape key okay now let's try reading some another image let's try reading the pick one okay so let me just run this script all these pictures are available in the defile in the video description ok so let's just run it we are getting this so hot magenta and in the medium spring these are color names actually I do not know these colors - question well it's looks good because we know that this is green and it's printing - question green to learn pink a set rain right okay so they are quite lot of colors and we do not know all this rationally so this is it guys for these videos I hope you learned something new if you have any doubt you can ask in the video comment section if you are new to my channel if we learn something like and subscribe in this video ok thank you see you in the next video thank you
Info
Channel: pyGuru
Views: 25,937
Rating: undefined out of 5
Keywords: python tutorial, python tutorial for beginners, python programming
Id: JxmmOktlN6U
Channel Id: undefined
Length: 34min 44sec (2084 seconds)
Published: Wed Jun 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.