简体   繁体   English

Google Cloud Vision PHP — 提出批量请求

[英]Google Cloud Vision PHP — Make Batch Request

I need to perform an OCR analysis on an image for an university project.我需要对大学项目的图像执行 OCR 分析。

I am imposed to use PHP, unfortunately, on the Google Cloud Vision Documentation few are the code sample using PHP...我被迫使用 PHP,不幸的是,在 Google Cloud Vision 文档中很少有代码示例使用 PHP...

I succeed to perform OCR on one image at time but 80% of the time I have a lot of images (around 20) to treat at once.我成功地一次对一张图像执行 OCR,但 80% 的时间我有很多图像(大约 20 张)需要同时处理。

So I tried to use BatchRequest, to minimize the API calls, as specified here but I can't found how to build the $requests array they put at the top.所以我尝试使用 BatchRequest 来最小化 API 调用,如此指定但我找不到如何构建他们放在顶部的$requests数组。

Btw I tried other APIs like Tesseract but the recognition is not accurate enough to use.顺便说一句,我尝试了其他 API,例如 Tesseract,但识别不够准确,无法使用。

If you only want to perform a batch request you can just use batchAnnotateImages using ImageAnnotatorClient .如果您只想执行批处理请求,您可以使用batchAnnotateImages使用ImageAnnotatorClient Below you can find a sample using it as well as a way to create a request variable.您可以在下面找到使用它的示例以及创建request变量的方法。 Also, I include below a asyncBatchAnnotateImages sample but I recommend the one I mentioned earlier.此外,我在下面包含了一个asyncBatchAnnotateImages示例,但我推荐我之前提到的那个。

Using ImageAnnotatorClient with batchAnnotateImages将 ImageAnnotatorClient 与 batchAnnotateImages 一起使用

<?php 

require '../vendor/autoload.php';
  
use Google\Cloud\Storage\StorageClient; 
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature_Type; 
use Google\Cloud\Vision\V1\ImageAnnotatorClient; 
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;
use Google\Cloud\Vision\V1\AnnotateImageRequest; 
use Google\Cloud\Vision\V1\Likelihood;

$client = new ImageAnnotatorClient();

try {
    $feature = (new Feature())
        ->setType(Feature_Type::FACE_DETECTION);

    $image = (new Image())
        ->setContent(file_get_contents("../images/family.jpg","r"));

    $request = (new AnnotateImageRequest())
        ->setImage($image)
        ->setFeatures([$feature]);

    $requests = [$request];
    # note: you can add as many requests you want to perform. ie: [$request,$request2,..,..]

    $results = $client->batchAnnotateImages($requests);
    foreach($results->getResponses() as $result){
        foreach ($result->getFaceAnnotations() as $faceAnnotation) {
            $likelihood = Likelihood::name($faceAnnotation->getJoyLikelihood());
            echo "Likelihood of headwear: $likelihood" . PHP_EOL;
        }
    }
   
} finally {
    $client->close();
}

Using ImageAnnotatorClient with asyncBatchAnnotateImages将 ImageAnnotatorClient 与 asyncBatchAnnotateImages 一起使用

<?php 

require '../vendor/autoload.php';
  
use Google\Cloud\Storage\StorageClient; 
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature_Type; 
use Google\Cloud\Vision\V1\ImageAnnotatorClient; 
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;
use Google\Cloud\Vision\V1\AnnotateImageRequest; 
use Google\Cloud\Vision\V1\asyncBatchAnnotateImages;
use Google\Cloud\Vision\V1\OutputConfig;
use Google\Cloud\Vision\V1\GcsDestination;

$client = new ImageAnnotatorClient();

try {
    $feature = (new Feature())
        ->setType(Feature_Type::FACE_DETECTION);

    $gcsImageUri = 'gs://<YOUR BUCKET ID>/<YOUR IMAGE FILE>';
    $source = new ImageSource();
        $source->setImageUri($gcsImageUri);

    $image = (new Image())
        ->setSource($source);

    $request = (new AnnotateImageRequest())
        ->setImage($image)
        ->setFeatures([$feature]);

    $requests = [$request];

    $gcsDestination = (new GcsDestination())
        ->setUri("gs://<YOUR BUCKET>/<OUTPUT FOLDER>/");

    $outputConfig = (new OutputConfig())
            ->setGcsDestination($gcsDestination);

    $operationResponse = $client->asyncBatchAnnotateImages($requests, $outputConfig);
    $operationResponse->pollUntilComplete();
    if ($operationResponse->operationSucceeded()) {
        $result = $operationResponse->getResult(); 
        var_dump($result);
        #Your Folder output will have your file processing results.
    }
   
} finally {
    $client->close();
}

Note: To add on this, you can also check an official implementation of a similar case using vision client on this link but its a sample to detect text on a pdf file.注意:除此之外,您还可以在此链接上使用视觉客户端检查类似案例的官方实施,但它是检测 pdf 文件中文本的示例。

You can also find additional information on these links:您还可以在这些链接上找到更多信息:

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

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