Quickstart: Recognize domain-specific content using the REST API and PHP with Computer Vision

In this quickstart, you use a domain model to identify landmarks or, optionally, celebrities in a remotely stored image by using Computer Vision's REST API. With the Recognize Domain Specific Content method, you can apply a domain-specific model to recognize content within an image.

If you don't have an Azure subscription, create a Trial before you begin.

Prerequisites

  • You must have PHP installed.
  • You must have Pear installed.
  • You must have a subscription key for Computer Vision. You can follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.

Create and run the sample

To create and run the sample, do the following steps:

  1. Install the PHP5 HTTP_Request2 package.

    1. Open a command prompt window as an administrator.

    2. Run the following command:

      pear install HTTP_Request2
      
    3. After the package is successfully installed, close the command prompt window.

  2. Copy the following code into a text editor.

  3. Make the following changes in code where needed:

    1. Replace the value of subscriptionKey with your subscription key.
    2. Replace the value of uriBase with the endpoint URL for the Recognize Domain Specific Content method from the Azure region where you obtained your subscription keys, if necessary.
    3. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
    4. Optionally, replace the value of the domain request parameter with celebrites if you want to use the celebrities domain model instead of the landmarks domain model.
  4. Save the code as a file with a .php extension. For example, use-domain-model.php.

  5. Open a browser window with PHP support.

  6. Drag and drop the file into the browser window.

<html>
<head>
    <title>Analyze Domain Model Sample</title>
</head>
<body>
<?php
// Replace <Subscription Key> with a valid subscription key.
$ocpApimSubscriptionKey = '<Subscription Key>';

$uriBase = 'https://api.cognitive.azure.cn/vision/v2.0/';

// Change 'landmarks' to 'celebrities' to use the Celebrities model.
$domain = 'landmarks';

$imageUrl =
    'https://upload.wikimedia.org/wikipedia/commons/2/23/Space_Needle_2011-07-04.jpg';

require_once 'HTTP/Request2.php';

$request = new Http_Request2($uriBase . 'models/' . $domain . '/analyze');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey
);
$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'model' => $domain
);
$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>

Examine the response

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:

{
    "result": {
        "landmarks": [
            {
                "name": "Space Needle",
                "confidence": 0.9998177886009216
            }
        ]
    },
    "requestId": "4d26587b-b2b9-408d-a70c-1f8121d84b0d",
    "metadata": {
        "height": 4132,
        "width": 2096,
        "format": "Jpeg"
    }
}

Clean up resources

When no longer needed, delete the file, and then uninstall the PHP5 HTTP_Request2 package. To uninstall the package, do the following steps:

  1. Open a command prompt window as an administrator.

  2. Run the following command:

    pear uninstall HTTP_Request2
    
  3. After the package is successfully uninstalled, close the command prompt window.

Next steps

Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing console.