Search code examples
phpcodeignitergoogle-cloud-platformgoogle-api-php-clientgoogle-cloud-vision

Google Cloud Vision ImageAnnotator Google Application Credential File Not Exist Codeigniter PHP


I have try to implement the google cloud vision with API ImageAnnotator using a codeigniter PHP.

I have install the require google cloud vision using a composer to my third party directory in codeigniter.

This is the code looks like in my controller :

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



    }
}

I got the error :

Type: DomainException

Message: Unable to read the credential file specified by GOOGLE_APPLICATION_CREDENTIALS: file http://localhost/theseeds/assets/google_cloud_vision/credentials.json does not exist

Filename: D:\xampp\htdocs\theseeds\application\third_party\vendor\google\auth\src\CredentialsLoader.php

Line Number: 74

File: D:\xampp\htdocs\theseeds\application\controllers\Manage_center.php Line: 3188
Function: getMiddleware

I dont understand why this error occur :

http://localhost/theseeds/assets/google_cloud_vision/credentials.json does not exist

Because when i opened the link the file is there.

And this error :

File: D:\xampp\htdocs\theseeds\application\controllers\Admin_center.php Line: 3188
Function: getMiddleware

is a line code :

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

What is the proper way to use the google cloud vision ImageAnnotatorClient in codeigniter PHP ?

Is there a problem with the authentication to google cloud api ?

Thank You


Solution

  • I found the solution myself.

    This is how the right way to use the google cloud ImageAnnotator with service account key.

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