Hi, I am Asadullah Dal from AiPhile. Today
I'm going to show you how can we measure the distance using a single camera through detecting
a face all the explanation of code will be available on this video, all the information
you need on the blog post will be available on the description below I will pull the slides
which I am going to explain the mathematics and the majority mathematics I will suggest
a video which will be available on the my YouTube description so you can watch that
as well. But I'm going to explain a few things of mathematics the concept behind that as
well. And another Picture that the shows object has moved from its initial posture to the final position
which just change it in the distance . So, it comes closer to the camera, so that the
theta, has changed, which means the angle between the this will be changed as well.
So the focal length remains same, but the height of object on the CMOS sensor will be
changed as well as object comes closer to the CMOS, sensor, or, the camera the height of objects appear increases on the
CMOS sensor. So the height will become p initially the height was a now it becomes p in the on
the image plane or only object plan height wastage because it does not changes the distance
changes. So the distance becomes the minus m. so here is the equation B divided by f
10 theta to be equal to h divided by d minus m. So the distance because of that, we subtract
the team from the team. So that distance become, that . Now here is the equations. These are
being divided. And I will explain all the equations on the another video, I will link
that in the description below because it provides lots of information. So that will be a best
for you in case you want to take dive into this. So the final equation I'm going to show
you here is this t equal to m one minus A divided by B . So this is the final equation.
Now for the actual implementation of this is different because it's a static image,
we are going to use it for a real time detection so that there will be slightly different.
So we're gonna go into this. So let's get into the code . So I have already opened up
cmd. And actually, it's simple project. It is in the , Distance Measurement directory.
So first, let's open the code code. Yeah. Nothing here just there is har cascade
file. And reference image reference, which I'm going to show you here is my reference
which I've just captured the image in my head, the distance from my camera to the my face.
So now you can see here that I'm using Ruben to measure the distance from my face to camera
for that Pretty straightforward, I have already created a script for that, that you can capture
that reference me. So here is the script that's really simple. And just detecting face in
my instance. And in case you want to know how can you capture just press C on your camera
and it will show you that it's capturing all that really straightforward thing else. So
we'll just move to the Distance Measurement. So, we have to create a simple file fairly
simple and straightforward. Now, here is the code which simply takes the frame from camera
and shows on the screen. So, simply we and here we are just defining came an object a
frames actually turns in finite groups actually video is simply the frames running. And then
finally loop by loop. So we are just defining while loop while the frames that are available.
Our our camera Ross window will be shown are simply the frame will be shown on the screen.
Unless there is a free either we can break this loop by just using the PV to weight key
function, which Jake if any keys are pressed on the board or not soy II code is taking
that and when they just wait key simply waits for the time. And then in case we press or
key or keys just say the key from the keyboard. So we are just pressing when we press Q cause
we have a string argument that is a Q so q will break the loop. In the end we are just
releasing the camera which we initialized first is the camera object now then we are
just destroying the windows. Let's turn the code now here are the frames coming directly
from the camera is running in the loop. I'm here I'm just using my smartphone camera to
for the game video because my laptop camera is not that good. So I want to show you that
as well. So in case I've just changed the argument here to zero down here in things
so here is my laptop's camera, that's not that good. You can see that here. And I'm
also going to open up a remake here you can see the difference of quality here in that
camera it's coming from my cell phone here is the cell phone now you can see here I'm
just using head showing to the MS smartphone camera so I my smartphone camera is the more
better than my laptop camera. So I'm using that and so guess you your laptop camera's
not the good and you can my watch my video I have already created How can you use this
empty webcam it's really simple. Just you have to download this. But in case you have
not figured out how you can watch my video. So let's go further into it. We're just going
to create a function So, here I've just defined a function which
can detect the eyes using heart gasket, we're just defining object a classifier, which can
detect the face in the frame. So, here the function of theta as a plea will return the
face with in the major or you can say the phrase in the frame. So, first of all, we
are just converting the image into gray or BGR image into gray and then we are just detecting
the face of finding the face and assigning the values to this function. Here is some
parameters required some like multi detect multi scale, it requires some image and there
are some parameters here it is the parameter of scaling means, how much you scale up or
scale down the image in Article Fs, when the image is larger than you get find ways more
accurately Or more accurately simply, but at the processing power will be increased
as well. So, the frame rate will may reduce in case your machine is not that much powerful
and this is 10 years neighbor adaptdx spin and it find the fifth r which is nearest it
something like that, so, I can't find out figure out what is this, but nearest the nearest
neighbor means, how close it is to be faced. So, this parameter also fails here we are
just iterating through the faces. So, in case there are multiple types found, it will just
extend the x y edge and by weight simply it will extend the heart x y means the rectangular
value of the face will it will just extract the ROI basis as we are faced with So, it
will be x y and hibbett the height and width. So, we are just drawing the rectangle around
our faces. So, it is complete that where X and Y location in we are just starting found
x two plus c aiding the width of the face to reach the final position of a face and
then we are just using first coordinate of the why and then we are just adding the height
to two so that we can treat the maximum purchasing enough of height in case and then we are just
passing the color here I have just assigned the topple which contains three integer values.
And this is the scaling factor means how thick we are a certain line we need. So another
thing is we are just assigning w which with our face to face and then we are returning
back here I have just I'm going to run this code here I'm just calling this function and
restoring the width to the face within the frame. So as data we are just passing the
image to it made in our case it is so let's run this code let's run this okay. So here you can see the
prettier image or you can see the first has been detected a rectangle around this face
been drawn. So another thing we have to do simply have to create two functions first
function will simply find the focal length of the image in case you I have shown you
the presentation as well. So I have to open up presentation you can see the
so I'm saying what this focus And you can see this is the focal length is from a lens
to camera the object with the focal length. So, it will be remained constant unless it
will not change much, but we have to find how can we find this as simply we will use
the reference made in this condition as well and then we will measure the distance using
another function I will create both of these functions and are coded back and then we'll
I will return back to you oh here to run this code again forever the
server time I fought some issues but I'm running checking for hours fine understands.pi So,
you can see the face This is being detected in frame as well as in the reverse image is
a tactical drawn here you can see this is the focal length has been found. So, we're
going to use this focal length to wire the distance we just going to close this window
I don't know why this is freezes, but the code is here. Here you can see the fifth now
these two function I've created a one is for focal length finally, for that I we need a
distal Meier distance we have known distance it will be known distance. So, I just tried
it No, but I will correct this as well. Oh here this is this code, we are using this
known distance here in the focal length finder as simply we are calling this mere distance
it is the known distance which is said 30 centimeter away from the face to camera, other
services it is the width of face in centimeters is that I hear I'm just still defining colors,
which I maybe already Express on here is yes we are we are in distance by or distant using
find focal length in real width means the real world with and within the reference image.
So, the focal length will be equal to real with minerals within the reference image multiply
that by the mere distance or it will be this known with unknown actually known distance
and the real width which will be done known with here in August. So, to advance main known
we thought it will return the focal length simply is that and they still find a subject
required the focal length deal with a which is done on with an end and face within the
frame till we are using distance finder to in the frame. So we are saying that face with
indifference distance actually is equal to or real face to face with multiply by the
face. Coconut so focal and we are we can we find the focal length we will use this focal
length in here and we will just divide the world focal and multiply by the real bit to
the actual frame based in the same so we will be returned the distance over I am just here
I've just read that image from the directory here. It The reference image now, then we
I'm just calling this function to detect phase and return type it actually this concept attends
a bit. So, we are using this with to find the focal length. So, here is the focal length
normal distance, we know that unknown width and that reference the first image with sort
of reference will basically take this image with it when you confuse it. This means with
actually will be from this to this it will be formed in the form base now let's move
ahead only strand this code here you can see the face has been detected and distance has
been estimated there's around 2627 because the pointy box now is stable. So it will change
accordingly, but it's estimates but fairly accurately, as not that errors difference
are in accuracy different or nearly accurate. Now, another thing I have
to explain the court then I will show you the how much it can face detector can detect
and how much of it can show you the face esteem distances division. So it's totally depend
upon the face detector or any object detector which to take the object so that you can you
can estimate it, find the distance. But actually the this face detector works pretty well.
Now, here is the code you can see this is simply a when I actually showed you that that
we have found that with up in the prim, so we are just checking if VAT is equal to zero,
then I do not find the distance because when there is zero, we will find a shoe of we will
fall into the issue of a zero division, we don't want that. So we are just saying that
if the focal length or something like that with a frame is equal to zero, you do not
have to find the distance samples that. And other thing we're just finding the distance
in story to distance vary. But then here, I'm just showing it on the screen, I just
drew it in just simply drawing the text on the screen. Here I've just created f straight
and it just does in the distance variable here. So it is the position and the font and
the skill level how much it now the fourth trigger should be and white is the color.
And this is the thickness of the text. So we can increase up to let's think two will
increase to one. So you can see this is larger takes. Well, I have to open up a CMD here.
So here you can see the distances become larger. So I have to show you that how far it can
go. So first thing, we're going to use this distance dimension into the report. Another
thing you can use this into any programming tool, because you can process all the image
processing on your computer and connect through Wi Fi to Work program and better Beltone because
I can't afford that don't so I'm not going to use this. But we're going to use this into
Genesis report. Since a price reduction is not that stable, we can change it into the
QR code detection because your code is fairly stable then face detection because the face
detection wonderbox are not that stable. Another thing you have any concern or Command, leave
them down below. I will put all the ill mentioned thing in the description below that you cannot
say that I will put provide you code as well. So thank you so much for watching Allah Hafiz
Transcribed by https://otter.ai