Google Cloud Vision客户端库(PHP)图像标签-结果数量?

时间:2019-03-21 08:34:42

标签: php google-api google-api-php-client google-vision

我正在使用上面提到的PHP库(Google Cloud Vision客户端库v1)为图像分配标签...到目前为止,一切都很好。一切正常,除了返回的结果少于Google测试页上的结果...据我了解,它与“ max_results”参数有关,该参数默认为10,但我无法找到在哪里/如何设置手动... 在Python上存在一个类似的问题,它就像将其作为参数传递一样简单-我已经尝试了许多在PHP中执行此操作的选项,但显然我做错了...

以下是文档的链接:https://googleapis.github.io/google-cloud-php/#/docs/cloud-vision/v0.19.3/vision/v1/imageannotatorclient?method=labelDetection 我猜我必须将其传递给“ optionalArgs”参数...但不确定如何做到这一点...

这里或多或少是我的代码:

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

$this->client = new ImageAnnotatorClient();
$response = $this->client->labelDetection(...THE IMAGE...);
$labels = $response->getLabelAnnotations();

if ($labels) {
    foreach ($labels as $label) {
        // do something with $label->getDescription()
    }
}

有人知道如何在$ labels数组中获得更多结果吗?

2 个答案:

答案 0 :(得分:1)

新方法

由于我提供的其他答案似乎已被弃用,因此我将提供一个使用setMaxResults method on the Feature object的示例。

$imageAnnotatorClient = new ImageAnnotatorClient();
$gcsImageUri = 'some/image.jpg';
$source = new ImageSource();
$source->setGcsImageUri($gcsImageUri);
$image = new Image();
$image->setSource($source);
$type = Feature_Type::FACE_DETECTION;
$featuresElement = new Feature();
$featuresElement->setType($type);
$featuresElement->setMaxResults(100); // SET MAX RESULTS HERE
$features = [$featuresElement];
$requestsElement = new AnnotateImageRequest();
$requestsElement->setImage($image);
$requestsElement->setFeatures($features);
$requests = [$requestsElement];
$imageAnnotatorClient->batchAnnotateImages($requests);

不推荐使用的方法

Image constructor

中指定了maxResults值

可以在Image对象的源代码中找到example of this code

 $imageResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
 $image = new Image($imageResource, [
   'FACE_DETECTION',
   'LOGO_DETECTION'
 ], [
     'maxResults' => [
         'FACE_DETECTION' => 1
     ],
     'imageContext' => [
          ....
     ]
   ]
]);

答案 1 :(得分:0)

好,所以对于仍然需要此功能的任何人来说,这都是一个可行的示例

    use Google\Cloud\Vision\Image;
    use Google\Cloud\Vision\VisionClient;

    $imageResource = fopen(__DIR__ .'/'. $fileIMG, 'r');

    $thePic = new Image($imageResource, [
         'LABEL_DETECTION',
          'LOGO_DETECTION',
          'TEXT_DETECTION'
     ], [
         'maxResults' => [
             'LABEL_DETECTION' => 20,
             'LOGO_DETECTION' => 20,
             'TEXT_DETECTION' => 20
         ],
         'imageContext' => []
     ]);


    $vision = new VisionClient();
    $result = $vision->annotate($thePic);

    $finalLabels = array();

    // do the same for $results->text(), $results->logos()
    if($result->labels()){
        foreach ($result->labels() as $key => $annonObj) {
            $tmp = $annonObj->info();
            $finalLabels[] = $tmp['description'];
        }
    }

但是...如官方文档中所述

  • 请注意,此客户端将在我们的下一版本中弃用。顺序
  • 要获取最新功能和更新,请采取
  • 有时间熟悉{@see Google \ Cloud \ Vision \ V1 \ ImageAnnotatorClient}。

所以我仍然需要一种使用ImageAnnotatorClient类来实现此目的的方法...有任何想法吗?