【发布时间】: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