Raspberry PI : 06 : Install Open CV for Facial Recognition

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so we're going to take a look at setting up the camera for uh and getting facial recognition running on our raspberry pi so let's go ahead and get started um i'm going to be looking at the raspberry pi over team viewer mainly for the purpose of the video so it's hard to um run obs on the raspberry pi while you're doing other things teamviewer also also uh takes up a lot of memory so it would be much better to have the raspberry pi like attached directly to a monitor with a keyboard and mouse plugged in but uh it's going to run on teamviewer right now so maybe give it a shot if you want okay so uh here we are we're we connect the raspberry pi uh however your preferred method is um let me go ahead and remove this it was me running a test of it okay so the first thing we're going to do is that we're going to go ahead and download the github repository that we're using for this project and it's going to kind of get us going okay so here we are in um in the browser on raspberry pi uh and we've gone to this pie face github repository now what we're going to do here is that we're going to go ahead and download the code so we'll click on this green button and download zip then we're going to click on the up arrow and go show in folder this is going to take us to our downloads area and i'm just gonna go ahead and re-download it so i don't get a copy more prepared here um all right so we're gonna go ahead and download it again oops download and once it downloads which should be pretty quick we're going to right click and go extract here and then we're going to move the repository out onto the desktop it's not really a repository at this point it's just a folder with the files in it okay and then on the front page of the repository the readme has a bunch of the instructions on it so uh so let's go ahead and take a look at those instructions so the first thing that we're going to do here is we're just going to kind of follow these down so we've physically connected the camera to the raspberry pi already um we're going to go ahead and uh make sure the razer pi is turned on or the camera is you know set up to be read into the raspberry pi so we're going to go to raspberry menu preferences raspberry pi configuration and then we're going to go to interfaces and we're going to make sure that camera says enabled uh if you switch it from disabled to enabled you want to go ahead and reboot okay which we don't need to do here the next thing that we're going to do is that we're going to change the size of the swap file so the swap file uh the swap file is basically if your computer doesn't have enough ram if it runs out of ram the operating system will start to use the hard drive as ram okay so that's what a swap file is now as computers have gotten more and more ram they use swap files less and less but because we're going to be using a large amount of ram to install some of the software we need to go ahead and increase the size of the swap file on the raspberry pi so we're going to go ahead and open up the terminal these code files you should be able to just copy and paste these so we're going to go ahead and select that ctrl c to copy okay so in the browser we're going to control c to copy in the uh in the terminal control v to paste so what we're doing here is we're saying subdue it means run as the super user so like super user do okay so run is the super user now now you have to be logged in as a person with subdue privileges and i'm probably saying it incorrectly so i say it's to do but say it how you want so uh okay so uh so basically it says run as a super user because you need administrative privileges to edit the file we're gonna edit nano is just a text editor in the terminal and then this is the path of the browser so d p h y s swap file that's the name of the file that we're editing so we're going to go ahead and hit enter and nano starts up so nano is a is a program that just runs in the browser notice that i've already changed it so we would go ahead and arrow down it's a pretty small file so the swap size basically i already changed it 2048 on on this raspberry pi [Music] sorry about that it's originally 100 uh so uh yeah changes 2048. all right the next thing we're going to do so once we do that let's say uh you're going to go ahead and control x so notice these fi these this is how you control nano down here so this control x let me just go ahead and change something just so nano lets me save it correctly uh so basically we're going to control x to exit it'll ask hey do you want to save the modified buffer that means do you want to write the changes to disk and we do so we do shift y and then it says the file name to write it we just leave it as the default and hit enter okay so now we've run that we've changed the swap file this definition file for the size of our swap file now we have to do is we're going to use the system control utility so highlight control c click into the uh terminal control v we're going to use this the system control youtube utility this is kind of in the most recent versions of ubuntu based systems this is used all over the place so just kind of get used to it so we're going to sudo that means to run it as the super user the program we're running is system ctl like system control and then we give commands to that so then we're going to say restart and then we give the name of the thing we want to restart like this will be a service that you want to restart um okay so that's what we're gonna do hit enter it restarts take a second okay in the meantime let's go ahead and move on so so basically we've already downloaded and unzipped the repository we moved the unzipped file to the to raspberry pi desktop and now we're going to go ahead and we're going to install uh we're going to install a bunch of stuff and kind of get the computer ready to run the the facial recognition so let's go ahead and do that we're going to open pi face master it's kind of surprising that didn't run more quickly there uh by the way if you're running if you're using team viewer if you go to view change it from auto to select to optimize speed and you're going to have a much better time with it okay so we're inside of the repository and first thing we're going to do is go to this util file and take a look at the files here so we have this um whoops so hard to read the names on those okay so we basically have two files we have this dot sh file this rpi underscore opencv install.sh and then simplecamtest.pi okay so those are the two files in there and let's go ahead and take a look at this opencv install it's not really an install it's more like a script to install a bunch of stuff so it literally is a shell script so that dot sh is a shell script and all it is is we could type we could copy and paste all of these commands so basically you could go in here this is a this uh this um and then this line is a comment the rest of them though you could just you could just copy these one by one and paste them into the terminal okay but instead of doing that basically i've just collected a bunch of lines of of shell commands and put them into one file called sh file sorry it's a text file with an sh extension and basically what we're going to do then is we're going to execute this inside the terminal okay now now in order to do that in order to do that we need to go ahead and make this file executable okay which i have not done in the in the repository okay so what we need to do here is that uh in order to open up a terminal if you notice here we have this our terminal right now is is in the um is in the folder that's the home folder for the user so we're at pi at raspberry pi so that's our username and then the computer name so pi is the username raspberry pi is a computer name colon and then the path okay so our path right now if we were to type ls for list and enter basically we would get all the files that are in our home folder so we take a look here uh basically if we go to uh this is the root of the drive we're on when we go home pi if i were to click on this basically this is the exact same thing now i hit control h to show hidden files but i can undo that so basically this is what we see here if you look at my ls listing and then you look it down at this back or forward slash home forward slash pi this tilde right here is just shorthand for the home folder of the user we're logged into all right now if i go into desktop you'll see where we're now looking at the files on the desktop if i open that up now we're back to where we were before this util now either i can either copy this i can copy this path control c and i can go cd space control v and then paste i can change this the directory to here to this folder by doing that i can also uh in some um this program that's running here is just a directory browsing program but what i can do in this in the one that's installed in raspberry pi is i can right click on a folder and go to open in terminal and what that does for us is it opens a terminal at the location that we just selected so when we right clicked on util notice that now i'm at the home folder for my user desktop pi face dash master and then util so i'm inside of this folder so i'm right here all right so that's what we need so we need to basically we need to have a you have a uh have a terminal open inside of this folder so the next thing we're going to do is that we're going to set this uh sh file to be executable okay so so basically the easiest thing to do i've noticed that i don't know if it's a glitch inside of that i found that this file was being reported as executable but then was giving me permission problems so we're just going to do it in the terminal here so you're going to copy this so do chi mod um chamod i don't know how to say any of this because i've yeah i don't discuss it much so uh sudo means as a super user chimad means to uh change or modify the uh permissions plus x means to make something executable and then this is the file that we're chi modding so we're we're chi modding or ch modding i really don't know how to say it uh uh this file to be executable so go ahead and hit enter on that and now it's executable okay so now when we now when we execute the file it will run what are we executing both so before you run something like this you definitely don't want to know what you're doing so let's go ahead and open it now notice when i double click it now it gives me the option so now it's executable do i execute execute internal open or cancel we just want to see it in a text editor so we're going to click open and here we have it so so basically the first the top portion of this is all notice they all start with sudo apt right that is an error right there oh i'm glad i looked at it because i accidentally typed and that suddenly app would have given us a problem there okay so notice that all the lines all the way down to here start with subdue space apt so that's remember that's our saying hey run this as a super user apt is the program that installs software on ubuntu or debian based systems with ubuntu being the most common and i believe raspberry pi is a derivative of ubuntu or debian either way either way um we use app to install things okay and then and then basically that automatically gets software from uh the repositories for the correct uh version of our os okay so basically the first thing we're going to do is we're going to remove a couple pieces of software you're going to get rid of libreoffice and wool from emerge this is just to try to like free up some space on your raspberry pi then we're going to clean and auto remove so that basically just cleans up the installed software then we're going to update this is a common so do apt update and so do have to upgrade means that we're going to go get we're going to go say hey app to go get us all the updated versions of all the software we have installed and then we're saying say hey app update everything that should be upgraded okay so that gets us up to the point where everything should be current like we cleaned it up and it's current then we're gonna install a bunch of stuff okay so these are all things that we need to make uh make our uh facial recognition run we're going to be using the opencv we're going to be using opencv so with opencv um we need to just one second here um with opencv what we're going to need to do is that opencv has to be actually compiled on the raspberry pi um so and the version you're going to download is actually going to be in a different order so basically between here and here between here and here uh basically uh you are going to actually you know what let's so uh this is what it's going to look like when you download it so okay so this area right here is basically us installing opencv and compiling it on the computer this is going to take like two hours to run okay so this first part is going to take like 10 minutes this next part may take like two hours i suggest you leave it like overnight if your if your raspberry pi freezes don't unplug it just let it run for a minimum of two hours okay um yeah if it freezes up and you don't know if it's done or not let it just let it keep running for at least two hours okay uh then this last part i wanted to do this without a virtual environment i couldn't do it so uh mainly because the fact that's opencv dash contrib dash python that's like what we need to run opencv inside our python code that has to apparently that has to be set up inside of a virtual environment it gave me errors when i tried to not do that so all a virtual environment is is it's just a folder so so basically when we when we create a virtual environment it just means that we're not going to use the python uh installation for the computer we're going to get a whole python installation inside of a folder this folder the the the folder that we're going to install to is this home it's going to be on our desktop called pi face virtual environment okay but that means that whenever we whenever we want to run anything as far as our facial recognition programs go we have to type this into the we have to you can copy and paste it but we have to put this into the browser first to activate the virtual directory or sorry the virtual environment it's just something you get used to working in python for new users a little bit complicated all it is it's a folder that's basically what it is people say virtual environment and sounds really complicated it's a folder okay so basically you just put a python installation inside of a folder you activate it inside of your uh inside of your terminal with this source there's a few different ways to activate it this is my favorite way so source and then you just basically point it to whatever the virtual environment path is in this case that's the path right there and then bin activate and then once you do that everything that you install so then this next part is hey now that we've activated the virtual environment we're going to install stuff pip is the way that you install things into a python installation in this case a virtual environment we're gonna install numpy pillow then we're gonna upgrade pip wheels and setup tools and then we're gonna finally install this opencv contrib python um one thing that that has been written on the github for opencv is that basically you don't want to install multiple versions of this opencv so you don't install both opencv python and opencv contrib-python just install one or the other okay um i will work out whether this has to be done first or second i'd rather do it second i'd rather have the virtual environment all the way done before you try to install this but it's safer to have it in opencv in before you install it this opencv dash can trip so so once again this part is going to take two hours okay so if you want to delete it first and then run it yeah anyway just run the whole thing like i'm going to show you so okay all right so let's go ahead and do that so for me i've already installed a open cv on this machine if i ran it right now it would just take a really long time okay if it runs and it breaks okay so if you run this and it freezes your computer you let it sit two hours maybe overnight when you come back just delete this portion out of this uh open this install.sh rpi underscore opencv underscore install.sh and then run it and then run it again without this portion okay so if you if the opencv installs you can just run the whole thing again and and basically we'll get you that virtual environment so there you go all right so i'm going to go ahead and remove this so that we can go ahead and run it and remember i've run this before so it's going to be like a lot quicker on mine okay so there is our install script so let's go ahead and run it so the next thing we're going to do um that this dot forward slash this is kind of a throwback so basically the period means to run a a uh the period means to run something but on on debian and on ubuntu and on previous versions you couldn't run any file without first giving it a uh um a directory so basically this means yeah you're basically giving it a directory with the period forward slash so okay so that's how you run things period forward slash and then the name of the executable file so let's go ahead and run it okay we type it in hit enter and basically what you're going to see this uh do on the on on this raspberry pi is this has already been run so all of the all the installations are already done uh which is uh which makes it just go a lot faster so um because basically it's just gonna run through and not need to do much at the beginning here so okay so now we're up to this apt upgrade okay so let's just keep track of kind of where we're at here so so basically remember that i took out the opencv install a chunk of code so basically we are to uh we just went to did auto remove now it's doing the apt update okay then it did app upgrade now we're on to this uh the pip three okay so it says hey virtual invite virtual env is already installed okay that's great uh okay so now we're in this section right here basically it's creating the virtual environment notice it's a python 3 virtual environment and we're creating it on the desktop you can see that the folder has already been created on our desktop right here now it's installing numpy and pillow so we're right we're down to right here okay so it's installed numpy and pillow and this right into our virtual environment it's double checking pip wheel and setup tools may not be necessary to be honest with you okay so now it's going to go ahead and start to uh to install the actual opencv which is what the software we're going to use for the facial recognition basically have to have to download again okay so at this point we have installed uh opencv um so basically uh this video i ran the install script um without actually compiling and installing uh opencv remember that you have to let it run for a really long time even if even if the raspberry pi becomes non-responsive just let it run for like so uh that happens and it's best not to have it on teamviewer or some other remote connection it's best just to have it up to it okay so let's go ahead and continue on here so uh the next thing we're going to do to ensure that we are have everything installed correctly we're going to go ahead and run this simple cam test so uh once again we're going to go ahead and open the terminal uh always forget that it's a single click and right very high not a double click all right uh so basically what we're going to do is we're going to cd so i'm going to control copy uh the path to our folder terminal i'm going to go cd and then control shift v to paste into the terminal enter now our path looks like we're in this uh and then we're gonna go ahead and uh so to run all the commands uh in um in here remember that we're using a virtual environment now there's a couple ways to do this okay the easiest way is just to tell uh it's just to tell it where the python is so basically if you if you type so if we type python and then the name of the script simplecam pi right name and copy okay shift v okay so if we were just to do this right now we wouldn't be using our virtual environment we would just be using the python installation on the uh on the computer and how you know which python you're going to be using if you just type in which python okay so notice that the python we're using is in user bin python okay so what we want to do is we want to start using our virtual environment and how we're going to do that uh if we go in let's take a look at our virtual environment so so we can do it a couple different ways if we want to use our virtual environment we can do a couple different ways okay the easiest way is to probably just go into our virtual environment knows i just opened the pi face virtual environment right here i went into the bin and basically i went into python and i can just right click on the python file like the x shortcut to the executable here uh and i can just right click into the um sorry i can just go to the terminal and ctrl shift v and i can just paste in the path to that python there we go okay so you can just specify which python and then then basically this python will use the environment closest to it so this python because it's in the virtual environment it will use the installed packages closest to it like relative to it so that's probably the easiest way to do it all right it's just basically to just go find the file specify which uh which python you're gonna you want to run it on um and this saves a lot of time and and in the case of all your all your python scripts you can just put the top of the file uh exactly which python you want all you want them to run on also but this is way just a point terminal okay so basically we say hey go use this python that's at this address right so let's go use this python executable that's where we installed everything into our virtual environment right and so that will work okay so here's our pi test oh look hey so uh okay so our pie test worked that old man in the ceiling all right so uh oh so so what's happening with this test i try to close the window so basically this will keep this keep these two windows open until you stop it running so how you're gonna stop it running is you're gonna control c is the shortcut in the terminal to stop something so basically it will just keep running it will if you even if you try to close the windows it will just open them right back up until you control c and stop running the program okay so the next thing we're going to do here is that uh so in that case we just went and found the python that we wanted to use it's inside our virtual environment we can just specify it like that we can say hey use this specific python installation on the computer it will use the closest set of packages that are installed in this case in our virtual environment it's very simple now if you don't want to do that every time what you can do is you can you you can use the source command so you can go source and basically watch so watch what's going to happen here so this source command and there are a few different ways to do this i think this is the most straightforward basically what we're going to do is we go into the bin folder or our virtual environment and it's this activate folder activate a file here so we're going to copy path and basically we're going to go source and then we have the the path to our virtual environ environment and then slash bin forward slash bin forward slash activate enter and notice what happens now is that now everything that we're typing in is relative to our virtual environment this is really confusing for people starting out though okay this idea of virtual environments it's just a folder with the python uh installation in it that's all it is and then and then basically there's a way to uh make it everything that you're running uh in in the terminal relative to that environment or or basically use that environment that you specified so the reason why this makes it a little bit more simple is can now we don't have to specify the python we've already specified it by going into this virtual environment so now if we uh so i just highlighted this python i control c i copied the return well that's why i just ran okay so now we do run the simple pi cam test again it works great uh inside of our virtual environment so so if you're going to run a lot of commands and you're you know or you have a small screen you know maybe it's better but virtual environments are needlessly complicated a lot of times so um but they're really super duper useful so so the reason why you need a virtual environment is that all these different projects have different um have different uh requirements as far as software in fact they'll have like you know one one project will need some certain version of a certain library another project one different version in this way it's not practical to have a single python installation on it on a computer that's running multiple projects you kind of need this virtual environment so once we're once we're in here we can deactivate the virtual environment just by closing the terminal and then opening a new one we can also just type in deactivate okay and that's a basic virtual environment it's a folder with python executable and and the packages that we installed on it um it's it's sometimes it's sometimes a lot easier just to source uh the virtual environment uh so that everything you're doing is inside the virtual environment including like pip installs and stuff okay so enough about virtual environments so we we've tested our uh so now with that simplified cam test if that runs we're all set all right so so basically if that if that runs we're ready to start doing our facial recognition so if we open up pi face master here and we go into face detection all of these scripts will run in the same way that i'm about to describe to you we are only going to go through so this basically this face detection smile detection all of these will run but we're just going to run through uh building the facial recognition okay so so we already have these open uh so let's start let's take a look at the first one here so we're going to run these in order so the in the first one we build a data set in the second one we build a classifier and then the third and third script we run that classifier to actually identify uh people in the video in a video feed all right so here we go so i'm going to go ahead and source activate i'm clicking up in my terminal to get like a history you can also search your history of control r just type in source right so i can search the history there i get my source i can hit enter so either way up does a history of commands and then control r lets you search that history very very very useful all right so now that we're in that virtual environment we can just type in python and that's python remember we saw was just a shortcut it shortcuts to in this case raspberry pi installs uh python 3.7 all right so now we're going to go ahead and run this script so we've got detection here python face detection ctrl shift d oops and i'm in the wrong folder okay so uh so remember uh i remember if we look here the mistake i just made is we're still in that util folder so let's go ahead and copy the path to this to the folder the facial recognition folder click in the terminal cd or shift v and change the path now we're in that facial recognition folder now we're going to up arrow because we can do the same command just from this folder now now we're running the command python and then that first script there okay enter am no integer i mistyped integer or am and i miss typed in uh user id and then press enter so in this case what's gonna happen is this gets turned into a uh a numpy array later on and you know in the second uh in the second script maybe the in this script yeah this script so basically the id has to be a number of some kind so just use an integer in fact it specifically converts it into an script so so use an integer in this case i'm going to use a 0 so 0 is my user id and by the way this this right here is just uh is just in the um in the script here so there's i will correct it github repository so uh okay so we're gonna go ahead and hit enter zero hit enter all right just placing phase capture look at the camera and wait okay so there's me i'm going to kind of get up close kind of turn a little bit kind of down it's capturing capturing capturing okay and when it's done it will just it will just finish all right so what has happened here is that the script created in this facial recognition folder created this data set so basically it took 30 pictures of me so if we open this if we go ahead and open this up notice that it kind of cropped to the face itself pretty odd photos because i was talking at the same time all right so uh yeah so so basically just took 30 photos saved into this data set notice that it's a simple it's a simple way of saving it so we saved it user period user id period and then and then the number right so user one would be user.1.1 right okay so so there we have it there's the data set um so if you want if you if you actually use this on a lot of different people you would not want to delete this dataset folder right sure so uh yeah just be careful all right so now we're going to go ahead and we're going to run the second step and now we're going to train the classifier so basically what we're going to run now is we're going to run this opencv uh we're using this uh lbph face recognizer create and then cascade classifier and then this is basically the definition of which classifier we're going to use um and then yeah there's there's a couple things we'll go through script uh it's pretty straightforward okay so next thing so we're gonna uh probably easier so python name of the script we're still in this facial recognition folder we're going to hit enter and now the now opencv cv is going to train so notice that now the trainer data uh the trainer folder is here and run it ran really quite quickly it's amazing that it cannot classifier that quick okay and now finally we're going to run this facial recognition script okay so now notice that um that i have kind of messed up here so his name is frank oh so uh so basically what we have here is that the user id matches the index in this list now there's a lot of different ways that this could get done right so there's a very simple way of doing just writing you know hard coding this list of names into a script as a list is just very very very simplistic but as a minimal example i think it works pretty well um okay so let's go ahead and do that so so my name should come up now as user zero none is user one right so these indices are it's a zero base index right so in a list this is position zero this is position one it's it's a list that starts so if we were to refer to this list names zero would be frank names one would be done okay so let's go ahead and run this and we should see a box with my name on it um on space copied the path we could run this so we could copy the whole path okay so here is oh look at that so basically uh the facial recognition is running there's me there is not me this one is unknown right oh wow it's very interesting minus torvald is uh it's just listed as me okay so let's see here oh unknown all right so okay so we had more uh more uh people in the database i'm sure that it would work in fact let's go ahead and try this probably just make the video as quick as possible but it's pretty interesting so okay so let's go ahead and run it again and see if we can get we can get a better uh result here so we're going to go ahead and run a data set and this time we're going to create a second um second person and this time it's going to be one okay now we're going to have to each time you add a person you have to run the face training the trainer the trainer again um you can also add a number of faces and then run the trainer right so you can right you can basically add all of your people running the first script then run the trainer and then run the uh run the classifier person all right so let's see how notice it's kind of confusing here okay so let's see if it functions okay so there we have it there we go okay all right guys so uh so that is it so that at that point your uh your facial recognition should be running um best of luck
Info
Channel: Education Public
Views: 14,634
Rating: undefined out of 5
Keywords:
Id: ZPnAnGe9PXc
Channel Id: undefined
Length: 41min 32sec (2492 seconds)
Published: Wed Jul 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.