错误调用未定义的方法 Google\Cloud\Vision\VisionClient::imageAnnotator()

问题描述 投票:0回答:0

我正在使用

google vision
在我的
Laravel
项目中进行图像搜索,

首先我在存储图像时分析图像并将其保存在数据库中以供以后搜索。

这是我正在使用的代码片段。

use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\VisionClient;

$file_name = $product->slug . '_' . time() . '_' . $i . '.' . $image->getClientOriginalExtension();
                    $file_size = $image->getSize();
                    $file_type = $image->getMimeType();
                    $path = public_path('images/products/' . $file_name);
                    Image::make($image->getRealPath())->save($path);

                    $vision = new VisionClient([
                        'keyFilePath' => public_path('turkey-383308-e3c56a43b464.json')
                    ]);

                    $imageAnnotator = $vision->imageAnnotator();
                    $response = $imageAnnotator->annotate(
                        $imageAnnotator->image(file_get_contents($path), ['LABEL_DETECTION', 'IMAGE_PROPERTIES'])
                    );
            
                    $labels = $response->getLabelAnnotations();
                    $colors = $response->getImagePropertiesAnnotation()->getDominantColors();


                    // // Analyze the image using the Google Cloud Vision API
                    // $imageAnnotator = new ImageAnnotatorClient(['credentials' =>public_path('turkey-383308-e3c56a43b464.json')]);
                    // // $imagePath = storage_path('app/' . $path);
                    // $image = file_get_contents($path);
                    // $response = $imageAnnotator->annotateImage($image, ['LABEL_DETECTION', 'IMAGE_PROPERTIES']);
                    // // Extract relevant features from the API response
                    // $labels = $response->getLabelAnnotations();
                    // $colors = $response->getImagePropertiesAnnotation()->getDominantColors();
                    // $landmarks = $response->getLandmarkAnnotations();

                    $product->media()->create([
                        'file_name' => $file_name,
                        'file_size' => $file_size,
                        'file_type' => $file_type,
                        'file_status' => true,
                        'file_sort' => $i,
                        'extracted_features'=> json_encode([
                            'labels' => $labels,
                            'colors' => $colors,
                         ])
                    ]);

我一开始尝试了

ImageAnnotatorClient
(注释代码) 但是有这个错误:

期待 Google\Cloud\Vision\V1\Feature

使用时

VisionClient
我明白了

调用未定义的方法

Google\\Cloud\\Vision\\VisionClient::imageAnnotator()

laravel google-cloud-platform google-cloud-functions image-recognition google-vision
© www.soinside.com 2019 - 2024. All rights reserved.