FACE RECOGNITION + ATTENDANCE PROJECT | OpenCV Python | Computer Vision

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to learn how to perform facial recognition with high accuracy we will first briefly go through the theory and then learn the basic implementation then we will create an attendance project that will use webcam to detect faces and then record the attendance live in an excel sheet I upload videos on a weekly basis so don't forget to subscribe and give this video a thumbs up if you find it useful so let's get started [Music] so today we will be using a face recognition library to actually recognize our faces and differentiate between different people now before we start with the implementation let's have a brief look at how this library works so here we have an article 10 by the author himself his name is Adam and we will have a quick look at it so that we can understand how the backend works so there are a couple of steps required to perform this facial recognition so the step one is actually finding the faces and they are using the hogg method at the back end which is histogram of oriented gradients and once we have the this is a sample of hogg and once we have the face that is the bounding box where the face is present then they apply a word because for example if you look at this this is a frontal image and if you look at this this is a little bit tilted and for the network to understand a human this will be completely different than this so what they do is they try to make all the images look something like this so they use the D live library at the back end if you want to know more about D lib and how it works I have a tutorial on that you can check it out in the description so what happens is that they find the facial landmarks and using these landmarks they actually work the image so that it looks more like this so it is more centered and once they have this image they will send this to a neural network which is previously trained and what that does is that gives us the encoded features now what are these features now as a human this is a good example as a human if you think about this it might be the different might be the difference between your eyes the distance between your nose and the lips the size of your lips you know these are the things that you might think of but for a network it automatically trains self to find these best measurements so that is what the network has done already so what it does is for us we only feed in the image and it will generate 120 different measurements for us so for example for this image you can see that this was the input image and it generated these 128 measurements now using these 128 measurements we can define a person and using these measurements we can differentiate between different people as well so the last step is to differentiate them and to find the matches so to do that you can as you can see these are simple numbers you can use any machine learning method over here they have used SVM classifier the linear model of it and using that they can differentiate between these measurements and they can tell whether the measurements are of the given person or not so that is the theory the brief theory and if you want to learn more about this I will leave this link of this article in the description now before we can use this library we have to install the C compiler now if you go to the visual studio website in the download section we can download the community version for this now once we download that we will have we will have something like this in our installation and what we have to do is we just have to select the desktop development with C++ so that is the only thing that is required so once we install that we can come back to our PyCharm project and we can install the dependencies so let's go to file settings and then we will go to the project interpreter and we will add the dependencies now the first one is C make so we will write C make we will install that then we have D lab now I actually face some issues with the deal apply Brady and I will not recommend using the latest one as it yields a lot of errors so what I recommend is to use nineteen point one eight so we can install that then we will install face recognition then vampire and the last one is OpenCV so once they are done installing we are going to import them so the first one will be import CV 2 and then import numpy numpy as NP and then we will import our face recognition library so I can ports face recognition so we saw that there are a lot of steps that we have to perform to actually do the facial recognition at the back end but because we are using this library we just have to call a few functions to run it now what I have done is I have broken this down into 3 steps and we are going to look at step by step how we can use this library so the first step is basically loading the images and converting it into RGB because we are getting the image as BGR but the library understands it as RGB so we have to convert it now if we look at our directory we have images basic now we are running the script basics so that's why we are using this one what we have here is two images of Elon Musk and one image of Bill Gates so if we open up this is the image of Elon Musk and then we also have a test image for Elon Musk and then we also have a test image for Bill Gates so what we will do is we will train we will get the encoding of our normal image and then we are going to use the test image to see that if it can find the picture of elan over here or not so coming back to the steps the first thing we will do is we will import our image so we will write image and on is equals to face recognition dot load image so it gives us a function to load images so that's why we are using this so we have image basic and then we have Elon Musk so then we will convert this into RGB so we will use C v2 dot CVT color and then we will write image a lon and we will write C v2 dot color BG r to RGB then we can write C v2 dot I am show and we will give it a name so we will say Elon Elon Musk and then we will write image alone and we will write C v2 dot weight key and we will give a delay of 0 similarly we are going to import our test image so we will copy this and over here we will write image test will copy this and paste it and we will change this to a lawn test so then we can copy this again and we can paste it here and then we can write here test and this one is test as one so if we run this and there we have our two images so this is the test image and this is the normal image that we will use for encoding so this was step one now the step two is finding the faces in our image and then finding their encoding as well so to do that we will first detect the face so we will say that face location is equals to let me write the face location is equals to face recognition dot face underscore locations and all we have to do is we have to send in an image so we will send in image alone and because we are only sending in a single image we will just get the first element of this then we are going to encode the face that we have detected so we will write and code alone is equals to face underscore recognition dot face and codings and then we are going to send in our image which is image alone we will say the first element here as well and the the face locations are right now we are just doing it so that we can see where we have detected the faces so to see that we can write CB 2 dot rectangle and we can draw it on image alone and then we have to give the location of the face so now for the face location it actually returns us 4 values let me show you so let's print this out you so there you go you can see that it prints out for different values these values are basically top right bottom and left so based on that we can give the x1y1 and x2y2 in our rectangle so we will go back here and then we are going to write the first element will be face at one face look occasion and then the second one is face location at two and we will copy this and these are the second points so this will be actually this will be 3 and 0 3 and 0 and that will be 1 and 2 and then we have to define the colors so the color let's put it purple and then we can define the thickness let's run this and see what happens and there we have it so now we can see that this is the face that we have detected the same thing we can do for our second image which is the test image now in this example we do not need the face location all we need are the encoding the face location we are just doing so that we can see where the face is located and whether it's located properly or not so we can write here face location test and then image test this again test or let's remove an image just in this we will have to replace let's run that again and there we have it so now we're detecting both the faces in both the images now so far we have done two steps now we are moving on to the third and the final step the third step is basically comparing these faces and finding the distance between them so as I have mentioned before we are getting the encoding so the encoding for Elan and the encoding for the test image and then we are going to compare these encoding so these are 128 measurements of both the faces and what we will use at the back end is the linear SVM to find out whether they match or not so let's write it down so we will say that results is equals to face underscore a recognition dot compare faces and we have to given a list so we don't have a list at this point so we are just this is the list of the known faces so right now we have only one face which is Elan so we are going to use that so encode Elan and then we are comparing it with which and codings and code and code tests so if we print this out we will find our results so let's run that and there you go so as you can see it gives us a value of crew which means it has found these both in coatings to be similar so they are of the same person but what if we change the test image to Bill Gates Bill Gates let's run that and there we have it so it detected the face properly and then it is telling us that this is false the encoding is do not match so this is good but sometimes what happens is that you have a lot of images and there can be similarities so what you want to find out is how similar these images are so you might want to find the best match so to do that we will find the distance so we will write here face distance is equals to face underscore recognition dot face distance and again we have two given list so we don't have that so we will write just encode a lon and then encode test and this will give us the distance now the lower the distance the better the match is so if we print the distance as well so we can write here face distance and let's run this so here we are getting a value of zero point eight four and if we change this to alone then we are getting a value of zero point four three or zero point four four so you can see that there's a clear difference when they actually match and when they don't match so the final step we can do here is just to display this on the actual result image so we can write here see v2 dot foot text and we can put it on the test image what we want to put is basically our our results so we can write results and we also want to put in our what do you call the face distance so face distance the distance is its it's in decimal places and it's very long so we can round it off so we can say round to two decimal places and then we can give in the origin let's put fifty fifty and then the color no the color the font so CV 2 dots font let's put any font and then we have the scale which will put as 1 then we have the color let's put it as red and then we have the thickness let's put it as 2 so let's run okay we are facing a problem yes because this is an array it's not a value so we have to get the elements so we are talking about the first element of this and there you go so right now it's showing that it's true and it's 0.44 and if we go back and we change this to Bill Gates now this gives us false and it gives us a value of 0.84 so next we are going to create an attendance project based on this facial recognition library so let's go ahead and create a new file we will call this endurance project and what we will do is we will first import our libraries as we did before so let's copy that so now let's look at the steps the first step is again to import your images and then convert them into RGB so this is the first step now but you look if you look at this whenever we have to bring in a new image we will have to write it manually you give it a name and then store it and find its encoding that will be very messy process so what we can do is we can create a list that will get the images from our folder automatically and then it will generate the encoding for it automatically and then it will try to find it in our webcam so to do that as you can see here we have the images attendance and we have three people here Bill Gates Elon Musk and Jack Ma so what we will do is that we will ask our program to find this folder and find the number of images it has and it poured them and find the encoding for them so to do that we are going to use the OS so we will write import OS and then we will first import our images so we will write here path is equals to images attendance and then we will create a list of all the images we will import so we will call this images images and then we will also write the names of all these images so we will we will take the names of each one of them and when we output the result we will use these names so instead of creating a new list where we have to manually type all the names we can just take it from our image itself so here we can write let's call it class names so the first thing we will do is we will grab the list of images in this folder to do this we will write my list is equals to OS dot list electri and we will give it a bath so if we print this out so as you can see we have Bill Gates we have Elon Musk and Chacma so we have all the three images so next we are going to use these names and import the images one by one so we can write here for CLS which is our class in our my list we are going to import each of the classes we can use the load image but here we are going to use the normal I am red function that comes with open CV so we can write our current image is equals to C v2 dot I am read and then we will read our current image which is basically our path and then slash and then CL CL is the name of our image so for example the first one will be Bill Gates dot jpg so CL s class or let's let's put it with other s then we are going to append so we will say images dots append and we are going to append our current image and the last thing we will do is to append our class name as well so class names dot append and inside here we don't want to append just the name like this because it's bill gates dot jpg we just want bill gates so to do that we can write or s dot Pat dot split text and we want to split CL and then we will grab the first element of it which is zero so this will give us bill gates instead of bill gates dot jpg so we can print this out to see if it works properly class names and let's comment this out so as you can see the list was like this before and now it is without the extensions of the files okay so next we are going to start with our encoding process so we know that once we import our image we have to find the encoding for each one of them so what we can do is we can create a simple function that will compute all the in codings for us so we can write here find and coatings and coatings and we will require a list of images so images then we will create an empty list that will have all the encoding at the end so we will call it and code list is equals to empty and then we are going to loop through all the images so for IMG in images so the first thing as you know we have to convert it into RGB so we will copy this line so our image is equals to image converted to RGB then the next step is to find the encoding z' now to do that I think this part is done so we can remove this let's go back to the basics and let's actually copy all of this and then we can paste it here and then we can comment it out so the next step is to find the encoding so here you can see we are finding the encoding so we can write here and code and then over here we can just make it image so this will find the encoding and then we will just append it to our list so encode list dot append and then we will write encode and at the end we will just return this return and code list now to run this we just have to call this function so we will say and code lists for known faces is equals to find and codings and we will send in our images now to test if it works properly we can print out the length of our encoded list known so let's run that and there you go so now we can see we have three and codings in our list so we don't need to print this but what we can do is we can print whenever we have found the encoding so that because it takes a little bit of time so we can write encoding completes so we know it has done this step so now we are moving on to the third step the third step is to find the matches between our and codings but we don't have an image to match it with now that image will be coming from our webcam so let's initialize the webcam so we will write gap is equals to C v2 dot video capture video capture and we will give it 0 as our ID and then we will write a while loop to get each frame one by one so here we will write success and then we will write image which is equals to cap dot read so this will give us our image now because we are doing this in real time what we want to do is we want to reduce the size of our image because this will help us in speeding the process so we will write that our image small is equals to C v2 dot resize and we will send in our original image and then we will we don't want to define any pixel size and we will write none and then we will define the scale so the scale we will put as 0.25 and 0.25 so it is 1/4 of the size and then as you know we have to convert it into RGB so we will copy this and we will convert it into RGB and then the next step is to find the encoding of our webcam so here we can copy this now in the webcam image we might actually find multiple faces so for that we are going to find the location of our faces then we are going to send in these locations to our encoding function so to find the locations we can use this so we can write here faces in our current frame and then we can find all the locations in our small image and then our encoding z' we will write encoding of current frame and then we will send in our small image and along with that we will send the faces the location of the faces so faces current frame now we are moving on to the third step where we are finding the matches so what we will do is we will iterate through all the faces that we have found in our current frame and then we will compare all these faces with all the in codings that we found before so to loop through we are going to use both of these lists and to loop through them together we can write 4 so the first thing we will find is the encoding so we will write encode face and then we will also find the location so we can write in zip and then we will write encodes of our current frame and the faces of our current frame so what this basically does is so one by one it will grab one face location from the faces current frame list and then it will grab the encoding of encode face from encodes current frame so because we want them in the same loop that's why we are using zip then we are going to do the matching so we will write here matches is equals to face recognition dot compare faces and then as you remember we have to send in a list so the list of our known faces is basically encode least-known so we will send that and code list known and we want to compare it with one of the in codings which will be our encode phase so we will write here and code phase and then we are going to find the distance so phase distance is equals to face underscore recognition dot face distance and then we are going to send in the same parameters so encode list known and encode face so because we are sending in a list to our face distance function it will return us a list as well so it will give us three values because we have three known faces and it will give us the distance of each one of them so the lowest distance will be our best match so let me show you what I mean so if I print face distance this will be a list I actually didn't connect my webcam let me connect the webcam so we can run this so now I have connected my webcam and as you can see we do not have any output because we are not showing the webcam image but if I bring my webcam near to a Bill Gates image you can see that it starts giving us values and as you can see the value of the first element is the lowest so because the image I am showing is of Bill Gates that's why we have the lowest one at the first element and the second and third are four alone and Chacma that's why they are higher values so what we have to do is we have to find the lowest element in our list and that will be our best match so to do that we can write match index is equals to num five dots minimum at our face distance so this will give us the index so in this case it would give zero because this is the first element if it was this it will give us one so once we have that index now we know which person are we talking about so what we can do at the end is we can simply display a bounding box around them and we can write their name so to do that we can write if matches and then we can write index match index and then here we can write our name is equals to class name at our index so mass index and because it's a name we can convert it into capital letters in case the name is in lower lower case so we can write upper and we can print this out so prints name and let me run this and see what happens so here if I bring the image close to Bill Gates and it will say Bill Gates so what we can do is we can create a bounding box around the face and we can write the name so to do that let's first show the image as well so we can write here cv2 dot I am show and we don't want to show the small image we want to show the original image so we will write here webcam and we will write here image and then see v2 dot weight key is 1 so if I run this there you go so you can see that we have the image of Bill Gates and it's giving us Bill Gates as the output okay so in order to create that rectangle we need to first find the location and because we already have done that because we have all the locations what we can do is we can say y 1 x 2 and Y 2 and X 1 is equals to face location so this face location is basically this coming from our locations of our faces so then we are going to draw a rectangle see v2 dot rectangle and we want to draw it on our original image and then we are going to write here x1 y1 and then x2 and y2 and then we are going to give it a color let's give it green so 2 5 5 and 0 and then we will give it a thickness of 2 and then we can draw another rectangle c v2 dot rectangle on our original image now this one will be X 1 and Y 2 minus 35 and then this is X 2 and then Y 2 and then 0 to 5 5 and 0 and then this will be CB 2 dot filled now this is the method that the author used I'm using the same method to display because it has a rectangle at the bottom and then it shows the name which actually looks nice so we are using the same method so CV to dot put text and we are going to put the name so we will write here name because the name is already in string format we don't have to convert it and then we will write here X 1 plus 6 then Y 2 minus 6 and then in we are going to write C v2 dot font we will pick any font and then we will give it a scale let's give it one and then we will give it a color so let's give it white color and do we need to give something else let's give it a thickness of two so if we run this you will see we have a problem so let's run that so there we go so we are detecting properly but the location is wrong and the reason for that is that we have scaled in we have scaled down our image to 1/4 the size and then we are performing all these calculations so in order to revive that actual values again we have to multiply our value with 4 so what we can do is we can write here this equals x 4 then x 4 x 4 and x 4 so if we run that now so there you go so now it's showing it in the connect place and it's showing the name as well Millie gates and if we try out another image so it's giving us a LAN mask properly and then if we try another image and it's giving us in Jack Ma so now we are able to detect all of them properly so we can comment these outs because we are already displaying now the next step would be to mark our attendance so to do that we can create a new function and we will go up here we will write here mark attendance and all we need is the name to write down our attendance so to mark the attendance we will just write the name and the time that they arrived now you can experiment with this you can add a lot of different things you can even link it to a database and you can do a lot of different things but to keep it simple we are only going to use name and the time they arrived now to use the time we will need the library for a time and date so we will say that from dates time import dates time so we will be using this so now what we will do is we will create an attendance file so we will go over here we will create file neil and we will create a new file and we will call this attendance dot CSV CSV stands for comma separated values so the values we put in will be separated with commas so we will write that in and we will ignore this and then inside we will write name comma and then time so these will be the two values that we will enter so we will save this let me go back and save and then we will go back to our attendance and over here we will open this file so we will say with open we are going to open attendance no no no this attendance dot CSV and we want to read and write at the same time so we will say write plus and we will say as F now we are going to read in all the lines that we have currently in our data the reason for this is that if somebody is already arrived we don't want to repeat it so we will create a list so we will say my data list is equals to F dot read lines and we can print this out my data list so if I just call it here mark attendance and if I just put a in here and then if we run this you can see it gives us name and time so next we are going to go through this list one by one so we will say that four line in right now we all only have one line so later on we will have multiple lines so we will go through each line and we will say my data list we want to find the entry so we will write entry is equals to line dot split so we want to split them based on comma so then we will have name and time separated so name will be our first element so we can write we can create a new list here we can write here name list this will be an empty list and we want to put all the names we find in this list so our entry is basically two values we just want the first one so we will say name list dot append and we are going to append the first element of our entry so entry add 0 so entry 0 will be name so it will append only the names in our list so once we have the list of all the names we are going to check if the current name is present or not name not in name list if the name is already not present then we are going to find that time so we will say now is equals to date time dot now this will give us the date and time and we will only write the time so we will write our date string is equals to the date and time string is equals to now dot string and then we are going to write the format the format is basically percent age hour and then percent age minutes and then percentage seconds so then we can write F dot write lines and we can write F and then we always want to write it in a new line so we'll put a new line and then we will put curly brackets and we will write in the name then we will write a comma because there are comma separated values and then we will write in our time so date time string and there you go so if I send in the name let's say a lon it should enter the value so let's run that so if we go to our attendants and there you go so it entered in on and it entered that at the current time 10:43 and see zero for seconds so now whenever we find the face we have to call this function and give it the name so we will remove this from here and over here whenever we are matching we are going to send in to our market and it so here we will just say the name and that is pretty much it so if I go back to what you call the attendance list so as you can see Alon is already there so even if I put the image of Alon in front it will not do anything because it's already there now it will only mark the attendance for who is left Bill Gates and Jack Ma so let's show that and let's see how it works so here we have Bill Gates so we have shown Bill Gates and then we will go to Jack Ma and there you go so if we stop this now and you can see that the two new entries have been added so you can see Bill Gates and then you can see Jack Ma so this looks weird because we are not used to of reading tables like this so if we go to our project directory and if we open this in Excel it will look normal so here you can see that we have the three entries at their given times so the final thing we will look at is how easily can we add another class or another human being so what we can do is as you can see here I have another image of Jeffery Preston and what I can do is I can just drag it in my images attendance and that's it so now it will be able to find the images of Jeffrey as well so if I run this now if I bring in the image you can see that it has detected it properly and if I close this and go back to my attendance you can see a new record with the correct name has been added so this is how easy it is to actually add new people all you have to do is just bring in the images of whoever you want to add to this folder so this is it for today's video I hope you have learned something new if you liked the video give it a thumbs up and don't forget to subscribe and I will see you in the next video
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 1,477,662
Rating: undefined out of 5
Keywords: FACE RECOGNITION, ATTENDANCE PROJECT, FACE RECOGNITION & ATTENDANCE PROJECT, face recognition python, face opencv, face recognition opencv, face recognition attendance system, face recognition, face detection, attendance system, deep learning, facial recognition, opencv, python3, face recognition with python, opencv3, tutorial, coding, cv2, computer vision, video analysis, face detection opencv, face recognition opoencv, face detection using opencv, how to do face recognition
Id: sz25xxF_AVE
Channel Id: undefined
Length: 52min 23sec (3143 seconds)
Published: Thu Jun 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.