首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP - google vision API -找不到类

PHP - google vision API -找不到类
EN

Stack Overflow用户
提问于 2020-10-09 18:20:57
回答 2查看 416关注 0票数 0

我在尝试使用google vision API时收到此错误消息

代码语言:javascript
复制
Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\VisionClient' not found in C:\xampp\htdocs\index.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 7

我在windows上用的是xampp。我使用composer安装了Google api (以管理员身份)

代码语言:javascript
复制
composer require google/cloud-vision

我也运行(以admin身份)

代码语言:javascript
复制
composer install
composer update

google cloud sdk已安装。

这是我的代码

代码语言:javascript
复制
<?php
require 'C:\Users\MyUser\vendor\autoload.php';
use Google\Cloud\Vision\VisionClient;

$path = 'caption.jpg';

$vision = new VisionClient([
    'projectId' => 'my-project-numbers',
    'keyFilePath' => 'my-key.json'
]);

// Annotate an image, detecting faces.
$image = $vision->image(
    fopen($path, 'r'),
    ['text']
);

$tadaa = $vision->annotate($image);

echo '<pre>';
var_dump($tadaa->text());
echo '</pre>';

?>
EN

回答 2

Stack Overflow用户

发布于 2020-10-09 18:41:47

根据官方文档,我建议使用这个php客户端库:

代码语言:javascript
复制
# imports the Google Cloud client library
use Google\Cloud\Vision\V1\ImageAnnotatorClient;

Quickstart: Using client libraries Vision Api

票数 1
EN

Stack Overflow用户

发布于 2020-10-09 19:46:50

使用https://cloud.google.com/vision/docs/ocr中的代码

下面是index.php

代码语言:javascript
复制
<?php
namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

$path = 'caption.jpg';

    $imageAnnotator = new ImageAnnotatorClient();

    # annotate the image
    $image = file_get_contents($path);
    $response = $imageAnnotator->textDetection($image);
    $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();

?>

我还是得到了

代码语言:javascript
复制
Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\V1\ImageAnnotatorClient' not found in C:\xampp\htdocs\index.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 8

这是composer.json

代码语言:javascript
复制
{
    "require": {
        "google/cloud-vision": "^1.2"
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64278132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档