Placeholder Image

字幕列表 影片播放

  • what's going on, everybody.

  • And welcome to a tutorial on facial recognition with the face recognition package.

  • So to get started, you should be able to just do a pip install face recognition, but depending on your operating system, depending on whether or not you want to use Cuda and so forth, there's many different paths that you could take for the installation.

  • So check out the textbook version of this tutorial.

  • If Pip install face recognition just doesn't do it for you.

  • So with that, what I want to do is basically just to kind of an overview of the package and how to use it.

  • The package itself is actually extremely simple.

  • And there's really, like, three calls you need to make to the package.

  • The rest is just boilerplate kind of stuff that you need to do around the code team, make use of it.

  • So to get started, basically, I'm going to be working in the following directory.

  • So this is the code that I'm gonna be working on just as a reminder.

  • Never name your code the same thing as the package you're gonna be using.

  • You don't call this face recognition dot pie, then we're gonna have to directories.

  • One directory is known faces, and inside of there is more directories.

  • Uh, in this case, we just have one.

  • It's called Syntex.

  • Now each directory in the known Faces directory is goingto have basically a directory of an identity that you wish to identify.

  • So just as a reminder, face detection is just finding a face right in imagery or videos or whatever.

  • Facial recognition is identifying that face.

  • So face recognition goes a step further the text face, and then it recognizes that face tells you who that person is or whatever.

  • So inside of this directory, Centex, we've got three images.

  • None of these images, the greatest in the world.

  • But you can see here we've got this one.

  • This one's kind of goofy because it has obviously got that e g headset on.

  • But the face itself is actually a pretty good face.

  • And then this one is not very good.

  • The eyes are covered up in all that.

  • So not the greatest images, but kind of it'll get the job done.

  • So, uh, you can do whatever you want.

  • All these images are on the text based version of Detroit You can use my images if you want, but I really encourage you to use your own images, grab some images online or whatever, do whatever you intended to do it face facial recognition.

  • So those are the known faces that I have.

  • And then there's also a directory of unknown faces.

  • In this case, we don't know the identities.

  • So it's just a bunch of images, both of me and of Daniel.

  • Uh, and the idea is to see, can we recognize her own face?

  • This one's an interesting one, because it is the deep fake of Snowden face on my face.

  • Hey, wait.

  • Um Okay, so this is also some depicts, so this will be very interesting to see which ones that detects or or non or what?

  • Uh, anyway, so that's how everything set up.

  • You've got the face recognition directory that you're gonna work and you've got known faces, identities and the images of each of those identities.

  • They've unknown faces, which is just a bunch of images that might have many phases.

  • So in this case, we only had all these images.

  • There's actually one person, although there is like in this one, there's you know there's a face back here, the back of a person's head, another face here and so on.

  • So I mean, there can be multiple faces, but most of these just have one face.

  • I guess this has a lot of faces as well.

  • Anyway, it doesn't have to be all like the unknown faces don't really need a single identity, But for known faces, he's really need to be just one face.

  • That is the identity of whoever this directory is.

  • Okay, So once you've got those things set up field for deposit, if you need a second to set everything up, otherwise we can get started.

  • So, um, you just move some stuff around, so the first thing we're going to use right it makes it imports and then set some constants.

  • So our import face recognition, That's the package that we intend to use.

  • Reimport s our import CV to.

  • So you'd also have to pick and stole python dash open CV.

  • I always forget which one it is.

  • Uh, it was stolen.

  • I think it's It's either open C v dash.

  • So must be open C v dash part.

  • So, uh, stole it on as probably it.

  • Uh, I guess I upgraded, but that probably just broke the whole tutorial.

  • Uh, my life.

  • Okay.

  • So continuing long once we have those things.

  • So, yeah, O esto iterated over directories CV to to do image tests.

  • We're gonna use CV to draw rectangles and label the images and stuff like that.

  • So now we're going to have a known faces, dirt, and that will be known faces.

  • And then we will have an unknown faces.

  • Er so unknown faces.

  • You're unknown faces.

  • They were gonna have a tolerance.

  • Now, this is a degree of, um, try another word besides tolerance.

  • But the the lower the tolerance, the less chance we're gonna have for, like, false positives.

  • So if you said are very, very low tolerance, But you only have, like, one or two pictures of the identity.

  • Chances are it's never gonna make a match.

  • So the default with face recognition of 0.6 if you want to make more matches, but you're willing to risk being incorrect on the match that you make, you can set tolerance to be like 1.0, very high.

  • But if you want a Onley match, if it's a freak.

  • A for sure thing, said the tolerance to be quite low.

  • I have found 0.6 to be pretty good, but as we'll see, we'll probably have issues with that, both missing predictions and then as we try to lower, it's quite challenging.

  • Ideally, you would have, like 100 images off, you know, somebody's face in different lighting conditions and looking up in every all directions and stuff like that.

  • But it's actually it's still pretty darn good.

  • Even if you have, like what I just used here some pretty crummy images.

  • So Okay, so that's tolerance again.

  • The higher you go, the more likely you are to make matches, but you're likely to get a lot of false positives.

  • The lower you go, the more likely you are to get.

  • I guess you would call false negatives, right?

  • So you're gonna mismatches.

  • So now, uh, runs a frame underscore thickness.

  • This will be in pixel values.

  • Basically, we're gonna draw square around the head.

  • How many pixels you want them to be?

  • Just really just good.

  • That's gonna depend on how big of an image or using in our case will go to.

  • I'm gonna use three.

  • Uh, And then we'll do font thickness that will also said that to be, too.

  • And then model I'm gonna use CNN.

  • So convolution a ll neural network.

  • You can also use hog, which is I was once a hierarchical, um, hissed a gram something Radiance.

  • Mr.

  • Graham oriented radiance.

  • I don't know, something like that.

  • Anyway, it doesn't matter.

  • What it stands for is like kind of the older way of doing object detection.

  • So you can use either one you want.

  • I suspect hog is gonna run better on the CPU.

  • So if you're only on a CPU, you didn't install the Cuda de Lib, Then I might suggest that you use hog instead.

  • That's quite literally H o G, um, or feel free to take it with both.

  • CNN will still run on the CPU.

  • It just might be kind of slow.

  • So, um okay, so once we've got that, the next thing we're ready to do is start loading and faces.

  • So, to do image facial recognition, we need to be able to do kind of two things, right?

  • We're gonna detect the face and then for every unknown face that we happen to find we need something to compare it to.

  • So the first step that we're gonna do is load in all of our known faces, Um, so that we have something for the unknown faces to compare to.

  • So, uh, well, come down here and we will just Frank's just debugging information loading known faces.

  • And basically, we're gonna have to list.

  • We're gonna say known faces.

  • These will be the actual encoded faces.

  • You'll know what I'm saying.

  • Minute and then known names.

  • These will be the name's associate ID with those faces.

  • So as we populate this out later, when we go to compare the unknown faces, we'll see.

  • Does the encoded unknown faces match any of the encoded known faces?

  • If so, at what index value did we find one?

  • And then we will just reference the known names list at that exact same index.

  • And that's boom.

  • The identity.

  • So so known faces no names.

  • And now we want to do is generate over all the new faces and store that information.

  • So four name in os dot list dir uh, known faces dirt.

  • So that gives us the name.

  • Now, remember, that was the identity directory.

  • Now we need to generate over all the images in that directory.

  • So we're gonna say four books or file name in OS not list dir.

  • And we will say known faces dir on, then slash name.

  • And this also needs to be Ah, variable.

  • Cool.

  • You also could use the West I path.

  • Thought joined.

  • Um, I haven't really decided which one I like better is using, like, f strings for pats or os dot path.

  • Not join.

  • I mean, it's like it's meant for this, but unless I've had the joint came before, we could do really nice f strings like this.

  • So I find this to be quite legible.

  • So anyway, if anybody knows a good reason why not to use their strings for pads, let me know.

  • Um, okay, So once we load in that file name, uh, we want to find him.

  • We're gonna say image equals.

  • So this is the first call.

  • That's a unique face recognition call face recognition dot face, um, underscore and coatings.

  • And we want to do this for the actual image.

  • Ah, that we bring in and we have brought in the image.

  • So So?

  • Actually, we want to say is, uh, So really, this will be our encoding wth e image is going to be face recognition.

  • Don't, uh, load image file.

  • And then here Here's what we're gonna do.

  • We'll do another extreme, uh, known faces.

  • Your face is sure, um, name and then the file name.

  • Funny.

  • So then we load the image with facial recognition.

  • We encode that image, and then we're gonna add these tour lists.

  • We're gonna say known faces dot upend the encoding, and then known names got upend the, uh, name.

  • Cool.

  • Okay, so now that we've got that, uh oh.

  • And also, when we do facing coatings, this is going to encode all of the faces that it finds.

  • So, um, what we actually want to do is we're gonna say the encoding will be at the zero with index, so we only want the first face it finds so again, your identity images.

  • So these ones here really ought to just have one face, and it should be your identity, right?

  • So every you should only have one face in these images.

  • Each of those faces really needs to be, um, the identity here.

  • I mean, it should be pretty obvious, but in case that's not obvious.

  • Um, there you go.

  • So, uh, so when we encode the images, it's gonna find all of the faces unless you specify otherwise, which we'll show you in a little bit.

  • Um, so we're gonna say at the index zero.

  • That's the face that we're interested in.

  • So So now we want to dio is so we've got the known faces so we can compare to them.

  • Now we want to do is want literate over all of the unknown images, find all of the faces and then for each face, compare that face to each of the known faces.

  • So okay, lots of lots of looping about to occur.

  • So now we're gonna say is print.

  • Uh, process is unknown, Uh, and cool.

  • So what we're gonna do is we need Thio iterated over.

  • First the Unknown faces directory.

  • So four file name in os dot list Duerr unknown faces dirt and again, we don't have identities, so we don't need to generate over those directories.

  • But instead, what we're going to say is print, huh?

  • I swear it Hit the caps lock, print, file name.

  • Just so we know where we are now we want to do is load in that file name.

  • So we're gonna image equals face recognition dot load, image file and we want to load in on that path.

  • So again, f string, we're going to say unknown faces dir slash uh, filing lips.

  • Don't forget this file name.

  • Cool.

  • So we're loading in that file.

  • So we've got the image now we want to do is we want to actually find, um, all of the faces inside of that image.

  • Now, this is not something we did before because we're kind of just assuming we already know there's only gonna be one image and so on, but in this case, for us A locations and what this is going to do is do face detection for us, find all of all of the locations for faces, and it's just gonna find the coordinates.

  • So I say locations equals and that's going to be a face recognition dot face, um, under space, um, score locations and it's gonna look for those locations in the image.

  • And then the model that we're going to use is going to be a model, so, uh, Okay, cool.

  • So now that we have all of the locations that we have.

  • We're ready to make the encoding sze so in and coatings will be evil.

  • Do you face recognition dot face in coatings And we will do this for image and the locations.

  • So we're gonna actually specify where are these locations that we wish to encode?

  • Um, And it again, we just We didn't do the locations thing up here for the known faces, because it's irrelevant.

  • We don't care where the known faces are in their respective images.

  • It's just not important.

  • But we definitely care where the unknown faces are, so that later we can go in and label them so Okay, so, uh, encoding.

  • Okay, So once we've done that, we are ready to start drawing.

  • Well, we're illiterate.

  • Try to make matches, but we want to be able to draw on the image.

  • So first, we're gonna convert this image so we can use it with open CV.

  • So we're gonna say image equals CV to dot And what we're looking for his convert color.

  • Yes.

  • Convert color.

  • Um, and we are going todo this on the image, and then we're going to see the two dot um color R g s e r g shoot.

  • I think we're looking for our g b to B g R um so color underscore rgb to be ji article.

  • So also, just as a side note, uh, this video is sponsored in part by kite, which is a complete engine four python by far the best auto complete engine.

  • Both It's learning as your typing things here till it kind of give you suggestions on your auto complete also has, like snippets for, like functions, especially really well known functions like when I'm working with, like, tensorflow and care.

  • Ross.

  • It's amazing for like like when you do like fit mints and trainings and stuff like that, it helps you just, like write those lines super fast for something like face recognition.

  • I don't really know that it knows too much about face recognition, but still, it's been helping a ton on some of these auto completes.

  • And at least for me, I hate most auto complete engines.

  • I think they're just trash.

  • I'm usually like working around the auto complete rather than getting any benefit at all from it.

  • So I actually really like Kite and then also just as a side note as your coating it Use this kite copepods thing.

  • It's basically like a rolling documentation.

  • So whatever the last thing is that you were working on you like basically, wherever your cursor is, it's just constantly updating with the documentation.

  • So if you kind of forget, like, what are these things, or how do I use this thing or whatever?

  • It's just it's already there.

  • I spend so much time like having to Google like a specific function or like converting the color.

  • I don't have that memorized.

  • I don't have like I couldn't remember.

  • It was a color on her score.

  • I can't remember, you know?

  • So it's just really convenient to have that stuff they're so kites like, totally free to use works on pretty much every major editor.

  • Um, I definitely suggest you check it out.

  • I've really been enjoying using it myself anyway, So, uh, okay, continuing along.

  • So now we've got the image we've converted it to be gr because BTR is what open TV thinks is best.

  • I don't know.

  • So, uh okay, so we've got our image.

  • Now we're ready to dio is iterated over the encoding tze and locations for the unknown faces that are found.

  • So what we're going to say is, uh, come down.

  • One more line four face under scorer in coding, and then face underscore location, um, in zip in coatings locations.

  • So for each of those, what we want to do is first we want to see Are there any matches that we confined?

  • So here comes what I believe to be the third and final line off the face recognition package, and the rest of this stuff is just like like I said, just code to do all the other things that you want to dio were say results equals face underscore recognition.

  • Don't compare faces, compare faces.

  • And then we're gonna compare known faces to, uh, the face encoding at the moment.

  • So we're basically this will be the list of known faces that you have.

  • This is the thing that you're trying to compare to see.

  • Are there any matches?

  • Uh, and then, at what tolerance do you want to use again?

  • The default tolerance is 0.6.

  • That's what we're gonna go with to start.

  • But I'll show you guys some variances as well.

  • So now what results are gonna be is it's gonna compare the current encoding to every single face that we know about and that's gonna return to us a list of bowling.

  • So it's just true or false, and you might have multiple true's on then it, you know, like in our case, we only have one known face.

  • So it's either gonna be hate or any of these a Centex match, you know?

  • But in the future, you could have many known faces, so just keep that in mind.

  • But this will be a list of Boeing's going through the known faces for any bowling.

  • That is true.

  • You would want to say, OK, at what index was that true?

  • And then check the known names list.

  • So in the known names list, you're gonna have that list of identity.

  • So at the exact same index, you'll have the name for that face and coding that we know about.

  • So what we're gonna d'oh in this case is we're just going to say currently match equals none, and then we're gonna see if true in results.

  • So as long as we got a single true, we're going to say the match is the known names.

  • And then we're going to grab the index of basic orders a results dot index for true.

  • So again, in this case, we're kind of cheating a little bit, but But we're just gonna get the one single identity here on.

  • Then what we'll do is we will say, print match found on.

  • Then we'll say match will make this an f string.

  • Cool.

  • All right, so cool.

  • So now all we need to dio is Cem open CV stuff toe like, draw the rectangle and say who?

  • The matches?

  • Because obviously it's one thing to, like, find a match like we can prove that we found a match.

  • But what we're really more interested in is like seeing it on the image.

  • You know, everybody knows that iconic, like boxes around the head thing.

  • So, yeah, we got we definitely have to do that.

  • So So what we're gonna say here is, uh we need to get the coordinates for the match, So we're in the way.

  • That way, you're gonna d'oh anytime.

  • Like we're going to see vi tue dot rectangle to draw a rectangle like around the face to draw a rectangle.

  • You just need two things.

  • You need the top left, coordinate in the bottom, right, Coordinate.

  • And then it draws the rectangle.

  • So we're going to grab top left.

  • And that is going to be the face location, Um, at the 30th index of three.

  • And then face, uh, phase underscore location at the index of two.

  • Or actually, this will be zero.

  • Yeah, uh, and then we'll just copy this top left, and this is bottom right bottom.

  • Right on, then.

  • For bottom.

  • Right?

  • Yes.

  • One into two on.

  • Then we're gonna do color.

  • For now.

  • I'm just gonna say color 0 to 55 so green.

  • Uh, and if you want, you can check out the textbooks for this tutorial for a much more fancy version off dynamically picking a color based on the identity.

  • Maybe in the next video, too.

  • When we do it with, like, video, we can show that because in this case, we're gonna run out of time.

  • We're already out of time.

  • Uh, but we're just gonna do it on images.

  • But of course, you can do this exact same thing on video because video is this a succession of images.

  • So, you know, we can.

  • Probably something on video and then kind of make this a little more advanced.

  • So I've got lots of other things to show you, depending on interest here.

  • But anyways, for now, we'll just have a static color.

  • But that you can check the tech space first editorial for a function Daniel came up with, which is pretty cool s So now we have our color.

  • Now we want actually draw rectangles, make a rectangle.

  • It's a CVI to dot rectangle Now again.

  • So CV to kite is a little better at CV to they know a lot of things.

  • They're so lots of great suggestions from here.

  • So anyway, image, we're gonna draw the rectangle on the image.

  • We're gonna draw it at top left.

  • I have no Hey, my answer.

  • I have no idea what I've done.

  • Or is this just frozen on me?

  • I think I think I might have.

  • Yeah, Wolf, That's ugly.

  • Oh, dear.

  • Just what you want to happen during a tutorial?

  • I don't really know, but you open sublime.

  • It'll restore.

  • Okay, I think I think we've survived.

  • Okay, So Okay, Stevie to doubt.

  • Rectangle Eso top left bottom, right?

  • Yes.

  • Uh, and then whatever color we decided to use and any frame thickness now that will draw the rectangle.

  • But we also kind of want to label the rectangle.

  • So the next thing that we're gonna do is again a top left bottom.

  • Right?

  • So I'm actually gonna copy this, come down here and paste.

  • Now, this rectangle is going to be just like at the bottom.

  • So we got a big rectangle, then we'll have a smaller rectangle on this is just gonna house the text at the bottom of that rectangle.

  • So in this case, we don't want exactly the same dimension.

  • So it's actually gonna be three two, and then we're gonna pull it upwards.

  • So we're actually going to say one and then at the face to work as a plus 22 pixels up.

  • Okay, So top left bottom, right.

  • Cool.

  • Not weird drawl that rectangle.

  • And actually again, I'm going tol ah p this I'm going to come down here paste So image top left bottom right color.

  • And then rather than frame thickness, it's easy to see two dots.

  • Um, it's just filled.

  • Maybe Yeah, filled.

  • See, that's convenient.

  • So s O that we've drawn that rectangle.

  • Now we want to actually.

  • So the rectangles there it's filled with green Whatever color was or the custom color.

  • If you want to use a cool function now, we're ready to actually put the text there.

  • So we're gonna say CV to put text.

  • Yes, image.

  • What's the text?

  • Whatever the match was on, then the location for the text is gonna be this, like, 32 But then we're gonna kind of place it in a slightly different location.

  • So at the things coordinate we're gonna say, plus 10.

  • And then over here, we're going to say, plus 15 just to kind of adjust it somewhere in the middle.

  • Uh, then we're going to specify our font.

  • So the font that we want to use again cv to dot um Hershey simplex, that'll be fine.

  • Uh, then we're going to say is your 0.5 will be the size.

  • Now, you can specify the color in this case, we'll just do, like, kind of like a barely off white.

  • And then whatever our font thickness waas, if you want to make that a multi line function, her multi line of parameters feel free.

  • That's quite quite a long line.

  • Okay, so that puts our text on the image.

  • And now another.

  • We've done that.

  • Basically, what we want to do is we We want it for every unknown image.

  • We want to do this whole block all at once.

  • And then when we're all done right for each image, we want to show the image.

  • So I'm gonna say, see you to see me too dot him show.

  • And we're gonna show, uh, the final name and then we're going to, uh, the actual image.

  • Did that just freeze again on me?

  • Are you kidding me?

  • Okay.

  • Trying that again.

  • Him show.

  • I need to figure out why is that lagging out?

  • Okay, so we show this is the title of the image, just in case you don't know what CBT is doing.

  • And then this is the actual image that we wish to show.

  • Then we got to do a CVI to wait qi zero.

  • So, just as soon as we're ready and then CV to save you two don't destroy all window or the window really doesn't matter.

  • But if you want to destroy the specific window, you can give the file name Now I'm on the booty, and for whatever reason, open CVI doesn't like a boon to very much.

  • Um, and the destroy window won't really work.

  • So my solution is to secure all windows and maybe Mac, I'm not sure, but I think it's for any Knicks type operating system.

  • But anyway, I'm gonna comment that out, and I'm actually gonna say See what?

  • You don't wakey.

  • 10,000.

  • So it's in milliseconds, so 10 seconds.

  • But if we press a key at any point will continue to the next image.

  • But anyway, it's super messy.

  • I so rarely work with open CV on a boon to toe.

  • Actually, I work with it, but I don't really display things live.

  • So I don't really run into this issue very much, but anyway, that would be a temporary solution to that problem.

  • Uh, that's not the directory we want to be in.

  • So let me just open it here.

  • Open in terminal will do.

  • Python face rec.

  • Example.

  • That pie.

  • So see how we've done So it was the known faces.

  • Boone.

  • Um, Okay.

  • No match here.

  • That's good.

  • I'll press a key.

  • Hopefully, we're gonna Great.

  • Awesome.

  • I think it would just close.

  • It will be no good.

  • So I just keep pressing keys for now.

  • Okay, so then here, we've got a ton of matching Centex and did not detect that Is Centex.

  • Okay?

  • But did clearly detect put your jokes in the comment section.

  • Clearly detected a definite Snowden faces Centex.

  • That's funny.

  • Uh, okay, um no dissection here.

  • Detection there, but also detection it, like, clearly, I'm just a man with glasses to the machine because both of those Okay, fine.

  • Also another, um, deep fake.

  • Although this one's a little less far along, so no real surprise there.

  • No detection.

  • That's good.

  • That's good.

  • Pretty challenging detection.

  • And it got that one.

  • That's pretty cool.

  • Another good detection.

  • Okay, so that was all of them.

  • Very good.

  • Um, so the only other thing is okay, So in that one image, like we got a false positive, right?

  • That guy in the background.

  • So the next thing you could do is like Okay, well, let's say we want a lower tolerance, you know, like, I don't know.

  • Uh, let's go with 0.3 tolerance.

  • Um, let's run that again.

  • Nothing.

  • Nothing.

  • Wow.

  • None of these actually we're in trouble.

  • I think 0.3 is going to be too low.

  • Oh, boy.

  • So?

  • So we did not get that guy in the background.

  • But as you can see, you said the tolerance too low.

  • You get no matches.

  • So many false negatives here.

  • Really.

  • Not a single match, Not a single match.

  • Okay, let's try 0.5.

  • Just go slightly different than the other times.

  • Okay, so that worked out, Um, part pretty well.

  • I mean it miss some of these clear examples, but that's okay.

  • I got that one.

  • Um, did not get that one, which is actually correct.

  • But move, miss that one.

  • I do think that's a hard one.

  • Got that one, though?

  • Cool.

  • Okay.

  • So you can see that you could kind of play with it.

  • And I will say, if you had more faces, Maur examples, you're gonna be even more accurate.

  • You can keep your tolerance kind of high.

  • You're gonna get much less false negatives.

  • Um, so you feel free to tinker around.

  • Like I said, you could do this not only on images, but also video frame rate is gonna very significantly depending on what kind of machine you're running it on.

  • If there's enough interest, I will follow up this video with another video doing this with video and then hopefully adding something a little more unique and interesting to that we'll see if we find up doing it.

  • Um, but if you have questions, comments, concerns, whatever, fill free to leave those below.

  • Otherwise I will see you guys in another video.

what's going on, everybody.

字幕與單字

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

B1 中級

用Python和face_recognition庫進行面部識別。 (Facial Recognition with Python and the face_recognition library)

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