How to Use Python for Basic Facial Recognition!

LeMaster Tech
183 followers
13 Video Views·Apr 21, 2025
Python is an extremely powerful tool for doing data science and analytics, and using the OpenCV cv2 module, you can do a lot of awesome image recognition and processing, including facial recognition! It is a little tricky to incorporate cascade processing files, but fortunately you can use preconstructed ones available free online!
Grab the code and all assets you'll need here:
https://github.com/plemaster01/pythonFacialRecognition
Don't forget to leave a like, subscribe to the channel, and consider becoming a super supporter of LeMaster tech here!
https://www.patreon.com/LeMasterTech
what's up you guys welcome back to the
channel in today's video I'm going to
show you how to do basic facial
recognition using the opencv module and
python code
[Music]
so the basics of what we're going to
need today is the opencv module which is
uh basically computer vision it's a lot
of python stuff we've done it before on
the channel for some basic like image
transformations to make it like black
and white or blurry but it also can do
really Advanced uh facial recognition
with just a few lines of code and there
is a pretty important piece that we'll
touch on when we get to it that's
basically your cascader which is
essentially the XML code that's going to
actually check for the faces for you
we'll talk about that more as we get
into it but so let's just start you need
to import the CV2 module and if you
don't have it installed already you'll
need to run pip install and the command
for this is actually opencv Dash python
it's not just pip install CV2 so just
keep that in mind if you don't have it
installed already and now what we want
to do first is get uh user values so we
want to get two things we want to get
the image path which for me I just have
a photo that I called
beetles.jpg like that and so whatever
image you're using you just want to
direct it to that I always leave a copy
of my photo in the project folder
because I don't have to do a whole bunch
of advanced pathing but you could here
and then the second thing this is uh
kind of the black box of the project is
you have to give it this Cascade path so
it knows where to look and my Cascade is
in uh is in the project folder and it's
called har Cascade frontal face default
har Cascade frontal face
frontal face
default and uh dot XML and that's that
is kind of just the me waving my hand
saying just accept that that's what it
is for now because
um a Cascades it seems really complex
the XML file is 33 plus thousand lines
of code so it seems super complicated
but Cascade essentially means a series
of small waterfalls that's kind of what
the English word means and that's the
right way to think about this code it
goes through your file and it makes a
number of small decisions and based on
what it sees there it goes and it makes
a different decision so I'm not going to
use this video to talk about how to set
up a Cascade there are great ones
already put together I believe this
particular one comes from intel if you
want to grab this Cascade file I will
leave a link to the GitHub repository
for it in the description of this video
so you can grab it from there and now
what we're going to do is we're going to
create a hard Cascade so
we need to actually tell our filed our
program to use that so we give it a face
Cascade and it's equal to CV2 dot
Cascade classifier so that is something
built into the CV2 function that it
knows how to use a Cascade file and then
we're just going to path pass in the
Cascade path to use it okay so now what
we're going to do is read the image and
so the image is equal to
cv2.im read and then image path
and the next step strictly speaking is
uh an optional step but this is much
more accurate if you put the image in
grayscale before reading the uh the
faces so you're gonna use the cv2.
convert color or CVT color function and
it's just going to be pointed at your
image and then use CV2 Dot and this is
going to be color underscore vgr to gray
so uh it's just essentially saying go
from color to grayscale and we're going
to do that right before we detect the
faces
in the image te image in the image and
then what we're going to do is we're
going to make a list of faces so we're
going to say faces equals our face
Cascade that we already set up and then
dot detect
multi-scale just like this and then we
need to give it a few arguments first is
going to be just uh let's identify that
it's going to be grayscale
and now the next three are actually
things you'll probably want to play
around with in your program but I'll
talk briefly about them as we put them
in scale factor is going to kind of
determine what uh size the faces are
going to look at and so this can kind of
impact uh based on the overall size of
your image what size faces should we be
looking for and really good values are
sort of between 1.05 and 1.3 1.4 and
we'll play around with this a little bit
once we get the code written but really
the way to get that dialed in is just
sort of guess and check which sort of
brings us to Min neighbors as well
um you can find all of the documentation
on this uh like opencv2 face Cascade
online if you're super curious about
what this is but this is essentially
like how many of the signals that
indicate it's a face do we need to look
forward to indicate that it is a face
so lowering this a little bit might
increase the sensitivity of your face
detection but it also might give you
some things that aren't faces so it just
kind of you to tweak those is kind of do
you want to catch everything but also
catch some garbage or do you want to
miss out on maybe some smaller harder to
detect faces and then Min size is going
to say like below a certain size don't
look for that at all we're not going to
register that as a face and there is an
opposite one that's Max size we're not
going to use that right now but you
could say above a certain size that's
not face under a certain size that's not
face and that would kind of let you only
look for faces at a certain depth from
you but so for now let's just do that
and then let's create a printed sorry
about that let's create a formatted
print statement that's going to tell us
how many faces they found so we'll say
found
and then uh we'll just throw a zero in
here but we're going to use the dot
format command so found curly brackets
uh zero faces and then we'll say dot
format and then we just tell them what
we want to stick into our formatted
option in that string and uh that'll be
length of faces and this is the exact
same thing as putting an F here and
saying instead of zero
length of faces so that's really just up
to the programmer I guess this is sort
of my standard I do it that way more
often but so now what we want to do is
we want to draw a rectangle rectangle
around the faces and what we'll do is
we'll say 4 X and Y starting coordinates
and then width and height
in faces so those are every face is
defined by an X and Y starting
coordinate and then how wide it is and
how tall it is if you've seen some of my
Pi game tutorials it's the same concept
you need these four things to fully
Define a rectangle uh your X and Y
starring coordinate and then your width
and your height
and so for each of those in faces we'll
do CV2 dot rectangle on top of our image
and then it needs our X and Y
coordinates we'll give it X and Y and
then it needs to know uh its stopping
point and so that'll be X Plus width and
Y plus height because the uh kind of
standard for computer Graphics is zero
zeros in the top left so you can think
of it as your y star and coordinate plus
height is actually going down the screen
okay and so then uh what color we want
to be I'm just gonna make them green
rectangles because they really pop
against most images like this bright
green is really not a very natural color
so it should show up pretty well and
then just give it a width of two so it's
easier to see
and then we're gonna run this C v2. I am
show which actually displays the
rectangles uh it'll pop the image up
with the rectangles over it and so we'll
call this faces found and we'll put it
on top of the image and then we will
just do the CV2 dot weight key zero
which is just going to make sure it
stays up on the screen it doesn't close
as soon as we run it okay so here's the
Beatles photo uh you probably saw it in
the intro that I want to use they're all
just looking a little confused this I
think is just in the Sergeant Pepper's
era
um but so if we run this with kind of
these default values that I put in here
you can see it's pretty good but we
actually got for some reason it detects
George's chin and neck as a face so
maybe what we need to do is we need to
step this scale factor up to 1.3 maybe
that'll help reduce the sensitivity
and now you can see when we run it it
does find the four faces which is great
um and you can also see down here in the
console window it says found four faces
so that's great it's telling us how many
faces we have but so let's do this with
a trickier photo right let's do it with
the Sergeant Pepper's cover because this
is obviously a little bit more lower def
it's not really a big photo like that
every face in it is quite a bit smaller
and so what you'll see if I run this and
I change this from beetles to beetles
two now you're gonna see it should
detect a ton of faces it only detects
six okay so it's not sensitive enough
right now so what we can do is we can
reduce this scale factor to like we can
try 1.1 and now it's going to find 17
faces so it's doing a little bit better
we can bring this all the way down to
like 1.05
and that should get pretty good and you
can see it picks up 32 faces it actually
even picks up the face of the statue
it's getting each of our beetles now so
it's getting closer but you can tell
like it's a low def photo
um it it's actually picking up on sort
of weird faces and it's forgetting some
that should be like sort of obvious
um but so then you can take a look at
like reducing your minimum neighbors
requirement to like three and now we get
42 faces and you can see it's not great
when you reduce some of those parameters
we are getting a ton of faces fairly
accurately but we also got like a knee
and this guy's chest and it's not even
picking up this guy's face
um so like there are a lot of things to
consider here you could reduce your
minimum size because they're obviously
pretty small faces and now it registers
it's picking up 46 so these really are
the key parameters you're going to want
to play with if this basic facial
recognition ignition doesn't get you
close enough to your end goal you might
want to go online and look up the CV2
face Cascade multi-scale parameters and
sort of do some reading on additional
things that you can do
again I will leave this code in a GitHub
link with the two images that I used as
well as the full XML file the XML file
is uh pretty beefy it's going to take a
little while to load on your computer
and download because it's 33
000 lines of code but I hope you found
this useful let me know in the comments
below if you have any questions on what
you saw here today or want to see
anything specific in the future or like
a project built off the back of this to
see potential applications I hope you
found it useful thank you very much for
watching and until next time good luck
with your projects bye
