Face Recognition with Real Time Database | 2 Hour Course | 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 create real-time face attendance system that is linked to a real-time database you can see at the back end this is our real-time database it has all the information of the students it has my ID number it has the attendance time uh major name standing all of that information and as you can see it shows here that I have been already marked and I can Mark only after 30 seconds so if I hide my face and 30 seconds are gone I can mark it again it's loading it shows my name it shows the total number of attendance and it says it has been marked now if I come again it will say already marked so it will not mark this attendance so that's the basic idea I will hide my face so that it can show you the status so we already have other students as well for example we have Emily Blunt so if I bring In Her Image it says loading and Emily Blunt was detected and and her uh what do you call attendance is marked we also have Elon Musk so let me just Google Elon so here we have the image of Elon Musk and there you go it says loading and Elon Musk is marked and if I bring it again it is not going to detect uh it will say already marked so if I bring it again you can see it says already marked so after an x amount of time you can mark it again otherwise the system will not detect so here right now you can see my face was detected if I remove it it will say already marked because not enough time has been passed by it's it's checking the time from here and if you see here you will see it updates in real time so when over here it will update there you go so you can see the orange the total number of attendance was updated and the attendance last time was updated as well so this is an amazing project that you can use to implement it in real time and if you want to learn how to create this on a web app then you can check out our Kickstarter page which is live right now with the world's first web development course for computer vision which will teach you all the basics of computer vision along with all the basics of web development so we will integrate both of them and we will create more than 30 web apps to create these amazing projects so here you can see it is a little bit laggy because we are not using asynchronous functions but on the web development part we are going to use asynchrist functions so it will download the data and it will show you the real-time webcam feed at the same time so it will not be uh in a series function it will be parallel it will running side by side so this project is also part of the web development course so if you wanted to implement it on web do check out our Kickstart page otherwise you can do it in python as well and create your interface and add these graphics and add this real time and all of this is for free of course but our premium course for web development is paid and it is 30 off right now on Kickstarter so do check it out so let's go ahead and create this project this video is sponsored by brilliant.org the most significant way to learn anything is by doing it for yourself brilliant engaging Hands-On lessons will let you learn six times more efficiently through Interactive Learning that you can do by viewing lecture videos they're interactive and wide range of courses have a variety of subjects including algebra statistics algorithms and much more learn computer science fundamentals without the need of coding and instead of using rigorous mathematics explore the internal workings of artificial neural networks through practical testing design Innovative algorithms that a computer can process quickly enroll now and learn anything of your interest check out the link brilliant.org Murtaza to get started for free with brilliant interactive lessons the first 200 listeners will also get 20 off their annual membership if you would like to level up your computer vision skills do check out our premium courses that are available on our CV Zone platform links are in the description so without further Ado let's get started [Music] so today we are going to create this face attendance system that can be used for schools for employees for a company and so on and what we are going to do is we are going to use this face recognition library now you can see this is the GitHub page and uh thanks to Mr what's his name again Adam okay his name is Adam so thanks to Adam and all the contributors for creating this library and we have done a previous video on this but today what we are going to do is we are going to take this technology we are going to take this library and we are going to integrate it with a real-time database so that we can upload all the data on a server and then when we are running the software we can download the data we can view it and we can upload more data to it we can update it and there's also this medium article that you can read again by Adam it explains how this face recognition Library works it is pretty good to see how it works it uses face recognition uh sorry face detection then it uses these features it extracts um and creates these encodings which you can see at the very end over here so it will create these 128 measurements that we will call as the encodings and based on these encodings we can use a machine learning model to actually compare which uh of the two faces match with each other so they are using svm classifier to you to do that so you don't really have to go through all of this and read it out but it will be better if you do if you understand the underlining principle of the library so it will be useful for you so what exactly are we going to do we are going to follow these steps so face application with real-time database first of all we look at the webcam we will import our parameters and then we will run our webcam then we will add the graphics so we have an interface in which it will show whether it's active whether we are detecting the face loading and all the stuff and then we will create an encoding generator so this has to be done once so for example if you have three faces three students you have to do it for all three of them so you run it once it will generate the encoding for all three of them and whenever you have a new student you will have to create the encodings again so you'll have to update them so you will run this script so this will be a separate script then we have to do this face recognition so the face uh the encoding generator is going to generate a file in which we will have all the encodings that encodings will be used by the face recognition script and it will be able to detect the faces so you can put both of them in one script but that is not very useful because every time we have to run you should not be generating the encodings because they are pretty much the same each time then the next step is basically the database setup so we are going to set up our database so that we can send in and we can receive the values then we have to add data to the database again we will create another script for this in which it will become easier to add that the data otherwise you can actually go to the website you can go to the the database itself and you can manually add the entries but that will take some time so instead of doing that we can just create a script and we can just copy paste for each student and change the data and we can upload it all together at once and then we also have to add so images are stored in the data itself so the database is stored in our real-time database and the images are stored in a storage bucket so these two are separate so we will handle them separately but at the end of the day the results will be shown simultaneously then we have to update the database Whenever there is an attendance Whenever there is a face detected we have to update the database so we will do that and then once we have detected the attendance then it should not take another attendance until an x amount of time so normally it will be one day so 24 hours or the next day you can take attendance that's the idea but for Simplicity we are going to give it a limited time for example 30 seconds so after 30 seconds it can take the attendance again because we just want to see the effect I don't want to sit here 24 hours just to show you that it works right so that's the idea so let's go ahead and get started so the first thing we have to do we are going to import all the images or the sources everything the files and then we will start with installation the installation is a little bit tricky but it's not that difficult but I'll show you how it's done so let's bring in the resources first so what exactly do we need so first of all we are going to create here a new directory and we will call it resources now these are the sources then we can also create another one by the name images or we can do it inside this so let's create it outside so we will keep it separate so images and within these images we are going to add our three faces or three students that we are going to detect so let me open this up so open in Explorer and I will just drag and drop the images so we can see them as well there you go so these are the three images why is this not showing so this is Emily Blunt this is Elon Musk and this is yours truly so what can we do is instead of writing one two three we can give them IDs because based on the ID number the student should be detected not based on their name because name can be similar but the ID number is unique so here we are going to give unique IDs so three two one six five four that's my ID and then we will give it to Elon let's give him the id963852 and then for Emily we are going to give 741852 so these are the or let's change that eight five two seven four one yeah so these are the three IDs that we are going to use now we have the images in the images folder so these are the three students if you wanted more you can add more as many as you want then the resources in the resources folder we are going to add we are going to add background and the modes so let me open that up and show you what do I mean let's open that up in the Explorer so here in the resources we have modes actually let me show you the background first so this here is our background so this is the attendance system Graphics here is the webcam that will be displayed and on the right hand side you will have the information of the student so you'll have the image and all that stuff and where does that come from that comes from the mode so here if you click on mode so these are the modes that we will see so the first one will be active the second one will be of the actual student it will have the image this part here is the total number of attendance this here is the ID number this here is the major then we have these three uh this is which year did they join and uh how many years have they been in the University and this one I actually forgot what was this uh the the um the standing of the student is it good standing probation period or bad or whatever you want to write here so it's just for showing that you can add lots of different parameters and you can actually display this information okay so now that we have our images and we have the resources folder with the modes and the background we can move on to installations now for installation what we need is we need a c compiler now the C compiler the easiest way to install the C compiler is to go to visual Studio website you can go to downloads and download the community version which is available for free and when you are installing it make sure you select the desktop development with C plus plus so we need the C plus plus compiler and this is the easiest way to install it so you just go ahead and do that it will take a while because it's a uh it's quite large and then once it's done then we can go on to file settings by the way the project name is face recognition real-time database so this is the name of the project this is using pycharm if you're not familiar this is one of the best Ides for python okay so here we have the project we'll go to python interpreter and we are going to add so here first of all there's a list of libraries we have to install and make sure you install it in the same order so first of all we are going to install cmake then we are going to install dlib then we are going to install face recognition recognition where are you face recognition uh okay let me just type it recognition there you go face recognition uh what else do we need we need Siri Zone CV Zone CVS or there you go so a CV zone is going to install uh opencv and it will also install numpy if you don't want to install that it's up to you you can just go ahead and install uh numpy and opencv separately the reason we are using CV zone is because we are going to take advantage of one of its functions which will give you a fancy rectangle so we are going to use that and we will go to opencv I believe it will already be installed but we are going to change its version and we will downgrade it to 4.5.4.60 this is a little more stable than the previous uh the newer ones that's why we are doing this otherwise if you want to stick with that it's it's fine as well so I believe all the installations are done I'm using python 3.7 if you are using a latest version that's fine as well as long as it doesn't give you an error so if it does the specific version of python I'm using is 3.7.6 so you can upgrade if you want I believe it will still work with higher versions 3.8 3.9 but if it doesn't you can always come back so these were the installations now what we are going to do we are going to create a main main project file and in this file what happened oh there's already a main my bad so we are going to delete that and in in this file we are going to add all the code with all the real-time database and everything working now the rest of the code that we are going to use uh will be separate and different scripts and we will name them differently so here what we can do is we can start with the webcam so here we can write uh import CV2 then we will write cap equals CV2 dot video capture and I am using multiple cameras so I will write one uh but you can write whatever you are using okay so over here do we need to set let's just set it just in case oh actually we do need to set it so uh the width we should be one two eight zero and the height should be the height should be number four is 720. so we have to specify this because we are using graphics and the graphics are based on these Dimensions so if it increases or decreases you have to change those as well so then we are going to write while true we are going to write success image equals cap dot read and then once it has red we are going to write CB2 Dot um I am show and we were going to write let's say display or let's say face attendance uh 10 10s if I can spell it right and then we are going to write the image and then CB2 dot weight key and we'll give a one millisecond DeLay So this is pretty standard you can say is the boilerplates template code for running a webcam so let's wait for it and there you go so now you can see me and the webcam is running fine so now let's go ahead to the next step we have done the first part which is webcam now we have to do the graphics so let's import the graphics so we have our Graphics in the resources folder in the background so we are going to write here image background CV2 dots I am read we are going to give in the resources and background dot PNG it will load the image and then we simply have to display it so we will go here and we will write cb2.im show and we are going to write here face uh 10 Dance Dance come on and then we will write image background so this will be the actual uh output that we are going to use so here I will change this name and we will write here webcam so now we have the webcam and we have the image background so let's run that and see if it works there you go so this is the attendance system and this is our webcam it's running fine so that's the idea actually I made a mistake now I recognize it this is not supposed to be 1280x720 it's supposed to be 640 by 480 because that's the next thing we are going to do if you look at the image background this is the part where we have to add our webcam image and this is 640 by 480 the complete image is 1280x720 so that was the mistake so now what we have to do is we have to take our webcam and we have to overlay on the background because we don't want to display these two things separately so how can we do that it's very simple we will simply write image background at what exact point equals image so what are those points so we have the starting point and the ending point now I have already checked that earlier and I have written it down so here what we will use is 162 so that's the height the starting point of the height 162 then 162 plus 480. that's the last point of the height then the starting point of the width is 55 and the ending point of the width is 55 plus 640. so that's the basic idea let's run that again and see if it works and there you go so now we have our attendance system you can see the webcam running in real time and we have overlaid the webcam image on our graphics so this is good now we can actually hide this we don't need that and what can we do next is basically bring in the modes so we should have the ability to change the modes the modes are basically active then the information of the student then marked and then already marked so these are the four uh modes that we can use so let's go ahead and import all these images so we can do it one by one or we can just give in a loop and it will store it in a list so that's a little bit better to do so that's why we are going to import it as a list so for the list we are going to Simply given the path so you should have a path and then you should have the for Loop so we can write here folder for the mode uh path equals we are going to write three sources slash modes so this is our folder mode path and then the the list that will contain all the images we are going to call it uh image mode list equals empty and then for the for Loop we are going to write 4 paths in folder mode path um no no no no no what are we doing folder mode path folder no actually oh I forgot that we actually need another path so we need mode path equals OS dot list directory and then we will get the name of all these uh what you call images so if you are confused like I was 30 seconds ago then let me show you what exactly I mean so if I print this out uh mode back list you will see that it will contain one dot png2.png three and four so the reason we gave this is so that if you're changing in uh you have different folder structure you can simply type it here instead of writing it here doesn't make a big difference though so there you go so it contains all these names 1.png2.png3.png4.png so now we can take this part and we can add it to folder mode path and it will give us the complete uh link of the image so that's how we can import so that's why we need this so we are going to remove this for path in mode path list we are going to write image mode dot list dot append we are going to add it to our list what exactly are we going to add we are going to add an image that we imported so image cv2.im read that's how we import it and what exactly is the path the path is this folder mode path plus this path so we are going to write OS dot path dot join and we are going to write folder mode path and path so that will add the images to our folder and just to make sure we are going to write print the length of image mode list so that will tell us whether we have imported all the images or not it should be four images there you go it's giving us the value of 4 it means we have imported all the images correctly that will let me just hide it out so that if you wanted this you can test it as well now it's it's good if you actually commented out uh not commented out if you comment it so here we are going to write importing importing the mode images into a list right so that's what we are doing now what we'll do is we will add this to our what you call background image so we'll copy this part we'll paste it and instead of image this time around we will have one of these images uh the the ones which are in image mode list so image mode list at any instance so for example we'll put zero so it will give us active then it will give us image we can actually test it out so that's the idea and then here we have to change the values because it's it's not the same position so what exactly is the position let me check so we have 44 then 44 Plus 633 and what happened there nope 633 and then we have eight zero eight eight zero eight eight zero eight plus four one four so again you can check it yourself so here you can see now we have the active mode and what we can do is we can check the rest so if I put one here it will give us the next mode which is for the student it will show the image and the data of the student there you go and then what we can do is we can put two and it should show us marked there you go it's showing us marked and then if we put three it will show us already marked there you go so these are the four Images the four modes that we are going to use so now what exactly can we do we can simply add a dynamic value here we can add a variable and based on that variable it will show us the stages so that's how simple this is so this part pretty much for graphics and for the webcam is ready the world's first computer vision web development course is here the power of computer vision now goes into browser hey everyone this is Murtaza from the YouTube channel muchasa's Workshop which has now over 300 000 subscribers thanks to all of you I've been working in the computer vision field for the past nine years and have found one big problem while it's easy to create computer vision projects it is very difficult to bring them to the market as final products trust me I have over 100 YouTube tutorials on computer vision with python which are excellent to get started but providing it as a solution to a company is extremely difficult and not effective so now comes the solution we have created the world's first computer vision web development course here you can create your own computer vision ideas and deploy it to the web as web apps this will allow you to create solutions that can be shared with anyone with just a web URL no installations no running behind Library versions whether you are creating a mass detector for a small company or a traffic analyzer for a governmental project using computer vision web development you can create and deploy your computer vision algorithms at ease while maintaining and scaling them but what exactly can you do on the web well pretty much everything with the rapid advancement in the computer vision field now it is possible to run simple computer vision algorithms to complicated custom object detectors right in the browser this was not possible a few years back but now the technology is here but it can be very difficult and frustrating to get started here is where we got you covered we have carefully designed this course to take you from the very basics of web development right up to the point where you can create your own computer vision web apps at ease this course is not a purchase but it is an investment we have designed it for hobbyists Freelancers and software firms that want to level up their computer vision services imagine providing an automated shirt size measurement to a clothing company or customer engagement statistics to a shop owner or creating a license plate cloud-based recognition system this course is meant to monetize the skills that you learn so what is included in this course we have split this course into four parts in the first part computer vision web Basics you will learn the basics of web development including HTML CSS and JavaScript we will go through the integration of computer vision and web development along with the opencv basic algorithms we will also look into creating elegant graphical interfaces for your web apps this part will include three projects in the second part computer vision web Advanced you will learn face detection body detection hand recognition face mesh detection body segmentation and much more to create some Advanced projects here we will create five projects to solidify what we have learned in the third part computer vision web expert we will learn the basics of deep learning with tensorflow on the web here you will understand the fundamentals of object classification and object detection you will use pretend model for testing and then learn to create your own models from your own data this will allow you to create object detection for any type of custom objects we will also create three projects in this part this part will also contain a free python course that will make it easier for you to create your own models the final part computer vision web apps is dedicated to commercially viable projects we will create more than 20 web apps to get the maximum exposure to real world applications these will include automated shirt size measurements customer engagement car counter face attendance text extraction AR glasses face mask detection pistol detection and much more this course is meant to boost your career to new heights the computer vision industry is expected to be worth 48.6 billion by 2025 with the average salary of a web developer being 82 000 and for a computer vision engineer being 135 000 in USA the merger of both these Technologies is a deadly combination that will soon be in great demand so it's best to get on this train in the early stages we will also provide certificates of completion so that you can proudly add it to your resume we will be using the world's largest computer vision Library opencv along with the most popular deep learning framework tensorflow so you don't have to worry about the latest tools and Technologies and don't worry if you are a beginner who is unfamiliar with HTML CSS and JavaScript or even an intermediate programmer who has these skills but needs some polishing we have got you covered we created crash courses that will teach you the basics to get started and only for our Kickstarter backers we will be providing free HTML CSS and JavaScript ebooks along with their video courses to get you started there is a reason why this has not been done yet on a large scale it is very difficult to create lots of commercial projects that are optimized to run on the web not only that going through all the bugs and finding their fixes takes a lot of work but we took the first step and started the process and now we need your help to bring it to reality we have written the code for almost 70 percent of the projects and the basics and now we need to start production although this is a challenging task we are not new to this Arena we have delivered eight high quality and information dense courses prior to this course that have been sold in over 75 different countries we are confident that we can deliver this course but need your help to do so so please join our Kickstarter campaign by taking a pledge or sharing our mission with others so these two are done now we are going to move on to the encoding generator so we have to encode we have to generate the data for all those 128 measurements and we have to store it in a file and that file we will import it in our face recognition and then it will display whether it has detected that face or not so that's the main idea so here what we can do is we can create a new file and we are going to call it and code and code generator so in this encode generator we are going to generate all the code that we need the encodings that we need of the faces so here we are going to import CV2 then we are going to import face recognition and then we are going to import nothing else okay so now we need to import all the images once we have the images the images I'm talking about are the faces so we have to import these faces once we have these faces we are going to encode them one by one and once we encode them one by one we are going to store them in a list one by one and then we are going to dump it uh using the pickle Library so we can import pickle so yeah that's the idea so here the first thing we will do is we are going to import all the images and this will have pretty much the same standard as we did earlier for importing the mode images so here if we paste it out so here we can write importing d uh students images there you go so what exactly is the folder uh folder path let's just remove the mode so it is images so we don't have to write anything else we'll just write images and from the images we are going to get the list of directory so let me import OS as well and then we will get the list of all the images so so here we are going to write image list here we will write image list and here we are going to write folder paths so if we run this now let's see if it Imports all the images we have to change this to image list okay so we are importing three now one more thing we have to do is to import the IDS as well so here each image has an ID on it so the name of the image is basically the ID so we have to extract that so what we are going to do we are going to write here uh student IDs equal empty so here when we are importing we are going to import the name so here we already have this if we print out if we write here print mode pathless why is it mode we need to remove that path list okay and then we write here bat list so here we will copy and paste and let's see what do we get so here we are getting all the names so what we need to do is first of all we need to get this name right we need this ID part so we can get this part it's very easy all we have to do is we have to write uh what was it path but that is exactly what we will get this dot PNG but now we have dot PNG so we need to remove that so if I if I print this let me show you if I print this so it will print all of them with the path uh with the png.png so we need to remove this dot PNG so how can we do that we can do that by OS dot path dot split text so here we can write OS dot path dot split split text and here you have to given the path that you want to split so this is the path that we want to split so let's print that out there you go so now it has split it up into two parts one with the ID and the other one is dot PNG so which one do we want we want the first one so the first one to get it all we have to do is we have to put here zero so that is the first element the zero is the first element right so if we run this now now you are getting the exact value three two one six five four so we have removed the dot and we have removed the dot PNG the reason we are doing this and we are not just saying get the first six letters is because the numbers can change it might have three digits it might have ten so this is the correct way to do it so now that we have this all we have to do is we have to put it in student IDs so we are going to copy this and we are going to write here uh what is it student IDs students IDs dot append and we are going to append all of this so we can remove this we can paste it here uh just for your reference I will keep it and I will comment it if you wanted to see you can see it as well so again we are going to check uh the student IDs if they are imported correctly actually we can print the whole ID thing so we can write here student IDs and let's see if it is imported correctly so here you can see it's extracted properly and we are getting the names or the IDS of our students so now to run the encodings to create the encodings we are going to create a function so we are going to send in the list in this function and this function will generate all the encodings and split out or spit out um a list with all the encodings so we are going to write here find and go codings and we are going to give in the images images a list let's see or did we name it yeah it's already there image list so we will just call it a little bit bigger a little bit different so images list and we are going to write here pass so what exactly are we going to do we are going to Loop through all the images and we are going to encode every single image so we will say or image in image images list first of all uh we we have a few steps how to encode the first step is to change the color so the color space just to make sure we are going from BGR to RGB because that is what the library uses so opencv uses BGR the face recognition Library uses RGB so we have to convert it so we'll write here image equals CV2 dot color uh CB CVT color and we are going to write image and CV2 dot CV2 dot color underscore bgr2 to RGB there we go so now the first step is done we have converted it the second step is to find the encodings so we will write here encode equals face recognition dot face encodings and then we are going to give in the image and we are going to get the first element of this so this is how exactly you can find the encodings of that image now once we have that what we have to do is we have to put it in a list so we will write here and coding and code list equals empty and we are going to write encode list dot append and code so it will Loop or Loop through all of these images and it is going to save it now once we have that what we will do is we will return this we are going to write here return encode list and that will generate all of our encodings so here let's call this function so we are going to write and code list known so all the known faces equals find encodings for all the image lists so this will generate the image list and it will save it here and then we can simply write here print and coding complete and above we can write here above that sorry above here we can write encoding started dot dots so here the encoding has started here it will generate and then encoding has completed so if there's a lot of images this will take a while so it's better to show it like this okay so that's the idea let's plate and let's see if it works and coding started encoding complete so that's very good uh we can actually print out the encode list known just to see what exactly does it output so there you go we have some values here that we can see we have an array of all these different values so that's good so it means it's working so what's the next step now we need to save it in a file we need to save it in a pickle file so that we can import it when we are using the webcam so um but when we are saving it in a file we need to save two things one is the encodings and the other is the names or the IDS so which ID belongs to which encoding right so we need to tell that so we will write here encoding list known with IDs equals we are going to put encodeless known and then we are going to put student IDs so these are the two lists that we need to store it in our file so we are going to generate this pickle file so how can we do that it's very simple you have to write file equals open and then you have to given the file name so we can write anything let's write encode uh encode file dot p and then we have to give in some permissions so we are going to write w b and then we have to send in or dump the lists in this file so we will write here pickle dot dump and we are going to dub and code list known with IDs and we are going to dump it in our file then we simply can close so we will write here file Dot close and we are going to write file saved file saved so that's the idea let's see if that works there you go encoding started encoding complete file saved so if we look at our directory you can see we have encode file dot P so this is the basic idea behind encoding now we have extracted all the encodings and now we will go into the face recognition part so in the face recognition we are going to take these encodings and then we are going to apply it to see whether we are detecting any faces that are known or not so how can we do that it's very simple it's very easy to follow so let's go ahead to our main file and then we are going to import it here so here after importing mode images we have the while loop and before the while loop we are going to write import the encoding and coding file or let's let's not call it import let's call it load load the encoding file so the loading part is pretty much similar to the saving part first of all we have to open the file so we'll write file command file so first of all we have to write file equals open which file do we want to open we have to given the name so here we will write encode file.p and then we have to give in some permissions again so we will write RB so this is for reading and then we are going to write and code lists with IDs did we write the same name and codeless known with IDs so let's just copy that and code list known with IDs equals pickle dots load file so this will add all the uh what do you call lists all the information into this encode list known with IDs now we need to extract we need to extract it into two parts so the way we converted this we added these two into a single list we right now we have to separate them from the list so again we will copy this and codeless known and student IDs equals and codeless known with IDs and uh before we do that we can simply close this so we can write here file Dot close there you go so that's the basic idea behind this so let's run this and see if it prints prints student IDs let's see if we have extracted the correct we are running code generator we need to run main again so let's run that there you go so now we have the three IDs of all the students so that's good and it means it's working so I will keep this here just for testing so if you wanted to you can test and we can also add something like this loading let's say loading encoded file encode file and encode file loaded there you go so just just to make it a little bit more Interactive so here it will say loading encoding file encoded encode file loaded there you go now what we have to do we have to take these encodings and we have to check in with the new faces whether any of these match or not so this is very simple it might seem a bit complicated but actually in reality it's very simple all we have to do is we have to write a few lines of code so let's go ahead and do that so the first thing we have to do is we have to uh make our image a bit smaller because it takes a lot of computation power so we will squeeze it down we will scale it down to uh one fourth of the size so here we are going to write that our image small CV2 dots uh what was it resize and we are going to resize our image and we are not going to give in any actual numbers what we are going to give in are the scale values so not the pixel values we are going to give in the scale value so 0.25 and 0.25 so this is the smaller image and again if you remember we have to convert it into RGB from BGR so here we can just simply copy and paste so where was it so CVT color we can copy that and paste it here and we will write image small equals this image small so bgr2 RGB so this this step is very important of conversion otherwise it will not work properly so once we have done that now we need to feed in the value to our face recognition system it will detect and then it will give us some output so we need two things the first one is the faces in the current frame and the second one is the encodings in the current frame so here we are going to write face current frame equals face recognition dot face locations face recognition dot why is it not showing dot face hello locations yeah there you go and we will give in our image small and then once we have the location of our images we need to find their encodings so we have the previous encodings of our known faces we need to find the encodings of the new ones and then we will compare so this is for the new ones so we will write here and code encode currents frame equal and we will write here face recognition dot base underscore encodings and we will give in the image small and we will give in the base current frame so these are the locations and these are this is the image so go ahead and find because we don't want to find the encoding of the whole image we want to find the encoding of the face so we are giving the location of the faces and we are telling that this is the image extract it and find the encodings so that's the idea once we have that then we can go into the next step which is actually let's write it below this and we are going to Loop through all these encodings and one by one we are going to compare it with our uh generated encodings whether they match or not so we are going to write or encode face and code base and base location base lock in encodes current frame and faces current frame so what exactly is happening here uh we need to write here zip because we cannot um we cannot unzip using uh the for Loop we want to use the for Loop for both of these lists at the same time so we have to use the zip method otherwise we have to write two different Loops separately so what we can do is we can extract the information one by one so the extracted information of this will go in this and the extracted information of this will go in this so that's the basic idea now we need to find the matches so matches equals face recognition dot compare faces and we need to compare it with the encode list known we are going to compare our current phase our current phase is this one why is this like this n code c o d e what is wrong with my spellings today and code face so we need to write that and then again for the distance we can find the phase distance as well uh the lower the distance the the better the match so we will write here uh face distance and here we will write phase distance so again encoding list known and encode phase now this will give us a list of uh actually let me show you so because I if I explain it will be complicated let me just show you what exactly are we getting so this is matches and then we will we will print matches so let me put this as a string so you can see what is happening yeah I was using the wrong key okay this is matches and this is face distance okay so let's write here face distance so let's run that there you go so now you can see both of them have three three values because we have total of three images in the known directory because our encoding generator generated three images three encodings so it will match with all three so let me just go in the very beginning so now it will first give you true or false matches whether my face the current face from the webcam matches the first image from the images that were generated so here it actually matches my face so that's why it's saying true it did not match with Emily Blunt it did not match with Elon Musk so that's why it is giving false now how good of a match it was will be given by the phase distance so the lower the phase distance the better the matches so here it's 0.38 here it's 0.8 here it's 0.76 so it is matching with the other faces but only 0.8 only 0.7 but with my own face it matches 0.38 so that's why it's giving here true and uh for the others it's giving false now if I bring in the image of let's say Elon Musk then it will give uh the second one or the third one the third one it will give true because that is the face of Elon Musk so let me run that and let me bring in my phone and I will open an image of Elon uh let me open a lawn mask there you go oh it went back okay there you go so here if you see Elon Musk now at the end it is going to say true let me push it here there you go now it's stable and the last one you can see here you saw that it is showing the last one as true and the least value is 0.4 which is of the third one as well so this means it is a matching with Elon Musk so that's the main idea so how can we extract this value and how can we make use of it so the first thing we will do we are going to get the index of the least value the least value so whether it is 0.3 0.8 0.7 which one of these is the least we want the index which means for this instance we would we would need the value 2 because this is 0 this is 1 and this is 2. so how can we get that information we will write here uh match index equals numpy dots arguments a r g minimum and then we are going to give in the face distance numpy is not imported so we'll write here import num pi as NP there we go so now it will give us the value index value let me print that out so here we will write match index and we will write here match index in my case it should give zero there you go so match index is zero because it's detecting my face and my face is the first one so it is giving the value of zero and this is the least value now once we get the zero value we are going to check it uh in the matches list if that value is true we will say face detected the correct face is detected so that is our criteria so how can we say that we can say if matches at match index is true then we will print face let's write known face detected there you go so let's remove these so that it's not confusing foreign there you go so it is saying known face was detected that's the idea and to make sure that we are getting the correct one what we can do is we can print out the ID of this one so that um we have the correct information of which one was working so or which one was detected so here what we can do is from the list what was the list name it was student IDs so student IDs at match index so we can print that there you go so three two one six five four three two one six five four is the correct ID that was detected let's check for Elon or let's check for Emily Blunt yeah actually Elon is already open so let's check his so there you go and let me bring down the value so we can see so here it's showing nine six three eight five two so here nine six three eight five two is Elon so that is correct now this is uh a little bit weird that what we are doing so what we can do instead is we can actually show the face and show the name so that uh it looks a little bit better so we have printed this out uh we have commented commented this out and what we are going to do next is we are going to uh draw the rectangle around the face so at least we know it if something is being detected now uh what we can do is we can use the normal rectangle uh provided by opencv or we can use CV zone so the CV Zone rectangle is a little bit fancier so we are going to use that if you want to use opencv it's fine as well you can simply write CB2 dot erect rectangle and you can use that or you can write CV Zone dot um we did we import CV zone no uh import CV Zone import CV Zone and we are going to write CV Zone Dot uh Corner rectangle so that's the idea so uh to fill in this we need the image so image background and then we need the bounding box so the bounding box information is what we need the rest you can remove or you can keep uh you don't have to give any other information but if you want to uh we can remove the lines to remove the lines I believe you have to write RT is equals to zero RT RT equals rectangle thickness RT means our tangle thickness is zero so this is the idea so to use this we need the bounding box so the bounding box information what we can get is from the face location so how can we get that let's write it down here so it is very weird it is y1 and then X2 and then Y2 and then X1 equals face location so that's how they mapped it so we have to use the same uh same idea so then we are going to copy this and we will paste it twice and the reason being we have to multiply it by four because we reduce the size by 4 1 4 so now we have to multiply it by one uh by four so that it is matching so all of them are multiplied by four so that is well and good now we can simply put our rectangle so how can we create the bounding box information so first of all to to have the bounding box information let's just write here bounding box equals to have the Monique box information we have to uh remember that our image is not starting from zero this is zero of the image we have to add an X and Y value which is for starting from here then our images starts so here we are putting it in image background not the actual image so we have to give in that offset so what is that offset uh in the X direction is 55 and in the y direction it's 162. so uh we are going to write 55 plus X1 then we have to again give 162 Plus y1 then we have the X2 and Y2 but we need the width and height when we are talking about bounding box it includes X Y width and height not X1 y1 X2 Y2 it's not like that so what we have to do is we have to uh get the X2 minus X1 so X2 minus X1 and then Y2 minus y1 and that should give us the bounding box so if we run this we should have our bonding before did we know we have to put it back so image background equals this there you go so now if we run it it should have the rectangle around there you go if I move you can see the fancy rectangle again it's it's up to you if you wanted to change it you can use the opencv rectangle as well it's um it's fine okay so that's the basic idea and so far what we have done let me go back to our image we have created the webcam we have started the webcam we have added the graphics we have created the encodings we have done the face recognition so the next part is the interesting part where we will start the database setup so we are going to add our database and then whatever information we are getting we are going to upload we are going to download and we are going to do all of these different things so now the database that we are going to use for this project is basically Firebase and the reason we are using this is because it's real time it's very easy to set up and you can actually use it in real projects I have seen it being used in mega projects as well and the best part is that you can scale as you go along it's very easy to scale this database so let's go ahead and set it up so we are going to go to Firebase and then you have to log in it's free to use so just go to Google console sorry go to the console and here you are going to create a new project so add a project here let's enter the project name we will call it what should we call it base 10 then a real time there you go and we will hit continue and we will enable everything no need to be shy and then default account for Firebase and we will create the project so let's wait for it to finish and what we can do is by the way what we have to do is we have to create one file that will actually upload all the data to the server and then the other file it will actually help us download the code so the other file will be basically what we are using so far it's the main file so we can download the data from here and then we can upload the information of attendance up updated attendance from here but for the generation of code we have to add the images so these images once we store them once we have uploaded them we should be able to access them because if you look at the resources folder in the modes here you have to show the image of the correct student that is detected if you don't want to show the image that's fine you don't have to use the storage but if you do want to show that you have to upload the images as well and doing it manually again it's not a very good thing then why exactly are we coding if we have to do it manually so we will upload the images man uh automatically and then we will download them automatically and it will display it based on the face detected so that is the idea now it's the project is ready we will go to the console face real time blah blah blah and we will close this here we will click on build and we are going to go to our real-time database so here you can see uh we are going to click on create database and it's United States that's fine we are going not going to start in lock mode we are going to start in test mode we are going to enable it and here you go you have your first database setup that's how easy it is by the way this uses a Json format and that is exactly what we are going to use for our project as well so here we have the build and in the build we have seen real-time database now we need to link it we will do that don't worry don't go ahead then we also have storage so if you click on storage you are going to see your storage we are going to click on get started in production mode no we need it in test mode we are going to click next and after this location is set you cannot change it blah blah blah we will move ahead so it's creating our storage buckets in which we will store all these files all the faces all the images now these images by the way they have a particular size they are they are 216 by 216 I believe yeah 216 by 216. so all of them are the same size so if you don't know how to do that you can go to canva or you can you open paint and crop the image and make it smaller so exactly 216 by 216. so all of them are same so it will be easier for us to use otherwise we have to add some code to check what is the size of the image then we have to crop it manually and not manually we're using code and then we have to do it so try to avoid that uh what happened he didn't create creating default pocket so it's creating uh let's wait for it to create and then we will start uploading stuff on it so I was facing some issues with the other database I'm not sure why it's not able to create a new database uh it's not able to create a new storage so this is the old one uh the only difference is the name School attendance the rest of it is pretty much the same it's a real-time database as well there is nothing inside it and if you click on storage you'll have this storage now this is the link to the storage and this is the link to the real-time database don't use these links use your own create your own otherwise it will not work properly so once we have this we have the storage and the real-time database what we need to do is we need to go to settings project settings here we need to go to service accounts and in the service account we are going to go to python Python and here we will generate new private key so we are going to create and this will create our private key and then we have to copy this code and add it to our uh python code so that it runs so this is basically they're telling you how you can use this so let's put that to one side then we'll open this up and here what exactly are we doing uh we need to add we need to set up the database once we have that then we need to add data to the database okay that's good so the the images we are going to do later on first of all we need to learn how to add data to the database so we will click on python file and here we are going to write add data to data base okay so we will copy and we will paste the code then we have to just drag in the secret file so and we can just call it this service accounts key so this is basically the service account key you can see it here it contains all the confidential information so don't share it with anyone else and you can create your own for your own database as I've shown you so it's going to use this file dot Json and what we have to do is we have to import these as well the Firebase admin and the Firebase admin package so we'll go to file settings and we are going to add Firebase admin so we are going to install that we'll hit close and we'll hit OK so now the package has been installed successfully and we can move on so if I just run this now uh nothing is pretty much going to happen uh actually it gives an error because the path is wrong so we will just remove that so it's directly in the same folder as this file so we do not have to write any paths so here service account key and the add data to database is in the same folder so we all we have to do is we have to write the file name so that's why it did not give any errors and it just ran so the second thing we have to do is we have to give the location of the the database the real-time database so if we go to real-time database and let's close this here it will show this link we have to copy so here we can click copy reference URL and then over here we have to paste it again we have to paste it as Json format so we are going to put brackets and within those brackets we have to tell what value it is the value is basically data base URL and then we have to give in the URL of this so that's the idea there you go so that is pretty much it and then we have to create a reference so what exactly is going to happen this is our main directory and it has null right now inside there's no values there's no uh format there's no data so what we will do is we will add a format we will add a pen and directory which will be students inside that student's directory we will have the IDS of all the students and inside those IDs will have the values of all the required things so all the required information so first of all we need to create the reference for this so we will write reference equals DB so we need to create the database reference so we will write here from Firebase admin import DB and here we will write DB dot reference and we are going to give in the path so students so this is basically the path the reference path of our database so it will create a student's uh directory over here and inside that we will have all the IDS of the students now what we have to do is we have to add the data so the data we will write data equals again it's Json format then we have to give in the value and if we want a value within a value then we will I will show you how it's done so our value will be based on our student so here we have images so this is the ID so as I mentioned earlier it will be based on ID so we will write here three two one six five four so that's the ID number and in this we will have another Json So within this we have another Json which is the value of this and inside that we will have all the information of the student so for example we will have name and the value of the name so this is called key and this is called value in Json format we have key and value so this is the key and all of the information inside will be the value and within that we have another key another value another key another value so that's how it works so my name is Murtaza Hassan and then we have to give in a comma if we have more values uh then we have major so major we are going to give robotics that's what I did and the spellings are wrong so very good Robotics and then we will give starting year starting here and that is basically 2017 let's say that was not my starting year though I'm old now anyway so then we have total attendance total underscore at 10 dense if I can spell it right no I cannot uh total indentance let's say I already have let's say six attendants and then we have standing uh well I'm a good student I was a good student Stan the only thing I can't do is I can't spell so so I'm going to put G for good and then for ear we have ear number four and then we'll put comma again I forgot it here that's why it's giving error and then we will write last attendance time so this will be the time uh that was attended last so last time for example the student attended at the date of 24 and the next day they can only do another attendance so that's what we are going to do so here we will write last last underscore attendance uh underscore time then we are going to give in the format so it will be the first one will be here so 2022 and then we have to give in month and then we have to give in the date so let's put 11 and then we have to give in the time so 0 0 let's say 5 4 and let's say 3 4. so this is the time so this is the date this is the time and based on this we will check the date and time right now and we will minus it from here and then we will see whether the time is enough to count another attendance or not so this is just a random value I've put so now this is for one student so what I want to do is I want to show you this in real time so I've put the database on one side and I've put this on one side let's just remove this and there you go so now if I run this now it should in real time update over here so let's click on ADD and let's wait for it and nothing happened oh why did that happen so oh yeah sorry I just went too far I have to add we didn't actually send the data we just wrote it and I got excited uh what we have to do is we have to send it so we will say for key key and value in our data dot items basically it's a dictionary so it python it's a dictionary but it's a Json format so that's how you um unzip a dictionary in Python so we will write reference dot child so if you want to send the data to a specific directory then you have to use child and we will put it in the key and what value do we put we will write set value so it will take the key and the value so this will be the key and all of this will be the value and then this will be the key and all of this will be the value so yeah that's pretty much it now if we run it hopefully it will turn up here there you go so it created students that is the folder let's call it folder and then inside that folder we have another folder which is 312654 which is my ID and if I open that up it has last attendance time which is this robotics has the name the starting year attendance and all that now if I change anything for example if I change my name and if I only write Murtaza and I run it it will only update this part oh sorry this part the name so that's how it works so that's why it's real time it's good and it's fast so if I write there Hassan and I update it now it is updated with Murtaza Hassan so again if I have more attendance I can add 7 to it and it will automatically update the total attendance to 7. so that's how it works so now we can just copy this part this part and we can paste it twice and don't forget the comma because for every other value we have to add a comma so this is the next one and then we have another one so this is the one after that so this basically normally is done through an admin panel so when the admin actually registers a student they will add it or initially the information in their database this is this is what we are doing using a python script so you can create a graphical interface for this too for adding as well but you will need to use a pi game or Tinker to actually create that so uh then we have uh the other numbers what were they let me check because I forgot or Emily Blunt we have eight five two seven four one and for Elon Musk we have 963852 so here we are going to write the names so Emily Blunt and her major is let's say economics and she started in 2018 total attendance is let's say 12. standing is bad she didn't attend a lot but but uh or let's say okay it's bad I'm not going to go into too much detail so here is number two and last and this time we're going to keep the same here we will write Elon Musk Elon Musk and his major is let's say physics and he started in 2014. and this is okay that will be too much let's put 20 20. and he his ear is the second year as well so she started in 2018 about wrong 2021 and the year will be one my bad and uh his standing is uh good as well and attendance is this and then this time so now if we run this it will automatically update it and we will be more students added so we have uh the Emily Blunt and we have Elon Musk so all the data has been added to our database so at any time you want to update the basic parameters the name the major the year or something like that you can come back here and update it otherwise uh the attendance total attendance number and the last attended stay time we will update it from our main code that will update uh whenever we have detected the face so that's the basic idea so this is good now we are done with our step number six add data to database so we have done that now we will add images to database not the database well the storage so let's go here and as you can see we have our uh project overview storage so we'll click on storage and this is our storage so pretty much uh what we have done with our database we will do with the storage and it will give us something similar so we have to add the images and then we have to upload it then we have to download it and all of that so we have to create a bucket now the images we should not create another file to upload the images we should do it with the same file over here so what we are running the encodings we are automatically adding the images we are importing them so why not upload it then you can do it in add to database as well or you can do it when you are generating the encodings so let's do it here because the code is pretty much done here so let's just be lazy and add it here so we are already getting all the images and we are storing it one by one so why not one by one send it to the database as well so here we are going to copy this Firebase admin all that stuff uh we need the credentials and we also need we also need the storage so we will write here import from from Firebase admin import storage so we are not going to use credentials I believe we are just going to use storage so then we are going to pretty much use the same code we are going to copy it from here we are going to go to encode generator we are going to paste it here now in the credentials we have given the database but now we need to give the storage so here we will write storage uh what was it bucket bu c-k-e-g the link to the storage bucket is again we will copy it from here uh but this time around we have to remove some parts so we will remove the GS dot slash slash so we are going to remove that so once we have this we are going to go into our loop the loop in which we are going through all the images so what we can do is we can put inside this code uh the the name list basically or we can write it here where should we write for path uh actually we can write it here as well so here we will write file name equals we will write OS dot path dot join right and this is folder path and this is the path okay I've removed something from here so this will give us the file name so once we have the file name then what we can do is we can create a bucket so buckets we use c k e t equals storage dot pockets and then we will write blob we are going to create a blob to actually send it blob is equals to bucket dot blob and here we are going to write the file name so we will write blob dot upload up load from file name and then we are going to give in the file name there we go so this is going to send the data over here so what exactly do we need to send so what it will do is it will create a file it will create a folder called images because that is included in the path so it will create a folder called images and in that images folder it will add all these images so if we run the encode generator did it work let's refresh because this is not real time so you have to refresh and there you go so you have the folder images inside that you have this or did it do it wrong I think it did it wrong uh no let's delete all of them and we are manually going to write the will not use OS dot join so here we are going to write f uh then this will be within our brackets then we are going to put slash oh come on we're going to put slash and then we will have our paths there you go so yeah that should work now let's run it uh okay there's a space there should be no space Images slash dot PNG does not exist why is that uh images dot PNG yeah it does exist what is the issue okay there is a bracket here why is there a bracket here something is wrong why is there a bracket uh okay let's right here uh student ID whatever we are getting here let's try this and we will write manually oh okay okay okay my bad there is this okay let's run that again and there you go so no errors and refresh there you go now we have a folder images inside that folder we have all these images so these images we have uploaded and then we can download them uh whenever we require them whenever the face is detected we can download the specific image and then we can use it in real time so how can we do that uh let's look at the next step add images to database we have done that now we need to update the database in real time uh whenever the attendance is increased and we need to fetch all the data from the real-time database and add it to our Graphics so let's go ahead and do that so here in our main file what we are going to do we are going to add the code for the database so we will go to the encode generator so we need the credentials for the database and we need the credentials for the storage bucket so we will copy both of both of those actually let's just copy all of this and we'll go to Main and we are going to paste it here so now we have our Storage storage and we have our database URL as well so now what happens is that once you detect the face once you recognize the face and we know that it has been detected correctly now we will fetch the data we will download the data from our database the real-time database and then we are going to display it for a few seconds once it is done we are going to show marked for a few seconds and then we will go back to active so this is how it will work so if we open this up in Explorer so the first thing will happen is it will show active right now it's active then when it is detected it will download the data it will put it on this and then display it and then once it's done we will wait for a few seconds maybe one or two seconds and then after that we will show marked and then once it is marked we will go back to active and then if the student comes back again then we will say already marked you don't have to mark it again so uh the first thing is that we have to show that it is active so to show that we will simply write image mode list we will write here mode type so here we are going to create a new variable we'll call it mode type equals zero so when it is 0 it will show us active so if I run this now if I run the main it is supposed to show us active there you go so it is showing us that it is active once the face is detected we only need to download the information in the first frame we cannot keep downloading because it will be very inefficient so we have to download it only once in the first iteration so to do that we are going to add a counter the counter equals zero so once we detect it we are going to write that if the counter I'm writing it in English if the counter equals 0 if the counter is zero then make the counter equals one so if it was previously 0 make it 1. right so that is the main idea and once it is 1 here we will check if counter is not equals to zero then we are going to do something then we will write counter plus equals one so it will keep counting so for the first frame for the very first frame we are going to say if counter equals 1 that is the first frame then what is happening then we are going to download all the data we will download it and we will show it at the same time so how can we download it uh it's very simple first of all we need to find the information of the student once we get it so we are going to save it in an ID so here we are going to call it ID equals what was it ID equals student IDs IDs at match index so uh whatever index was matched we need the ID for that so this is our ID for example it will be three two one six five four it will be saved so if if you want to make sure that we have an ID we can put 0 over here and then it once it gets it it will show an ID and once we have the first iteration we are going to use that ID number to get actually let's write -1 because 0 is also an ID yeah so here we are going to write counter is one then we are going to write students info equals this is the part where we are going to download so how can we download we will create a reference we will write DB dot reference and in this reference we are going to give a string we know that the first one will be students so this is our main path the main folder in that folder we need to get the information of the student and inside that we have an ID so this ID is the one that we got so we need that ID and then once we have that we want all the information of that right so here we are going to write what's what's going on why is it like that oh there there needs to be another one no students ID why is this here sorry then uh what we need we need to get dot get that's it so we need to get all the information so here we will print it out print out the student info are we printing anything else nothing okay so only on the first frame it is going to download all the information and let's see if it downloads correctly there you go it has downloaded none excellent now there is a mistake somewhere DB reference is it student or students um do we need to give any reference other than that not really okay let me check again this part is it too dense or student we need to check that in the database so in the real-time database it is too dense three one two six five four maybe the ID is wrong let's check the ID first uh so here once we are getting the ID we are going to print it out prints ID three two one six five four it is correct the ID is correct so once we have this ID if counter equals 0 which it is counter is zero then it makes counter one if counter is not equal zero where is this yeah it's in the right Loop if counter equals one which it is then student info equals DB dot reference F students slash ID dot gets what is the issue over here okay let's just remove this and we are going to go back and let's see if this works is it only for the first frame or does not recognize the data at all none so the student ID was what three two one six five four and over here it's three one two oh oh that's the okay so that's the problem the problem is my typing so the data uh where we added the data I wrote this wrong three two one six five four that was a digits so eight five two seven four one that is correct nine six three eight five two okay so this is fine now actually we need to run it uh we need to run this because I change it here so let's run that let's see if it updates here there you go it's updated so these are now two different students so we are going to remove this one so there you go we have now the connect student and if we go to Main and I think we can put this it should work fine so if we run that now we should get the information of the student so let's run main again there you go so last time attendance major name standing starting year total attendance year number everything we are getting it directly uh from our database so that's perfect that is exactly what we wanted and we have it uh in the first frame only and in the first frame only we are going to update but before we do that we need to change the image we need to change the mode so we will make the mode type equals one so here we are going to write a mode type equals one because we need to update the mode which will go over here so once it is more type 1 but will it show before or after we'll have to check I'll tell you what I mean by that but let's just try it now so once we have the student info uh all we have to do is we have to put it in the correct uh areas so how can we do that so we can do that by writing here now this should not only be displayed when the counter is one it should be displayed at two three four five six and so on so what we will write here CV2 dot put text and we have to put it on the image background and then we have to put in the string what exactly is the string the string will be student info at what information the information let's say is total attendance total because this is a dictionary you can just reference it like this total underscore attendance and that's how you can get it so total attendance is this and then you have to give in the location so location I've already checked it's 861 and 1 1 2 5 for this particular text then we will give in CV2 dots fonts Hershey complex and then we will given the size which is one and then we will give in the color which is 255 255 and 255 which means white and then we will give in the thickness which is one so if we run this now we should get the total attendance okay we are not getting anything um why is that so here we have changed the mode to one more type is one so over here oh it's zero why did I put zero here it should be mode type so that's why it dynamically it should change so let's run that and there you go so this is uh first it was active and now it is the mode type is two where it shows all the details and here you can see it shows seven so the total attendance is seven and it has taken it from our database now to prove this that it has taken from our database we will change this total attendance we'll click on it we'll change it to 8. so now my attendance has changed if I click on this now it should give me the updated attendance number which is eight there you go so now it's showing me the attendance number as 8. so this is all good now we have to add it to all the other uh what do you call values so here we have to add name so I will copy this paste it here we will just change it to name and for the values the values it is 808 and four four five now the name is a little bit tricky because the the length changes a lot we have to center it and we have to center it automatically so I will tell you how to do that right now we'll just add it like this uh then we are going to add let's add major so the major is one zero zero six and four nine three then we are going to add uh let's say the ID so here we will write ID the ID is at where is it one zero zero six one zero zero six and four nine three okay I wrote one of them wrong major is supposed to be one zero zero six and five zero my bad and then we are going to write after major the standing standing standing uh standing is nine one zero and six two five then we have to write here here is one zero two five and six to five and then we have to write uh standing here standing starting here sorry what is wrong with you today starting here so starting here is uh one one two five and six to five so let's run this and see if we are getting all the positions correct except for the name I'm going to tell you how you can write the name as well uh okay the the sizes are wrong and I believe the colors are wrong as well because we can't put white here so the last three should not be white uh they should be grayish so let's put 100 100 and 100 let's copy that and we'll paste it here and paste it here for the sizing for the last three it is 0.6 0.6 0.6 and 0.6 and for the ones before that is 0.5 and 0.5 this is for ID and this is for major and the colors the colors are same I believe yeah and one more thing we have to change the name the name again will be uh we'll make it a little bit darker 50 50 50. so let's run that foreign there you go so the ID is correct major is correct the standing is correct number of years is correct uh the starting year is correct and the attendance is correct now the name is wrong because it is starting at the very edge and we need to center it so uh how can we do that so let's put the name at the start or let's put it at the end so that we have something separate for it so we will write it in so what exactly do we need to do we need to find the width of this text so if I'm writing Murtaza Hassan I need to find the width of this text so the width might be let's say 50 pixels so I will take the total width minus 50 pixels and then I will divide it by 2 and start it from there so I will push that 50 or 50 value a little bit further and then it will start it from there and then it will Center automatically so I I think it's pretty straightforward so I hope I don't have to explain it further so how can we get the size you can get the size by CV2 dot get size get size of the text and you have to give in the text so student info at name and then you have to given the font CV2 dot font Hershey complex so this should match all of this information should match otherwise it will be wrong and then we have the scale and the thickness so this will give us the size so what it will give us it will give us the width and the height and it will also give us another thing that we do not require so we'll put so we will add this underscore because we don't need it so once we have that what we can do is we can write that our width uh let's say offset our offset equals width minus this is the total width no this is not the total width my bad the total width it is ah the total width is 414. how do I know this because what we are getting is the width starting from this point till the end point so the width of this image is 414. so that is what we are writing so here we will write 414 minus the width which is of our text divided by 2. divided by 2. so that will give us the offset so and over here we will add it to the offset the width will add to the offset so if we run this now it should Center automatically so if we check in for Emily Blunt for Elon Musk all of them should Center automatically so here we have an error put text what's wrong with per text and what is wrong with protects where is it 50 51. 50 51 here foreign what exactly is wrong here the type is wrong okay um over here image background oh okay so we have to put double slash because it might not be an integer it might be a float so that's why it's giving another there you go so now it's automatically centered so if I bring in let's say uh let's bring in Elon Musk is that not detecting Elon Musk oh no it will not update it a second time because right now it's already only updating for the first frame so I will bring it before it starts so that it checks for a non-musk there you go so now even the Elon Musk is centered because um we have this code that is checking automatically uh how to center it so all of the information is being received the data has been received but not the image itself so now what we need to do is we need to get the image and we need to add it uh to our uh Graphics as well so how can we do that it's fairly simple so uh where are we getting the data here we are getting the data over here so we will write here get the data so here we will write get the image from the storage okay so how can we do that uh we are basically reversing the method we are writing here blob equals buckets bucket dot get underscore blob the bucket is not defined because uh we didn't create a any bucket so here we will write buckets equals storage dot bucket and then here we will write get Blob and we are going to give in the reference so it will be f and then we have the images folder if you remember in our storage so if we go to storage we have the images folder first and inside that images folder we have our uh images by their IDs so again we have to do the same thing we will write slash we'll put ID and that is pretty much it and then uh we don't have to write PNG do we no we do have to write PNG dot PNG because that is the complete name it is stored with the PNG name so we have to write that as well otherwise it will not work properly so then we have to create an array from this array equals numpy Dot from buffer so just think of this as a standard method of converting you don't have to go into too much detail import numpy as NP and prompt from buffer buffer buffer and we will write blob dot download as string and then we have to give in the numpy dot unsigned integers of 8 Bits so that's the idea and once we have that array now we need to convert it so that we can use it with opencv so we will write here image student equals CV2 CV2 Dot uh image decode and we will give in the array and we will give in the conversion CV2 dot color underscore BG R pgr A2 BGR so this will give us the image and now what we can do is we can use this image outside here to display so let's just to make sure that it works we can put here image student equals empty and later on we will keep adding it so here um after all of the text is done we are going to put in the image and to put in the image we'll write image background at what values equals image student so what are these values again I have done the math already and it is 175 and 175 plus 216 because 216 is the size of the image and this is the offset so the offset in the width is 909 and then we have 909 Plus 216. so if we run this now again it will download it once but it will display it again and again so let's find out and there you go so now you can see my image is downloaded number of attendants ID number major good standing year number total number of years in the University so that is the main idea so it is working very well right now so uh the next step is to update the attendance so every time it is uh we detect it it should update the attendance so we have to send the data and this we will only do once because uh we don't need to keep doing that every iteration so how can we send it it's very simple so here after getting the data and getting the image we are going to update the data so here we will write updates that uh of uh 10 dance so here we are going to create a reference reference equals DB DB dot reference and we'll give in the path so it will be of the student and ID over here like this and now using this reference we are going to send in the data so here we will write that first of all we need to update so student info adds uh total underscore attendance equals we are going to update so we'll write plus equals 1. and now we are going to update this value in our server in the database so here we are going to write ref dot child if you remember when we were sending the data we wrote ref.child dot set so we are going to do the same thing ref.child dot set and child what value we we need to update total attendance and we need to update it with this value so what are the updated values we need to update it with that so let's run that and see if it works so I'm going to put this on one side and this will be on the other side so let's go to the real-time database and I will open up my own there you go and here the total attendance is eight so if I run this now it should update to 9 as soon as it is detected there you go so it is updated to nine and it is also showing 9 over here so it means it is correctly adding the values to the database and if I run it again it should update it to 10. there you go so now the total attendance is 10 it has been updated so this is the basic idea of this attendance and then what else do we have to do now uh what we need to do is we need to create this timer thing so that uh once it is detected it will detect okay we need have updated we have marked and then it will go back to active to detect for a new student so how can we do that so the counter is already running so we will show all of this only when the mode type is 2. so what we will do is we will say that we will give it some certain values so over here I believe it is from 0 to 10 then 10 to 20 and then 20 to 30. yeah so greater than 20. so here it's a little bit tricky because we have to add it uh based on certain formatting if we add the if statement above another if statement or below the other if statement it will change the effect so we have to make sure it is in the correct format so the first thing we have to do is if our counter is less than 10 less than or equal to 10 then we are going to display all of this the mode type will be one and we will display all of this so we are going to do all of this there you go but if that is not the case if the counter is between 10 and 20 then we are going to display marked value but that is supposed to be done before this otherwise because the the only time it's updating the image is here so it will be too late for us to update so we have to update it again so here we'll first check do we need to update we need to update it when if counter is in between 20 or let's remove that 10 and 20. so if it's between that then we have to make mode type equals two and if we may change the mode type we need to update the image before putting everything on it so here we will write image background here will be more time so let's run that and see if it works so the first 10 frames it is supposed to show the image and all the data and then it will say marked there you go it's showing and then it says marked now once that is done we did not say what to do next the next part is when the counter is completely done when it is above if the counter and this has to be below the counter plus one if it's above the counter plus one there will be one wrong frame so add it below the counter you can try it out to see what happens but I have checked it and that's why I'm telling you if counter is uh greater than or equal to 20 then we are going to write counter counter equals zero then mode type equals zero basically we are resetting everything then the student info equals empty and the image students equals empty and we are going to update uh the mode type again because we updated the mode type we need to reset it quickly at the same spot so let's run that and see what happens so it should reset all of that there you go marked active then it will start again marked active then it will download it will show you can see the attendance is updated last time it was 14 now it will be 15. and then it will go to 16. so again that's not the right way you should not keep adding the attendance values so what we need to do is now we need to pause and we need to check whether you are uh whether the attendance was done an x amount of time after the previous attendance so to do that what we will do is it's very simple uh where did we update the attendance where are we updating the attendance okay so here we are updating the attendance so here we are going to say if the student time was more than 30 seconds from the previous attendance then we are going to Mark attendance because we are testing otherwise you will put the total amount of seconds as the actual hours for example you want it after 12 hours or the next day so you want it after 24 hours so we'll add it in seconds instead of hours so here we are going to write that our dates underscore date time object equals date time dot string to time now why are we doing this because the time over here is basically string whatever we are getting from the database is string we need to convert it into an object that the date time understands so we will tell the date time what is the time right now and minus it from this time so when you minus it it is not string minus string it is object minus object so that's why we have to convert it so string time we have to give in the student info of the last underscore attendance underscore time so the last uh then dense time and then we have to given the format where did it go then we have to give in the format the format we are using is basically I will write it down here it is percentage first of all we have the ear then we have percentage month then we have the percentage day then we have a space then we have hours then we have Colin then we have minutes and then we have Colin and then we have seconds so this is the format basically you can see it here this is what we are writing it that uh when you are creating the object create it in this format from the string date time is not added because we did not import it from from date time Imports date time okay so now it will have that now it is converted and now we need to find the date time right now so we can write that date time dot now it will give us that time minus date time objects this will be the seconds uh uh elapsed equals this but this will not be in seconds it will be still in this format so we need to convert it into seconds so we will write Dot the method we will call it dot total seconds so it will give us the seconds elapse so if we print it out we can print out the seconds elapsed so if we run this now before updating it will give us the time the seconds that were from the previous one so here we are getting a warning the string time is not there so it's strp my bad so it's string p so let's run that there you go so six six six uh sorry six thousand six hundred and two then six thousand uh 608 but now it's not updating the the last attendance time so we need to update it right away so that next time it should not be six thousand second next time it should be five seconds or six seconds whatever time it's taking so we will put it here reference style uh total attendance time and here the the time will be the date time right now the date time right now there you go so let's run that so now it will update the last attendance time as well so that will give us the corrects okay there is a problem date time dot now uh we need to convert it okay okay okay okay so we need to convert it dot string strf time and we need to give in the formats this is the format that we need to convert it into and then we can run it let's run that so here it should update there you go you can see it updated so now the time elapsed was six seconds rather than six thousand so next time it will be again 5.7 so now we can check how much time has elapsed so that uh whether we should update the person or not so here we can simply write after second elapse we can write if seconds elapsed is greater than 30 so this will be 30 seconds but if you want to do it in real time you should do it for how many hours or seconds you have to convert it into seconds as I mentioned earlier so if that is the case then we are going to update and add otherwise we are going to give in the mode number three so else else mode type equals three counter equals zero and we will still update our image so whenever we change the mode type we need to add the image background this code so that it updates in real time so that is the case and then if we run it let's see what happens so there you go so it is marked now it's showing active but now it's showing the image and all of that it's showing already marked but now it's showing the image the text and all that we need to stop that so here this is the part where it's showing all of this so all we have to do is uh we have to write here that if the mode type is not equals 3 then you can do all of this otherwise don't do it all of this there you go now it says already marked and then you have to push this in counter plus one it checks it here yeah so when the counter is above 20 it will go back to active so you just have to push it in it was in the wrong Loop so let's run that and as you can see the time and the attendance is automatically updated so it says already marked but before it says already marked it shows active and then after already marked uh if I remove my face it should say active again but it's not doing that so we need to fix that so if mode type is three so in this case if nothing is detected uh no not in that case if counter is not equal zero if that is the case let me just recheck it again okay yeah so we need to add another if condition that if the face is detected so here we will write if there is anything in the face current frame then do all of this all of this else mode type equals zero and then counter equals zero so if we play that again then we should when I put my face in it should remove the already detected or already marked so if I put my hand in it shows active then already marked and if I put my face here it shows active if I remove uh it says already marked so I need to wait 30 seconds so I'm going to block my face and hope for 30 seconds then once the 30 seconds are up it should say uh marked so let's wait wait wait wait wait wait wait wait wait and let's try it there you go now it marked again and now it's marked active and now it will say already marked so after 30 seconds we were able to mark it again now one more thing we can add which is a little bit um for for the Aesthetics of it once the object is detected once the face is detected you will see here it delays it lags so this is because it is downloading the data from the database if you want to remove this lag you have to use asynchronous functions so we are not going to go into that much detail we have covered this in our web development course in which we will create all of this in our website and once we have done that we are using asynchronous functions so it will show the video in real time and it will update and download the code download the database at the back end so it will not delay as much it will not get stuck so in order to avoid this as a problem in order to make it better what we can do is we can just write loading once it is detected so that way it will not look that bad it will not look like it's stuck it will just think that it's encoded it's loading so in order to do that let me check where do we add that yeah if the known phase is detected if the known face is detected which is over here we have our mode type and counter as one and in this when the counter is zero what we will do is we will write here CV Zone CV Zone dots put text rects and then we will put it on the image background and we will put loading l-o-a-d-i-n-g and we will put it at the position 275 and 400. and then we will update the image because it will not show it as it is so we will write CV2 Dot I am show you have to write the exact name so we used face attendance so we just we can just copy this and we can paste it here copy this part and paste it there you go so now once it detects it will say loading so I will cover my face uh now it's running if I remove no it's not saying loading that's not good why is it not saying loading uh if counter is zero oh yeah we need to add the weight key CV2 dot weight key we need to add a delay of one millisecond and then it will update so let's hide my face again okay if I put that it says loading and then already marked because I just did it that's why there you go so now 30 seconds were up so now I can Mark again now it says loading and already marked so if I hide it the loading is gone and now it's active and then we can Mark others as well so if I wanted to Mark I can mark for example let's say I want to Mark Elon Musk and I will go here and there you go loading and it shows Elon Musk so that's the idea it's marked and now I will do for Emily Blunt Emily blunts there you go Emily Blunt detected major economics total attendance whatever the attendance number was so that's the main idea so this was our complete project everything is updating everything is running fine so if you want to learn more about this if you want to learn how to actually implement this on a website and how to actually use it in real time you can check out our web development course which is computer vision web development course which is the world's first and it will teach you all the basics of web development as well as computer vision along with creating more than 30 web apps that will give you a lot of in-depth knowledge of how to create real world applications and solve real world problems so if you want to check it out our Kickstarter campaign is live right now go ahead and check it out do back it if you cannot just share it with your friends so that we can bring it to reality so this is it for today's video I hope you have learned something new if you like the video give it a thumbs up if you loved it share it with your friends and I will see you in the next one
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 203,888
Rating: undefined out of 5
Keywords:
Id: iBomaK2ARyI
Channel Id: undefined
Length: 140min 17sec (8417 seconds)
Published: Tue Dec 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.