php - PDF 版 Google 视觉
问题描述
我需要将 PDF 文件发送到 Google Vision 以提取和返回文本。从文档中我了解到 DPF 文件必须位于 Google Storage 上,因此我将文件放入我的 Google Storage 存储桶中,如下所示:
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,数组)#4 /home/domain/vendor/google/cloud/Vision/src/V1/Gapic/ImageAnnotatorGapicClient.php(216): Google\Clou in /home/domain/vendor/google/gax/src/CredentialsWrapper.php在线 200
我如何对此服务进行身份验证?我错过了什么?
解决方案
我意识到当文档在某种程度上缺乏组织、内容或好的示例时,这会是多么令人沮丧。这就是我最终自己做的事情,最终让我的脚本能够工作。希望它也会对某人有所帮助:
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'];
推荐阅读
- firebase - 在本地运行 Firebase 可调用 HTTP 云函数
- reactjs - 为什么 ts-jest 不加载我的自定义 tsconfig 文件?
- c++ - 我正在尝试制作一个基本转换器,但它不起作用
- tensorflow - KeyError:'未能格式化此回调文件路径:原因:\'lr\''
- php - 如何修复日期索引中的此未定义错误
- java - Puttty 中的 Hadoop mapreduce perfirmance 命令
- javascript - 动画元素移动到路径的开头/结尾?
- solidity - 尝试在 Pancakeswap 上编译和部署用于单产农业的智能合约
- assembly - ARM 程序集加密扩展
- javascript - 如果函数是异步的,如何将函数结果映射到列表?