Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

PHP - how to extract text from image using Google Cloud Vision

namespace GoogleCloudSamplesVision;    

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

    use GoogleCloudVisionVisionClient;

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




    use GoogleCloudVisionV1ImageAnnotatorClient;


    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:Windowssystem32configsystemprofileAppDataRoaminggcloud/application_default_credentials.json) is not within the allowed path(s): (C:/Inetpub/vhosts/xxxxxxx.com;C:WindowsTemp) in C:Inetpubvhostsxxxxxxx.comvendorgoogleauthsrcCredentialsLoader.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:Inetpubvhostsxxxxxxx.comvendorgoogleauthsrcApplicationDefaultCredentials.php:168
Stack trace:
#0 C:Inetpubvhostsxxxxxxx.comvendorgooglegaxsrcCredentialsWrapper.php(197): GoogleAuthApplicationDefaultCredentials::getCredentials(Array, Object(GoogleAuthHttpHandlerGuzzle6HttpHandler), NULL, NULL)
#1 C:Inetpubvhostsxxxxxxx.comvendorgooglegaxsrcCredentialsWrapper.php(114): GoogleApiCoreCredentialsWrapper::buildApplicationDefaultCredentials(Array, Object(GoogleAuthHttpHandlerGuzzle6HttpHandler))
#2 C:Inetpubvhostsxxxxxxx.comvendorgooglegaxsrcGapicClientTrait.php(339): GoogleApiCoreCredentialsWrapper::build(Array)
#3 C:Inetpubvhostsxxxxxxx.comvendorgooglegaxsrcGapicClientTrait.php(321): GoogleCloudVisionV1GapicImageAnnotatorGapicClient->createCredentialsWrapper(NULL, in C:Inetpubvhostsxxxxxxx.comvendorgooglegaxsrcCredentialsWrapper.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...