In this quickstart, you analyze a remotely stored image to extract visual features by using Computer Vision's REST API. With the Analyze Image method, you can extract visual features based on image content.
If you don't have an Azure subscription, create a free account before you begin.
- You must have PHP installed.
- You must have Pear installed.
- You must have a key for Computer Vision. You can visit the Microsoft Cognitive Services Web site, create a new Azure account, and try Cognitive Services for free. Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.
To create and run the sample, do the following steps:
-
Install the PHP5
HTTP_Request2
package.-
Open a command prompt window as an administrator.
-
Run the following command:
pear install HTTP_Request2
-
After the package is successfully installed, close the command prompt window.
-
-
Copy the following code into a text editor.
-
Make the following changes in code where needed:
- Replace the value of
key
with your key. - Replace the value of
uriBase
with the endpoint URL for the Analyze Image method from the Azure region where you obtained your keys, if necessary. - Optionally, replace the value of
imageUrl
with the URL of a different image that you want to analyze. - Optionally, replace the value of the
language
request parameter with a different language.
- Replace the value of
-
Save the code as a file with a
.php
extension. For example,analyze-image.php
. -
Open a browser window with PHP support.
-
Drag and drop the file into the browser window.
<html>
<head>
<title>Analyze Image Sample</title>
</head>
<body>
<?php
// Replace <key> with a valid key.
$ocpApimkey = '<key>';
// You must use the same location in your REST call as you used to obtain
// your keys. For example, if you obtained your keys
// from westus, replace "westcentralus" in the URL below with "westus".
$uriBase = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/';
$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';
require_once 'HTTP/Request2.php';
$request = new Http_Request2($uriBase . '/analyze');
$url = $request->getUrl();
$headers = array(
// Request headers
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => $ocpApimkey
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
'visualFeatures' => 'Categories,Description',
'details' => '',
'language' => 'en'
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_POST);
// Request body parameters
$body = json_encode(array('url' => $imageUrl));
// Request body
$request->setBody($body);
try
{
$response = $request->send();
echo "<pre>" .
json_encode(json_decode($response->getBody()), JSON_PRETTY_PRINT) . "</pre>";
}
catch (HttpException $ex)
{
echo "<pre>" . $ex . "</pre>";
}
?>
</body>
</html>
A successful response is returned in JSON. The sample website parses and displays a successful response in the browser window, similar to the following example:
{
"categories": [
{
"name": "outdoor_water",
"score": 0.9921875,
"detail": {
"landmarks": []
}
}
],
"description": {
"tags": [
"nature",
"water",
"waterfall",
"outdoor",
"rock",
"mountain",
"rocky",
"grass",
"hill",
"covered",
"hillside",
"standing",
"side",
"group",
"walking",
"white",
"man",
"large",
"snow",
"grazing",
"forest",
"slope",
"herd",
"river",
"giraffe",
"field"
],
"captions": [
{
"text": "a large waterfall over a rocky cliff",
"confidence": 0.916458423253597
}
]
},
"requestId": "ebf5a1bc-3ba2-4c56-99b4-bbd20ba28705",
"metadata": {
"height": 959,
"width": 1280,
"format": "Jpeg"
}
}
When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2
package. To uninstall the package, do the following steps:
-
Open a command prompt window as an administrator.
-
Run the following command:
pear uninstall HTTP_Request2
-
After the package is successfully uninstalled, close the command prompt window.