简体   繁体   English

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

[英]Google Cloud Vision Client Library (PHP) Image Labels - Number of results?

I am using the upper mentioned library (Google Cloud Vision Client Library v1) in PHP to assign labels to images... so far so good. 我正在使用上面提到的PHP库(Google Cloud Vision客户端库v1)为图像分配标签...到目前为止,一切都很好。 It all works, except it returns fewer results than on the google test page... as far as I understand it has to do with a "max_results" parameter which defaults to 10, but I am not able to find where/how to set it manually... There was a similar question here on Python and there it was as simple as passing it as a parameter - I have tried many options to do this in PHP, but apparently I am doing something wrong... 一切正常,除了返回的结果少于Google测试页上的结果...据我了解,它与“ max_results”参数有关,该参数默认为10,但我无法找到在哪里/如何设置手动...在Python上存在类似的问题,它就像将其作为参数传递一样简单-我尝试了很多方法来在PHP中执行此操作,但显然我做错了...

Here is a link to the documentation : https://googleapis.github.io/google-cloud-php/#/docs/cloud-vision/v0.19.3/vision/v1/imageannotatorclient?method=labelDetection I am guessing I have to pass it to the "optionalArgs" parameter... but not exactly sure how to do this... 这是文档的链接: https : //googleapis.github.io/google-cloud-php/#/docs/cloud-vision/v0.19.3/vision/v1/imageannotatorclient? method =labelDetection我猜我有将其传递给“ optionalArgs”参数...但不完全确定如何执行此操作...

Here is more or less what my code is: 这或多或少是我的代码:

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()
    }
}

Anyone got an idea how to get more results in the $labels array? 有人知道如何在$ labels数组中获得更多结果吗?

New Method 新方法

Since the other answer I provided seems to be deprecated, I am going to provide a sample that uses the setMaxResults method on the Feature object . 由于我提供的其他答案似乎已被弃用,因此我将提供一个在Feature对象上使用setMaxResults方法的示例。

$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);

Deprecated Method 不推荐使用的方法

The maxResults value gets specified in the Image constructor Image构造函数中指定maxResults值

An example of this code can be found in the source code for the Image object. 可以在Image对象的源代码中找到此代码示例

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

OK, so for anybody who still may need this here is a working example 好,所以对于仍然需要此功能的任何人,这里都是一个有效的示例

    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'];
        }
    }

But... as it says in the official documentation 但是...正如官方文档中所说

  • Please note this client will be deprecated in our next release. 请注意,此客户端将在我们的下一个版本中弃用。 In order 为了
  • to receive the latest features and updates, please take 要接收最新功能和更新,请采取
  • the time to familiarize yourself with {@see Google\\Cloud\\Vision\\V1\\ImageAnnotatorClient}. 是时候让自己熟悉{@see Google \\ Cloud \\ Vision \\ V1 \\ ImageAnnotatorClient}。

So I still need a way to do this using the ImageAnnotatorClient class... any ideas anyone? 因此,我仍然需要一种使用ImageAnnotatorClient类实现此目的的方法...任何想法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM