AI for Everyone LESSON 11: HOMEWORK SOLUTION Creating and Using Trackbars in OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number 11 the homework solution edition in our incredible new tutorial series where you're going to learn artificial intelligence or you're going to die trying what i'm going to need you to do is pour yourself a nice strong cup of iced 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 you guys for helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and let's talk about what i am going to teach you today what i'm going to do is i'm going to put present the homework solution for the homework that i gave you in the earlier video in lesson number 11. and what that homework assignment was was to create track bars that would allow you to control not only the size of your frame in opencv but to position of your window so that you could adjust the frame size and the position of the window o t f on the fly okay on the fly adjustment of window size and window position in opencv so we will need to switch over here and i will need to let you look at my desktop and then we will need to fire up the most excellent visual studio code we will come over here to the explorer view and i will click in a blank spot i will come up here to the little page plus icon and then i do believe this is going to be open cv dash program 14 dot p y and the dot p y is pretty important and boom a fresh new python program just waiting to be written now we don't want to always start from scratch we want to start with our little core program that will launch the webcam because it would get boring lesson after lesson to watch me recreate that you guys that are taking the class probably already have it but if you don't you can go to the most excellent [Music] www.toptechboy.com come down to this happy little search bar and you can search on the term faster launch of webcam and that will bring you to this lesson you can then click one time on the little double page icon that will select the program right mouse click and copy and then we will come back over here to visual studio code right mouse click and paste and boom we have our nice little program let's make sure everything is copacetic in the universe and that we don't break things by copying and pasting and boom there it is there is the window we are grabbing frames showing frames and we get a little video on our desktop okay so that looks really good so now what was the assignment the assignment was to create track bars and have those track bars control the size of your window and the position of the window and so i think where i am going to start is i am going to start by getting the x position and the y position of the window so in order to do that i think the first thing that i should do is i could should come down here and i should create the track bar and so in order to create that track bar what i am going to do is a cv2 dot a cv2 dot create track bar okay i'm going to create the track bar and then where do i want to put it in the window my track bars my track bars okay and then what is this one going to do hmm well this one is going to control the position this one is going to create this one is going to control the position of the track bar and so we will say the initial position will be zero and then let's say that we could move it all the way out to 2 000 pixels on our screen okay that would probably be good be able to move it over about 2000 pixels and then in order to process any monkey business with this track bar any monkeying around with this track bar we have to have a callback function so i'll just call it my call back 1. now the thing is if i am going to create that there's several things that are going to need to happen i'm trying to think here what needs to happen well the first is i'm telling you to put this track bar in a window called my track bars that window does not exist yet so i'm going to copy that name and i'm going to create that just by saying named or i'm going to say cv2 dot named window and what window do i want to create i want to create my track bars right and that way when i say to put the that way when i say to put the don't forget something here i think i probably did i created a track bar i said put it in the window my track bar but i did not give it a name and so i am going to need to give a name to this which is what i do first okay we'll get going here what do i want to call the track bar i'm going to call this track bar x position like that okay so i create the track bar x pos for x position i put it in the window my track bars and therefore i've got to before i do that create the window a named window of my track bars and so now when i say to put xpos in my track bars window there is such a window if that all makes sense okay now i also think that if i'm going to create that my track bars window i ought to put it somewhere and so i'm going to do a cv2 dot move window which window do i want to move i want to move my track bars like that okay and then where do i want to move it well i want to move it over by the width of the frame okay and that is width and then i want to move it down 0. now where did this width come from well we set the width of our frame to 1280 up here and then we set the cam we did a cam set to that width so how wide is that frame going to be it's going to be width so how far do i need to move my track bar window over in order not to be under that frame i need to move it over by width and then i'm not going to bring it down anything so i've moved the window i've created the window i've moved the window i'm going to give the window a size because if you don't explicitly size the window strange things can happen okay goofy things can happen sometimes it works sometimes it doesn't but i'm going to do a resize window okay resize window and which window do i want to resize i want to resize my my track bars i want to resize that window and how big do i want to make it i think i'm going to make it about 400 wide by 150 deep and by giving that a size then it's going to be well behaved so i've explicitly set the size and i've explicitly set where it is and so now i should probably have a track bar but this program would crash because i have not set my callback yet and so i need to come up here towards the top of the program and i need to define what do i need to define my call back one i believe i called it let's check and make sure you have my callback one and then when that trackbar call calls this function it's going to pass it a value okay and so i need to grab that value so it is this create track bar it is that entity that then is going to call my callback and when it does it is going to pass it a value and i darn better grab that value because that's the whole point of having a uh that is the whole point of having a uh track bar so now let's just say let's just see if this works so far so i'm just going to print value just to make sure that things are kind of working the way you expect i don't like going a long long long time without running the program let's just see we should have the little window track bar and then if we slide it it should print out the value just making sure that we haven't made a mistake so early on okay there it is and i've got the little track bar boom and now if i move it look at that look at that as i move it it prints out the value so i am getting a live and real value from my track bar and so that's good but really i don't want to print it what i want to do is i want to set a variable x position is equal to value but if i'm going to do that what did i better do if i just come in and start using x position it is a local variable and only the function my callback 1 is going to know about it so if i want to use x-pos and other parts of the program what it i better do i better make it a global so exposition is global and guys i really think that is all that i need to do here i just need to run out and i need to snag that value from that x position track bar and then i need to have it where the rest of the program can use it and this was so much fun i'm just going to copy it and i'm going to paste it only this is going to be my callback 2 and what am i going to get in my callback 2y position and here y position so now i'm going to create two track bars one is going to track x position the other is going to track y position and so we are going to come down here and we now need to create another track bar nice thing is we don't have to create another window we don't have to resize or move the window or any of that again we are just going to probably the easiest thing would be to capture that copy and then paste and then this track bar is going to be y position track bar we still want it to go in the window my track bars we want it to go in that window and this one since we're going to be moving it up and down maybe we best not move it 2000 we best just move it a thousand because your screen is not as high as it is wide and we need to call y callback 2 and then okay i think that looks good i think that looks good so we're going to get an x position and we are going to get a y position but now we want to do something with x position in y position remember our little friend mr move window down here we just move the window to the zero zero position just so it stays up in the lower up in the upper left corner now what are we going to do we are going to put it at x position and we are going to put it at y position all right now this is what you have to think about the first time through this loop we have not monkeyed with the track bars yet and because we have not monkeyed with the track bars we have not gone into my callback 1 or my callback 2. so just running the program before we touch the track bar the program does not know what xpos and ypos are because they have not gone in and snagged the values here from my callback 1 or my callback 2 which is just a long way to say we better set those before we run the program so we're just going to say x position is equal to zero and y position is equal to zero so what does that mean the first time we run the program right when the program runs it's still the the frame is going to be the frame the window is going to be in the upper left corner of our screen if i am thinking right so let's run this thing what else did i forget we'll find out look at that what's the good news the good news is the program didn't crash the bad news is we don't know if it's going to actually do anything but let's come up to this track bar and let's see if i can move it over boom who's your huckleberry i'm your huckleberry look at that okay were you guys able to do this let me know if you got your program working leave a comment down below i am legend if you are not able to get this working leave a i fold it up like a cheap launch here okay now let's see if the y value is going to work so i come down here and look at that the y value does work and i can move it all the way off the screen or back up okay so boom i've got x position i've got y position so i think that is pretty darn cool now what was the other part of the homework assignment was to set the size of the window now you might have done this by resizing the window the reason that i don't like to do this with resizing the window what if you came in and you had set the width and the height here to a small number and then you did a window resize to a big number then you would get pixelation because you are not grabbing at a high enough resolution so i think the better way of doing this is the better way of doing this is to do it with a cam set where we set the resolution of the camera and we want to set that from the track bar and then we want to be grabbing frames at that resolution if that makes sense okay so we are going to need another what we will need another track bar you guys leave me a comment if you did it the other way all right so this is going to be what this is going to be i'm going to set the width okay i'm going to set the width and it's still going to go into my track bars but the width uh i'm going to start it like remember this first number it's not to go from the first number to the second number it always starts at 0 and then it goes to the second number the first number that you put in is what the starting value is so i'm just going to set the starting value to width because that's what we've already said so the first time that it does this it is going to set it to width and then what do we want to go to i'm going to go to let's say 1920 because that would be sort of like probably the highest resolution that i could get from uh from this webcam and then this is going to be my call back three all right now the thing that you have to see is we only need to we only need to change the resolution only when you monkey with this track bar you don't have to constantly every time through the loop change the resolution of the camera and in fact that would probably be a bad thing to do that would probably be a bad thing to do so let's go ahead and grab this we're going to grab the callback 2 and then we're going to paste it but we're going to call it my callback three we are still getting a value okay but now what we are going to do is we are going to make global the value what am i trying to get from this track bar i am trying to get width okay and so i'm going to make global width and then i will set width is equal to the value that is getting passed into this function i hope that makes sense okay now you might be tempted to think okay i've got to go call did i do this wrong what did i do i was in the wrong place wasn't i okay this is still i did call back to there that was a rookie mistake let me fix that okay and this still is y position my position it's this one that i should have been editing and i should make global i should make width a global variable and then i should set width equal to value now you might be tempted to think okay now i've got to go and have a separate track bar to get height okay the problem is you can't really on a webcam set an arbitrary height and an arbitrary width because that ratio needs to be a standard ratio and kind of like the standard ratio is 16 across to 9 high so if you're 16 across your 9 high or some multiple of 16 that same multiple of 9 and so width and height are not independent variables you set the width and then you calculate the height all right and so now still we need height to be a global variable actually we don't okay because we're going to do it right here we're going to do it right here watch this man watch this width is going to be equal to value width is not going to be a global variable then we calculate height we calculate height and the height is going to be equal to what the width and then right we've got to keep it in that 16 by 9 ratio okay so what would i do i would multiply i would take the height i would take the width and i would multiply it by 9 and then i would divide it by 16 and then the width to height ratio would always be 9 to 16 okay it would always be that ratio now you know the the way to think about it is the height is shorter than the width and so you wouldn't want to multiply by 16 and divide by 9 that would put it up on its edge you want to multiply by 9 okay you want to multiply by 9 and then divide by 16 and then you will get the proper size now what is it that you know for sure what you know for sure is you know for absolutely sure is that if you just went in and put this it's probably certainly going to be a floating point number and most of these cvt 2 calls want integers so i better right now when i do the calculation force it to an integer so it will round the floating point number off to a round number okay and now that looks pretty good so i have width and i have height okay now what do i want to do i want to right now i want to do my cam dot set and then what do i want to set cv 2 dot you know what i think i could do here is i've already done this down here right i've done these two right here and i'm only going to check change the width and the height so i'll just snag those two and then i will put those two up here okay like that now let's see if i need to fix it okay i'm i'm changing cap prop frame width to what width and it knows what width is its value then i do a cam set cv2 dot cap prop frame height i set it to height it knows what height is because i calculated it here so if i mess with that third that third track bar it's going to call my callback three and then it is going to change the camera resolution but only when that third track bar is messed with now you see all of this i didn't have to make these global variables because i did everything that i needed to do inside of this function why did i do that because you only need to set the camera resolution when you change the track bar you don't need to do it every time the things that you need to do every time like you know moving the window you've got to do that down here which means down here and needs to know what xpos and ypos is which means that xpos and ypos had to be made global variables up here i hope that makes sense so let's let's try this okay let's come in okay the good news is the good news is i've got a window and the good news is it didn't crash and so now let's see what happens let's see if i can still move it yeah i can still move it in x okay i'm still moving my window nicely in x okay go back and i can still move my window in y that's working properly but now the big question is can i reset the size so i'm going to come here now understand that there's only certain sizes there's certain sizes that the the webcam will allow itself to be set to so every time you click this it's not going to change it in tiny increments it's going to wait till you get to its next accepted size and then it's going to jump to that but you see i can come here and let's see i'm going to try it a couple of times okay there it is you see it's changing boom look at that now what i will also warn you is i will warn you there is a certain smallest size that the camera will allow it to be set to so as you come smaller and smaller there comes a point where it's not going to get any smaller than its smallest size okay but now let's let's move it back to kind of a reasonable size and now let's see if we can still move it around boom look at that we can move it around with these other track bars okay let's see now if we make it bigger let's see how big we can make this look at that okay that's 1508 i'm going to move it back to the corner just so we can keep track of it i'm kind of happy that it's leaving this track bar i'm kind of happy that it's leaving the track bar window on top of the video window because that way it doesn't disappear okay let's go a little bigger just trying to see here okay and then if i come here i'm actually out to full screen okay because i went to 1920 which is the resolution of my screen let's see we probably still couldn't move this yeah it's just it's moving it off the edge of the screen okay and here we can move it down i would bet that it probably doesn't like negative numbers so i don't think that you can move it off the screen to the top okay and then let's bring it down to a more reasonable size there it is okay guys i think that is pretty darn neat and so what i need is uh let's see is there anything else we would want to do no i think that that is a pretty good stopping point but what i need you to do is leave a comment down below were you able to do this on your own leave a comment i am legend or if you are not able to do it on you on your own leave the comment i fold it up like a cheap walmart lawn chair you guys that are trying these homeworks and failing i want you to understand that's called learning that if you try to do it you get stuck you get confused you don't understand it and then you watch me do it and then the light bulb goes on then you really understand it you understand it because you struggled with it if you don't struggle with it and just come in and watch me do it you think oh yeah i understand it looks easy but you you never do anything on your own and you go through 68 lessons thinking that you understand things and then the first thing you try to do on your own you can't do so if you are trying and failing you are learning so don't feel bad all right and everybody nobody's able to do them all everybody struggles with them but that is what learning is about okay guys i feel bad i feel bad when i don't give you a secret word uh so i'm gonna give you a secret word today plus it's just a chance for me to share a little bit about what i have going on here at the old compound and i'll also kind of hear as i'm giving you the secret word i'll show you the upcoming studio right right now i am in uh you know yes i am in africa but our i'm in sort of our guest uh sort of a guess a you know a a guest house here as our main house is being finished and so i will show you the future studio and that is the future studio right behind those doors there and what i want you to see here is this is my studio desk now if you look at the ceiling up here the roof is made from thatch it is made from a grass thatch and the grass thatch is sitting on top of these bush poles so you have all of these bush poles that we had to build the roof from well the bush poles are longer than what you need for the roof so every one of those bush poles we had to cut the end off of well when you live in africa you can't just run down to the local furniture mart you've got to kind of build everything from scratch so what we did was we saved all of those end pieces that were cut off of these bush poles and then we used that to make the furniture and so one thing you can see our railing on our mezzanine was made from the leftover pieces of bush pole and then my most exceptional studio desk there where all of my equipment and studio will go we made that out of bush poles as well did we stop there no we didn't we also made all of our bedroom furniture and i think i will have to get a little further out of your way you see the the beds and the nightstands and all the stuff in the uh in the bedroom we made from bush poles as well so as we're building the house we're taking the scrap from the house and we are using that to build our we're using that to build our furniture but it's kind of like everything you got to make from scratch so the the house was made out of mud it was made out of mud bricks and then so it's a mud house with a grass roof and then when we got to things like the towel from the floor that was made from mud that was fired in a uh you know kind of like a big bonfire uh we fired the tile and so that was all made from scratch and then anything like door handles or any of that we go to a blacksmith and he gets a piece of steel and he forges it out so it really it really is kind of neat everything is made from scratch even the wood floors the wood floors there that was made from trees that we cut down and then sliced and then milled and so it's kind of been a really neat exercise the entire house has been built from scratch from local indigenous locally available materials and so it's kind of been an interesting and fun uh fun project so that leads to the secret word for today is bush poll okay the secret word today is pull so to show me that you watched all the way to the end you can leave the you know the always i am legend or i fold it up like a cheap lawn chair and then you can also tell me the secret word which is bush pull for today okay guys i am having a lot of fun making these lessons hopefully you're enjoying taking them next week what the lesson is going to be i'm going to actually start getting into we're going to start dipping our toe into the artificial intelligence world as we start thinking okay how could we track an object how could we lock onto and track an object in opencv and to introduce it the easiest thing that you can track on is color and so we're going to kind of jump in and start thinking about how would we track an object based on its color okay guys this is paul mcquarter from tub techboy.com if you like this lesson be sure to give us a thumbs up if you haven't already subscribe to the channel when you subscribe make sure you ring that bell so you'll get notifications when my future lessons come out and uh i think that's pretty much it i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,369
Rating: undefined out of 5
Keywords:
Id: n-nNyZ5ey-Q
Channel Id: undefined
Length: 31min 5sec (1865 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.