Placeholder Image

字幕列表 影片播放

  • you tired of going to watch your favorite show with one of your friends, but they don't know who any of the characters are, so they completely ruin it by asking you, Who's that?

  • Who's that?

  • Oh, who are they the entire time will?

  • No longer will that be a problem, because in today's video, we're gonna look at creating a facial recognition app where you can upload any photos you want, and it will tell you exactly who's in that photo.

  • Let's get started.

  • Now Let's take a quick look at the project we're going to build.

  • All we need to do is select an image here, and we just take any imagery one.

  • And then it's going to find all the faces in that image and give us the name of all the people for the faces that it recognizes to get started.

  • Doing this already have quite a few files in our project.

  • As you can see, I have a folder here with labeled images.

  • This is just facial images of all the different characters that were going to be able to put labels to their names.

  • So we have black widow, Captain America and so on and these are just face shots of all these different people.

  • You can find these yourself of you want, or you can find them on the get hub that I have linked in the description below.

  • And then also, besides these label images, I have the various different models that we're going to need it.

  • This is what our AI ai network is going to use to determine people's faces as well as recognize their faces from these images.

  • And these again are going to be in my get hungry bubbling below, so you don't have to go worry about finding them yourself.

  • Then, lastly, we did have a couple test images here.

  • He's just the various images that we're going to use that have these people's faces in them and again there in the rebuilding to below.

  • Lastly, I have the face a P I minimized library here, which we're going to be using to do the face recognition inside of this project, and all we need to do is add a new file here, which going to be our index dot html.

  • This is going to be for rendering our page, and we're also going to have a script dot Js, which is going to be all of our custom code that we're writing for recognizing these faces.

  • No, to get started, had set up our index dot html.

  • We could just exclamation point enter using Emmett Envious Code and that automatically generate this template for us.

  • And instead of here, we can just say face recognition because that's what we're going to be building and inside the body, it's going to be super straightforward.

  • We just need a single input, which is going to be a type of file, and we just want to give it an I.

  • D.

  • So we can reference this in our JavaScript.

  • We're just gonna say image upload, and then I'm going to use a few styles here inside of the body are inside of the head tag because I just want to be able to style our page that everything is centered.

  • And to do that, we'll just select the body.

  • We're going to get rid of all the margin, all the padding.

  • We want to make the with here equal to 100 view widths.

  • So it's going to take up the entire with, and we want to make the height equal to 100 view height, which again will make a take up the entire height.

  • We want to use display flex combined with justify content center in the line items center and this is just going to send our content horizontally and vertically.

  • Then lastly, we're just gonna use flex direction and we're going to make this column and this way are different elements stacked on top of each other.

  • As you can see, our selector and our image are stacked on top of each other instead of side by side.

  • Now, lastly, we're going to be using a canvas element to draw these different rectangles around the images of our characters.

  • So we need to absolutely position this so it goes right over top of our different image so we could just make this here a position of absolute make sure I spell that correctly.

  • Here we go, and now we can jump over into the Java script which is going to be were the really interesting part of this project come into play.

  • And if you haven't already seen my facial detection video, which uses the same exact library, I highly recommend you check that out Now, if you're certain parts of what we're doing here are what we covered in that first video.

  • And I don't want to cover them as in depth in this video.

  • So make sure you check out first if you haven't already.

  • Now moving on.

  • What we want to do is first get our image element.

  • So we get our image upload, there's going to be equal to document Don't get element by i d.

  • And we just want to get the i.

  • D.

  • Here of image upload, which is the idea that we gave our element.

  • And this is so that we can get the files that were uploading.

  • Next, we want to load the different models that were going to be using and the various models that were going to be using.

  • We're going to use face a p i dot nets dot and then in here we put the library name that we're using, so we're gonna use face recognition because we want to build to recognize the faces, So face recognition net and we want to just load this so load from you or I and we're loading this from our models folder, so we just pass it in here, a path to our model's full day.

  • We need to do this three times because we're using three different libraries here.

  • We also have faced Landmark 68 net and essentially, this allows our algorithm here.

  • It's a detect where the actual characters faces are so we can use the recognition to find out which faces which.

  • And then lastly, we need to use SST mobile net V one and essentially, what this is going to do is it allows our algorithm to determine which ones their faces.

  • This is just our detection algorithm right here, and we want to do all these asynchronous early so we could just say promise dot all because these all return promises throw them inside of here, make sure we put comments at the end of them because this is going to be an array of elements.

  • And then in here, when we finish loading all of these, we want to call a method which word is going to call start.

  • So let's create that function down here.

  • Start just like that in here is going to be all the code that we run after we load all of our different models.

  • And for now, the only thing we're gonna do in this start method that we're just gonna take our document body.

  • We just want to upend a single element here, which is just going to say, loaded.

  • This is just so we know when our pages finished loading all these different models and now what's actually run this using live server.

  • So we'll open this up here off to the side and you'll see we got our upload image right here for choosing our file and you notice are loaded.

  • Text is not showing up.

  • And that's because we had to make sure we include are different libraries inside of our index, that HTML so we can get our script tag here.

  • We want to make sure we defer it so it loads after our body is loaded.

  • We want a first load, our face, a p I library.

  • And then after that, we want to load our script tag right there.

  • What saved that?

  • And now we should see over here.

  • We see loaded pops up as soon as it's done, loading everything and luckily, loading those models is actually fairly quick.

  • Now back in their script, of Js what's actually configured this start so we can start recognizing faces using face detection in order to get started doing that.

  • The first thing we need to do is take our image upload here, and we want to add an event listener for whenever we are changing our element.

  • So whenever we actually select an image, we want to call the code in here.

  • We're gonna make sure that this is an asynchronous function because we're going to do a bunch of a circus code using the face a p I library inside of this function.

  • Now, as soon as our image is loaded, the first thing we want to do is get our image.

  • So we're going to create a variable here were to say, image and it's going to be equal to our face.

  • A p I will face a p i dot buffer to image and we're gonna take the image upload.

  • We want to get the files from it, and we just wanna get the 1st 1 since we're only ever uploading one file and we want to make sure we await this because it's asynchronous and essentially what this is going to do is it's going to take the file that we uploaded and convert it to an actual image element which is going to be usable with the face a P I library.

  • Next, we can use that to get our detection, Sze so we can create a variable here called detections again, We're going to await our face a p I.

  • And what we want to do is we want to detect all faces just like this and we pass in the image we want to detect the faces from, which is the image that we uploaded.

  • Then we want to do is we want to make sure that we're getting this with face landmarks and this is going to allow our algorithm to determine where the different faces are.

  • And we also want to get it with face descriptors, which is going to allow us to draw the boxes in such around our various faces of our characters.

  • And now, to make sure this is working, we could just take the document moves document dot body dot upend.

  • We just want a panda detections and dot length to see how many different detections we have now let's save this and as soon as it gets loaded over here.

  • What?

  • Select an image, for example, this one right here and it's going to upload that image and you can see it found six different faces inside that image.

  • But one thing you will notice is we're not actually displaying the image that the user's selected, so let's work on doing that now.

  • This is really easy to do.

  • Actually, we need to do is in here.

  • We want hopes document dot Upend our image.

  • Make sure we do that on the body.

  • And now, if we save this, wait for it to load, select an image, you'll see that the very first thing it's gonna do after gets that image from the buffer to image is going to actually upload that image here so that we can see it.

  • And if we scroll down, you see that says they're six faces and if we look at this, we have 12345 six different faces in this image, so we know that this is working perfectly fine.

  • Next, let's actually work on drawing rectangles around the different faces of our character, and the first thing we need to do that is to actually overlay canvas on top of this image.

  • And to do that, we need to put them inside of a position relative element.

  • So it was to do that now, which created container.

  • We're gonna call it container.

  • We want to set that equal to document dark lips, the document that create element.

  • And we just wanna create a simple div.

  • We also want to take the style of this container, and we want to make sure we take the position that we want to set this to relative that we can absolutely position our container inside of this.

  • And now, instead of actually a pending here are loaded.

  • We could just copy this and we can upend our container as well to our body just like this.

  • And now we're going to have that extra div being added to the body over element as well as the text loaded Now, with that container being added, let's work on creating our canvas and adding that as well.

  • This is going to be really easy.

  • We could just do constant canvas here.

  • We want to set that equal to our face.

  • A p I got creed canvas from media just like this and we're going to pass in the image and this is going to credit canvas from the image that we passed to it.

  • We also want to make sure every size this canvas to the crack size.

  • So first, what's at it?

  • We're just going to say document dot body dot upend And we want upend our canvas here and actually instead of opinion to the body, what we want to do is we make sure that pendant to that container.

  • So we want a pen, both our image and our canvas to that container.

  • So that way, they're both absolutely positioned on top of each other perfectly inside this relative container here.

  • And then the next thing we need to do is you want to get the display size of our different image right here.

  • So we're just going to say our with is going to be equal to image dot with, and we're gonna say or height is equal to image dot height.

  • Make sure I spell this properly just like that.

  • And now we can resize our canvas.

  • So to be that we could just say face a p i dot match spell that properly match dimensions and we just want to pass them here Our canvas element as well as our display size display size.

  • Just like that.

  • Every go on that's actually going to resize our canvas to be the exact same size as our image, which is perfect.

  • Now we can display these detections inside of our canvas.

  • Now we can display these detections to the user.

  • So let's remove this count.

  • Since we no longer want to display this and instead what we want to do is we want to Resize are detections.

  • So we're gonna create a variable here called resized detections.

  • We want to set this equal to our face a p i dot resize results just like this and we're gonna pass in our detections on where this is going to do is going to resize all our boxes for our detection to be the correct size based on the size that we pass it and in our case, we're going to just pass it.

  • Here are display size.

  • There we go, our detection Zehr now going to be scaled to our image to fit in our canvas properly.

  • Now, in order to draw that actual box we want to do is you just want to create a variable which we're gonna call a drop box.

  • We're gonna set this equal to a new face a p i books face a p i dot draw dot draw box and this is going to allow us to draw our box onto the screen, and we first want to pass it the box, which in our case, is just going to be from our resized detections.

  • So we're gonna use a loop here we're going to say we're gonna do for each one of these detections detections.

  • There we go.

  • So for each one of our detection is we want to draw a box and are boxed here.

  • Loves concept box is just going to come from our detection detection dot detection dot box.

  • This is going to be the box of the face that our algorithm determines for our individual faces in our image.

  • And we're gonna first pass that Tar dropbox, and then we're just going to put a label on it as well.

  • So we're going to say label and for now, we're just gonna place hold this as face.

  • But from here, this is where we're gonna put the character's name later when we work on face recognition.

  • And now we just want to draw that box so we could just say draw box dot Draw only.

  • Pass it the canvas we want to draw on to.

  • Now it's saved that way for two.

  • Load over here Click Choose file.

  • We're gonna choose this pile, and as soon as everything son loading, that should pop up and we should see boxes around all of our different faces, and you will notice that instead of her boxes being shown up over their faces, they're showing up all the way over here on the right.

  • And that's because when we position our element absolutely in our index here, we need to tell it.

  • We want it positioned with the top zero and the left of zero.

  • So that is going to show up properly.

  • Overlay over image.

  • Now again, let's go back here.

  • It's loaded.

  • Select our image way for it to do its final loading and processing, and as soon as that's done, it will pop up, and then we should see our face rectangle show over top of the faces of all the different characters with the label face.

  • As you can see, it's done that perfectly.

  • So now let's actually move on to the recognition portion where we can put the different names of these different characters instead of just the generic text face.

  • In order to do that, we need to parse all the different labeled images that we have here in this labeled images folder.

  • So to do that, we're gonna create a simple function.

  • Just put it all the way down here.

  • We're gonna give this function here a name of load labeled images.

  • Make sure you spell that correctly that we go and it's just not gonna take any parameters.

  • And inside of here, we're going to do all the loading for images.

  • And if we open up over here are labeled images.

  • The first thing we need to do is to get a name of all the different labels that we have, and this is just going to be truly straightforward.

  • We have Black Widow.

  • These are just the names of the folders that we have.

  • We have Captain Captain America.

  • We have Captain Marvel Hawkeye.

  • Let's make sure that all the capitalization is correct for all these Jim Rhodes and Thor.

  • And lastly, we got Tony Stark, and you will notice that I don't have all the different Marvel characters in here.

  • This is just a demonstration of just a few of the characters so that we don't have to wait here for hours for me typing on all their names.

  • And the next thing that we need to do is run a creative promise.

  • So we're gonna say return promise dot All because we're gonna create a bunch of different promises toe load all of these different images and detective faces in them.

  • So we want to do is inside this promises dot All we want to go through all of our different labels.

  • So we're just gonna say labels dot map This is going to go through all of our labels and return an array for us.

  • We want to make sure this isn't a sinking dysfunction, and each one of these is going to have our label.

  • So there we go.

  • Now we have a function inside here with all of our individual labels.

  • So Black widow, Captain America, and so on and inside of here, we want to create a simple four live and this four live is just going to go from 1 to 0.

  • So we're going to say I equals one.

  • We're gonna go.

  • I is less than or equal to two.

  • I course plus.

  • And the reason for this for Luke is because I have two images of every single character you can create more images or less images.

  • But the more you have, the more accurate the face recognition is going to be.

  • So now that we have this loop inside of here, what we want to do is actually lowered our image.

  • So we're just going to say constantly, JJ is going to be equal to awaiting the face a p i again.

  • But this time it's going to be calling a method called fetch image.

  • And in here looks just best image.

  • And in here we're going to pass our image text for the path to where images at.

  • But one thing to note is that live server doesn't actually serve the images inside of our folder.

  • So we can't just do something like slash labeled images, slash name and so on.

  • Make sure need something hosted on a website.

  • And luckily I have all these hosted on my get hub so we can use this.

  • You're all here, which essentially is just https slash slash rod I get hub user content dot com slash would've simplified slash the name of the repo, which is face recognition JavaScript, the master branch and labeled images.

  • And inside here is going to be exactly the same as this folder that you can see on the left here.

  • If you want to use your own folders and labels, they're gonna have to upload them somewhere on the Internet that your computer can access them using this fetch image and out.

  • The next thing we need to do is actually put the label.

  • So we're just going in here.

  • Put label is going to be the name of the full there we want to access.

  • And then we need to know which one of these folder images we want to access, which in our case, is either gonna be one or two based on this I variable and all of these air J pegs.

  • So we can just say dot j paid at the end here.

  • Now, this is going to load all of our different images into these different variables on we want to do is we want to actually detect the faces in those images so we can just say protections here is going to be equal to await face a p i dot detect a single face because we just have one face in all these different images and we're going to pass in the image.

  • And again, we want to do this with face landmarks and we want to do the face detection or with face descriptors that we go and that once we have all those detections, we want to add them to an array.

  • So to create that every but is gonna call this here descriptions were gonna initialize it to an empty array.

  • And then down here, we can take that descriptions that we want to push in our detections dot descriptor descriptor just like that.

  • And this is going to be what describes the face that was detected inside each one of our individual images.

  • And now, once we're breaking out of this loop right here afterwards, which want to return here a new face, a p i dot labeled face descriptor what's descriptors.

  • And in here we're going to pass it a label as well as the different descriptions that we created two for each one of our different images.

  • And then this.

  • All of this is going to return all of these labels inside of a bunch of different promises.

  • So when we use this, we need to make sure we're doing a synchronously.

  • So this is going to be in a sink function now, first start and what we want to do right before we do our loading, as we wantto created variable here, which is going to be called labeled descriptors.

  • Next from here, we can create what's called a face matter.

  • So it would say face match er, we're going to set that equal to hear a new face.

  • A p i a dot face match er and inside of here helps make sure this is capitalized.

  • And an inside of this face smasher we wanna pass in first are labeled face descriptors as well as the percentage value we need in order to recognizes as a face.

  • So 60% means that the algorithm is 60% sure that this is who that character is.

  • So as long as the values above 60% we say OK, that's fine.

  • We're going to accept that this is who the character is and 60% isn't pretty good value here to use.

  • Now, what we want to do is we want to use this face matter down here with our results in order to actually display the character's name.

  • So to do that, we're going to create a new variable called results.

  • We're gonna set that equal to our resized detections dot math.

  • We're gonna change the detection.

  • So which continues d for detections and we use that face muncher here and we want to find the best results.

  • So find the best match on we wantto find the best match from the D dot descriptor and where this is going to do is is going to go through all the images that we loaded, and it's going to find the best match that is above 60%.

  • So is there's none above 60%.

  • It's going to return nothing.

  • But as long as we have at least 1/60 percent is going to choose the highest percentage value, that's the closest.

  • So then that's going to give us all their results here.

  • So now we can come down here and loop over our different results instead.

  • And inside of here, we're going to have our result as well as the index of that result, because we need that access, our resize detections.

  • And then we can change.

  • This here is going to be our resized detections of the index of I.

  • And this is just because we no longer have access to that single detection because we're inside of a different loot.

  • And now our label found here is just going to be result dot to string just like that.

  • And now if we save this and we wait for it to load and like I said, this is going to take significantly longer now because we have to load all these different labels.

  • But as soon as all these revolted once, So as soon as your page loads that initial time, every subsequent image that you tried to recognize faces in is going to be quick.

  • And you don't have to do this loading every single time that you do an image recognition and you will notice that it doesn't actually load.

  • And that's because down here we called with face descriptors and a shooter's be with face descriptor.

  • So I've saved that.

  • So it's going to actually start reloading.

  • And again, it's going to take a little bit of time for this flow to texture show because it has two parts, all the different loaded images that we have inside of our folder.

  • And there you go.

  • We see it just finished floating.

  • So let's choose that same number one image that we've been using and you can see that it recognized.

  • Or it doesn't recognize Iron Man because we don't have any images for him.

  • Recognizes Captain America, Black widow, Hawkeye.

  • And, of course, it doesn't recognize hope because we don't have any images for him either.

  • And now, if we wanted to change images here to a different image, it you're gonna notice one thing is wrong.

  • It doesn't actually remove our image, and it shows are things over top of each other.

  • So we need to actually clean up our code every single time.

  • We change images that removing the old ones.

  • Luckily, this is actually really easy to do.

  • What we need to do is take our image and canvas variable and pull them outside, so we're just going to say, Let image and we're gonna say what canvas is just going to create two undefined variables of image in canvas.

  • And then the very first thing we need to do is try to delete them.

  • So what we want to do, because we just want to come up here, say image dot remove image dot removed and is going to remove the image.

  • But we only want to do this if the image actually exists.

  • So it is going to say if image and do the exact same thing with our canvas.

  • So if the campus exist, we want to remove it.

  • Now let's save that and wait for it to load again.

  • And now that's done loading what's open up our initial image that we've always been using.

  • You can see that that works fine.

  • And now, if we change it to, for example, this image you'll see all the old stuff gets deleted and the new stuff gets propagated on to hear perfectly.

  • And that's all it takes to do really simple face recognition.

  • If you're interested in my face detection video and I haven't already seen it, make sure to check that out linked over here also subscribe to the channel for more content just like this.

  • Thank you very much for watching and have a good day.

you tired of going to watch your favorite show with one of your friends, but they don't know who any of the characters are, so they completely ruin it by asking you, Who's that?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

使用JavaScript的簡易人臉識別教程 (Easy Face Recognition Tutorial With JavaScript)

  • 6 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字