AI on the Jetson Nano LESSON 12: Moving Video Windows in OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and I am here today with lesson number 12 in our incredible new tutorial series where you are learning artificial intelligence on the Jetson nano I will need you to pour yourself an enormous mug of iced coffee and I will need you to get out your Jetson nano gear and get ready to learn some cool news stuff what you don't have your Jetson nano gear yet look down in the description down below I have links to all the identical hardware that I'm using in this series and if we use identical hardware it makes a little bit it makes it a little bit easier for you to play along at home hey also want to give a shout-out to you guys that are helping me out over at patreon those of you who haven't signed up yet look down in the description down below there's a link to my patreon account think about hooking a brother up keeping me in coffee beans and in keeping my studio gear up to date so I can continue to improve the quality of educational material that I am providing for you but enough of that nonsense about talking about my patreon account and let's jump right in and get ready for today's lesson okay what we're gonna do today is sort of a follow on from last week's lesson in last week's week's lesson you learned how to use Python with the library OpenCV and fire up either the raspberry pi camera or a webcam and remember how it worked you fired up the camera you would go out you would grab a frame and show the frame and grab a frame and show the frame so you're able to actually within a Python program start the camera and display the video coming from the camera on your screen okay now as we get along in this series in this tutorial series and start doing more and more sophisticated things we're going to start ending up with a bunch of windows and as far as I can tell when OpenCV places a window it's sort of a video window it just seems to place it in ran locations and maybe if people know more about it than I do they could explain how it is the windows end up where they are but what's going to end up happening is we're going to be doing a lot of image manipulation and so we might apply a mask and we might have a little window to see the impact of that mask and then we might filter that result and end up with another window and then we might apply a smoothing function and end up with another window so we might end up with eight or nine windows and if those little video windows are just popping up in random places it's very hard for us to see what's going on and every time you run the program you try to get your windows oriented and so what I'm going to show you today is how to move the windows within the program so that you can keep your desktop organized because as we start really get in getting into this this is going to be important so what I will need you to do is come over and I will get out of your way here hopefully come over and open up your visual code studio which we installed a few weeks ago and then probably if you remember we left off with this OpenCV one but I don't know we were playing around with it so it would probably be good if you went to the website our website ww2 I'm and if you look I have a lesson on starting the Raspberry Pi camera or a web camera on the Jetson Nano I'll put a link down in the description where you can hop right over to this page on the website and then we can kind of get this core program that we developed last weeks so that we're all kind of starting from the same spot and this program just fires up the camera and then grabs a frame shows a frame grabs a frame shows a frame this is going to be our starting point for the next umpteen lessons and so we'll always want to go back and kind of start with this as a starting point and let me make sure I got the latest one here do a quick reload Ida played around and tweaked a few parameters and let me let's make sure that this looks good that does in fact look good and so if I come up here and click on this little tube age thing it selects all the code in that box let me see if I can take this back it's kind of hard my yeah there it goes okay so I'm going to click the two little pages right mouse click copy now I have that and now I'm going to come back to visual code studio and remember we made this folder this OpenCV folder you want to click on that and then come back to the main folder PI pro click the plus and now you're going to have a new file and we are going to call it we're gonna call it o open cv2 and then I'm just going to do - move went move window dot py the dot py is important that suffix is important because it tells everything that you're dealing with a Python program here and that actually matters and then I call it I call it open CV I call it open CV - because this is the lesson number two and then move window because that's what it does and so let me click enter and now I have a new window and let me close this one from last week and then I'm gonna paste this starting code from the website so right mouse click and paste ok so you can see that we are starting by setting for the raspberry pi camera a display width of 640 a display height the 480 flip makes it right-side up for how our our camera is oriented if yours comes up upside down switch the flip back to zero and you can see you've got two choices here you can either use these two lines of code you can use these two lines of code if you're using the Raspberry Pi camera or you can simply use this line of code if you're using a webcam okay so let's try this I'm going to uncomment out the raspberry pi lines the two lines that we would use for the Raspberry Pi I'm going to right mouse click and run run Python file and terminal boom you can see that we had it come up that is in fact from the Raspberry Pi you can see I'm putting my finger over the Raspberry Pi camera and it is coming up and looking good everything's good hello okay I'm going to quit that let's say if we wanted to use the webcam I also have a webcam on this Jetson Nano I would come down here and uncomment out this line of code and for me my webcam is probably going to be on one if you only have one camera then your camera is probably going to be 0 but if you already have something hooked up your webcam might be camera 1 so I'll try that right mouse click run Python file in terminal and boom look at that okay that is the webcam so you see with this little code depending on whichever camera you have you can get it running so let me quit that and then again this core I'm gonna go back to the Raspberry Pi camera do this do this now the Raspberry Pi camera should be hooked up okay so now the core program is really simple we come in and from cam our camera object we read it and we put it in the variable called frame and then we show it and we show it in a window that we named nano cam what do we show we show that frame that we read and then we sit and we look to see if anybody has pressed the Q key and if we press the Q key we break out of this while loop and then we let the camera go and we destroy all the windows so this is kind of our corporate program if you don't understand it go back and watch last week's lesson but okay let's see what we could do here so I do a show and after the show what I could come in and do is I could do a CV 2 dot move window CV 2 dot move window and then I got to tell it what window do I want to move well I want to move nano cam the one that we just created and then where do I want to move it I want to move it to zero comma zero because remember it was kind of showing it halfway off the screen or in different strange locations and so I want the upper left corner of the window to be in the upper left corner of the screen and this should move it there to this very nice location so let's try it let's right mouse click and say run Python file in terminal and boom look at that coming from the Raspberry Pi camera the window is 640 by 480 and it's in the upper left corner of the screen which is exactly what we wanted and so now as we make more windows we can keep those windows organized so let's go ahead and let's make another window another image window but let's try to shake things up a little bit and let's try to keep it interesting so rather than just showing a bunch of copies of that same image that same frame let's create a new frame and let's call that new frame grey ok and gray is going to be equal to C v2 dot C V T color that means convert color so I'm going to do a CV to dot convert color and then what am I going to convert we'll remember our frame that we just read in I'm going to take that frame and I'm gonna do a CV to dot an all uppercase color underscore BG r cuz remember we're in blue green red we're gonna go BG r to gray okay and do you see how it actually finds it and you can if you want to be a little bit easier you can click on it there okay so let's look at this line again we create a new frame frame is equal to cv to convert color of the original frame okay and then what do we do cv 2 dot color underscore blue green red to grey so now that should be just like frame only it should be gray instead of black and white so now with that what could we do well we could do another I am show so we're gonna do CV to dot I am show and then what are we going to name this window we're gonna call it gray gray video like that and now if we put it at 0 0 it would be on top of the other one and so what we want to do is we want to put it down below and so if our if we come back up here and if we set our display window to be 640 by 480 and if we think there's that little bar on the top of the window I think that if we put it at about 0 comma 500 that should be right below that top image and again if I put it at 480 I've got the little menu bar that's gonna be in the way so I think that probably 0.0 comma 500 would be pretty darn good and so let's check that out right right mouse click and I'm gonna say run Python file in terminal well something did not like what did it not like I said see v2 dot ameesha oh man that that is absolutely not the right thing to to I combined two stuffs I hope you guys were yelling at me what do I want to display I want to display gray and then what do I want to do I want to do a CV to dot move window and then which window do I want to move I want to move gray video window that we just made and where do I want to move it I want to move it to zero and I better get my outside my quotes there okay so it's gray video and then where do I want to move it zero comma 500 so instead of being up here zero-zero I want it to be at zero and then 500 down and that I think should work run Python file and terminal boom look at that okay do you see what we have we have our original video in color and then right down below it but not covering it up well maybe I should move it a little bit further down okay because it is a little bit this is this window is let's see if we made it 520 what it would do okay I need to go back and kill that window with a cue remember we can kill these things with a cue let's make it 5 20 and see what happens run Python file in terminal yeah I think that's a lot better you can tweak these things to meet your own requirements but you see now every time when I start it everything is exactly in the right spot so now I'm going to click on that and kill it and now let's go ahead and let's let's do let's do two more okay so what I'm gonna do is I'm going to show each one of these twice and so what I will do here is I'm going to copy these two lines all right and then I'm going to paste them here okay and now I'm gonna make two new windows I'm gonna have nano cam to nano cam too but this one instead of going down by 520 I'm gonna gonna want to go over by 640 so I'm gonna go over by 640 okay so this should be like our third window and then I'm gonna come down here and I'm going to make a fourth window based on this I'm gonna say copy and then I'm gonna come here and I'm gonna make a fourth window and this one we will call grey video 2 and then we will call this gray video 2 and this one we want to be down 6:40 and oh we want it to be over 640 and down 520 okay so what we're gonna do here I made the wrong one - okay are you guys keeping up so this is I'm gonna show grey video and then I'm gonna move gray video to be down 520 now I'm gonna make gray video - and I'm gonna make it to be down 520 but also over 640 so now I should have four windows okay does that make sense let's right mouse click let's run Python file in terminal hey you guys kind of got a freebie Wow oh look at that for and you see even with the for displaying four images it's still keeping up very nicely and let's look and see how well all right really we need to move it over by more than 640 do you see how this is kind of covering it up so again you got to kind of tweak these things but I think maybe we'll try six let me just try 700 I don't know why it doesn't come out exactly the number of pixels you would expect how you would expect it but I'm gonna make these 700 and see if that would kind of have them where they're that second column of Windows isn't covering up any of the first one so let's do a right mouse-click and say run Python file in terminal okay yeah I think those are just almost precisely side by side okay I really really like that I think that that looks really good so we got four windows they're all running nicely in there exactly where we want them to be so anytime we ran this thing we could right mouse click run Python file in terminal and boom look at that everything right where we want them okay let me quit what if we made them little windows let's try 320 by to 40 and then I would then need to kind of come in and make this I'll tell you it's really easy to mess up this this one we're gonna make let's make it 350 okay and then let's make this one ah 260 and then let's make this one 260 and let's make this one 350 okay so these should be smaller windows and they should be around where we want them okay run Python run Python file and terminal boom look at that all the little videos exactly where we want them now I should say what I just did is only gonna work if you were using the Raspberry Pi camera because the Raspberry Pi camera in this cam set uses these display things to where if you just fire off the the webcam it's not using those parameters okay so it's going to be 640 by 480 so let's go back to 640 by 480 640 by 480 okay and then what I am going to do is I am going to go back to here 700 on the position and I'm going to go what would that be 520 on the position and then I'm gonna go 700 and 520 you could imagine you could do this with parameters and really make it very elegant but that would be a pretty easy thing to figure out so let's do a right mouse-click and say run Python file and terminal okay so here we are again boom that looks good alright just let's have a little fun here I don't know if any of you guys have two cameras like me but what I am going to do here is I'm going to turn on both cameras now so I'm going to turn on that second camera okay and then this is going to be okay this one what I'm gonna do is I'm gonna put another Reed in here I'm going to read the other camera so I'm gonna say copy I'm gonna say paste and then I am going to call this second camera I'm gonna call it cam two and then I'm going to read the second frame from cam two and that's gonna be frame - okay so frame two comes from cam two frame comes from cam and then a no cam is going to show frame and then what I'm gonna want is nano cam - I'm gonna want to that to show frame two and this is some crazy stuff that we're doing down here now what we need to do here is when we convert to gray okay what would we want to do here we would also want to convert so I'm going to select that copy I want to convert that second frame - great - so I'm gonna say V control V and then I'm going to make this gray - and gray - is based on frame two and then down here I will want to show gray and then I will want to show gray - what is the chance that I did that crazy crazy man coding without making a mistake somewhere all right let's try it right mouse click run Python file in terminal boom look at that two cameras black and white in color uh-huh okay so let's look at this so this is the webcam you see the webcam my hand is in front of the webcam and I've got color and black and white on the right that's the webcam and then over here I've got the Raspberry Pi camera and so I've got two cameras running at the same time I'm trying to see does it look like there's a little bit of a difference in latency one two three four one two three four it looks like there might be a tad tiny a bit of more latency between these two things and it looks like the webcam is just a tiny fraction of a second slower yeah it's maybe a quarter of a second slower let's look in there and see if we can figure out what might be going on well what we do is it looks like we are it might be on the display side because I'm doing some things here okay although it must be I think the way I'm displaying them it must just be between grabbing them that there's a little bit of latency on the webcam that's not on the Raspberry Pi cam so let's try running this again okay that is really cool we've got everything just where we want it we got four windows and hey you also learned another command today you learned how to convert something to grayscale and so you can see that there's all types of different conversions to do and let me just explain that a little bit more again that if you remember the Raspberry Pi camera and the webcam OpenCV reads instead of red green blue it reads blue green red and so what you're saying is is that you're saying to convert color cv to convert color you want to convert the color a lot of frame and then how do you want to do it well you want to convert color okay blue green red to gray and then you end up with a gray image and if you look in there there's all types of different conversions that you can do but that's just a quick one okay guys this is getting a little bit more a little bit more advanced it was a really important lesson because as we start doing the image recognition and as we start doing the the image processing and the manipulation of the images we've got to have lots of windows and we've got to keep them organized this will allow you to keep your windows organized so what you guys need to do is go around go on and play around with these different parameters that you can use and move your move your windows around and kind of get comfortable with where the extent of your screen is in all the different places you can put them it's also kind of interesting if you have the Raspberry Pi camera and you make these a lot smaller and just really stack up a lot of the little video windows and so let your homework for today is play around with that okay this has been kind of a quick lesson to me it's been kind of a fun lesson and let's see I'll give you a heads up what we are going to do next week is we're gonna you know we can move the windows now but next week what we're gonna do is we're gonna resize the windows because as you're doing all of this masking and all of this filtering a lot of times you might want eight or nine or ten very small windows that let you see the progression of your image processing and then you want the final result big and so it's very helpful and very powerful to be able to resize windows okay guys this is polemic order from top tech boy comm appreciate you guys tuning in again really appreciate you guys that are helping me out on patreon look forward to talking to you guys next week and so we will see you guys later [Music]
Info
Channel: Paul McWhorter
Views: 14,893
Rating: undefined out of 5
Keywords: Jetson Nano, OpenCV, AI, Artificial Intelligence
Id: UfqdstwSews
Channel Id: undefined
Length: 25min 54sec (1554 seconds)
Published: Sat Nov 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.