>> All right. Let's try and actually
create some code that calls an API,
that someone else has written.
This is thing we frequently end up doing in our code.
Maybe it's somebody in their companies
written and stored as a web service,
or maybe we want to call
a web service that someone at a different company
is created for us but does
some neat functionality that we'd like to leverage.
So in this case, I want to call
the analyze image which is part of the Cognitive Service in Azure.
So if I bring up the documentation, and remember,
it's often going to come back to go look
through the documentation, do a search.
It tells you how to first create
the service because remember I can need that key,
so I can call it, and the way you do that in
with this particular one and you have to create the service first.
In this case, that does mean I need an Azure account.
So there are tutorials or tell you how to
go create your account for free,
and create the Cognitive Services resource.
So I already have my Azure account,
and I've already logged in at portal.azure.com,
so I've gone to the Azure page,
and then I say, I find
the easiest way if I know what I'm creating, and in this case,
I want to create because I know that
"Analyze Image" is part of the Computer Vision API.
So I've already logged in to the Azure Portal at portal.azure.com.
Because I know from the documentation,
that I need to create a Computer Vision API,
I can just go to the Portal and I find the easiest thing to
do to actually go and do a search for Computer Vision,
and you'll actually see it pop up Computer Vision rather
than digging my way through
all the sub-menus to find the one I want.
I can click on "Computer Vision" and it says,
"Oh, you must want to create a Computer Vision API?"
Yes, I do thank you.
That's how we get the key and the address I need to call it.
Let's give it a name, and let's just
call this playing pythonimageanalyzer.
So I'm writing this little, I will write Python code,
that's going to pass an image,
and it's going to analyze the image for me.
You do have to have a Microsoft subscription,
I'm just going to pick my Visual Studio subscription here.
Now, the address is going to
depend on where you have the service created.
So the way Azure works in the Cloud is when you create a service,
you actually get to pick which data center it's stored on.
So you want to pick one that's fairly physically
close to where you are at the moment,
I leaped up in Canada so I might pick Eastern US,
or there are some in Canada as well,
and then the pricing tier,
you'll notice there is a free tier F in this case is free version,
and for what I'm doing the free tier
is going to be perfectly fine,
a lot of times when you just want to try something,
you can try it for free,
and you only have to go to a paid version when you're
doing something in production,
and then the resource group.
A resource group is
like a file folder which keeps your stuff together.
So when you're creating multiple resources in
the Cloud multiple services,
you can put them in one resource group,
and that way you know where they're all located to delete them,
or modify them later.
So let's just create a new resource group called rgpythonfun.
So I'm just going to create a new one.
Then if I do anything else later,
I'll just keep it in that same resource group.
Think like a project folder.
So I can create that,
and then what's going to happen is
it's going to create that subscription,
but for the sake of simplicity,
I have already got one that's created.
Once it's done, I can actually go to
my all my resources and I will actually say,
"No, I don't want to leave that.
Thank you very much." It is "Validating."
Oh, it's still checking to make sure it's good.
There we go. So it's going off and creating that service for me.
Now once it's created the service,
I want to go to the "Overview" page.
That's most important page for me to be able to call it.
Because what'll happen is when
I go to the resource, and I go to "Overview",
it'll tell me the address of where
that service is located and the key I need to call it.
Remember those were two of the things I needed to call a service.
So here, the endpoint, that's my address.
So in this case, I picked East US,
so it's eastus.api,
that's how it knows which of the various addresses.
Remember all the different addresses we saw here,
the one that you use depends on where you create the service.
Here, we have that managed keys.
So if I click on "Show access keys",
it will actually give me the keys here,
and I'll be able to copy and paste them. There we go.
Two different keys, I can choose from,
doesn't matter what you use,
and I can just copy that,
and when my code when it asks me the key,
that's the key I'll put into my code.
All right. So now,
I have a web service ready to call.
Now, I need to go to my Python code and
actually call it see something happen.
So what I've done
here is I've got some code
and I'm just going to walk you through it.
This is in the GitHub repository,
so you'll be able to download a copy of this code,
play with it yourself,
create your own service and explore it.
This is when you really want to spend some time playing with.
You'll see some familiar commands.
I'm importing that requests library.
That makes it easier for me to make my HTTP calls from Python.
Importing another one called json,
I'm going to talk more about JSON in the next module,
but the data that comes back from
the service I called is in format called json.
So I'll talk more about that next module.
But I just need that for holding results come back.
I've already copied and pasted my subscription key over,
that I'm going to need to have.
So that Azure knows I have permission to call this analyze image.
I have my address,
this was from a service I created earlier so
the address is different than the one we just showed,
but if that's the address of the service I created,
and the address in this case,
you also have to specify but it's analyzed.
Because this is the address of the Computer Vision API,
and I need to specify,
but I want to call the "analyze"
function of the Computer Vision API.
How do I know that? Guess what?
When I know that? It's because I looked at the documentation.
So you can see here,
that when you call it,
you'll see here it says "Request URL."
You'll see it says, "Analyze."
So here's the address,
the only thing that changes is the server name,
and analyze is the name of the method.
So always in the documentation I find this stuff it.
Let's go back to our code.
Keep going through and see what else we can figure it out.
So we had the address.
So we know where we're calling. We have the function name.
Now we have the parameters we want to pass
into the function because anytime you call a function,
you have to pass it in the right parameters.
So I need to pass in.
What are parameters? Backed with documentation, visualFeatures,
a string indicating what visual feature types to return.
Multiple values should be separated by commas.
Valid values include.
So this is basically saying when I ask
this function to analyze an image,
I have to tell it what information I want back.
Do I want to know the colors that are in the image?
A description of the image?
If there's spaces in the image?
What objects or images?
Or tags that are an image?
So I've chosen to say give me back a description and the color.
So those might be the one. You could add more,
takeaway less and change those.
I've also decided that I would like to have it back in English.
So I would like if it says, "There's a bear,
I went to work bear in English."
So I specified my parameters.
Now in this case,
I have to pass in that actual image and I want to analyze.
So I actually have a subfolder,
but I've created on my computer,
and I'll have that in the GitHub for you as well.
It's called a PolarBear.
So it's just a picture of polar bear.
[inaudible] If I opened it up,
I can put that into an image object.
So I'm doing some Python here,
this is a little bit new,
if you look up this is the open command
that allows you to open a file,
and put it into an image object.
So I can pass that image to the function.
Now, I also have to pass in those HTTP headers.
That was something else I had to do.
In this case, what do I pass in?
Check the documentation what do I need for the headers.
Here we go. I need the Content-Type.
The Content-Types can neither be json,
or octet-stream or multipart form- data.
These are the Content-Types I can choose from.
Basically, that just indicates am I passing
the image as here's the address of the image on the web?
Or note here's the actual image?
If you're passing in the actual image, that's octet-stream.
So you'll notice my Content-Type is set to octet-stream.
The key is the key that I entered earlier.
I've just put it there.
By the way, right now,
I've just put the SUBSCRIPTION_KEY in the code,
not a best practice.
I'm doing it so you can follow my code and play with it,
but if you had that key,
if I kept this key, and you knew it,
you could call it,
and call the web service and I be the one paying for it.
Not the way I want things to work.
It's me giving you my house key.
You can come to my house and opened the door.
If I give you my API key,
you can go to this API and pretend you're me.
So I don't want to do that. I want to protect it.
Christopher's going to show you the right way
to store your API keys in an upcoming module.
So make sure you watch that one.
So now, I have all the information I need it.
I have the address.
I have the headers.
I have the parameters.
Now I'm ready to call this web service out there somewhere.
Well, on the data center.
So I call requests.post.
I pass it the address,
I pass it the headers,
I pass to the parameters,
and I passed it the data. But I've just set up.
I've got an extra line of code here,
but I find is really useful.
What this raised for status does is it basically says,
if that when I call that web service
something weird happens like an error.
Please raise an exception,
so I can handle it just like any other Python error.
Because that allows me to do my try final list and things
like that in my code which you learned in the error handling,
so that allows it to raise
the errors like an exception in my code.
So I can handle it the normal way.
All I'm going to do for now is I'm just going to
display the results on the screen,
don't worry too much about this code,
we're going to talk more about JSON shortly.
But if I'm lucky, then when I go here now, all that code,
but what's amazing is when I'm done, call_api.py.
Hopefully, it's now actually going off to
a server somewhere out there checking my key,
making sure I had permission, passing in the image,
and coming back and saying, "Hey,
here's a bunch of information about that image."
Wow. All right.
We've successfully called a web service.
How to understand that they did it came back?
We'll, cover that in the module on JSON.