Transcripts
1. Introduction : Hello, guys. It's
good to see you back. Once again, I'm back with a new class related computer vision. Today in this class,
we are going to create real time phase
recognization with the help of Open Civil
Library and Python. Hello. My name is
Jonatha Shorter. I am Fullstack web
developer Python programmer and online instructor. It can detect multiple
phases and identify the pass at the same
time using our webcam. Let me show you the
demonstration what we are going to build today
in this project. As you can see, our program successfully recognize my phase. Also it recognized Prime
Minister Modi phase. Then it recognized
Donald Trump phase. After that, it recognized
Justin Trudeau phase, and at last, it recognized
Georgia Meloni phase. Our program is very much capable to detect and recognize phases. To create this project,
we are going to use media pipe and phase
recognization module. From the next tutorial, we are going to
start our coding. We are going to start with
importing liabilities.
2. Install libraries and read the image file: So finally, we are in my
visor Studio code editor. And as you can see in my
current working directory, I already create a
Python file name recognizer dot Pi and a folder. A folder name known
underscore faces. In that folder, we
have some images. We have image of Georgia Meloni. We have image of Naren Romoi. We have image of Justin
Trudeau and at last, we have image of Donald Tram. These are all well known
pass all over the world, and for our recognizer, we trained our model
with those phases. Now, let's back to the
recognizer dot Pi five. And at first, I'm going to
import the important modules. At first, we need to
import OS module, Sim type, import OS. Using this module, we can access our working directories,
otherwise, the folders. Then we need to
import another module called CV two. This
is the main module. Simu type, import, cv two. With that, we need to
input another module. But first, we need to
download this module, and our module name is
phase recognization. Let's open the
Windows Power Shell and try to download this module. To download this module,
just you need to type, peat, install, and our modulem
is phase recognization. Then you need to hit Enter. After hit Enter, as you can see, requirement already
satisfied because I already download this module, but you might face some error
because of this module, because this module come with some dependencies such
as Nampi, Pillow and DL. But in your case, you can get error because
of this DL module, and we need to install
this module manually. Let me show you how we
can install this module. To resolve the problem, just open your browser and search delete Well
package download link. Then it provides you this link, this Github link, and you need
to open this Github link. Delete compiled
binary dot HL file and you need to download
this Wheel file. If I click on this
file, let me show you, as you can see, here it provides multiple HL file for
multiple Python version. If you're using 3.10 version, then you can use
this Wheel file. If you use 3.7 version, then you can use this Well file. I am using 3.11, so I download this one. To download it to download it, just select your
Windows version. I'm going to use this
one and I click on it. After that, you need to
click on Download Raw file. After click on Download
button, as you can see, it download to this folder,
this Download folder. It download to this
download folder. Now, let me show you
how we can install this WL file using
Windows Power Shell. At first, you need to
copy this file path. I copy the exact file path, and I'm going to open
my Windows Power Shell. And as you can see, I on
in my Windows Power Shell. To download this WL file, first, you need to type, peep, install, then you need to use space and you
need to use dowel codes. Then inside the dowel codes, you need to put the file path. After that, you need to use Ein, then you need to
provide the file name. To provide the fine name, I'm going to copy
the exact fine name. So from here, I'm going to
copy the exact file name with the extension and I'm
going to paste it here, Control V, and we need to hit Enter to download this
module in our system. Just hit Enter and it's going to compute
your download process. I already download it, so I'm not going to
download it again. This is how you can download any Hill file in
your local machine. Now, let's get to
the code editor. Then after install
the DL module, now you are able to install phase recognization
module in your system. So just type PIP install
phase recognization. You need to follow this method
only if your system not able to download D
module automatically. Let's import the phase
recognization module in our five, some type Import and our module name is
phase recognization. Apart that, we need to
install another module, which is media Pipe. To install media pyte module, you need to type this command. Pep Install OpenCb
Python space media Pipe. If you hit Enter,
as you can see, requirement already
satisfied because I already download this
module in my system. Let's input the
module in my system. Some type import,
and I'm going to import media Pipe as MP. That's it. After import
all the necessary modules, we need to load the known faces. I want to say, we need to load all the known faces such as
Meloni Modi, Trudo and Tram. Remember, you can use as much pass you
want. It's up to you. So here I'm going
to type a single command load known phases. After that, I'm going to
declare two variable. To store the known phases, we need to define two
variable, basically two list. Our first variable name
is known unc phase, under sco, encodings,
equal to I empty list. Then I'm going to define
another variable which is known phase name
known under sco, phase, under sco
encodings, underscoename. Names equal to blank. Our first variables store
the numerical encodings of each known pass and the second variables store the names named
corresponding to each pass. In our first list, we
are going to store the numerical encodings and
in our second variable, we are going to store
the name of the images, and it's going to get all the names from
the fine name such as Meloni Modi Trudeau tram I' going to take the image
name as a person name. Next, we need to look through all files in this
known phase folder. For that, we are going to use for loop. Let
me show you how. Sum rotyeFFlme in s dot, I'm going to use is DA function. Then inside the round
reces I'm going to provide the folder name
which is non phase, K Wn underscore phase, nonpass. Basically, this four loop
going to look through every file inside
the nonpas folder. Then inside the four loop, we need to construct the full
file path for every image. I want to say here, file name represent images, the image file, which is
inside this nonpas folder. We look through every image. So we need to construct
the full file path, so I'm going to define
another variable called PAT. But equal to here, I'm going to use Join function. Type Os path dot join. Then inside the
round recess, first, we need to provide
the folder name, and our folder
name is non faces. Non faces and
apricoma we need to provide the we need to
provide the file name. I'm going to pass the
final name variable. File name. It's going to join
the non pass and the current file to get the
full file path of the image. Suppose for Meloni,
it's going to get the folder name non phase, then it's going to add the
file path, meloit JPG. Next, we need to load the image in phase
recognization module. For that, I'm going to define another variable called
image called image. Image equal to here, I'm going to type phase
recognization dot and from phase recognization, I'm going to call a method
called load image file. Load image file. Then inside the
roundres I'm going to provide the path variable. Basically, it load the
image file into memory in a format that pace
recognization library can process. Basically, behind the scenes, it used Nampi alley. Next, we need to get
the phase encodings. For that, I'm going to define another variable
called encodings. Encodings equal to
phase recognization. Dot, I'm going to use from
phrase reorganization, I'm going to use
phase encodings. Then inside the roundresses, we need to provide the
image variable image. And I'm going to set this five. It is try to find all phases
in the image and generate a 128 decimal encoding
for each images. Basically, it return a list. Even if there is only one phase, still it return a list. Now if the pace is found, we need to set the
phase encoding to this list and the pas
name to this list. For that, here, I'm going
to use IP condition. If encodings, then inside
this IP condition, then I'm going to append
the encodings to this list. So types, non phase encodings
dot append function, append then inside the round
resis, I'm type encodings. Inside the square recess, I'm going to provide zero. Basically, it takes the first
phase encoding found in this image and append it to this list, non phase encodings. Next, we need to extract
the image finally. For that, we are going to
use split text method. Here I'm going to type Os, dot, butt, split text. Then inside the round braces, I'm going to provide
the file name. Then inside the square braces, I'm going to pass zero. Basically, it's going
to split the file into tupel such as melon dot JPG. Meloni is one tupel item and dot JPG is
another tapel item. Here we pass zero, I mean we are going to
extract the first one, so it's going to
extract only the name, meloni not the extension. That's why we use zero hair because we need
to extract the name only. And then we need to set
the names in this list. For that, we need to use
append method again. Summary type known phase
names dot append method. Then inside the roundreses
we need to append the names, but before I'm going to store, I'm going to store
it in a variable, which is name sum
type name here, name I'm going to provide this variable name inside
this append method. Name, and I'm going
to set this pile. Basically, this
code scan through the known phase directory. And for each image, it's going to load it, extract
the first phase encoding, and set the encoding, also the file name to the
list items to this list. And make sure all
the images you use, they have a clear phase. If you upload an image where you have multiple
phases on that image, then it's going to extract
only the first phase. It not going to recognize all
the phase from the image. That's why I will recommend
to upload those type of images which have only
single phase, like the ones. This is the first part
of this tutorial. And the next part
of this tutorial, we are going to working on media Pi phase detection setup and we are going to
start the camera. Thanks for watching this video, Stay tuned for the next part.
3. MediaPipe and face detection setup: Hello, guys. Good
to see you back. This is the second
part of this tutorial. And here, I'm going to say
media pipe phase detection. So at first, I'm going to
type the single command, which is media pipe,
phase detection setup. After that, here, I'm going
to define two variable, and our first
variable name is MP, media pipe, phase,
underscore detection. Equal to aminotype P
dot solution phase under sco detection, phase
Underscoe detection. After that, we need to
create the drawing utiles. For that, I'm going to define another variable which
is MP underscoe drawing, equal to p dot solution, drawing utiles,
underscoe utiles. Now the is, what is the
meaning of this two variable? What can you do with that? In our first variable MP
phase understood detection, we load the media Pi
phase detection solution, and in our second variable, here we provide utility function to draw phase
landmark on images. Now, let's start the camera. For that, I'm going to
define another variable, and our variable
name is cap, CAP, Cap otherwise captured equal to here we need to call CV
two dot video capture method, video capture and
inside the round brass, we need to pass the source. As a source, I'm
going to pass zero because I have only one
webcam in my system. After that, we
need to initialize the phase detection module
with a minimum confidence. For that, I'm going
to type with MP, underscore phase
detection dot from here, I want to run phase detection. Phase detection. Then inside the
round roses here, I'm going to set minimum
detection confidence. Man underscoe detection,
underscoe confidence, equal to, and I'm
going to set it 0.6. Aber that I'm going to type as Phase detection, phase
underscore detection. Then inside it, here, I'm going to run a while loop. Here I will type y, cap dot is opened, is opened. Then inside this while loop, the ins this while loop here, I'm going to declare
two variable. First one is for
Bullen variable and second one is for
the image frame, and you know the process. I'm type RET, comma, and to stort the
frame mut frame, equal to cat read
method, Cap dot read. Then I'm going to
say p condition. If it is not true, I want to say I I not Bian value is true, then inside this IP condition, I want to break the loop. Pick, that's it. Now the question is what
I basically did here. At first, we open our
default web camera and save it in a cab variable. After that, He we initialize phase detection model with
minimum confidence of 0.6 and her use with statement because it is ensure proper
resource management. I automatically close
the model when it done. Then I read the camera frames
continuously using Hilou. Cab dot is open method, checks if the video
stream is active, and this is for Boolean value, the frame variable capture image from the webcam and
then see a condition. If no frame is received, then it going to exit the loop. Somehow our camera
connection is break, then it going to break the
loop if there is no footage. After that, inside
this while loop, I'm going to set a IP condition. So type I CV to do wait key. Inside the round process, I'm going to wait for
ten millisecond and and zero xFF equal to equal
to ORD method, RD. Inside the roundresses,
I'm going to pass Q, then I want to break the loop. After that, outside
the while loop, I'm going to release
the catcher. Cap release method,
cab dot, release. Also, I'm going to destroy
all the window after release the programs type CV to dot, destroy all Windows. Now. Now inside this while loop, I'm going to convert BGR to
RGV frame because by default, OpenCV read our
frame in BGR mode. But for phase detection, we need RGV frame. So here are our type
RGB underscore frame. RV frame equal to CV
two dot, CVT color. Then inside the
roundresses, first, I'm going to provide the
actual frame as a source, some type frame, then a coma, here I'm going to pass CV two. We need to pass the color code, which is color BGR RGV because we are going to
convert big to RGV frame. That's it. Next, we need to
extract the phase location, also the phase encodings. For that, I'm going to
define two more variable. In our first variable Imo type
phase underscoe locations. Phase locations equival
to phase detection, dot, dot, phase location method, phase
underscoe locations. Then inside the round
verses here I will pass the RGV frame GV frame. After extract the phase
location from the RGV frame, also, we need to extract
phase encodings. For that, I'm going to define another variable phis
underscoe encodings. Fit encodings equal to herm
type phase recognization, from phase recognization, I 12, sorry, also you need to
run phase recognization, not detection, phase
recognization. This one. Using phase
recognization module, here we extract the
phase location. Then using phase
recognization module, we are going to extract
the phase encodings. Here, I'm going to tie
phase recognization, dot, I'm going to
type phase encodings. This method. Then inside the
round resses here, we need to pass first, we need to pass the RGV frame, type RGV frame, comma, then I'm going to pass this
variable phase location, phase locations, this one. I'm going to set
this one. Now let me explain to you basically what
basically it going to do. Phase locations
retrat the coordinate of all detected
phases in the frame, and our phase encoding computes 12 dimension phase encodings
for each detected phase. Now we have phase location
and phase encoding. Next, we need to
recognize the phase. For that, we are going
to use a follow, but not in this tutorial. And the next part
of this tutorial, we are going to covert
the remaining part. Thanks for watching this video
stay tuned for our next.
4. Main loop that going to recognise the face: Hello guys. Good
to see you back. This is the third part
of this tutorial, and here we are
going to run a loop. We are going to
run the main loop that going to
recognize the phase. For that, we are going to
use follow, but before, I'm going to type a
single and comment, and I'm going to
recognize phases. After that, we need
to start the follow. We need to run a four loop
through each detected phase. So I'm going to type for
inside the round presses, I'm going to take some
variable for the coordinates, some type top, comma right. Bottom, t. After
that, phis encoding. Phase encodings. Fizz encoding in Z Z IP Z inside the roundress
hemmed pass, phase, location, face locations, fizz encodings, phase encodings. Then inside this four loop, but let me explain this fo
loop what I just did here. What is the meaning
of this four loop? Basically, here we look
through all detected phase. These loops iterate over
each detected phase. Here, phase locations contain bounding box
coordinate of phases, which found in the frame, and phase encodings contain facial feature vectors
for each phases. Basically phase
recognization module use 128 vector points
to identify phase, and then we use Zip method. It going to pairs each phase
locations with its encoding, and top right, bottom lapped are the coordinates used to draw
a rectangle around the face. After that, inside
this four loop, we need to find the matches
to compare the faces. For that, I'm going to declare another variable called matches. Match is equal to
here I'm going to use a method called phase compare from
phase recognization. So ti phase recognization, dot compare phase,
compared phases. Then inside the round ss, here, I'm going to pass
total two variable. Non phase encodings, non phase encodings
and phase encoding. Phase encoding. He compare the current
phase encodings known phase with non phase encodings. As you know, in our
non phase encodings, here is told all the
phase coordinates and their features from these known phase
images such as Meloni, Modi, Trudeau, Tramp, et cetera. It's going to compare
the current phase with the known phase images. If you do not found any match, then I want to name it unknown. Sutname equal to unknown inside the double
course not unknown. It will be overwritten if the
known phase is not found. Basically, it is the
default label name. And next, we need to use the shortest distance
for the best match. For that, we need to
calculate the distance. So here, I'm going
to type another single and command and I'm going to pass use the shortest
distance for the best match. I'm going to type a
variable name and our variable name
is phase distances. Phase undersco distances
to calculate the distance, we need to use a method
from phase recognization, which is phase distance, sum type phase recognization, dot phase distance
method, phase distance. Then inside the round recess
here we need to compare with non phase encodings with current phase encodings
type, non phase encodings. Comma, phase encodings, phase
encodings. Phase encoding. Basically, this function
calculates Ecledean distance between the current phase
and the all known phase. Similar distance mean
more similarity, and we use this method to find the best match from
the known phases. Now we need to run
an I condition to ensure there is at least one
known phase to compare with. Here I'm going to type I lane inside the round versus phase distance,
phase distance, greater than zero, then
insert this IP condition, then inside this IP condition, we need to get the index of smallest distance for
the best possible match. So I'm going to define
another variable, and our variable is
based undersce match, underscore, index equal to here I'm going to run a method. I'm going to tie phase distances
dot Arg mean, this one. This method going to
return the index of smallest distance for
the best possible match, and we set it in this variable. Now, if the match is found
from the best match index, then we need to replace the unknown name
with this file name. I want to say this filename. If it is tram then
we need to name it Tram if it is Trudeau then
we need to name it Trudeo. So for that, I'm going to use HIP condition inside
this IP condition. Type I type IP matches
inside this square basis, I'm going to pass
best match index, then inside this HIP condition, I'm going to overwrite
the name variable. Then the name become equal to known phase names inside
the square bases from it, I want to put the best match
index, best match index. If it found the match, then
it's going to put the name. Then we need to draw
the rectangles. We need to draw the rectangles. Around the face. For that, outside this IP condition. Outside this IP condition, I'm going to use
rectangle method, similar tit CV two
dot rectangle. And inside the roundresF I'm going to provide
the frame name in which frame we are going to draw draw the rectangles,
which is frame. After that, we need to
define the position. As you know, we already
extract lap top and bottom position to
draw the rectangle. At first, we need to provide
the top left corner. Here inside the round braces, I'm going to provide the lab value lab and then I'm going to pass to value, top. Next, we need to define
the other corner, which is right bottom, which is right bottom. Inside the round braces, I'm going to type right bottom. After that, after that, we need to define the
color of this rectangle. Inside the round braces, I'm going to use
this colored code. B, for blue, I'm going to
use zero for G for green, I'm going to use
255 and for red, I'm going to use zero again. Then we need to provide the thickness of this
border, which is two. After that, draw the
rectangle, also, you need to put the text,
the name of this person. I'm going to use
put text method, some type CV two dot, put text. Inside the roundssF
we need to provide the name frame in which frame we are
going to put the text. Then we need to provide
the text, which is name. After that, we need to define the position to define
the position of the text. Here inside the round brrss
we are going to start the text from the left
side, lift and comma. This is for Xxs and for the xs, from the top POP top, I'm going to minus ten film. Autocoma, we need to
define the font face. And here, I'm going
to use CV two dot, CV two dot, font hoch
simplex, this font. Then we need to define
the font scale. Here I'm going to use 0.8. After that, we need
to define the color. We need to define which color
we want to show the font, and I'm going to
use the same color. So we use zero comma
255 comma zero. Cream colored and then they want to provide the thickness of the
fat which is two, is good for each, and that's it. After that, we need to display. We need to display the
frame using IMF method. Outside the fall loop here, I'm going to display the face. So tie CV two taught IMF Mathod. I am sure that inside
the round dresses at first want to
provide a name for the window and its name
is face recognization. And then Apacmo we need
to display the frame, the actual frame which
we want to display. And I'm going to sub this file. So we are done. And before I run this
code in my terminal, I also want to put my own face in this non phase
list in this folder. So I put my photo on this list. This photo is taken
from my webcam, and I'm going to use
this photo also. Now we have total five
phase to identify. So let's battle the code and set this file and run this
code in our terminal. Now, let's run the code. So I'm going to hit
the Run button. After I hit the run button, we need to wait for some
time to start the webcam. Afterwards sometime,
as you can see, it start my webcam. As you can see, instantly
it recognize my face. It creates a square
box around my face, and above the square box, it print my name, joint. And if I try to show
you some other faces, so in my keyboard, I
already print the faces. And if I show it in
front of camera, as you can see, it's also
recognized other faces. It recognized the other world
leader faces such as Modi. And if I ship to the next phase, so I'm going to change the face, as you can see, also it
recognized President Trump face. At the same way, if
I change the image, then you can see it recognized Canada ex Prime Minister
Justin Trudeau. As you can see, we track, also it recognized the face, and then come our last
pace Georgia Malone. Also it recognized
her special features, so we are nicely done our job. I hope now it's clear for you, how we can build our own face recognization model
using Open CP. Thanks for watching this video. Stay tuned for our next project.