【问题标题】:Google Cloud Vision ImageAnnotator Google Application Credential File Not Exist Codeigniter PHPGoogle Cloud Vision ImageAnnotator Google Application Credential File Not Exist Codeigniter PHP
【发布时间】:2019-08-23 04:35:34
【问题描述】:

我已尝试使用 codeigniter PHP 通过 API ImageAnnotator 实现谷歌云视觉。

我已经使用 composer 将 require google cloud vision 安装到我在 codeigniter 中的第三方目录中。

这是我的控制器中的代码:

defined('BASEPATH') OR exit('No direct script access allowed');

use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;        

class Manage_center extends CI_Controller {

    function __construct() {
        parent::__construct();

        include APPPATH . 'third_party/vendor/autoload.php';
    }

    public function index()
    {
        $this->load->view('index');
    }


    function upload_ocr_image()
    {               
        //img_data contain image => i just shorten the code.
        $img_data = $this->upload->data();                                          

        // Authenticating with a keyfile path.
        putenv('GOOGLE_APPLICATION_CREDENTIALS='.base_url().'assets/google_cloud_vision/credentials.json');
        $scopes = ['https://www.googleapis.com/auth/cloud-vision'];

        // create middleware
        $middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
        $stack = HandlerStack::create();
        $stack->push($middleware);

        $imageAnnotator = new ImageAnnotatorClient();

        # annotate the image                
        $response = $imageAnnotator->textDetection($img_data['full_path']);             
        $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();



    }
}

我得到了错误:

类型:DomainException

消息:无法读取凭证 GOOGLE_APPLICATION_CREDENTIALS 指定的文件:文件 http://localhost/theseeds/assets/google_cloud_vision/credentials.json 不存在

文件名: D:\xampp\htdocs\theseeds\application\third_party\vendor\google\auth\src\CredentialsLoader.php

行号:74

文件: D:\xampp\htdocs\theseeds\application\controllers\Manage_center.php 行:3188
功能:getMiddleware

我不明白为什么会出现这个错误:

http://localhost/theseeds/assets/google_cloud_vision/credentials.json 不存在

因为当我打开链接时,文件就在那里。

还有这个错误:

文件: D:\xampp\htdocs\theseeds\application\controllers\Admin_center.php 行:3188
功能:getMiddleware

是一行代码:

$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);

在 codeigniter PHP 中使用谷歌云视觉 ImageAnnotatorClient 的正确方法是什么?

google cloud api的认证有问题吗?

谢谢

【问题讨论】:

    标签: php codeigniter google-cloud-platform google-api-php-client google-cloud-vision


    【解决方案1】:

    我自己找到了解决方案。

    这是使用带有服务帐户密钥的谷歌云 ImageAnnotator 的正确方法。

    defined('BASEPATH') OR exit('No direct script access allowed');
    
    use Google\Cloud\Vision\VisionClient;
    
    class Admin_center extends CI_Controller {
        function __construct() {
            parent::__construct();
    
            include APPPATH . 'third_party/vendor/autoload.php';
        }
    
        public function index() {
            $this->load->view('index');
        }
    
        function upload_ocr_image() {               
            $img_data = $this->upload->data();                                          
    
            $vision = new VisionClient(['keyFile' => json_decode(file_get_contents('credentials.json'), true)]);
    
            $imageRes = fopen($img_data['full_path'], 'r');
            $image = $vision->image($imageRes,['Text_Detection']);
            $result = $vision->annotate($image);
    
            print_r($result);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 2022-12-27
      • 2017-07-07
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多