Google Cloud Vision ImageAnnotator Google 应用程序凭据文件不存在 Codeigniter PHP
Google Cloud Vision ImageAnnotator Google Application Credential File Not Exist Codeigniter PHP
我尝试使用 codeigniter PHP.
通过 API ImageAnnotator 实现 google 云视觉
我已经在 codeigniter 中的第三方目录中使用 composer 安装了要求 google 云视觉。
这是我控制器中的代码:
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();
}
}
我收到错误:
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
我不明白为什么会出现这个错误:
http://localhost/theseeds/assets/google_cloud_vision/credentials.json不存在
因为当我打开 link 文件时,文件就在那里。
这个错误:
File:
D:\xampp\htdocs\theseeds\application\controllers\Admin_center.php Line: 3188
Function: getMiddleware
是一行代码:
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
在 codeigniter PHP 中使用 google 云视觉 ImageAnnotatorClient 的正确方法是什么?
google 云 api 的身份验证有问题吗?
谢谢
我自己找到了解决方案。
这是使用带有服务帐户密钥的 google 云 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);
}
}
我尝试使用 codeigniter PHP.
通过 API ImageAnnotator 实现 google 云视觉我已经在 codeigniter 中的第三方目录中使用 composer 安装了要求 google 云视觉。
这是我控制器中的代码:
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();
}
}
我收到错误:
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
我不明白为什么会出现这个错误:
http://localhost/theseeds/assets/google_cloud_vision/credentials.json不存在
因为当我打开 link 文件时,文件就在那里。
这个错误:
File: D:\xampp\htdocs\theseeds\application\controllers\Admin_center.php Line: 3188
Function: getMiddleware
是一行代码:
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
在 codeigniter PHP 中使用 google 云视觉 ImageAnnotatorClient 的正确方法是什么?
google 云 api 的身份验证有问题吗?
谢谢
我自己找到了解决方案。
这是使用带有服务帐户密钥的 google 云 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);
}
}