Hi everyone welcome back.
So in this book you are going to be starting with the classification class which is really where all
the heavy work goes.
OK.
So first of all we need to talk about a couple of things.
OK so we'll going to first open Firefox and we're going to go to a link so we're going to write Tancer
flow and then we're going to write in section v3.
OK.
So inception version 3.
OK.
When you write it you should see the first link here.
Image recognition if you click on it and then go inside there you to see that you know they give you
a model and then they talk to you.
If you go down to use you as an API then you're going to see that there is a file called classify underscore
image doppio.
And it's very simple you're just going to go into this link so it's going to tell you go to this link
start by cloning this links are going to click on this link.
It's going to open a good repository with all the models of the answer flow and then what you're going
to go is you're going to go to models tutorials image and then Image it.
So we're going to go come here and then you're going to this is already on Mars.
If you look at the or there's already models here.
So what you're going to do is let me try to make this bigger.
You're going to go.
Let's see if we can make a slightly bigger Yes.
There we go.
I'm sure you're going to go into tutorials.
And then we're going to go into image and then Image net and then you should see or classify underscore
and Match.com.
OK so we're going to click on this year we want to copy all of this file files are going to take this
file from start go all the way up.
Copy all this file because this is the file that we're going to be using to classify them.
Great.
So I'm going to copy this file and then I'm going to come here.
Can I see this year and make a new document.
Paste this and then control s go into desktop and then into misclassification web.
And then I'm going to save it here as classify.
So underscore image wise I've just downloaded this file and saved it here and that's it.
OK so this model is supposed to be able to train an image that you give it.
And I'm going to show you how you can use it in this video.
OK.
So first of all the thing that we were going to do is we just look.
So first of all I want I want us to look at the file and just get a feel of how this is working.
Before we actually use the file OK.
So the first thing is when you call this file it first get some arguments OK.
So you pass some arguments into this file.
So the first argument is the model directory.
So you need to download it where you download the model directory from.
OK.
So this model is what we're going to download.
OK.
So this model is if you go here if you go here you're going to find there is a right or in the first
link.
So if you go back you're in this link here we're going to find full data underscore your else.
Let me just zoom in here.
So go down.
You're going to find that your ally here and this is where the model is going to come from.
So we're going to copy this thing here.
OK.
Because we want to download the model locally and then we're going to go here and then paste and then
click enter.
Now it should tell you to save up to save the model.
So the inception model is very big.
It's actually if not super big race 85 megabytes or relatively big.
So you're going to save the file.
I already downloaded it so I won't download it again but once you download this file then you're going
to go.
So you're going to close that and then you're going to go to where you downloaded it.
So in my case I've downloaded the model on the desktop so you can see it here.
Inception here.
So I'm going to cut this.
And then I copy it going to image classification.
And then what's so image classification wrongful there I guess.
So image classification folder.
And then I'm going to go into web and then I'm going to paste it here.
So I'm going to leave it here.
OK.
So paste it paste this thing here and that's it.
That's it for me.
So I'm just going to leave the model here and you can see why I've left it here in a few seconds.
OK so what is this doing.
Let's go back to the arguments.
Let's let's zoom out a bit.
So first of all we need a model directory so it's selling.
We need to tell tensor flow which model to use to predict what classification the images.
So what model is going extensor flow going to use to predict whether this is an animal or a zebra or
a giraffe or so on.
OK.
The second argument is image files so you need to give this file of your an argument which is image
file.
And this image files that you are to an image where where it can infer what or classify this image.
This is the image that we want to classify.
Right.
This is the image that we're going to be sending out.
And then also number of predictions.
So how many predictions do you want.
So for example if you remember when we were talking about the API that is going up to give you the top
five predictions.
So for example it might give you the first one is 80 percent the second one is 5 percent.
The second 31st percent 2 percent 1 percent and so on.
Right.
So it's going to give you the percentages but in descending order of how positive it is off this prediction
and then another thing is going to parse all these arguments and put it in flags.
So it's going to put flet into it into flags and then it's going to say denser flow.
Please run the function main.
OK.
So let's go back to the function menu.
So this is the function main.
So first of all it's going to be to call the function maybe download and extract.
So this function what this is what it was what it's going to do is if you have the model this inception
model then it's not going to do anything.
It's just going to return.
But if you don't there is going to use this data you are all here to download them all.
And that is going to return.
OK.
So in other words even if I remove this.
So let's go see you in Maine here even if I remove this line.
This file would still work.
Right.
But I'm just going to leave now the second thing is going to find that you are EHLO that image.
So it's going to get to you or of that image that you want to classify.
And then it's going to call the function run in friends an image.
OK.
So it's going to inference.
So if you have an image and we want to do some inference on you know what's inside of it.
So if I go to run interference on and let's say if I go to the function it says run introns an image
so it takes an argument or an image file and then it returns nothing.
OK so what is going to do is do.
It's going to do some machinery new stuff.
So we're not going to go into too much details since everyone might know what machine learning is or
how to use.
Actually the answer to most people no machinery used but how to use underflow.
But it's basically going to start predicting what this image might be and then if you come down here
you're going to say top k is equal to predictions are X or it.
So it's going to get to sort of prediction in descending order.
So it gives you the best the best prediction for example 80 percent prediction a panda or a zebra.
Then it's rancid.
The number one right so cupcake is going to be two to sort the predictions that the model here predicted
and then is going to go for each one of them is going to score is equal to prediction of ID and then
it's going to pre-print the human strain so the human strain here is basically zebra or that's a giraffe
or human or Angelina Jolie.
So the mystery here is what this model thinks this image is off.
And then the score is the percentage of how confident it is.
So for example I'm 30 percent sure that this is Angelina Jolie or I'm 40 percent sure that this is bracket.
OK.
So we it's going to print what what it thinks the prediction is and how confident it is off this prediction.
OK for each of the predictions.
All right.
Hopefully this makes the sense.
So when we're writing the classify function what we're going to do is actually we want to get this top.
Dictionary that has all the predictions right.
So we got what we're going to do is we're going to you're right in this fight we're going to call this
fight.
OK.
We're going to call this a subprocess.
And given the image file and give it all the other stuff.
And then this file is going to predict the what this image is and that it's going to write in this directory
over here.
It's going to write a file with a dictionary.
Then once this happens in Abu Dhabi why I'm going to read this file which has the predictions and then
I'm going to load it into a dictionary.
So you're going to see what I mean by that.
And if just a few seconds.
So first of all let's write the class.
So class classify resource.
So what I did just was I wanted to make sure that you understand even a very high logic of what's happening
in this file.
So because we're going to come here and we're going to be hitting this area over here in a few minutes.
OK.
So the first thing is we have a class called classified.
So what are we at what what does it support what method does it support.
Post something on his own defined post self.
And then first of all we want to get the posted data as usual.
So you can your request that get your score.
So that's the first thing.
And then the second thing is we're to get the user name is equal to hosted data username.
Right.
And the password of password is equal to posted data and then password right.
So now the third thing we need is you or else we're going to need your elders to posted data.
But then this time is going to be you or L or write code.
So now we have everything that we need.
Now the first thing is well we need to verify the credentials of this user.
OK.
So for this I'm going to write a function just to make this it just do not clutter this area I'm going
to write a function called verify credentials that is given a username and password and it tells me
if there is any error or not and if there is an error it returns to me the return Jaisalmer that I am
going to retrieve.
So when I do return some error is equal to verify credentials username and password.
OK.
So if air is true so if there is an heir so the user name doesn't exist or your name doesn't match the
password then this is going to be true.
And this is going to have the return Jaser.
But there is this is going to be false.
And this is going to be none.
OK.
So I'm not to say if air then returned the sun if I returned Jason.
OK.
So so far I haven't written to verify credentials.
Right so I can put here.
I asked AG to do OK because I haven't done it.
All right.
So the second thing is we need to verify that the user had enough tokens right.
So I say tokens is equal to users that's fine that's fine.
And then I want to find the username with the username that is posted and then got access the first
ones since there is only one of the Great.
And then I'm also going to access the element tokens right.
All right then I'm going to check if tokens is less than or equal to zero that what I want to do is
to return an error of 0 3 not enough tokens right.
You remember from here three or three out of tokens.
So the way I'm going to do this instead of having to do a return Jasen equal to this and then status
and then message right instead of having to do all this for every time I want to retune something I'm
going to write the helper function.
OK.
So I'm going to say If tokens is less than or equal to zero then return Jaison if I.
OK.
And then I'm going to write a new function called let's see let's call it generate return to action.
We generate return dictionary and he's going to have two arguments status and comment.
OK.
So for here I want us I want the status to be three of eight and for the comment to be let's say not
enough tokens.
Right.
So what this is going to do is to generate a dictionary for me with this and this and this message and
return it and then I'm going to just have to fight and return.
So instead of having to do it on multiple lines we can just do it on one line.
Right.
Makes the code much shorter.
So there's also a to do here.
So let's put a hash tag to do here because we still haven't written this function.
So the next thing we're going to do is we want to get the image OK from the euro.
So not to say R is equal to.
So this is how we use requests of an s.
So R is equal to your requests don't get.
And then I'm going to put the U R L in there.
OK.
So I'm going to get.
What's the content of this you are.
And then I'm going to say returned J is equal to just an empty.
So I'm just going to leave that as an empty dictionary for now that I'm going to say with open.
So I'm going to open 10 that JP J Gates I'm going to just store a temporary image and then it's going
to be W. B as F as efforts and then I'm going to do.
F dot right.
So I'm going to write it too.
I want to write into f the content of R.
OK so are has the image that I want then I'm going to open a new file called template that JP G.
I'm going to write into the content of art which is the image itself.
OK.
So that's how simple it is to download an image in Python all right once I've done that then I'm going
to do process is equal to.
So I'm going to say process.
I want to do a new process so I'm going to say some Prozess dot be open.
So this is the syntax open and then Python classify underscore image Doppie y.
So this is the file over here.
So I'm going to open a new subprocess with this file Paice and classify underscore image not quite and
that I'm going to pass the argument that the firewall.
So the first one was models underscore or I think it was.
Let's check.
So it was modeled on your score directorates.
No models it's a model underscore it or tree is equal to.
And then I'm going to write dot because the font the the the the inception file as in this year this
folder.
So it's in the current folder and the image so the image file is in this folder and it's at dot slash
10 and the GBG right.
Because if you remember we downloaded the image and we store it in temporary GBG.
So now what we have is the image file is stored in dot slash stamped GMP.
OK.
Now we're going to do.
Process dot communicate.
OK.
Zero.
And then we're going to process that.
Wait.
So we're going to wait until this process here is done.
And this final and this subprocess writes the response Jason in a file.
Let's say it's called text.
Yes Steve.
OK.
So don't wait for it.
And then now I want to get the of the the open the file that they have.
OK.
So I'm going to do with Open Text DXi.
So what what writes this file is actually in this subprocesses over here that opens this file.
So with open text that DST as.
And then for example or let's say as G.
And then we're going to do return J-Zone is equal to Jaison load and then G.
OK.
So I'm going to load the the the the dictionary that I stored into the the file text of the XTi And
then finally once since I have the return adjacent here so I now have it.
So once I exit out of all of this I'm just going to say Well first of all I want to return the return
Jason to the user.
But before I do this I need to take away one token from me because I used my service.
So when I do users that update users update and then inside here we're going to do an user name and
then it's going to be username.
And what I'm going to update them with is I'm going to set his tokens to be one less than what he has
and the new dollar set.
And then hear him say tokens are going to be tokens.
The variable minus one.
So now he has one less pair.
So you can see that we already got the tokens right.
So he has one less token to worry about.
OK.
And now once we have done that we just returned to him the response which is generated by this file
over here.
Now let's go to this file and now we want to change this file to reflect what what this users actually
is expecting right.
So how do we write this returned Jason into the final text of the XTi.
So we're going to go up here and then you can see here that there is a top Caze equal to predictions
done on X or it slides a number addiction.
OK.
So after that I'm going to write return Jason is equal to an empty dictionary ok.
Then inside this loop here I'm going to say return J-Zone human unstring is equal to a score.
OK so for each you ministering I'm going to give it a score.
All right.
And then once I'm done with this process.
So once I've generated this return Jason what I'm going to do is I'm going to store that or dump that
into the text.
64.
So I'm going to do with Open Text dx d as F and then I'm going to do Jaison that dump and then return
J.
So I'm going to return this I'm going to dump this returned Jason and that I filled with that with the
results and then I'm going to do Also a half into after the file text of DST and that's it.
So we've just added a couple a few lines and then basically it is written into a file called texta DXi.
Then from here what happens is after I run this command this command finishes so after post is dead.
Wait what i do is I open the file text Dickstein that this file here has written and then I get visé
Sunfire.
So this way I've sent it there result from this classify image file into my file apt up yours.
OK.
So hopefully this made sense I wasn't and wasn't you know too difficult to understand.
Now there's a couple of things that we need to do first.
We have a couple of to do is right so there's a few functions that we haven't done.
So the first one is to verify credentials.
So why do you fine verify credentials.
We have a username and password.
OK.
OK so how is the function going to look like someone to say first if not use or exists or it doesn't
exist then what are we going to do.
We're going to return generate return dictionaries.
I'm going to.
So I'm going to return this generate return dictionary and when I generate a new dictionary with the
air feel one and the message invalid username username and then I'm also going to return true because
remember this function returns two things.
It returns a dictionary and true right.
The air here is going to be true.
OK next I'm also going to do the correct password correct.
Underscore password is equal to verify.
Password.
So I'm gonna do a new function called the verify password that I'll give the user name user name and
the password and he's going to tell me whether this user name matches the password or not.
Then unless if not correct password.
Then what do I do.
Well I return.
Generate return dictionary of 3 0 2 and then invalid password.
And also true because there's an error here.
OK.
What is the password is correct and the user exists then that means that I there's no errors I return
none and false.
So zero errors and none came.
The dictionary is just not great because we don't need it.
There is no errors.
Right.
But then we've used here the generator your own dictionary and verify password so you need to write
them.
OK so the first one is the verify password.
So you find verify password we take in a username and password and then if not user exist username then
we're going to return false.
Right because this this user name doesn't exist so the verify verifying the password failed.
Also then we're going to hash get the hashed password so hashed password is equal to users not find.
OK.
And then inside the phone fine are you going to get their username the correct user.
So user name user name and then for the hashed function I want to get the passwords I'm going to do
zero and then password.
Right.
This is the hash password not the plain text.
After I've done that I'm going to say if be script write the script that hash password and then password
not encode UTF 8 UTF 8 bits 7.
So UTF 8 with hash password.
So this should be hashed password is equal to hash password.
Then I'm going to return true.
OK.
So we're going to return true in this case because the passwords hash is matched match.
Else I'm going to return false.
So this is not correct.
The Richards-Ross or it.
So now we need another five to five on charities that generate return dictionary ok it's around right.
Define return or generate return dictionary.
So it's defined generate return dictionary and it's going to take a status and a message.
Right.
So I'm going to say return Jaison is equal to status is status and the message as the message right
and the.
And we just return this return return Jason and that's it.
So we generate just using the status of the message we automatically in one line generate this Jason
File here without having to waste a lot of lines.
So that should be it for the classification.
Right so we've done everything.
And then finally we have the refill function which we'll do in the next video games are going to stop
here and in the next video we're going to pick up from where we stop.
OK.
So until the next video recording.