谷歌PDF视觉识别。

9

我需要将PDF文件发送到Google Vision以提取并返回文本。根据文档,我了解到PDF文件必须位于Google存储中,因此我会像这样将文件放入我的Google存储桶中:

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

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'keyFilePath' => '/my-keyfile.json',
    'projectId' => PROJECT_ID
]);

$bucket = $storage->bucket(BUCKET_NAME);

$bucket->upload(
    fopen($_SESSION['local_pdf_url'], 'r')
);

它可以工作。当我重定向到另一个页面,该页面应该将该文件发送到Vision,但这就是失败的地方。我找到了一个示例函数。以下是代码:

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

use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\AnnotateFileResponse;
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\GcsDestination;
use Google\Cloud\Vision\V1\GcsSource;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\InputConfig;
use Google\Cloud\Vision\V1\OutputConfig;

$storage = new StorageClient([
    'keyFilePath' => '/my-keyfile.json',
    'projectId' => PROJECT_ID
]);

$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];

当我运行第二个脚本时,我收到以下错误提示:
致命错误:未捕获的DomainException异常:无法加载默认凭据。请查看https://developers.google.com/accounts/docs/application-default-credentials获取更多信息。文件路径为/home/domain/vendor/google/auth/src/ApplicationDefaultCredentials.php:168,堆栈跟踪:#0 /home/domain/vendor/google/gax/src/CredentialsWrapper.php(197):Google\Auth\ApplicationDefaultCredentials::getCredentials(Array,Object(Google\Auth\HttpHandler\Guzzle6HttpHandler),NULL,NULL)#1 /home/domain/vendor/google/gax/src/CredentialsWrapper.php(114):Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(Array,Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) #2 /home/domain/vendor/google/gax/src/GapicClientTrait.php(326):Google\ApiCore\CredentialsWrapper::build(Array)#3 /home/domain/vendor/google/gax/src/GapicClientTrait.php(308):Google\Cloud\Vision\V1\Gapic\ImageAnnotatorGapicClient->createCredentialsWrapper(NULL,Array)#4 /home/domain/vendor/google/cloud/Vision/src/V1/Gapic/ImageAnnotatorGapicClient.php(216):Google\Clou在/home/domain/vendor/google/gax/src/CredentialsWrapper.php的200行。
如何为此服务进行身份验证?我缺少了什么?
2个回答

6

我意识到当文档缺乏组织、内容或好的示例时,它可能会让人感到沮丧。以下是我自己最终采取的方法,使我的脚本能够正常工作。希望能对某些人有所帮助:

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

use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\AnnotateFileResponse;
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\GcsDestination;
use Google\Cloud\Vision\V1\GcsSource;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\InputConfig;
use Google\Cloud\Vision\V1\OutputConfig;

putenv('GOOGLE_APPLICATION_CREDENTIALS=/my-keyfile.json');

$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];

5
错误提示表明出现了身份验证问题。要解决此问题,请查看并遵循使用服务帐号中的说明以进行身份验证。
“用于身份验证的帐户必须具有访问您为输出指定的 Cloud 存储存储桶的权限 (角色/编辑器或角色/storage.objectCreator 或更高级别)。” - 更多信息请参见此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接