【问题标题】:How to properly set up Google cloud vision on my localhost in PHP?如何在我的本地主机上用 PHP 正确设置谷歌云视觉?
【发布时间】:2018-10-05 16:58:32
【问题描述】:

我想使用 Google Cloud Vision 来检测图像属性。我已经在 Google Cloud 上创建了一个帐户,并在此处 (https://cloud.google.com/vision/docs/detecting-properties#vision-image-property-detection-gcs-php) 的其中一个代码 sn-p 上找到了确切的解决方案。

我复制并调整它以达到我想要的效果。我使用 composer google/cloud-vision 安装了他们的包。

这是我的代码:

<?php 

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\VisionClient;

 $projectId = 'YOUR_PROJECT_ID';
 $path = 'event1.jpg'; 

function detect_image_property($projectId, $path)
{
    $vision = new VisionClient([
        'projectId' => $projectId,
    ]);
    $image = $vision->image(file_get_contents($path), [
        'IMAGE_PROPERTIES'
    ]);
    $result = $vision->annotate($image);
    print("Properties:\n");
    foreach ($result->imageProperties()->colors() as $color) {
        $rgb = $color['color'];
        printf("red:%s\n", $rgb['red']);
        printf("green:%s\n", $rgb['green']);
        printf("blue:%s\n\n", $rgb['blue']);
    }
}

detect_image_property($projectId, $path); 


?> 

所以当我运行我的代码时,它会抛出这个错误:

Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\VisionClient' not found in C:\xampp\htdocs\vision\index.php:12 Stack trace: #0 C:\xampp\htdocs\vision\index.php(28): Google\Cloud\Samples\Vision\detect_image_property('YOUR_PROJECT_ID', 'event1.jpg') #1 {main} thrown in C:\xampp\htdocs\vision\index.php on line 12

现在我想知道我的下一步是什么,我的下一步是什么
$projectId = 'YOUR_PROJECT_ID'

*如果这个问题需要更多解释,请在评论中告诉我,而不是投反对票。

谢谢。

【问题讨论】:

    标签: php google-cloud-platform google-cloud-vision


    【解决方案1】:

    @Abiodun Adetona

    Project-Id :这是私钥,我们必须使用 Google Cloud Vision 生成例如 - https://cloud.google.com/vision/docs/libraries#client-libraries-install-php

    根据错误,我们可以说它无法找到您的文件 - Google\Cloud\Samples\Vision;

    为了避免这种情况,我们必须加载 require __DIR__ . '/vendor/autoload.php'; 使用前的文件

    namespace Google\Cloud\Samples\Vision;
    

    【讨论】:

    • 创建密钥时应该选择哪个服务帐号?
    • 这是我在顶部添加require __DIR__ . '/vendor/autoload.php'; 时出现的错误。 Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in C:\xampp\htdocs\vision\index.php on line 6
    • 谢谢。这是一个新错误Fatal error: Uncaught Google\Cloud\Core\Exception\ServiceException: { "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }
    • 在文档cloud.google.com/vision/docs/… 中,完成第6 步后,检查Linux/macOS 或Windows 的示例以设置环境变量GOOGLE_APPLICATION_CREDENTIALS。
    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多