我已经尝试使用Codeigniter PHP通过API ImageAnnotator实现Google云愿景。
我已经使用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中使用Google Cloud vision ImageAnnotatorClient的正确方法是什么?
对Google Cloud API的身份验证是否存在问题?
谢谢
答案 0 :(得分:0)
我自己找到了解决方法。
这是使用带有服务帐户密钥的Google Cloud ImageAnnotator的正确方法。
var usuario=req.body.username;