Raspberry Pi LESSON 53: Using Trackbars to Track Colored Objects in OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number 53. in our incredible new tutorial Series where you're teaching your Raspberry Pi who's boss what I'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder is actually sponsoring this most excellent series of video tutorials and in this class we will be using the sun founder Raphael kit for Raspberry Pi now most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon you can hop on over there and get your gear and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in lesson number 52. so first of all how many of you guys were successful if you were successful leave a comment down below I imagine double chest bump and if you are not successful leave a comment down below I folded up like a cheap Walmart lawn chair now I hope most of you got it because really it was a pretty straightforward extension of the different things that I taught on how to track on color and how to do track bars and then you're kind of just bringing those two lessons together in one but indeed it is very good practice okay so enough of this talking let's jump over here and see if we can actually jump in and start coding so I will come over here and I will get out of your way and what I'm going to do is I'm going to program this thing up as usual we don't want to start from scratch so we will come over here to www.toptechboy.com you'll want to find this program calculating frames per second you can use the happy little search bar and then you should come up with this lesson we'll come down here we'll click on the little two-page icon we have copied it we will come back over here and we will paste it okay so that looks pretty good let's just run it to make sure the universe is in proper working order and boom there it is I think you guys know by now that I don't like to call my image I am I like to call it frame so I'm going to change that and then I'm going to put the text on frame and then I am going to display frame and that way I'm just always thinking in terms of my data set is a frame if that makes sense now what was the homework assignment the homework assignment was to track your object of Interest based on color but to train opencv to tune in your parameters using what using track bars okay and so I think we are ready to jump in here and what are we going to have to do we are going to have to create our track bar so I'll come uh this looks like a convenient place in the code before we start our while loop and I'm just going to jump in and I'm going to start defining my track bars well if I'm going to make a track bar I've got to have a window to put the track bar in so I'm going to go ahead and create that so I'm going to say CV2 dot named window and that will just create an empty an empty empty opencv window and we'll call it my tracker like that close the string and close that now I've got a window into which I can put my track bars so I'm going to do a CV2 Dot create track bar and what is my first one going to be well I'm going to get my Hue low value where do I want to put this track bar I want to put it in the window that I just created my tracker like that and then what else do I need I need to give it a start value well what would be a good start value I'm going to start for a low Hue value of 10 okay that'll be my start value or that'll be the initial value and then how far do I want this track bar to go well remember Hue goes from 0 to 179 so we need to put the maximum value it can have at 179 now I have to define the function where I've got to name the function that I'm going to go to if you mess with this track bar I'm just going to just to make it easy I'm just going to call them on track and this is going to be on track one so if I mess with the Hue load track bar it's going to send me to on track one we'll come back in a minute and we will set up these on tracks but for right now we are going to need how many of these track bars will Hue low Q High sat low sat High valo Val High six so I'm just going to paste five times three four five six like that so now I have all of those let's come in and the function is going to be on track one on track two on track three on track four on track five those are going to be the functions I've got to Define in a minute now on Hue low it's going to go it's going to start at 10 and go to 179 and then this would be Hue high like that and that similarly would go from 10 to 179 let's say I'm sorry let's set the high at least higher than the low so let's set it to like 30 okay and now on track three uh Hue this would be sat low okay and if that's sat low then this is going to be sat hi okay and so now we've got one two three four five six we've got all of those taken care of 10 uh you know initial value of 10 go to 179 initial value of 30 go to 179 and then on my saturation low I like to start at about a hundred but then we got to remember this goes all the way up to 255 so we've got to put that as a limit on my on my SAT High I'm going to go ahead and start all the way at 255 and the maximum value is 255. go back and watch that those earlier track bar lessons if I'm going too fast for you so we have hulo huha sat low sat high now I'm going to have Val Lo and I'm going to have Val hi valo Val Lo vowel pi and then the vowel low I'm going to start I'm going to start it at about a hundred and then this goes all the way up to 255. my initial value on the high I'm going to go all the way up to 255 and then the maximum value is 255 and so man I think I have my track bar set up I think I've got my track bar set up but now we have to not forget we've got to be diligent and not forget what we got to create those functions now okay we've got to create those functions and so let's just jump in and start defining our function so I am going to Define and then what is my first uh what is my first function it is down here we called it on track one so I'm going to Define on track one okay I'm going to Define on track one and what do I do when I mess with Hugh low when I mess mess with that first track bar Hue low it's going to go out and it is going to go to the function on track 1 when it goes to on track one it's going to grab the value from that track bar I've got to put it somewhere I'm going to put it in the generic variable vowel and now I've got to define the function well on track one this function on track one knows what Val is none of the rest of the program knows what Val is so I'm going to create a global variable that everyone will know about I'm going to call it Hue low but now I've got to tell it what Hue low is Hue low is equal to it's equal to that valve that I just grabbed and now just to help in debugging I'm going to print what I'm going to print Hue low that I just got okay and then what is the variable Q low like that okay so I have that first function on track one to find so now what do I need to do I need six of these so I think it would be helpful to copy and paste so I have one now I have two now I have three now I have four now I have five now I have six and just to make sure I did that right let's go ahead and name them so I've got on track one and then we'll edit the rest of it later but on track two on track three on track four on track five and on track six and those just match all of these function calls so I called six different functions I gotta go to find those six different functions on track one is Hue low Hue low Hue low well on track two is going to be what the high value Hue High and then we've got to change this to Hue high and then this is going to be Q high and then this is going to be Hue High Okay so we've done Hue low we've done Hue High track file 3. is uh Hugh sat okay so this is going to be sat low this is going to be sat low this is going to be set low and this is going to be sat low I think it is faster to copy and paste what do you think if that sat low then this one is going to be set High set High and then this is going to be set hi and sat hot we are getting through these those four okay Hugh sat now what Val so this is going to be Bow Low vowel low Bell low and this is going to be Val Lo and now finally we are going to have Val High Val High and then Now High okay so now I've got the track bar set up and I've got the function set up I'm kind of thinking that I could probably run it at this point just to get rid of my silly errors and make sure that this thing is is going to kind of have the the typo type errors out so let's go ahead and hold our breath and run it okay well we've got this that came up and then we've got the track bars okay and then what I need you guys to look at is I need you to look down here and see if I change these track bars if the things work right so let's change Hue low and you can see Hugh low is going from 0 to 179 and then I've got Hue I've got Hue High changing I have sat load changing foreign changing Val load changing and Bell High changing it so guys do you see like we've gotten all of the kind of skeleton of the program put together and it's working so now it should be pretty easy to come on in and finish this thing and getting it to do what we want it to do okay so what do we want to do we want to track on an object of Interest so now based on those values based on those parameters that Define that kind of slice of the cake that we want to keep we've got to go in and now see if we can grab that slice of the cake so we're going to come down here and what did we do we we have our frame now okay we have our frame and that is good and then we put we put the frame we've got the frame and we put the text on the frame so now let's come in and start doing our magic we've got to Define that Slots of the cake we want to keep so we're going to have a lower bound and that lower bound is going to be a numpy DOT array this is open the function call open the array what is the lower value it's low Q low sat and low vowel like that okay close the array close the call now if we're going to use numpy we better import the library up at the top so we're going to come back up here and we're going to say import numpy as in p right okay so now it will know what that is if we have a lower bound we're going to need an upper bound and that's in P dot array and that is going to be did I do that right I think I might have done that wrong vallo yeah I did that wrong it's uh Q low [Music] sat low and Gallow I hope you guys were yelling at me but I caught it before I ran it [Music] okay then we'll come here we will open the array and this will be Q High set High inval hi [Music] close the array close the function call and we need to spell upper right [Music] we have an upper bound and we have a lower bound but now if we are going to go in and try to create a mask we can't do it in the RGB color space we've got to do it in the what HSV color space so we're going to go frame HSV is e HSV HSV is equal to what CV2 Dot how do I do that I need to say CV2 dot CVT CVT color so I'm going to convert the color I'm going to convert the color of what I'm going to start with frame and then I'm going to do a CV2 Dot color underscore and let me make sure you guys can see that color underscore and I'm going to go from the BG r to the HSV color space like that so now I have my frame in what I have my frame in the HSV color space now that I have my frame in the hs3 the color space and I have an upper bound and a lower bound I can what I can create my mask and my mask is going to be CV2 dot in range so the mask is going to be grabbing the pixels that are in the range end range of what will it needs to be in the range in the range from uh well first of all what are we looking at we're looking at frame HSV so I'm going to look at Frank frame HSV and I'm going to keep everything that is between lower bound and upper bound like that okay so now I have a mask now I want to display that mask so I'm going to make a smaller one just to make it easier to display so my mask small is equal to CV2 dot resize and then what am I going to resize I'm going to resize my mask my mask stays the same size it's just now there's a new mask called my mask small it's going to be my mask and then how do I want to do it I've got to give it a tuple okay and this one is going to want the width and then the height right it wants X and then y when we're doing opencv commands I've opened the Tuple now it's going to be int of display width divided by 2 so I'm making it half as big comma int display height divided by two and then I've closed the end close the Tuple and then close the function call I think for just good measure I'm going to go ahead and see if I can display that so I'm going to say CV2 dot i m show what I want to show my mask and then which one I'm going to show my mask small okay so now I have my mask right now that I have my mask I want to create my object of Interest right and how do I do that my because what is this mask this mask is if something is between the upper bound and lower bound on that mask the pixel is going to be white if it's not between upper bound and lower bound that mask is going to be black but now I want to create a new frame and that new frame just shows my object that I want so how do I do that I and the mask with the original image so now my object is going to be an and between the original frame and The Mask so my object is going to be equal to CV2 Dot bitwise underscore and and then what do I wanna and well I've got to have frame and for some reason you put frame in twice that is my original picture right and now what mask am I going to use mask is equal to my mask I'm using the full size mask that my mask small is just to make it easy to display okay so now I have ended the mask with the image and now I should only end up with what my object my object of Interest okay now what are we going to come down and do we are going to come down and we're also going to show that so I'm going to say CV2 dot i m show what I'm going to show my object okay and then ah I'm going to make my object small too just so that it displays better my [Music] object [Music] small is equal to CV2 dot resize and what am I going to resize I am going to resize my object and then I'm going to grab this Tuple up here because it should be the same Ctrl C it should be the same so I'll just paste that in there okay and then it's all closed up good so I'm gonna show my object close the string and then it's going to be my object small [Music] okay it doesn't like something up here okay I closed the int and I close the Tuple but I didn't close the reset okay so now let's hold our breath and let's run it all right we got some action here so let's see if we can get our Windows organized and guys you guys that are just using one screen could do some do some move window commands and get this to kind of come out nicer okay here I'm going to put my object up here and then I'm going to put my tracker over here and then I'm going to put my mask here okay so I'm not tracking anything I am not seeing anything what does that mean between the lower bound and the upper bound across this whole image Nothing fits the profile and so the mask is empty the mask is empty and my object is empty but now let's put this in here and look at that we are already almost tuned in on that but let's see if we can just tune it in absolutely perfectly because I kind of guess lucky didn't I on that one okay look at that boom giddy up we are tracking on an object of Interest now it's not very impressive because I just have happened to guess something that worked pretty well to begin with but let's go to a different object of Interest let's come here okay that one we are going to have to adjust to see it like I'll come down and then I'll come down you see now if you look up in the upper right it's kind of picking up my hands so I need to tune out my hand I can probably also tune out my hand by bringing this up a little bit and this up a little bit and then oh my goodness almost perfect look at that got another object of Interest okay let's see what would be real hard it would be hard to do green on green right that would be real hard so I probably can't really do that but let's come up here and see wow look at that do you see this green is different different enough from the green on the green screen that I can actually track that man I'm just getting kind of obsessive compulsive how about purple do you think I can the difference between my purple shirt and this purple is probably going to be hard but let's see if we can do it I'm getting the purple there maybe I need to turn down the SAT yeah look at that I'm kind of amazed I'm actually kind of amazed that I can do this and you can see that because of the reflections there's a lot of different colors on that cup all right guys giddy up is that not cool or what so we're doing our first little bit of uh we're doing our first little bit of artificial intelligence and how are we doing that first little bit of artificial intelligence we are tracking an object based on color and we're able to go in and dial in on the object that we want to track using track bars okay so right now what we can do is we can see our object of Interest moving around but opencv doesn't really know mathematically where our object is and so if I put a little color on my finger and wanted to track my finger position which might be something that would be very useful to do if I look at my object of Interest I can see it moving around I've isolated but I don't mathematically have its position in opencv and so that's what we're going to learn how to do next week we're going to learn how to use Contours where you can once you've found your object of interest you can mathematically track its position and so that is what we are going to be doing next week and it's pretty fun and we're kind of really getting to the point of doing some pretty cool stuff here I hope you guys are having as much fun taking these classes as I am making them if you enjoyed the lesson give us a thumbs up if you don't mind leave a comment down below because that always helps us with the old YouTube juice if you haven't subscribed to the channel already subscribe when you do ring that Bell and you'll get notifications of our future video releases and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcquarter with toptechboy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 4,672
Rating: undefined out of 5
Keywords:
Id: c54SqeX5xyU
Channel Id: undefined
Length: 26min 48sec (1608 seconds)
Published: Thu Mar 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.