Face Recognition in Python | Part 1 | FaceNet, MTCNN, SVM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hello guys welcome to my next video today we will cover face recognition topic by using face net and empty CNN models let's create Google collab so face recognition we will cover this face recognition topic into two videos so this video is part one so in part one we will create model and the model will classify human faces within the labels whether this phase is is belongs to me or belongs to another man and other people and in part two we will talk the same concept but in real time [Music] so let's rename it I mean so first of all we should connect our this Google collab to my Drive so let's connect in order to connect you should run this cell so let me to make it much more bigger 28 I guess is good so let's run again so I'm connecting it yes hello it's okay we connected to we're going to drive to this Google so first of all we should we should install empty model install empty CNN so in my drive if you go to face recognition folder in data set I have four persons four humans so Taylor Swift me Robert Downey and Jenna so let's select one image copy pass send now I will take that image and I will show you how empty CNN works and how we will use mtcnn manually and then we will create clouds which will do all these things automatically so before before diving into the coding so first we need to import all necessary libraries so first of all import CV2 SCV in Port OS importance of tensorflow stf then we will import multiple tip import multiple clip By plot sdlc and we will set OS dot Environ and while and TF CPP mean log level like this level equal to two you're not looking normal totally Pi plot here should be my plot so here here it is so we will read image by using cv.m read function we will give the location of file and s SCV at as opencv read images as gbr pgr sgbgr format Channel format and TLT matplotlib reads images as RGB Channel mode Channel format so here we read this image right if I want to visualize it into in the Mato clip it will be a little bit different image for example in Show image so if we do like this okay we should run this cell so here here as you can see this is that it reads RGB image each it reads RGB but our image is BGR so so we will convert it so in order to convert it we will use Siri functioncv.cvt series dot color and we will give you the image and we will give CB node to color from EGR to LGB we will convert it and it will show it so here's here's now the real image ah if you look to background the image initially is this kind of background has that kind of background if we for example if you choose maybe image let's use image five image five here is the image in RGB Channel format so now we read image right we import image now we should implement the empty CNN model in order to extract only face only my face as boundary box so we will in we will use mtcnn so from mtcnn empty CNN import Mt CNN to get it in and write the detector equal to MC CNN and so this effect also receives RGB file so our image is already converted to RGB channel so now we will give this image to this model so results equal to detector.detect image detect faces and we will leave our image here is tensorflow is running as a back end of this model so if we go to results results will show us many things first of all box boundary boxes then confidence level level and other things but we only need this box it's a corners of face so we will do next what we'll do next is that we will Define the laundry box from these results we'll get them so X Y reads height will be results so here you can see it is only one piece so only one output in one list is one output so result of zero and then we will Define we will need only box so yeah here it is so now we will draw rectangle in this image rectangle X and Y and second column will be X Plus weeks and Y plus height and then we will give color I will give blue color so this is also RGB that means that this is represents for green color this represents for blue color so red and green is zero there's no it will not make sense it's also only uh poor black dark dark dark blue so here it is so the model empty CNN detects my face is boundary so next step is that we will take from this image only that face set position that box so we will use image and we will give y till y plus right and then X 2 X Plus which I think it should work so my face who is this one and let's show it your team don't show my face so okay okay I'm doing some mistakes here so if I don't I think here's a mistake Maybe here's no white so drawing it should show okay PLC no team show so only my face and it's blue so here here it is but the next step is that we should resize it because face net takes as input 160 to 160 160 to 160 size of image so we will use not the CV function CB dot resize very common very kind of simple simple structure so again my face then we will give Target size 46160 and two so at that position here it is 160 to 160 160 160. so we have done all of these things so my face here is a array and what we need now what we are looking for now is that we should automate all of these things I am doing it by myself taking that image changing its Channel type giving to empty CNN and resizing it and taking that so I should use it and I should do this all of this thing automated so let's create a class let's close it so part two automate the the processing so let's create class so face loading so we'll create a function definitely so what I want from this class is that I will give only the ads directly and this class should automatically detect labels and put all images into X array and all the labels in the white array so as output as you see from this class X and Y to train the model the definition directory is also directly equal to directory and then self.target size equal to 160 to 160 self dot x equal to empty array let's make it first of all well Delta Y is equal to also empty of a so now we'll create another function let's do extract Place function extract fix so it will receive only a file name and based on file name it will extract you as a image image array so we will use so okay image will be CB dot image read file name so we will convert this image to CV dot CVT color with image and then CV dot color and then and then what we did so convert it and then we will Define we will take results so we will use this one the result equal to detectable detector maybe it's better to make Servo detector and give here the detector and DC and then again maybe it's better so here I will get results let's make it a little bit small maybe less X Y read height and we will assume that every in one image is one phase one little deck box so then we will extract no before doing that we will do another important thing is that X Y will be equal to Absolute of X and absolute of Y for debugging process then we will extract only that part so this will be then image y to y plus height and then X X Plus which is array so after that we will resize the space phase array then CV dot resize we will give face and self.target array basis so it will receive directory so this whole faces so what it will do is that it will receive only directly and based on that directory it will create X and Y so that for X it will go extract phase function for y it will be just just dedicate the name of that the class so here cases equal to empty list then for image name list there then we will give directory so we will so in the past it will be the directory the last image name and then single base will be self.extract these we will use his test so it will return it's array for single phase so what example will do is take that one and enter faces faces of the band single base so it's better to do it with try and accept oh accept exception SE will pass then returned basis so yes then the bus function it will be load classes I know maybe names or functions a little bit complicated or a little bit messy but that's what I came up so then we will check each subdirectory for for that for from the directory for classes in the directory so we will create four sub directory in OS dot list there we will give self.directory so for each subdirectory we will so let's make a pass so passive will be self.directory Plus sub zero directory Plus this one plus three plus another slash here's a path so we will give this pass to both faces so faces equal to self.load basis so we are giving this function to here and we will give as a directory this some directory some paths so for example subclass here is sardor this is supplies this is supplies this is a supplies so then faces and then we will take phases from here and decide at the same time after receiving the faces we will create labels it is sub directory name or any in the range of land basis so for each other is labels and then let's create a print function print load it successfully then we will give the sense of labels Etc so like that we don't give assign these faces 2 x 0. extend we will use extend it's it's much lower anyway it doesn't matter even support y don't extend labels so for each subdirectory it will run so for each other here it will take faces and its labels and save to X and Y which is here and then at the end at the end we will return this self Dot X as numpy and we load array then we dot s array and then in B dot as array for example y so here it is so okay okay I see this is a problem labels a function labels labels it is so let's take a sound then we will call this function to them we will call before calling before calling let's create another function another function it will be also useful F Dot Plot images total yourself and win it actually we don't need limits it's not necessary even so here number of columns we will do assign three number of rows will be based on the lens of self dot y divided by and columns so we have columns we have also n so we can create sub plus subplot so PLC dot subplot so the block and rows and columns and Index right so we'll create function four image in itself dot X no that's okay number Dimension itself in the rate we'll use any later it's also okay anyway index Plus here num plus one example and we will build the dot introduce all the image go to the game show we will give the image and we will turn off the access to show the image clearly so this function should also help so let's see if any mistakes or it also cures so then face loading this loading is flower plus let's push tab so here's face loading but we'll give directly let's copy this directory copy paste we will give you the directory and this loading Dot node classes so if you use this it will defrost as a return two things so let's define them x and y equal to them so now it's running for running is so it images if you look to images just wait so if you have to look images so here is images for Robert Downey these images for me so here is images for Taylor Swift and I use gender because she's now trendy mainly because of that so here's it's training examples images plot images I hope it is working yeah I know it's taking a lot of time in Ultra we will use another model we will use Cascade face detection model it works very fast but this model Works accurately much more accurately the face loading dot dot images we will give like this it should import images face loading images so it will show the images okay yeah I do it takes all the time now it's not it's done it's almost done okay NP is not defined oh oh I I thought it won't come by oh I forgot about number import number SMB so let's try it again again so here it is let's go it will show the images means that we automate all these things it will prove that so now it will be the part with face net so we will save this outputs from our problem our class for future users so anything or we will use any node uh how it was simply Dot all right for that mistake it was Envy dot saves saves compressed so we will use this so before you might face that let's import Face that face that clip install place in the camera Space Center here let's face that so it will import Face net then we will use from here is net import this net it's done okay it should be mistakes no must be one not 40. okay say that there's a mistake it's making itself like that but we'll do plus one to decrease that because it is limited because it only takes the integer maybe it's like I think you understand right if I take if this length is for example seven and if number forms three it would use a two but there's one inches left here so I would assume like okay we'll give one more extra so if you like this is that it shouldn't do yeah not 40 again number number maybe it's better to give two major I don't know why it's having a problem let's figure some plot number of columns another okay anyway choose images okay it's not clear I should fix it I should fix the size so PLC dot figure we will give 16 to 12 made it to work number comes three it's 40 40 divided by three it will be 13 15 1 14 14 39 51 Plus 140. so 13 14 13 3 it should work plus one it should work um I see I see I guess I should run I should re-run this this part and I should declare it again back but I'm not sure whether this will work or not so anyway let's do this running next time but now we will use our own function it's not self-dixable Justice if the X here and let's see if you'll see if you go no trouble big size equal to give it yourself hey now it's better right so here's images all of that images in the past here fix ice big size figure size 16.5 is enough okay maybe 18 to 16. maybe it's also better okay here's it so all images only faces so our class is working great so it takes images and it converts to okay it converts right here it converts to 16 to 16 and then it it put all of that things into X at least and into y with names respectively so here we will do part three phasenet so we will install facenet it's better to install Facebook rather than downloading from GitHub because I downloaded from GitHub too but when I downloaded and load that model this was not matching with requirements of the model so there's a big problem big headache so it's better to load from this Library so Keras face net import Face net so then we will create another function so yeah we will create functions functions functions because functions are better what makes life much more easier isn't it so we will declare that So Def get embedding so it will give us an image face image and what it will do is that it will take the face image and it will convert the face image type there's a two Floors 32 and then we will take that image we will add another dimension so if this image will be NP dot expand dimension of what phase image and X is equal to zero because and so forth requires four dimensional in our case this face net fixed image is three dimensional three-dimensional so it's 160 to 160 to three channels but we need four for the images it should be first one it doesn't matter now Maybe not maybe one and 160 160 and sweet it asks for from us to transitional dimension okay then we're going to use this image to the model then y hat equal to and better dot embed things we will take a face image and we will return the Y head only zero that is the first one we are assuming in all of these things that one image has only one picture only one picture so here the function is ready yes and now we will create one for Loop in order to put all of these images into the face net so embed that x equal to empty list so full image in X we will get embedding we'll call this function and give the image and as return we will receive the one-dimensional no actually we will receive 512 dimensional image so we will receive one dimensional to one dimensional and finally 5x12 512 dimensional image so very interesting very powerful in the model behind how is model if you're interested about how Facebooks I also looked backend how this phase networks it consists of many revolutional layers and at the end it gives us this 512 dimensional image this unique image and everything has the unit and every Facebook has this kind of unique image this is 500-dimensional array and basically what we'll do next is that we will compare this arrays to each other which is similar which is not single division that's all very powerful a little takes this one so what we will do is that we will and then we will convert this this list to numpy so equal to NP dot s array so we will go to arrays so what it's saying so multiply expand Dimension expand so maybe maybe expand Dimensions like this okay it's working it's training so actually if it's not working pretty fast done so now we will save this x embedded X and Y feature users users it will help I I it will definitely help in part two so we will use NP dot safe Z compressed function so we will give the name basis embeddings done four classes but npz and we will give the embed that X and Y should appear here executed in the interview here so it's here now so now part last part we will create simple CVM model to compare each these arrays 512 dimensional arrays to each other and then we will see how they are working so we will save it for future use now let's import so let's go from basically learn.model selection import train test speed so extreme so extreme white train from first extranez test and Y trains and Y test so we will call this function trying to splits and we will give that the X and Y we go to shuffling Shuffle you have to do that a random state just just in case why I'm talking about making Shuffle it's true because if you call this y you can see first all of them is Robert then it's in all of them it's me and then Jenna so it's when we will split this image Maybe that Taylor Swift all of the images of Tyler's field will be in the test split so that's a problem then model will not learn train on the images of Taylor Swift and it will work very poor on the test set so before before doing this one as you can see this we should use labeling dot model selection for the labeling coding right now from SQL downplay processing obviously I make mistake with processing import label encoding label encoder so in the coder equal to label encoder encoder dot fit why encoder Dot bit and transfer transform why so our why now is numerical we will give it so we will Shuffle it so if it's good now it's good so we will we'll split it now we're looking for CBM from SK loan Dot screen dot CDM import CBC and then we will create a model so model equal to CVC and then kernel is linear and then probability is true exactly true and then we will feed the model the X strain then y train activities and we'll predict why let's train so model Dot predict so we will predict on big strand first and then y press test model dot predict Visa analytics test we know so from sqlarn.metrix we will import accuracy Suzanne we will compare accuracy so accuracy is called we will give first Y2 so it is y trained and then we will give y press string let's see accuracy so 100 accuracy very good it works perfectly so this net is working very good absolutely good so score why test if you will put white test and we will remove the white price test let's see here how it's working actually here accuracy score here's also it's working it's showing 100 accuracy so now we are using here three models first model is our we are in first model we are using this empty CNN and as a second model we are using face net and as a third model the simple model to classify we will using CVM so now let's put Custom Image so let's try let's try let me do with my image so excuse me first image right no it's intense okay it's in try and set I will import from here and drive my drive basic recognition here's my image okay guys so the image the image will be CV dot image read I will read that image then T image will be CV Dot CVT color T image and then we will give CV dot color CB dot color PDR to RGB so it will give the uh it will return the RGB RGB image and then we will give this image to our detector detector the detect faces the image so here will be we will receive fixed y with height we will only use box output let's see yes it's training so it's done so T image now we will take only the face so te will be the Y to Y plus height and the X to X Plus which and then we will resize it CB dot resize the image and Google is 160 160. and we will give this to function get embedding image best image let's see if it's working or not okay okay a simple mistake so let's check test image shape just in case let's say so we should add one one more Dimension here so the simple we will do simple thing this image test image dot shape okay okay the snitch okay now it's two dimensional array yes I think yes it's better to have this thing it's embedding then we will put this thing into the model so why Reds will be modeled to predict the test image okay a bunch of times because of that okay it's having trouble I make I made the same thing so let's run again for this cell but it's happening the kind of problems happens we drain it so if I will run it it will say error because we need two dimensions so we will use this image will be this image now it's done here's why Prince so here's the right tool what is the right two means so we'll use so that what we use as a encoder so encoder equal to inverse transfer we will give white rats and it really turns output so here it is it's showing me example so simply this tutorial is finishing the ending up so thank you very much for watching so you can try by collecting your own data sets and training on doing the same Concepts in your environment and it's not too much difficult and everyone can do it just follow these steps and that's all in in part two video we will talk about doing the same thing the same automations the same taking images from video from Real Time and from real videos and transferring and predicting the human presence per person so here what is disadvantage what is the problem here in this code this big issue is that our mtcnn model works very slow very very slow it takes a lot of time and this is not good solution for real time because in real time code should work very fast at the same time maybe maybe yeah maybe one one second delay is okay but this empty CNN model Works slow we will replace it with another model so anyway so face net is good very good well designed it will uh well structured model so thank you for watching I hope you enjoyed this video if you like it just press the thumb up and don't forget to subscribe and share and write in comments how do you like the videos and this video and what what do you want to recommend to me to improve the videos so guys bye [Music]
Info
Channel: Sardor Abdirayimov
Views: 17,419
Rating: undefined out of 5
Keywords: facenet, mtcnn, python, tensorflow, face recognition, google colaboratory
Id: bG2tNYs7gw8
Channel Id: undefined
Length: 35min 53sec (2153 seconds)
Published: Tue Dec 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.