且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-04-13 22:42:14

我通过使用SDK文件夹内以及PHP脚本内的key.json解决了它两次,

I solved it by using the key.json inside the SDK folder and also inside the PHP script, so two times.

Google官方云文档中的示例代码是完全没有价值的,即使正确初始化了SDK和PHP包,仍然会出现500错误.

And the example codes in the official google cloud documentation are completely worthless and are still giving 500 error even with SDK and PHP package correctly intialized.

我在github上找到了一个可以修改的工作代码:

I found a working code on github which I slightly modified:

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>';

现在,经过许多小时的努力,终于奏效了!

Now after many hours of struggle it finally WORKS!