Help Center/ Image Recognition/ SDK Reference/ Using the PHP SDK/ Demo Project of Image Tagging
Updated on 2024-12-05 GMT+08:00

Demo Project of Image Tagging

This section describes how to use the Image Tagging SDK by AK/SK authentication.

  • Replace the AK/SK in the sample code with the actual AK/SK. You can use either the url or image parameter in the initialized ImageTaggingReq to configure image information. This section uses url as an example.
  • endpoint indicates the regions and endpoints for Huawei Cloud services. For details, see Regions and Endpoints.
  1. The sample code for calling Image Tagging is as follows:
    <?php
    namespace HuaweiCloud\SDK\Image\V2\Model;
    require_once "vendor/autoload.php";
    use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
    use HuaweiCloud\SDK\Core\Http\HttpConfig;
    use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;
    use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;
    use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;
    use HuaweiCloud\SDK\Image\V2\ImageClient;
       // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
       // In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables CLOUD_SDK_AK and CLOUD_SDK_SK.
    $ak = getenv('CLOUD_SDK_AK');
    $sk = getenv('CLOUD_SDK_SK');
    //Replace image.xx-xxx-xx.myhuaweicloud.com with the endpoint of the region where the service is enabled.
    $endpoint = "https://image.xx-xxx-xx.myhuaweicloud.com"; 
    $projectId = "";
    $credentials = new BasicCredentials($ak,$sk,$projectId);
    $config = HttpConfig::getDefaultConfig();
    $config->setIgnoreSslVerification(true);
    $client = ImageClient::newBuilder(new ImageClient)
      ->withHttpConfig($config)
      ->withEndpoint($endpoint)
      ->withCredentials($credentials)
      ->build();
    $request = new RunImageTaggingRequest();
    $body = new ImageTaggingReq();
    $body->setLimit(10);
    $body->setThreshold(60);
    $body->setLanguage("zh");
    $body->setImage ("Input image encoded using Base64");
    $request->setBody($body);
    try {
      $response = $client->RunImageTagging($request);
      echo "\n";
      echo $response;
    } catch (ConnectionException $e) {
      $msg = $e->getMessage();
      echo "\n". $msg ."\n";
    } catch (RequestTimeoutException $e) {
      $msg = $e->getMessage();
      echo "\n". $msg ."\n";
    } catch (ServiceResponseException $e) {
      echo "\n";
      echo $e->getHttpStatusCode(). "\n";
      echo $e->getRequestId(). "\n";
      echo $e->getErrorCode() . "\n";
      echo $e->getErrorMsg() . "\n";
    }
  2. If the recognition result is displayed on the console, the execution is successful. For details about related parameters, see Image Tagging APIs.
    1
    2
    RunImageTaggingResponse {"result": {"tags": [{"confidence": "98.01", "type": "Tree", "tag": "Tree", "i18n_tag": {"zh": "Chinese characters for tree", "en": "Tree"}, "i18n_type": {"zh": "Chinese characters for tree", "en": "Tree"}, "instances": []}]}} 
    Process exiting with code: 0