简体   繁体   中英

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

Installed the SDK and PHP Package and I'm getting error 500 no matter what I do!

How can I get this running?

This is the error I get:

[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

I have done everything exactly how their documentation said to: https://cloud.google.com/vision/docs/setup what am I suppose to do now?

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM