PHP - 如何使用 Google Cloud Vision 从图像中提取文本

Edu*_*ruh 0 php google-cloud-platform

namespace Google\Cloud\Samples\Vision;    

require_once('../vendor/autoload.php');

    use Google\Cloud\Vision\VisionClient;

    $vision = new VisionClient([
        'projectId' => 'xxx',
        'keyFilePath' => 'xxx.json'
    ]);




    use Google\Cloud\Vision\V1\ImageAnnotatorClient;


    function detect_text($path)
    {
        $imageAnnotator = new ImageAnnotatorClient();

        # annotate the image
        $image = file_get_contents($path);
        $response = $imageAnnotator->textDetection($image);
        $texts = $response->getTextAnnotations();

        printf('%d texts found:' . PHP_EOL, count($texts));
        foreach ($texts as $text) {
            print($text->getDescription() . PHP_EOL);

            # get bounds
            $vertices = $text->getBoundingPoly()->getVertices();
            $bounds = [];
            foreach ($vertices as $vertex) {
                $bounds[] = sprintf('(%d,%d)', $vertex->getX(), $vertex->getY());
            }
            print('Bounds: ' . join(', ',$bounds) . PHP_EOL);
        }

        $imageAnnotator->close();
    }

    echo detect_text('read.png');
Run Code Online (Sandbox Code Playgroud)

安装了 SDK 和 PHP 包,无论我做什么,我都会收到错误 500!

我怎样才能让它运行?

这是我得到的错误:

[01-Feb-2020 19:09:48 UTC] PHP Warning:  file_exists(): open_basedir restriction in effect. File(C:\Windows\system32\config\systemprofile\AppData\Roaming\gcloud/application_default_credentials.json) is not within the allowed path(s): (C:/Inetpub/vhosts/xxxxxxx.com\;C:\Windows\Temp\) in C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\auth\src\CredentialsLoader.php on line 100
[01-Feb-2020 19:09:50 UTC] PHP Fatal error:  Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\auth\src\ApplicationDefaultCredentials.php:168
Stack trace:
#0 C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\gax\src\CredentialsWrapper.php(197): Google\Auth\ApplicationDefaultCredentials::getCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler), NULL, NULL)
#1 C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\gax\src\CredentialsWrapper.php(114): Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler))
#2 C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\gax\src\GapicClientTrait.php(339): Google\ApiCore\CredentialsWrapper::build(Array)
#3 C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\gax\src\GapicClientTrait.php(321): Google\Cloud\Vision\V1\Gapic\ImageAnnotatorGapicClient->createCredentialsWrapper(NULL, in C:\Inetpub\vhosts\xxxxxxx.com\vendor\google\gax\src\CredentialsWrapper.php on line 200
Run Code Online (Sandbox Code Playgroud)

我已经按照他们的文档所述完成了所有操作: https: //cloud.google.com/vision/docs/setup我现在应该做什么?

Edu*_*ruh 5

我通过使用 SDK 文件夹内的 key.json 以及 PHP 脚本内的 key.json 解决了这个问题,所以两次。

谷歌云官方文档中的示例代码完全毫无价值,即使正确初始化了 SDK 和 PHP 包,仍然会给出 500 错误。

我在github上找到了一个工作代码,我稍微修改了一下:

require_once('../vendor/autoload.php');

use Google\Cloud\Vision\VisionClient;

$vision = new VisionClient([
    'projectId' => 'xxxx',
    'keyFilePath' => 'key.json'
]);

// Annotate an image, detecting faces.
$image = $vision->image(
    fopen('read.png', 'r'),
    ['text']
);

$tadaa = $vision->annotate($image);


echo '<pre>';
var_dump($tadaa->text());
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)

现在经过几个小时的努力终于成功了!