OpenCV Python Tutorial | Creating Face Detection System And Motion Detector Using OpenCV | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone this is Sora from Eddie Eureka today's session is all about computer vision using OpenCV in python so I suggest you to go through the basics of Python I will leave the link in the description box below for now let us move forward and have a look at the agenda for today so guys this is what we'll be discussing today we will begin by understanding what exactly is the meaning of computer vision then how a computer is an image then I'll tell you what is OpenCV and how it works after that we'll see how to create an image detector as well as a motion detector using OpenCV so I hope you guys are clear with the agenda let us move forward and we will understand what exactly is the meaning of computer vision so before I explain you anything guys I believe all of you are on Facebook and the moment you upload any picture on Facebook there's a feature called Auto Attack right the Facebook will give you suggestions to tag people who are there in the image right so how do you think that happens there happens all the curves of computer vision so computer vision is an interdisciplinary field that deals with how computers can be made to gain high level understanding from digital images or videos so the idea is to automate tasks that the human visual systems can do so a computer should be able to recognize that this is a face of a human being this is what a lamppost looks like this is basically a statue so things like that right so I hope I'm clear what is the meaning of computer vision and I don't think so I need to explain you one of the applications of computer vision right so let's move forward guys and we're going to see how a computer reads an image now this is very interesting so notice the image that is there in front of your screen right so our normal human can easily tell that there is a New York skyline in this image but can computers really see this well the answer is no computers see a matrix of numbers between 0 to 25 0 to 255 right so for a colored image there will be firstly 3 channels and red green and blue and there'll be a matrix associated with each of these channels right and each element of this matrix represents the intensity of brightness of that pixel all of these channels will have their separate matrices and these will be stacked on to each other to create a three-dimensional matrix so a computer will interpret a colored image as a 3d matrix I hope I'm clear so when I say that the size of an image is 700 cross 700 and it is the colored image that means there are 700 rows 700 columns and there are 3 channels because it's a colored image similarly if I say 300 cross 700 that means there are 300 rows and 700 columns and if it's a colored image there'll be three channels now in the example which is there in front of a screen we have an image which has a size of B cross a and since it is a colored image there'll be three channels one thing to note here guys for a grayscale or of black and white image there's only one channel so if there's a black and white image and I say that the size of the image is 700 cross 700 it means there are 700 rows and 700 columns and one single channel right so I hope I'm cleared for a black and white image there's only one channel for a colored image there are three channels popularly called as RGB and each element of the matrix represents the intensity of the brightness of the pixel so this is how basically a computer reads an image it will read an image in the form of matrix it depends if it's a colored image then it'll be a 3d matrix and if it is a grayscale or a black-and-white image then it will be a 2d matrix similarly if I want to calculate the number of pixels all I have to do is multiply the number of rows number of columns Ali number of channels so if I say that the size of the image is 700 cross 300 cross 3 because it's a colored image then you can go ahead and multiply these numbers and you'll find the number of total pixels so I hope you have understood this let us move forward and we are going to focus on what exactly is OpenCV now first of all OpenCV is the library which is used for computer vision it was first developed in the year 1999 at Intel by Gary Brad sky and the first release came out in 2000 let me tell you that OpenCV supposed wide variety of programming languages such as C++ Python Java X vector and also supports different platforms including Windows Linux etcetera etcetera now OpenCV python is nothing but a Python wrapper for the original OpenCV C++ implementation and in an open CV all the images are converted from or to numpy RS right so if there's any image that OpenCV will first converted into an umpire Eric right and this makes it easier to integrate it with other libraries that uses numpy for example Syfy and matplotlib so I hope you guys know about numpy if you and aware of this particular liability go ahead and check out a detailed tutorial on lump I I'll leave the link in the description box below but for now understand that all the images will be first converted to an umpire array right so that's how OpenCV works since we saw that computer will first convert the image to a matrix in this case it will convert it into an umpire matrix or an umpire array you can see now will directly jump into certain basics of OpenCV so what I'll first teach you is how you can read in Mitch how you can perform basic operations for example resizing the image or displaying the image and things like that so that that's what I'm going to first focus on so for that what I will do is I'll open my pyramid I'll execute the code there but let me just first explain you what I'm going to show you so first of all I'm going to teach you how we can read a particular image the first thing you need to do is obviously import the opencv module then we are going to read the image with the help of this I am rate function so this one represents that it will be a colored image if it is a colored image and if we put 0 here then it'll convert it into a black and white or a grayscale image right and let me tell you that OpenCV will read it as an umpire array so basically python stores the images as numpy arrays or matrix of numbers so if it's a colored image it will be a 3d matrix and if it's a grayscale image it will be a 2d matrix so what I'll do is just quickly open map hi charm and I'll show you certain basic operations there itself so guys this is my fight um so what I have done here is I've imported the CV 2 module and what I'm doing here is I'm using this I am read function and I have given the path to the image so you can see the name of my image is flying dot jpg which is present in this particular path and one basically tells it to keep it as a colored image itself right so since I was telling you that all the images are converted to numpy RSA it is a colored image it will convert it into a 3d matrix let us see if that happens or not so I'm just gonna print this and let me go ahead and execute it and you will obviously see a 3d array here right similarly if I go ahead and convert this to a grayscale image if just all I have to do is put 0 here and we have a grayscale image here so let me just go ahead and run this and you will notice a 2d R notice the difference case right and if you wanna see what kind of a matrix this is all you have to do is type in hair type and it will show you which is an umpire in dimensional array all right so I've told you earlier as well that OpenCV will convert all the images to an umpire now what if I want to know the size of my image for that all I have to do is use the shape function and even though the size of the image since it's a lump array all I have to do is IMG dot shape and let us see what is the shape of our image so it is 600 cross 800 because we have converted this colored image to a greyscale image that means we have 600 rows 800 columns and one single channel because it's a black-and-white image now if I convert this to a color image you will notice the difference in the shape and you can see that 3 is added here which basically means there are three channels RGB so there are 600 rows 800 columns and there are three channels so I hope you guys are clear how OpenCV reads an image or how to know the shape or size of the image how do we know that whether it's a color image or a black-and-white image what is the difference what do you mean by RGB channel so I hope you guys are clear with that if you have any questions you can go ahead and mention that in the comment section will reply you ASAP so we saw how to read an image we saw how to print an image how to print the N dimensional array then we also saw how to print the shape of the image and what will happen if we converted it into a colored image then we'll saw that 3 was added in the end which basically means 3 channels right now what we are going to do is we are going to display the image how we're gonna do that we are going to use this weight key function in which if we don't specify any parameter and we keep it as zero it means that the moment user presses a key the window will be closed and if we specify a time frame for example 2,000 milliseconds then the window will wait for 2,000 milliseconds and then it'll be destroyed so this destroy all windows basically closes the windows based on my weight 4 key parameter so if it is 0 the moment any person presses any key then all the windows will be destroyed and if I have given certain parameters like 2000 milliseconds then it will wait for 2000 milliseconds and it'll automatically close the window so I hope our you have understood this let me just go ahead and open my PyCharm and I will show you there as well so guys this is the pycharm all I will do here is I basically want to print it or I want to display my image so I'll use this I am so function I have to give a name to this image to let it be a legend and we are going to type in here the name of the image here this is my file name and this is the image name right so I have given the path here if you can see now what I'm going to do is I'm going to add the wait for key parameter so for that all I have to type here is C v2 dot weight key and for now I'll just keep it zero which means that the user has to press any key to close the window right now what I'm gonna do is destroy all windows function and let us go ahead and execute this and we will see the image and the moment I press any key it is gone similarly if I type in here 2000 milliseconds right so it'll appear for 2,000 milliseconds and Lata matically go away right so I hope you have understood how we can you know display the image and how we can close the windows by using bit key and destroy all windows function so this is pretty easy guys I hope or you have no doubts here and still if you have any doubts again I am Telling mention it in the comment section I will reply you ASAP so now we saw how to basically read an image how to perform basic operations how to show the image how to close the windows and things like that now I'm going to tell you how we can resize a particular image so let me just go back and what I'll do is I'm going to resize this image I'll I'll go back to my pie charm again and let me just create one more variable let it be resized which is equal to C v2 dot resize the name of my image and the shape that I want so probably I want 600 Cross 600 right let me just go ahead and run this I want 600 cross 600 so instead of IMG I'll type in here resized and let us see what happens once I do this so you see that I have a resized image here right now obviously that is not symmetrical right so what can I do to make it symmetrical I can just go ahead and divide it by two or three the way I want to reduce the size so for now I'm just going to divide it by two and let us see what happened so basically if you see the code here as well what I'm doing here I'm reducing it by two right so image shape divided by 2 and I'm converting it into an integer because even if it comes out to be a float value I will convert it into an integer so new image shape is equals to all image here by 2 that's what I'm doing basically right so again I'm going to use the resize function for that I'll open my pie shop so all I have to do here is type in here and to convert it into an integer IMG dot shape function I'm going to use 1/2 I'm going to close the parentheses similarly here as well and open the parentheses IMG dot shape 0 / 2 and close the parentheses and I think we are good to go so the size of the new image will become half of the old image all let us go ahead and run this so yes we can see that we have reduce the size let me just keep it a zero so that if I press any key that only the window will be closed now similarly I can increase the size as well so let me just go ahead and multiply it by two and increase the size so my new image will be twice the size of the old image let us see if that happens or not we'll run this and yep it's it's a pretty huge image let me just close it too now alrighty then fine so we have learned how to basically resize animates let me go back to my slides and we are going to focus on what we are going to discuss next so we saw how to resize the image next up we are going to see how we can detect a face in a particular image so it's pretty easy guys what you need is an image obviously you need to create a cascade classifier and it will contain the features of the face right so that your code can determine okay where is the face then we will what we'll use will use OpenCV that will read the image and the feature file I will convert the image into numpy RS so it will so it's for the row and column values of the face right so number n dimensional array the face coordinates and it will display the image with the rectangular face box let me repeat it again for you guys what we need for phase detection we need an image then what we are going to do is we have a casket classifier which contains the features of the face then we will use open CV that will read the images and the feature so it will convert that image into a number array and it will search for the row and column values of the faces of the face numpy n dimensional array basically the face rectangle coordinates and then what we will do is basically will display the image with the rectangular face box right so what we are basically doing here is we are going to first read this casket classifier I will give the path to it then what we are doing is we are reading the image that we are talking about will give the path to it as well we are converting it into a grayscale image see v2 dot color or BG 2 BG or - gray is basically going to convert it into a black and white image or grayscale image then what we are going to do is search for the coordinates of the image right so we have this gray image we are going to search for the coordinates with the help of detect multi scale this is basically to figure out where is the face right let me just drill it down a bit so this will create a casket classifier object right and this is the path to the XML file which contains the face features then we are converting the colored image to the grayscale image after that what we are doing we are using a method to search for the face rectangle coordinates so this detect multiscale is basically a method which will help us to search for the face rectangle coordinates and scale factor basically decreases the shape value by 5 percent and until the face is found so smaller this value the great and is accuracy so scale factor is something I don't want you to focus too much right now this decreases the shape value by 5 person until the face is found it keeps on decreasing it range is smaller this value is the greater is your accuracy right and after that we are just printing the type and the face and how we are going to ask the rectangular face box for that we are going to use a for loop and a method to create the face rectangle this is nothing but a method to create that rectangle this is our image object right and this is nothing but the RGB value of the rectangle outline so I am going I have given it as some color you can give it whatever color that you want now this the width of the rectangular face box and these are nothing but the coordinates X comma Y then we have X plus width and y plus h so this is nothing plus X plus W plus comma y plus H right so I hope you guys are clear with it and let me just quickly open my pycharm and I will execute the code there I've already written the code so let me just take you through it it's very easy guys I have a cascade a classifier object here that I've created which will contain the face features then I am reading the image which I want to create a rectangular face box around or detect the face I'm converting it into a grayscale image then what I'm going to do is I'm going to determine the coordinates of the face for that I'm going to use this detect multiscale method then I'm going to add rectangular face box around it for that I'm going to use this for loop and this rectangle is nothing but a method to create that rectangular face box after that I'm resizing my image and that's all I'm doing so what I can do is I can divided by 2 so that it becomes easier for you to see but I don't think so I need to resize this image what I'll do is I'll just keep it the way it is and we're gonna see that particular image itself I don't think so I need to resize it so I'll just type in here IMG and we have our code ready that'll determine that will detect the face in the image yep it successfully did that so I hope you guys have understood how we can detect faces in a particular image using OpenCV so guys this is nothing but the type right so we have an umpire in dimensional array so this is nothing but the coordinates of the face now if I go back to my slides you can see here these are nothing but the coordinates which I have displayed here right it's not easy guys now let me go back to my slides again and we are going to see how we can capture videos using OpenCV interesting right let's see how to use OpenCV to capture video with computer webcam now before I move forward guys let me just tell you a few things so what we'll be doing will be using OpenCV for reading frames or images one by one so what basically a video is video is nothing but multiple images or multiple frames which are displayed very quickly so that it looks like a video so what we'll be doing will be using lutes to build a window where images will appear really fast so that you can see it as a video something like this guy he's basically smoking a cigarette it is nothing but two images but since I'm doing it pretty fast you can see it as a video like he's smoking although I don't want you guys' for now let us see how we can capture video using OpenCV the first thing we need to do is import OpenCV obviously then what we are going to do is create a video capture object and this number basically tells the computer to use her built-in camera right if I want to use an external camera for example I have an external cam and I can put one to use that similarly if I have two external cameras and I want to use a third cam I can put two here apart from that even if I have a video file I can give the path to that video file as well then this will basically release the camera in some milliseconds right so let me just quickly execute this and you will see what happens there right I'll quickly open my PyCharm let me just go basis of py file and I'll just type in here video equal to see v2 dot video capture and I'm typing in here zero to use my built-in camera and after that I'm going to release this so I'm just going to type in here video dot release that's all I have to do go ahead and execute this run it again notice that the camera light will be on for split seconds again I can execute it'll be on for split seconds right so let me go back to my slides and I'll explain you there what I'm talking about so basically when you execute the code you will notice that your cam light switch is on for a split seconds and then it turns off so what we need to do is we need to add a time delay and for that what we need is a time mode you alright so what I'm going to do is I'm just gonna type in ahead time not sleep 3 which will stop the script for 3 seconds for 3 seconds your camera will be on right so when you execute the code the webcam will be on for 3 seconds let us see if that happens or not I am going to execute this now so that I'll open my pie charm again so this is my pie charm again guys I need to first I'll time module here and now I'll add that time delay time not sleep for 3 seconds keep my webcam on and here we go we have successfully added the time-delayed was that easy guys now let's add a window that shows the video till now we didn't see the video we didn't see what's happening right so how can I add that window it's very very easy guys I hope you guys know this this is basically a video capture object now this check is a bool data type that returns true if python is able to read the video capture object this is just basically a true and false kind of a variable that will return true if python is able to read the video capture object otherwise it'll return false frame is an umpire array it represents the first image that video captures right since we saw that video is nothing but multiple images which appear is really fast and it looks like a video right so the first image of the first frame is basically what we will store in this particular frame the numpy array of that particular image so it is a numpy array it represents the first image that video captures we're gonna print both check and frame and we're gonna order time delay as well so let us this quickly open the pycharm and we're gonna execute this code there let me just open it for now what we are doing here is check comma frame equal to video dot read let me just print check and I will also print frame and let us go ahead and execute this we'll see what happens all right so what happened here Python was able to read the video capture object that's why we have got the output as true basically a check has returned true because Python was able to read that particular video capture object apart from that this is nothing but the numpy array for my frame rate which is pretty visible I don't thinks I need to explain you why it is an umpire a wide as three dimensional I think we have discussed quite a lot about numpy RS in the previous slide so for now I think you have understood this what checking frame is check is basically a bool datatype it will return true if python is able to read the video capture object and frame is nothing but the N dimensional array so guys and now we will see how we can actually create a window right so what we need to do is we need to create a frame object which will read the images of the video capture object and we will recessive Li show each frame of the video being captured so all I am doing here is using this imshow function again right and I'm going to give a name to it and I'm going to show all the frames which are going to come till I close the window all right so it's pretty easy guys for three seconds you're gonna see a video being displayed on the window and for that I'm just quickly going to open my PI charm and I just need to add this imshow function there and let us go ahead and do that cv2 dot I am sure I'm gonna give a name say let us call it as capture and then I type in here frame now we need to use this wait for key function wait key let us keep it as 0 so that whenever I press any button it will close the window and finally I'm going to type in here C V to destroy all windows so now let us go ahead and execute it so we will see a window that will capture our first frame and let us see if that happens or not here we go so yes the moment I press any key the window will disappear all right so this is how we can display the video on the window right let me just go back now how to capture the video instead of the first frame rate for Atilla what we did we just captured the first frame that appeared in from the camera right because that is basically what video is basically what it is nothing but multiple frames which appear really quickly and that comes out as a video so what we need is now we need to capture the entire video so for that what we'll do we'll use a loop because I've told you earlier as well that is what we're going to use in order to capture the video we will be using a veil loop right and the condition will be such that unless check is true Python will display the frame because what is check check will basically return true if bison is able to read the video capture object and if it is not able to read it then it becomes false so our condition will be such that if check is equals to true then Python will display the frames right it will display the frame on a window so let me just take you through the code that I have written here video is equals to see v2 dot video capture equal to 0 which is basically a video capture object I'm not going to repeat that I have a variable a which is equal to 1 while true a while Python is able to read the video capture object it'll enter the loop a is equal to a plus 1 which will keep on increasing check comma frame so it will print the frame basically the umpire array object and what I am doing here is converting the frame the image into grayscale image then I'm going to show that grayscale image and I have given it a time frame that is one millisecond so it will be that this will generate a new frame after every one millisecond so this will basically generate a new frame after every one millisecond right it is nothing but a wait key and after every millisecond the frame will change all right guys and the moment I press Q this break statement will come out of the loop and the window will close right prende this will print the number of frames that we have captured right and we did not release you know destroy all windows you know right let me just take you through the code again we are importing cv2 and time module then what we are doing we are creating a video capture object after that we have a variable a equal to 1 which is nothing but it will tell us the number of frames that we have captured Y true which means that while pythons able to read the video capture object increase the a value by 1 then you know these two statements we printing nothing but the frame the n-dimensional re object then we are converting that frame to a grayscale image then we are showing it and after that we are generating a new frame after every one millisecond that's what it is doing her then once we press Q we are our the loop and console will print the number of frames and all those things right so let me just quickly open my PI charm and I will execute it there all right so for that let me just remove these things for now right so after this I have a video capture object that you can see below this I have a variable a equal to one now I'm going to use Veilleux p-- while true enter the loop a equals a plus one now I'm over here I'm just going to print the frame and now I'm going to convert this to a greyscale image krei is equals to C v2 dot CVT color frame comma now I'm going to type in here C v2 dot BG r underscore CB 2 dot C olor underscore b g r2 gray alright next up we are going to use this imshow function let me just show you what I'm going to do here I'll just type in here cv 2 dot I am sure let it be capture again and I'm going to show you the grayscale image and then finally I want a new frame after every one milliseconds for that I'll type in her cv 2 dot wait for wait key and after every 1 millisecond and over here I'm just going to use a conditional statement which says that if key is equal to equal to R or D Q and then of colon break the loop right and now I'm going to a which is basically the number of frames now video taught release and then finally CV to dot destroy all windows right so I hope you guys have understood the code I'll just quickly take you through it input couple of Modi of CV to and time then we are creating a video capture object we are defining a variable a equals to 1 and while the condition is true that is my pison is able to read the video capture object we are going to create two variables second frame with check is a bool or type variable frame is nothing but an N dimensional array which will read the video right after that we are printing it printing the n dimensional object converting it into a grayscale image we are showing it we are also mentioning that after every one millisecond the new frame should appear and then once I use a press Q then the window will disappear and the control will come out of the loop right and then we are printing this number of frames video dot release we know we know what is destroy all windows so let us see what will happen once we execute this and here we go so there is an error so all I have to do is and define this variable key and here we go so let me go ahead and execute this so up you can see the frame that is behind me and you can see my hand as well so let me just quickly press Q and you'll see that window will disappear all right so I hope guys you have understood that how we can actually you know capture the video and how we can display it on the window and then things like that so I hope it was clear to you all guys so let me go back to my slides again and now we're going to talk about a motion detector right so this is our use case guys and I know all of you must be waiting for it right so let me just take you through the problem statement why we are executing this use case right everything exists for a reason so let us understand what are the problems that this motion detector will help us to solve so you have been approached by an organization that is studying human behavior now your task is to give them a camera that can detect any motion in front of it and it should return a graph which should contain for how long the object of the human was in front of the camera so this is how the graph should look like this should be the starting time right the moment object appearing from the camera for how long it's stayed in from the camera then it went off then again it appeared at this particular time then again it went off so this is how it should look time at which object appears in front of the camera and the time at which the object moves away from the camera so how we are going to execute it so this is the flow diagram for that guys on first of all we need to save the initial frame so what will happen since I have told you earlier is well video is nothing but fast moving images all right so what we will do the moment we switch on the camera the first frame the first image that will appear we will save it right then we'll convert that image to a Gaussian blur image you don't need to worry about it I'll explain you what is Gaussian blur image then we'll take the frames with the object and converted into Gaussian blur image so basically this is done to give us the accurate results right so Gaussian blur image is something like that you don't have to go into too much detail here to execute it but just understand it just to increase the efficiency we are converting our images to Gaussian blur so we are converting at first image also to Gaussian blur and the subsequent frames as well that will appear will also be converted to Gaussian blur image then what we'll do we'll calculate the difference the difference between the first frame and the frames that will appear after the first frame since the first frame is stored already we are going to subtract that with the frames that will appear after that right then what we need is now because of this window okay there's a change in the frame the initial image looks like this but now since there's some movement or some motion we can see that there's a difference in the subsequent frames right so we know that there is motion or there is some object but that object might be too small that we don't want to capture that for that what we do we define a threshold that will remove the shadows and then two small objects or other noises so basically we will define a threshold or the size of the pixel that should appear as and it will be considered as an object right I don't want a flying house flight to be considered as an object they're just an example now define the borders of the object right so we need to give a border or a box there right basically a rectangular box around the object and then we are going to calculate the time an object appears and exits the frame I hope you guys are clear again guys I'm repeating the logic what happens you first save the initial image in a frame what happens the moment you switch on the camera the first image that will appear that will be saved that will be converted to a Gaussian blur image similarly whatever frames that appear after that initial image will also be converted to Gaussian blur image now the difference between the first frame and the subsequent frames will be calculated now that difference will tell us whether there is an object in front of the camera or not because if there's a change from first frame then there is definitely some object but that object might be too small and I might not want it for that we will define a threshold all right so this should be the size of the object in order to consider it as an object or any motion in front of the camera then we are going to define the borders around that object we'll add a rectangular box and then finally we are going to calculate the time when object appears and exits the frame right we'll save that in a data frame and after that we are going to visualize the data frame that's it we are going to create a Farnaz data frame for that now the question is how I'm going to execute it right so guys this is pretty much from what we did last time as well so what we have here is we are going to create a video capture object then we are converting the frame color to grayscale then we are converting the grayscale image to gaussian blur image and this if statement will basically store my first frame or my first image the moment I switch on the camera whatever image comes first it will be stored in this particular first frame variable right so the statement says that if first frame is none when there is nothing in the first frame it will enter the loop the first image that will appear will be stored in the first frame and after that continue means it'll come out of the loop right let's see what we are going to do next after what we are going to do is we are going to calculate the difference since after all you earlier as well we are going to calculate the difference between a first frame and the subsequent frames which I have stored in gray right so this will calculate the difference after that I have defined a threshold now it provides a threshold value such that it will convert the difference value with less than 32 black if the difference is greater than 30 it will convert those pixels to white so basically whatever object is there and from the screen it will convert that to white right whatever the difference is so that's why I have given the color as well and this is basically the difference value that I've defined a threshold here similarly I've defined one more threshold right you can see it over here as well Thresh Delta that I'm using here after that what I'm doing is I'm defining the contours or you can say the borders so basically the borders around that object which appears now I am adding one more threshold guys this will remove noises and shadows or even a housefly which I don't want to detect right so basically it will keep only that part white which has area greater than thousand pixels so if my object has area grid smaller than thousand pixels it won't be detected because I don't want to detect it right so you can change it the way you want you can keep it 5,000 you can keep a 10,000 you can keep it 200 that totally depends on you right and this is basically to create a rectangular box around the object in the frame so I hope you guys are clear with it and finally we are just displaying all the things and yeah you can see it over here as well and you know what it is basically frame will change after every 1 millisecond this will basically close the window and this will close all the windows now what we need to do is we need to calculate the time for which the object was in front of the camera for that we will create a panda's data frame right so pandas or data frame is what we are going to use here there it will have 2 columns start and end basically when object appears in front of the camera and when it went off and we are going to tweak a couple of things we are going to define this variable called status status at the beginning of the recording is zero and as the object is not visible right so if there is no movement then it will be 0 because there is no object that is there in front of the screen right but it will change the status when the object is being detected the status will become one right similarly we were going to append the status list the status for every frame for every frame we are going to append it this basically tells us deal second-last status list value alright these two condition statement will basically record a time in a list when change occurs let me tell you how so if my status under scold lists the last value of this list is actually equal to equal to one which means there is a object but my second last valued says that there was no object before in the previous frame that means there is an object which has appeared right so that basically the time ant which the object has appeared will be stored here again which will be stored here after that if you notice the second conditional statement which says that my last frame does not have anything I mean there's no object in front of it in from the camera but in my second last frame there was an object that means the object went off right so it will note that time as well when the object was not there in front of the screen that simple guys so this will basically create a panda's data frame that will have two columns start and end which will tell us when the object appeared in front of the camera and when the object went off from the camera right so this is how it is and then what we are going to do is we are going to create or use this for loop in which we are storing the time values in a data frame right I think you guys are aware of pandas if you aren't aware of pardners I'll leave a link in the description box below which is nothing but a detailed tutorial about Furnas and data analysis using it so go through it if you are not aware of pondus it's very important library a thing every Python should know about it and then we are going to store it in a time dot CSV file so it'd be a CSV file it's a time I and time is equals to I plus 1 ignore index so there basically we don't have any index values here right we don't want to display the index so there is no index value where this for loop is pretty self-explanatory so let me just go ahead and execute this first and then I'll tell you how we have visualized it so here is our motion detector guys I've explained to you the code in detail and if you want the code you can go ahead and you know mention your email id and we will reply you back we will basically provide you with the code so if you're looking to execute it on your own go ahead and mention that right so what I'll do is I'll first you know won't come in front of the camera I will go away because it has to first capture the first frame then only it can identify the difference between the subsequent frames right so guys I'll execute it now and what will happen it will first capture the first image which is there in front of the camera and any movement that will happen it capture that right the first frame will be stored then the difference between the first frame and the subsequent frame will be calculated in order to determine if there is a object in front of the camera or not then we go ahead and execute this so guys you see a plain background there's nothing right so this will be saved the first frame and then let us see what will happen if I bring in my hand so it kate is there's a rectangular box around my hand you can see right so it is basically capturing the motion around the camera and all of these things are saved in my pandas dataframe right so you can notice my hand is coming in and then it is going off so all the time at which the hand appears in front of the camera and the time at which it went off will be stored in my pandas dataframe because I have two columns start in it right so if I just minimize this let me show you the Gaussian blur image as well so yeah in that Gaussian blur image I have defined that you know to keep the object white if it is greater than thousand pixels right so this is what comes up right yes this is Gaussian blur image once this is Gaussian blur yeah and this is a gray frame which you can see over here let me press Q it will close all the windows now let me show you this time search CSV file so yep this is how it looks now let me go back to my slides again now basically what we are doing is we are going to plot it on the browser using the bouquet library now guys do explain bouquet it'll take a lot of time right I've just written the code here to show you how the graph will look like I'll be coming up with a separate tutorial and okay if you guys want so if you really want a tutorial on bouquets which is nothing but a library that helps us to visualize the data on the browser you can go ahead and mention that in the comment section that we want a tutorial on bouquet as well right for now I'll just go and open my PI chama I'll execute the code there so this is my code that'll help us to visualize the graph right so it is basically using the motion detector which I have here right from this file it will take DF the data frame which we have created so let me just go ahead and run this right so again I'm just gonna put behind couple of times and gonna put my other hand as well yes so yes I think it's a good enough size of the data frame and it should appear in the browser the moment I press Q you'll see that there'll be a graph that will appear on our screen and here we go guys so this is the graph that I was talking about and time start time so let me just increase the size for you guys so yeah this is how the graph will look like start time end time so we have everything here so guys so I hope you have understood this and it was an interesting session for you and mention what you feel about this particular tutorial in the comment section along with whatever doubts you have and if you're looking for the code the entire code you can mention your email id as well and if you want a separate tutorial and okay let us know in the comment section so guys that's it for today's session I hope you have enjoyed it thank you and have a great day I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 496,251
Rating: 4.917871 out of 5
Keywords: yt:cc=on, opencv python tutorial, opencv tutorial, opencv python tutorial for beginners, opencv, opencv install, opencv python 3, opencv python windows, opencv python projects, face recognition, face recognition using python, face recognition using opencv, motion detector using python, motion detector, opencv face recognition, opencv tutorial for image processing, python tutorial, python training edureka, python tutorial for beginners, python programming, edureka, python edureka
Id: -ZrDjwXZGxI
Channel Id: undefined
Length: 40min 29sec (2429 seconds)
Published: Fri Sep 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.