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

问题描述 投票:0回答:2

我在PHP中使用上面提到的库(Google Cloud Vision Client Library v1)为图像分配标签......到目前为止一切都很好。这一切都有效,除了它返回的结果少于谷歌测试页面...据我所知,它与“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数组中获得更多结果?

php google-api google-api-php-client google-vision
2个回答
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);

不推荐使用的方法

maxResults值在Image constructor中指定

可以在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' => [
          ....
     ]
   ]
]);

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类...任何想法?

© www.soinside.com 2019 - 2024. All rights reserved.