Don't Buy Security Camera! Build Your Own || Computer Vision || Open cv tutorial || python project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
don't buy security camera or rather use computer vision to build your own security camera with just 30 lines of python and this is super simple super fun and super crazy so let's get started and before getting into the detail please please like this video and subscribe to this channel otherwise grandma will detect when you are scrolling through your crush's profile and she will turn off the wi-fi and to get started with this fun python project you will definitely need to have python installed in your computer and we will use a fun python code editor called pi chart once the pycharm is ready all you have to do is to click on this new project and give some name to your project and here we will give it a name called security camera and then press enter once the pycharm is getting ready think about the core features of a security camera a security camera definitely needs to have three features the first thing it needs to read some kind of vision through camera or computer vision number two it has to detect some unusual movement if something is moving if a burglar is coming a tip is coming it needs to detect it and number three whenever it detects some movement it needs to warn or alert the owner of the security camera and if you can understand these three core features it would be super easy super fun for you to build your own security camera by using computer vision and to get started we will definitely go to the pycharm press ctrl a and backspace so that we have a clean slate and we will start with a cool python package and to get connected with this cool python package we'll go back to our google pro and ask for python opencv and opencv is a computer vision detecting package which is very popular and if you want to play with computer vision this is your starting point so my dear friend i will click on this guy and then you will see a tiny button to poke to this button it will copy the text here and this text has your pip uncle here so we'll come back to our pycharm click on this terminal and then we will press ctrl v to paste this pip and if you press enter pip uncle will go to opencv get all the cool things of opencv to your project so that you can utilize opencv for your project once the opencv is installed all you need just plain simple seven lines of python to create your computer vision that means it will open up webcam in your computer and you should be able to see whatever the webcam you have by using just seven lines of python so that's the fun begins for you and the first thing you will definitely whenever you install some package you need to import it so we will import cv2 and after that by using cv2 we will read the camera and we will declare a variable called cam and on the right side we will call cv2 hey cv2 you need to do some work and your work is to capture some video and inside the parenthesis you need to provide the index or the number of the camera that the cb2 has to read and in your case most of the time you will pass zero and it will work unless you have multiple webcam in my case i was playing around with multiple webcam that's why i place number one but i recommend you to go with number zero and if this doesn't work in that case you will change it to number one and think about when you are reading a camera you need to definitely turn its on right if it is turned off you'll not be able to use the camera at all and python works exactly the same way and that's why in the next line you will write a while loop and you will say hey whenever camera is open then i want to do some cool stuff if camera is not open then i cannot do anything so inside the loop you need to read the camera so that you can read whatever is happening on the camera and that's why you will retry it and you'll declare a variable you actually declare two variable here one is uh ret which is retribe another one is the frame of the camera and on the right side is just camera dot rig that's it and after that you want to show that in your computer and for that purpose you will go back to your bro cv2 and say i want to show something that means i am show and here you will give the name of the pop-up that will be coming in your computer and this is kind of your security camera or your grandma's camera whatever your call so you can call it's like a granny scam or something like this or security cam whatever or your own cam whatever you want and then you will pass the frame this is the video frame next thing you need to understand that whenever you are creating some kind of window some kind of pop-up you need some way to close this or kick that out and to kick that out you need to do two things the first thing you need to know that the user has pressed on the right key because there could be so many key on your keyboard but whenever the user presses on the queue key and that moment you want to turn off or shut down or destroy the window and for that purpose you will use a conditional and you will call cv2 and weight key that means you are telling cv to hey you need to wait 10 millisecond and then see whether the user presses some key on the keyboard and to know the key you can call ord and then inside the parenthesis put q this is getting the unicode of the key queue and if these two matches that means if user presses on the queue it will destroy the window that means you will be break out of this loop that's it my dear friend you just rode seven lines of python and by using these seven lines of python you'd be able to open up the webcam and then see yourself on your computer and that's super fun that's the magic of seven lines of python so we will just press on this play button and see the magic so click on this and then see and you will see myself in this camera that means you are able to see yourself turn on the camera that means you are getting started with computer vision and if you see you press like any other key other than q that means you press enter w e r or l o v e doesn't matter as long as you press on the q key these things get destroyed and that's what this line is doing so you are done with the first step of reading the computer vision your next step is to identify something is moving on the screen or not to detect the motion all you have to do is to write few lines of code and understand what is a motion means for example if my hand is static that means it's not moving and whenever it's moving that means in comparison to its previous position it has moved some spaces like some pixel or some inches or some kilometer or some miles it just move so if you compare this to hand if this is the static position of my hand and this is another hand if this hand stays the same and these hands moves and that way we will be knowing or understanding or will be able to capture that this hand is moving and if you can understand this parallel position the difference between the previous position and the current position you would be able to detect movement of any object in your computer vision so previously in our code we just have one frame right now since we want to detect the movement what we will do we will create two frame which is frame one and frame two and we will compare these two if these two is identical in that case we'll know there is no movement and if these two has some variation in that case we'll know there is some difference so let's play with it with just three four lines of code that's it so here you have one line of code in the line number four and this is where you are detecting the camera read so in pycharm if you press ctrl d d means like a dog david donald whatever you just press d it will copy the same line in the next line so if i just ctrl d you'll see this line is duplicated and this is your frame one and this is frame two so we'll rename this part of the variable as frame two and this one is frame one so what we have frame one and frame two and the next thing we will do will create a difference between these two frame that's it so we'll declare a variable called diff and this date would be cv2 and absolute difference between two frame the first one this is the source one is frame one and the second one is the source is the frame two so what we are doing we are capturing two instance of the camera and then comparing these two and all you can do here instead of frame you can just write d and then see how does it works so let's click on this button and see we will running this and you'll see in the camera if i don't move it's completely dark but if i start moving you see you can see me that means if there is a difference between my previous position and the current position you don't see anything but if you start talking talking talking and jumping jumping jumping you can see me and right now you're only seeing the difference if there is a difference you are able to see it if there is no difference you cannot see it and if you understand this part the rest part would be super easy for you so we will turn off this camera uh and then we will move little bit so here when we are reading the difference you notice the difference is very bright and colorful reading colorful images for comparison is not recommended because you might have some kind of error over there and that's why we will create another variable called gray and there we will convert this difference to to gray color and if we do so we will call cv2 and then convert color and we will provide the difference this is the current image that we want to convert and then we will call that the conversion color would be color and then you will do this rgb2 gray color conversion and if you display that gray whatever you have here and if you turn on this camera you will not see all these colorful things only thing you will see is the gray color so let's run this and then see whether it turns different or not and right now i'm talking very little you see a very small amount of me there and if you start the jumping right now you don't see like a different multiple color red green blue or something all you see is two gray color that means black and white so that's the way you can turn your colorful life to a black and white life if you don't like your colorful life for any way so i will close this and we will keep continuing few more step the next step is to convert this grade to a blur image and to do so you have to call the cv2 and there are different blood technique the one we'll be using is the gaussian blur and for converting to the gaussian blood you definitely need to pass that gray and then you need to provide the kernel size and here this would be five and five and after that you need to provide the sigma x and whatever it is just press zero and if you want to see the plot you would be able to see the blur here now if you click on the run button you would be able to see a little bit blurred version of the difference the gray difference that you are seeing before so we'll continue to the next step is to create some kind of threshold and threshold is a very useful thing because sometimes you need to remove or get rid of the noise and to get rid of the noise you need to call the cv 2 and then you will call the threshold and there again you will pass the blur and after that you will pass the threshold values and the maximum threshold values and some threshold type and that would be cb2 threshold and you can just pass binary so if you just display the threshold in this case you'd be able to see little bit sharper things and that's the way if you run this the difference you'll see these are little bit sharper and then brighter the next thing you will do is dilation so dilation is just opposite of threshold in threshold you were getting rid of noises or unwanted things and after getting rid of the unwanted things you are remaining with the actual things that you are interested about and once you have the interested things you want to make interested things little bit bigger and to do this you will do a dilation and for this purpose i will declare a variable called dilated and this dilated the value would be cv2 and then i will call dilate and here i will pass definitely the threshold and after that you need to provide some kernel or you just pass none and then you need to provide how many iterations you want to do this dilation and i just can pass three that means i want to dilate for three times the next thing you need to declare is a variable called contours so contours are nothing but is the boundary or the border of the items that you have detected they are moving so that you can visually see what are the things are moving and what are the things are static and for that purpose you will have two variables like contours the second one we don't care that's why we named this just underscore and on the right side you'd have cv two and then find contours that means find every contours available on the screen and there you will pass the dilated and then you will declare what type of mode you want to detect and there are multiple different ways but you can do like uh this the first one it came and the second is the method which would be your cv2 and then change approximately simple and once you have done this you are finding the control and the second thing you need to draw the contours so that you would be able to see the contours on the screen and that would be the fun thing here and all we have to do is to call the cv to and draw contours and you will pass the image or that you want to draw the control and this would be on top of your frame one and then the contours you already have this variable called contours and then the index if you want to draw everything just press minus one and after that the color and if you want green color just press 0 255 0 and if you want this the thickness of the contours to be 2 and just press 2 and the last thing you need to do is to instead of drawing the threshold you need to draw the frame one and by this way you are done with your step number two that means to detect something moving on your screen so i would be going here and then click on this run button and then i will see if anything moving on the screen i would be seeing a border of that item and to get started i will just sit as silently as possible so that i'm not moving and the camera cannot detect that i am moving so that's a challenge let's see how i am doing with this challenge though i was able to stay static but some my heart beat was burning here and you saw some contour there the good point is that if my mouth is moving computer vision can detect it that my mouth is moving my eyes is blinking my hand is jumping and rotating all this way but if you notice that not every moment i should be detecting maybe i should be detecting only the bigger movement if a person is moving if a burglar is moving in that case i should detect that and i should ignore rest of the things and we would be doing those detection of the right things in our next part so to detect the right part we will need to go through all the contours all the borders that you are seeing there and then identify those are bigger and get rid of those are smaller so i will comment this line and just press ctrl and then slash and this will be commented out and after this here you can run a for loop so for c in contours or whatever you have here and inside the for loop what you have to do you have to run a condition and that would be cv2 and then contour area and the pass the c and you can check if this is smaller than some particular number maybe 5000 or something like this and you want to do continue that means you want to skip this other than you want to show rest of the stuff so that's how you'd be ignoring the smaller contours or smaller things and you'd be only care about big things for some special big things and to do so you can do some other stuff here for example for every other contours which is not smaller than this you want to get their x y and width and height and all these things so in this reason you will declare four variable x y and then width and height and this would be cv2 dot bounding rectangle in pass the contour and this way for every single contour you will know what is the x axis position what is the y axis position and what is the width and what is the height and think about uh this object or some special object like this and if you have something like this and this is your x and y that means the contour would be from this and it will go all the way to here which is x plus the width and the another corner would be this is your y and that means another corner would be y plus height that's it and if you understand these two corner of a rectangle you should be able to draw a fancy rectangle here and i'm going to write cv2 and then rectangle and here i will pass where i want to draw this rectangle and that would be on frame one and after that i need to provide the first position which would be x and y this is the first corner and then second point would be the second corner and this x position would be x plus weight that means the x position and the width and this is like from here this is the x y and this point is x plus width and y plus height so i will write x plus width and y plus height and after that you need to define the color and that color would be your green color 255 and then 0 and if you want to define the thickness s2 and that's all you need to do and by doing this you'd be able to only detecting the bigger things bigger contours and bigger things that are moving so let's click on this run button and see how does it goes so here i'm detecting this right now my mouth is moving but i don't see any control but if i move my hand this contour is bigger and i see the bigger circles but if i move like this finger it doesn't move anything because this is smaller enough but if i do this in that case i can detect and this way you would be able to detect moving of a larger object maybe a human being and you would be able to ignore moving of smaller things if a cat is moving you are not interested to run an alarm for a cat moving or a small dog is moving or the tree is shaking you want to ignore those smaller things but you want to detect a human being a thief or a burglar is moving so we are close to this part and we will get into the third fun part of this project the final fun part is to make some noise and that noise would be a fun thing because whenever you're detecting if something is moving you need to make some noise to grab the attention or you have to notice that hey i need to pay attention a burglar is coming or something important is happening so i need to take preventive actions and that's the smartness of the security camera that you would be building and this is super easy all you have to do is to import a built-in module that means you don't need to install you need to call pip uncle and you just have to import wind sound and by using this wind sound you can make a noise and you have to put it in the right place and the right place is right somewhere here here if you pay attention that here you have multiple contours but you are ignoring those which is smaller that means you are not paying attention like your ex-girlfriend or ex-boyfriend they are very old you are not paying attention and you are paying attention to the big big things that means if the contour area is larger that means you are drawing the border and that case you need to make some noise because something vigorously is moving so we'll call this wind sound and then call the beep method and the beep method takes two input the first one is the frequency of the beep how louder would be the b so for example we put 500 the second is how there would be the beep and in our case we'll just put 200 milliseconds so right now i'll be taking a chance that means i will try to be as static as possible so that i don't hear a beep and then i will try to start talking and that talking would be maybe smaller i will not see any control that means i would not be hearing any beep sound but if i try to move my hand or do something i should be able to hear the beep and that way our security camera will become a smarter burglar detection so let's take the challenge be as quiet as possible so stay quiet and don't move at all so i'm clicking on this run button and let's see oh i did little bit movement in the beginning but i'm talking this is small talking so it's not happening but i'm moving my head so if it is a small movement then there is no beep that like my mouth is moving or my fingers are moving but if my whole hand is moving it's a bigger movement and my smart camera smart computer vision can detect it and we want to take it to the next level that means the beep sound could be even better and for that i will go to this project button and right click on this main file and then open in explorer and this is where is the project so i have an special alert sound and i will paste it here a lot dot web this is a web file this is an audio file similar to mp3 and if you want the exact same file i will put a link in the description so you can go to the github to download this sound and before downloading please please like the star on github so since i have this alert web here i would be able to see it here as well and i would be using here instead of the beep sound because beep is very boring and tiny i want something louder and bigger and something realistic as well so i would be calling the place sound of wind sound and it takes two parameter and the first one would be the sound file which is allot.wab and the second one is important and this is called sound ascent that means you are playing the sound asynchronously a synchronously means something is happening on the side you are not waiting for it think about a synchronous thing is like your mom or your grandma let's say you are telling your grandma you are hungry or your mom you are hungry and you are still playing your video games on your phone and while you are keep doing your stuff your mom is cooking stuff getting ready your food and then bringing on the table and then whenever the food is ready you are eating and you are not waiting for that task has to be done it's happening on the side it's happening in parallel something like this so right now we will have this alert sound and our detection will be continuing on parallel and while the sound is playing that means the sound will not be blocking the operation of the security camera because if it detects and it stops detecting the burglar might do some other crazy stuff so i'm going to detect the burglar and the sound would be happening on the side in parallel so let's do another challenge last time i wasn't able to stop myself this time i will be very static so that there is no sound in the beginning and then i will move my hand and i would be able to see some louder and realistic noise of alerting that some burglar is intruding in your house so let's take this challenge and i'm clicking on this run button i'm talking but it's not detecting so i'm going to move my head and then see whether i can hear this loud sound so this is a very alerting sound and whenever burglar is trying to come to your house by making this noise from your smart security camera not only you will wake up rather your neighbors and everybody around your area will wake up and will be able to capture the burglar so that's all from this video and you will notice that this is super simple with just 20 lines of code you are able to use computer vision you are able to detect some movement and not only that you are able to create some alert sound so that you can detect the burglar and if you watch this video until this point please please and like this video and subscribe to this channel and also you have to do one extra thing if you want to explore python in a fun way please please go to the play store and find this awesome app called programming hero and if you're a rich dude you can definitely go to the app store and find this fantastic app called programming hero and if you still see a red subscribe button underneath this video please please punch that red button to make it a gray color and by doing this your grandma and i will see you in the next video you
Info
Channel: Programming Hero
Views: 305,100
Rating: undefined out of 5
Keywords: Programming, python, web development, learn programming, programming tutorial, coding, software, python tutorial, python projects, python for beginners, learn python, python tips, python tricks
Id: UOIKXp82iEw
Channel Id: undefined
Length: 28min 33sec (1713 seconds)
Published: Sun Jan 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.