Google Cloud Vision PHP — 提出批量请求

Google Cloud Vision PHP — Make Batch Request

我需要对大学项目的图像执行 OCR 分析。

我不得不使用 PHP,不幸的是,在 Google Cloud Vision 文档中很少有代码示例使用 PHP...

我一次成功地对一张图像执行了 OCR,但 80% 的时间我有很多图像(大约 20 张)需要同时处理。

所以我尝试使用 BatchRequest 来最小化 API 调用,正如指定的 here 但我找不到如何构建他们放在顶部的 $requests 数组.

顺便说一句,我尝试了其他 APIs,例如 Tesseract,但识别不够准确,无法使用。

如果您只想执行批处理请求,您可以使用 batchAnnotateImages 使用 ImageAnnotatorClient。您可以在下面找到使用它的示例以及创建 request 变量的方法。另外,我在下面包含了一个 asyncBatchAnnotateImages 示例,但我推荐我之前提到的那个。

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

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

注意:除此之外,您还可以在这个link上使用视觉客户端检查类似案例的官方实现,但它是检测文本的示例在 pdf 文件上。

您还可以在这些链接上找到更多信息: