AI for Everyone LESSON 6: Faster Launch of WEBcam and Smoother Video in OpenCV on Windows

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 six in our incredible new tutorial series where you're gonna learn artificial intelligence or you're gonna die trying what i'm going to need you to do is pour yourself a nice enormous mug of iced coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're getting out your coffee as always i want to give a shout out to you guys who are 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 you are going to learn today and what you're going to learn today is you're going to learn how to squeeze a little bit better performance a little bit more performance out of your logitech webcam now in the setup for this class in the early lessons i tried to emphasize the importance that we're operating on the same webcam and we're operating on the same versions of opencv and the same versions of python that way when i show you something if you do what i do you'll get the same result that i get if you're on some random camera on some random version of python on some random version of opencv i don't know whether it'll work because things are changing so quickly that's why i want us in this class to try to stay on a similar a similar hardware and software platform if you don't have the logitech webcam look down in the description there's a link where you can get the exact same camera that i have but enough of this introductory banter and chit chat let's jump in and let's get ready to learn how to get more performance out of your webcam in order to do that i will switch over to my screen and i will move my big noggin out of the way and then what i am going to need you to do is i'm going to need you to fire off the most excellent visual studio code and what we're going to do is we are going to come over and open up our file browser and then you can see that we are in in our working folder python and i am going to want to create a new program and that new program i will come here i will click below the last program and then i will click new and we are going to create open cv dash 3 dot p y and the dot p y is kind of important and boom we have a fresh new python program just waiting to be written okay now what i want to just to do is i want us to sort of start with that just that fundamental code that launches the webcam because there's no reason for us to rewrite that code every time you guys probably have it stored over here already if you don't what you can do is you can come over to the most excellent www.toptechboy.com and in this happy little search box what i want you to search on is i want you to search on the term launch a windows web camera and then you should come to this little lesson here and you can come down here and i have the most excellent 10 lines of code click on the double windows right mouse click and copy now you've got those 10 lines of code and you don't need to rewrite those every time come over here come to our most excellent opencv 3 program ctrl v to paste that in there and now i am going to move the file navigator out of the file browser out of your way now just for good measure because you might have been working on my uh also on my visual python my 3d graphics class let's make sure that we are on the right interpreter so i will put control shift p control shift p and then we want to go to python select interpreter so if it doesn't pop up for you python space c lect interpreter and then it should pop up for you if it didn't already and we will wait a second for it to give us our choices and it looks like that mine was on 3.9.6 and what i want is i want python 3.6.6 in the virtual environment you guys if you're drive by shooters and have just dropped into this lesson you need to go back and look at some of the earlier videos where i show you how to set this up and then you can be in the same environment that i am in but for you guys that are in the class it will make perfect sense what i just said i'm going to have a sip of coffee okay so now what i want to do is just make sure all this copying and pasting nonsense didn't break something so i will come up here and i will click run and it seems like nothing happened oh there it goes okay and it looks like it's uh launched uh you know we said print the cv2 version 4.5.3 okay and now we have to wait a freakishly long time i mean this is like one of the fastest computers that you can buy right now and it took like what 10 15 seconds to launch the webcam and i don't know if you guys are struggling with that too but it's like very very slow to launch and if we're trying to debug our code that really uh that really slows us down i really shouldn't touch my eye with the virus going around you know shouldn't touch your eyes shouldn't touch your face okay but i i digress okay so if we look at this what three things do we really not like about what we are doing here what three things do we not like right the first thing that we don't like is we don't like that it takes 10 to 20 seconds to launch the webcam right and it seems to me it might just be my crazy old imagination uh my you know maybe my paranoia is showing through but it seems like every time i run this it's getting longer and longer and longer and it's the video as i'm making my class the video as we go on and on and on it seems like every time it's taking longer to launch that webcam so slow launch we don't like the other thing that we don't like is do you see how i'm not getting perfectly smooth uh motion and it seems like the more motion that i try to do the jumpier it gets that you know that it's struggling to do the uh compression that's the second thing we don't like and the third thing we don't like is it's coming up in kind of like a random size this to me looks like about 640 to 480 that is coming up in what if i wanted to do a different size now i realize on that last week's homework some of you guys struggled because your camera was coming up big and you couldn't get four windows on your screen and so you were struggling because your video window was too big to stack four of them together those are the three things that we're not happy about those are the three things that we're gonna fix today okay we're gonna fix those three things today so i'm gonna come here i'm going to quit out of this program we're going to keep these 10 lines and we are going to we are going to make them better and let's see guys i'm going to scoot out your way the first time you run the interpreter in uh with a with a virtual environment interpreter selected it will put your your shell down here in the bottom it will put it in it will put it in that virtual environment and i don't like to do it that way so i'm going to deactivate the virtual environment of the powershell now up at the top python is still running python is still running in that virtual environment i just don't like my powershell in it okay don't know why i just don't so i deactivate it and i don't like myself postage stamp size i don't like myself being a munchkin so i will come back to the medium size and now what we want to do is we want to see how we can improve the video capture now the first thing we're going to do is we're going to start sending it commands where we tell it what size we want we want to be the boss we want to be in charge not the camera in charge giving us some random size so we're just going to say i want a width of what is a good width 1280 and a height of a height of 720 pixels all right don't do random widths and heights you need to do a standard one what are standard ones well standard ones would be like if you wanted a big picture you would do 1920 by 1080 a nice sort of large but not freakishly large is what we're doing here 1280 by 720 you could do if you wanted a smaller one 640 by 360. and you see you want to kind of if you're just winging it you want to kind of go in factors of two and if you go in factors of two you'll probably land on a good one also 320 by 180 will work 160 by 90 would work also there's a different ratio that you could use 640 by 480 and then you could double that one or you could half it okay and so that gives you a lot of different choices but pick one that is sort of a standard because you can get some unexpected results if you just came in and said like you know 600 by 400 or just made something random up you could get some unexpected results all right now so we're going to use this later this is just setting variables this isn't setting the width and height this is just setting the variables up all right now what i want to do is i want to come on my video capture and this is the thing when we just say video capture one and yours might be zero okay your webcam might be on xero my webcam is on one if we just go out and tell it to look for that port to look for that webcam we have no idea what it is using as parameters we have no idea for what type of default parameters opencv and python are using so we want to start setting those parameters because if we set them then the video will give us the the characteristics that we want so here's a little helpful one and matt is going to be cv2 dot cap and that's like for capture and then d show and this is saying that we are capturing the frame and what we want to do with it is we're just going to do a direct show and so this is kind of like telling windows we're capturing the we're capturing the frame in order to show it and when we do that what that's going to do is that's going to let us tell it that so it doesn't have to sit there for five minutes and figure it out one of the keys to make this thing launch faster all right now the other thing that we want to do what was the other thing that we didn't like we didn't like that it was launching slow and we didn't like that it was giving us a random uh a random image size so we want to set that image so what we're going to do is cam dot set now why cam because that's what we named our camera object because we named it cam we are going to then do a cam.set set is the command and what do we want to set well we want to set cv to dot cap for capture and then underscore prop for property okay cap prop and then underscore frame okay and then underscore width and you can see now that it's kind of guested right so i'm just going to select that so i'm going to do a cam set cv2.cap prop frame width and then a comma and then what do i want to set it to width all right now we're going to use i'm not going to copy and paste that i'm going to do now a cam dot set and i'm going to do a cv2 dot cap prop for property underscore any guesses what we're going to do class class any guesses you guessed it height have you noticed how you say height like it's a th even though it's height and you would be surprised how many errors i've seen that have just had people banging their heads against the desk trying to find it because they spell height height like th like you say it all right but i digress and then that is going to be height right so we're going to set so now we're sending commands to the camera to set the width and set the height that we want and then we define those up here right now this one is another one that we have no idea what frame rate opencv is using so we want to explicitly set that and so we are going to do a cv2 dot cap prop frame and then underscore and the magic parameter is f p s for frames per second and then i'm going to go ahead and set that for 30. so 30 frames per second okay guys these four commands are let's see one two three four things that we've done so so far plus the next one will fix all three of the problems that we don't like you can't just do one of them they sort of work together you got to configure your camera and so from now on i think we'll want to use this method where we're always sort of setting our parameters just as a matter of good principle now we're going to do a cam dot set all right one of the magic things with video is the video is encoded it's sort of like a compression and a wrapper that turns still frames into a smooth running video and some of the stuff that it tries to do is compression so that you don't put too much too much demand on your cpu or your gpu depending on where you're you're running it and so this is going to specify the codec it's going to specify the format of how to go about doing that frame capture and which codec to use and so how are we going to do that on cam.set we're going to go cv2 dot cap prop and i know this is strange don't panic it's 4 cc and that's just the command that's going to let us set up the codec and now we put a comma and now we've got to tell it which one and that's going to be cv2 dot video video and then writer pay attention to my capitalization the v is uppercase and the w is uppercase video writer okay video writer and now underscore for cc okay video writer underscore 4cc and now we have to tell it which one so we put an open bracket i'm going to get out of your way before you can't see this okay so now we're going to tell it what 4cc video writer to use and what we are going to want what we are going to want is going to be a star a star and then an open quote and m j p g so it's like a moving jpeg an mjpg okay so look at that you got to put the star because that then is formatting the mjpg in the way that the cv2.video rider operation wants and so basically with this command you are setting the codec you are setting the format you're setting the wrap you're setting everything to one that will run smoothly on windows okay and this this and then setting the width and height and then telling windows that you want to direct show this should make things run fire up quickly the exact size we want and then also have it run a lot smoother so let's see if in fact this thing is going to run better take a sip of coffee before we fire this one off okay so i will come over here and i will hit the run button cv2 what is this nonsense cv2 has no how could i have possibly made a mistake so let me look at this error bigger and see where it doesn't like it in line eight cap prop ah i see it was my mistake it was not frame it just cap prop fps it was extrapolating from the earlier command so it's just cap prop frames per second okay you guys can see that right a little typo on my part let's give it another try hold your breath boom look at that okay what do you notice it launched in just a few seconds okay it launched in just a few seconds what else do you notice it's the exact size we asked for uh-huh boom all right and then what else do you notice you notice that it's a lot smoother okay it is a whole lot smoother now i'm going to quit out of that because that is a little bit bigger than anybody wants to see me and so let's come up and let's try a smaller one so let's come over here and let's go 640 by 360. okay and i know when you say 640 you think 640 by 480 but that's that old ugly sort of squarish that old ugly sort of squarish size to where the 640 by 360 is more like that what did they call it 16 by nine or something like that i don't i don't know but it's more like the the shorter wider that we have grown accustomed to so let's try running this again boom who's your huckleberry i'm your huckleberry look at that now we can get the size that we want now we can get the size that we want look at that right i'm going to queue out of that and then let's just for fun come over here and say 320 by 360 divided by 2 is 180. i'm always a little intimidated to do math head math live you know i teach there you go hello okay i teach math or i'm a you know long-term math teacher but still i don't do head math i do algebra i do calculus i can solve complicated problems but i've never put into my mind to try to do to try to do head math okay so so i always have to stop and think if i've got a if i got to do something like that okay look at that all right do you guys see how much smoother this was than it was before and so now we can control where the window is we can control how big the window is we can make it launch quickly and then we can make it run smoothly so i would say that this has been a pretty productive lesson but as always i'm going to need you to do a homework and the first is to show me that you've actually watched the whole video and if you want me to heart your comment put a comment down below and i need you to give me the secret word for today and the secret word for today is wait for it termite mound okay termite man why do i show you this this is like oh it's it's probably a couple hundred meters three four hundred meters from my house and when i tell people when i tell people how to get to where i live it's kind of complicated the instructions are kind of complicated because there's not street names here and there's not intersection names and there's not it's just kind of like one big blur and so part of the instructions that i have to give people if they're going to come and see me is turn left at the huge termite mound do you see that little kid this this termite mound it's like 12 feet high okay so the secret word for today is enormous term termite mound or huge termite mount all right and then also i need to give you a homework assignment and that homework assignment is now that you can control the position of the window the size of the window and whether the uh image is black and white or color what i want you to do is just for your screen and your computer just see how many of these little windows you can stack up like if you keep making them smaller and smaller how many can you put on your screen but yet still have them where they're running so it would probably be at some point that you're trying to show so many different videos that the thing isn't giving you acceptable performance but just sort of like see stack them together make them smaller and smaller and then give me like homework solution and what you need to do is if you actually do this you need to say i am legend okay and then after that how many rows and how many columns so like that homework from lesson five what we did two rows and two columns for four images but now that you can make your windows smaller you could do more but at some point you're going to have so many that it's not going to look very good so one thing is leave a comment down below giant termite mound and then secondly do the homework and see how many rows and columns you can get maybe you would say three by three or maybe it would be four rows and six columns or something like that but always so like if you say four by six understand that it's always row by column row cross column so you always list the rows first and then the columns and so your answer might be i am legend three by four and then giant termite mound and that way we're going to kind of confuse people who just read the comments who haven't actually watched the video all the way through okay guys i hope you are having as much fun taking these lessons as i'm having making them if you like the video be sure to give me a thumbs up and leave a comment down below when you leave a comment in the comment section it kind of gives me youtube juice and the old youtube algorithms and kind of helps more people find my videos like and share this if you have not already subscribed to the channel when you do make sure that you ring that bell and then you'll get notifications when my future lessons are dropped and so appreciate all that really appreciate you guys i'm having a lot of fun with this i really enjoy the premieres if you guys are not tuning in for the premieres make sure and do that we have a lot of fun with that i'm going to try to start doing the live streams again okay i've been having little connectivity issues with my internet but i will promise you i'll try very soon to start doing the live streams again all right guys this is paul mcquarter from toptechboy.com i will talk to you guys later [Music] ugh
Info
Channel: Paul McWhorter
Views: 4,474
Rating: undefined out of 5
Keywords:
Id: bJIzOniUaAw
Channel Id: undefined
Length: 25min 17sec (1517 seconds)
Published: Tue Aug 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.