OpenCV TUTORIAL #3 How to Record Video in OpenCV & Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this one we're talking about how to record video in open CV and Python now like many of our open CV tutorial videos we have a blog post that will reference what we do in the code here and this one's actually really important because of recording video is actually a little bit more challenging than a lot of other things that you can do in open CV the reason for this has to do with your system so a Mac and a Windows and a Linux they all have different codecs installed by default and sure we can upgrade our systems to make sure that we have all those codecs installed but I actually wanted to work with some stuff that works out of the box and that's where this blog post and this video will actually make the difference in getting those things started for you now the other part of this is if you run into any issues please comment on the actual blog post so other people can learn from those issues as well and we can also improve that blog post so all of us can benefit from it I did a lot of research on this mainly because I initially was doing it on my Mac computer and then I brought it over to my Windows and then things broke things weren't recorded and it just didn't work in the way that I was hoping but I got those things sorted out so now we have a blog post that's ready to go so without further ado let's jump in of course you have this blog post open now and we also are gonna be working off of that sort of base OpenCV stuff that you hopefully have seen before if you haven't pause the video now and make sure that you're working off of this this is just where we're gonna start so we can build everything that we need to build in this series right or this tutorial so what are we trying to do here we want to record video so we can save it right so this means that we have to take a couple things into consideration one of them is whether or not we need to change the resolution that's one of those things that you want to think about right off the bat and then the other one would be how many frames per second you want to record as well as the file name these things often times when you record on let's say your smartphone they just sort of happen by default they're already built in but for us when we record something in open CV we want to name those things ourselves and do those things ourselves so the first thing I'm going to do is right above all this I'm gonna go ahead and set my file name I'll say file name equals to something like video dot avi and of course dot avi if you're familiar is a file type right so it's an encoded file type it's a different type of encoding for videos another one is of course mp4 so dot mp4 those are the two that we are focusing on here because I've found that they work across systems the easiest avi is also probably the easiest to do because of the codec that's built in we'll get more on that in just a little bit and the next frames per second I'm just gonna add them as 24 frames per second so this of course is how many frames we want to record every second so if it's 24 that's what movies typically do or films to typically do 24 frames per second so that's what we're gonna do and then the resolution is going to be what we want to declare so I'm going to give it as 720p although my actual recording device can do 1080p but 720p that size resolution will significantly lower how much or how big my file will end up being now with OpenCV we can't actually record audio by default that's actually not what it's good for so if you're wanting to record audio OpenCV is not really gonna be the solution for you ffmpeg is probably gonna be the one so I just just wanted to make that note ffmpeg will most likely be what you're in abusing ok so now let's think about this and in terms of what is it that I need to do from here well I have this resolution so I'm probably gonna need to set this resolution as I go forward now if you look at the blue post itself we can see that I have this res function so I can go ahead and copy and paste this over that's that's just what I'll do and bring it in here and this is coming from a different blog post that we created as well as a video which you can see right there so this is meaning that I want to change this resolution based off of you know what I input here but as we see we actually don't have a height and width we don't have those breakdowns we don't have those standard dimensions for any given recording so I just go I went ahead and put those in those blog posts as well and we're gonna paste those in and these are those standard dimensions they are width and height so 480p it's 640 wide by 480 tall so those are pixels those are actual pixel values on how big they are and as you see they actually double roughly double each time well actually not quite double but I mean it's very close so that's something to think about when we're getting there we're building these resolutions and they're scaling based off of these as well so the video you're watching right now was it recorded in 1080p so you can actually watch it in that resolution and you would have these same dimensions that's kind of the idea here so now that we have the ability to set of resolutions and we have a dictionary of standard resolutions or dimensions we can actually start to think about how is it that we're gonna set these things so again this a lot of this stuff has to do with making sure our videos are recorded well I could have just jumped in and show you how to record videos because that's actually really easy and that's in the documentation but what I wanted to show you was a quicker way long term quicker way to change what resolution I might want and make sure that all of that is running smoothly when we actually go to the recording process so let's go ahead and create our method to do this and it's gonna call define get dims as in getting dimensions and then we're going to use the cap as in the video capture instance right there and then we will set what resolution we want to use and by default I did 1080p okay so what we do here now is we want to get the height and width of our defaults whatever default we're gonna put as our standard and dimension and the default I did was the width and the height of the standard deep dimensions of 480p now why I did this is because if just in case you accidentally put the wrong resolution here I still want some sort of resolution I want something in there so I use the standard dimensions one of 480 to make sure that that works so the next part would be just checking if the one we pass is in side of that dictionary and if it is then we'll use that one so we just can copy this part and change this from 480 to res now I've granted I could also just change how this method works to setting the resolution and doing all these different things but I'm gonna do it like this just so it's nice and spelled out for me on how everything is working and then after that I'm going to actually call this change res method up here and this is going to grab the height and width that we just got from that standard dimensions dictionary and then we want to return that height and width so again we want to actually grab these dimensions based off of our capture so down here this is where we can grab that so we'll just say DIMMs equals to get dims and of course we pass in cap as the capture and then our default resolution which we set up here so all I do now is say res equals to Red's or we could say my res let's just call it my res so we don't get too confused as we're looking through this of course the actual blog posts will stick with res and it does it a little bit differently than this but what this does now is it will change the size of my default frame to whatever that resolution I'm trying to capture which we can actually test down so let's go ahead and save this and I'm gonna jump into PowerShell right here and I've been doing a bunch of tests that's why you see some code there but if I go to record video to PI which of course is the name of my video and looks like I have a spelling error ah yes so change res I didn't actually pass in the camp that's the problem okay I thought it was spelling error it's not it's just that okay so that changes the capture device as we see here this is the video right now that's happening in real time so if I close that out and change the res again let's change it to something that doesn't exist 360 or 260 whatever but 360 if I save it as that and run it again what what happens is it goes down not to 360 it goes down to 480 because of that default stuff right here and then finally if I try to do 4k and save that and then run it again oops we need to close out that frame and then run it again it it tries to do 4k right so it actually upscales the resolution on this video and as you see it is choppy and that's not you guys I see this too so I'm going to close that out so that's where you know different systems will handle that actually different that in my experience that's how it's been so again I'm gonna go back to doing 720p and just leave it as that because that's a really easy way to do it so I've now got to the point where I'm ready to start recording whatever this frame is that was a little bit long but it's critical in doing what comes next which is actually grabbing and setting the video type itself so the first thing that I want to do now is above all this cap stuff we're gonna just add a new dictionary here called video type and honestly I'm gonna just go to the blog post that grab that's one so scrolling down a little bit I'm gonna grab these two lines here and we will discuss this and then we'll paste this in here save it now of course if those lines change for the blog post just use what's on the blog post because those should have been well tested so this is now the time to talk about the codex so the video type is coming off of well we do it based off of the file name so let's make sure we import OS up here too which is something I don't have imported right now but I need to import it so we import OS and we are going to use this get file type from our file name so in other words we want to have this dictionary here so I can switch between avi and mp4 which we'll see in just a moment on how useful that is but something you also should see here is this video writer underscore for CC now for CC has all sorts of video codecs that you might need to adjust for your own system which I actually already have open these are the video codecs so whatever video file that you end up using this is it right so there's all sorts in here and you might have to do additional installations on your system to make sure that those things happen so if you're interested in seeing those additional installations please let us know number one the codec number two what system you're working on so we can try and figure out ways to at least put that on the blog post so let us know in the comments of the blog post and you can let us know in the video too because I think those things will absolutely absolutely help okay so these codecs the X vid xvid has been the one that I've found that works very well on Windows and Mac for these two different versions so you can use it for avi or you can use it for mp4 and then this next method essentially just takes a file name such as the file name we give up here and it parses that filename into its extension and then it looks for one of the video types much like what we just did with the standard dimensions and it will look for that and then it will return that cool so now now is the magic now we actually write the out methods so what I can do here is I can grab the video type CV - and I'll just go ahead and say grab or get video type of that file name which we named up here right so video avi so what this should give me back by what it is now is it'll just give me back this value here so I can actually reuse that somewhere and where I'm going to reuse it is in this out method here which we are gonna use see v2 dot video writer it takes in the file name it takes in the video type and absolutely you can hard code this video type here and then it takes in the frames per second which we set up here which is 24 and then the last thing it takes is the dimensions that we have right here so dims we can just put in now of course you can actually put in with comma height there instead of dims just so you know but now we have our out method so this this actually is a way to write out our video we haven't written anything yet but this will actually create the file itself so at the bottom underneath cap dot release we want to do out dot release so when we actually leave our while loop here we want to do all of these things right so when everything's done or at least the capture or at least the recording and all that so the final thing is the easiest step is now just doing out dot write and frame we save that and there you go let's try it out it's gonna save it to this video name avi so I actually have a few of these already in here as tests that I was running so I'm gonna go ahead and delete these and they actually don't really matter so again this is just about testing and getting more familiar with these things so I'm gonna go ahead and run it now let's save everything let's make sure it's all running PowerShell back in there or your terminal depending on what you're using I'm gonna go to record video and right now it should actually be recording video twice right I've got my screen recording but then also this video recording and it should be an AVI file so I'm gonna go ahead and close it out with Q and we'll look in our video here and what do you know I actually have a length and some KB's in here now if you did this incorrectly you will not see a length and you'll probably see something like six kilobytes in here that's what I've seen so if I go ahead and open this up and just in my video player right off the bat it's playing videos so not to get confused I'm not actually saying what's in that video right now that is based off of you know what we just recorded so that's the AVI file as we see with this file type I can also do this same thing with a mp4 so let's just change the extension to mp4 save it and we'll go ahead and run it and it gives me this error right it's not supported with this format this is something you might see but it says it's not supported I closed it out and then I look at here I've got my kilobytes running and it says it's an mp4 file so let's go ahead and give it a shot and it still raw uploads it still works it still runs so granted it's giving me this error but it worked so this is the error of the codecs that you're going to you know sometimes have challenges with when it comes to working with open CV and that's why that's why this video is important and also what do you end up recording is important as well so like using for CC how we actually go about doing that those things hopefully will be updated what you guys see and also the additional research that I see and do now one other thing that I have noticed in my own tests hopefully we come up with a solution for this and if we do I will try to remember to put it on this blog post itself but the main thing here is I've tried to change this to being a gray frame so turning it into grayscale doesn't seem to work for me so if you guys find that answer out both on Windows and Mac please let me know but for whatever reason I haven't been able to record actually gray video using this method that is turning a frame into grayscale which actually let me just show you what I mean so if we go into some of our other stuff like camera tests we had this grayscale here so I'm gonna go ahead and copy this and bring it into my record video and instead of gray I'll just call this frame because it's now just transitioning that frame to grayscale and then it writes it and then up here I'll go ahead and just say video gray and I'll use dot avi because I know that works or at least I know for sure avi has the codec on my current system so I'll go ahead and run it and I'll run to record video we've got gray here looking okay we'll see I hit cue to exit and I jump back in video gray there's my six kilobytes so I've tried a few different things such as turning this back into color like like running this and then converting the color back for whatever reason those things aren't working so I'm hoping that maybe you guys find it if you don't that's okay we still have color video here and that part is awesome thanks so much for watching you guys make sure that you subscribe on join CFE comm /a youtube if you enjoyed this please give me a thumbs up there as well look forward to doing a lot more with OpenCV with you guys thanks so much for watching we'll see you in the next one [Music]
Info
Channel: CodingEntrepreneurs
Views: 58,068
Rating: undefined out of 5
Keywords: cfeopencvseries2018, coding, opencv, python3, cv2, computer vision, ai, keras, tensorflow, raspiberry pi, facial recognition, face recognition, video analysis, video security, raspberry pi video security, tutorial
Id: 1eHQIu4r0Bc
Channel Id: undefined
Length: 19min 59sec (1199 seconds)
Published: Mon Apr 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.