PHP-如何使用Google Cloud Vision从图像中提取文本 [英] PHP - how to extract text from image using Google Cloud Vision
问题描述
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');
安装了SDK和PHP软件包,无论我做什么我都会收到错误500!
Installed the SDK and PHP Package and I'm getting error 500 no matter what I do!
如何运行它?
这是我得到的错误:
[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
我已经按照他们的文档所说的完全做了: https://cloud.google.com/vision/docs/setup 我现在应该做什么?
I have done everything exactly how their documentation said to: https://cloud.google.com/vision/docs/setup what am I suppose to do now?
推荐答案
我通过使用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!
这篇关于PHP-如何使用Google Cloud Vision从图像中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!