0

我是第一次实施谷歌云愿景。成功创建产品集、产品并为产品分配图像。当我尝试执行发送 base64 编码图像的产品搜索时,结果始终为空。但是当我尝试使用来自谷歌云存储的图像时,它正在工作。知道为什么它不起作用吗?

    $productSearchClient = new ProductSearchClient();
    $productSetPath = $productSearchClient->productSetName(config('web.google_vision.project_id'), config('web.google_vision.location'), 2);

    # product search specific parameters
    $productSearchParams = (new ProductSearchParams())
        ->setProductSet($productSetPath)
        ->setProductCategories(['general-v1']);

    # search products similar to the image
    $imageAnnotatorClient = new ImageAnnotatorClient(); 
    //$image = 'gs://picfly-bucket/wendys-5.jpeg';
    $image = base64_encode(file_get_contents(public_path('gallery/test/wendy-search.png')));
    $response = $imageAnnotatorClient->productSearch($image, $productSearchParams);
    dd($response->getProductSearchResults());
4

1 回答 1

1

根据此文档,您的代码读取本地文件并通过在请求中包含内联原始图像字节(base64 编码图像)来查询 API。因此,您不应显式调用 base64_encode()。Vision 库默认使用 base64 编码。您只需调用 fopen() 即可打开本地图像数据。代码如下所示:

$image = fopen('gallery/test/wendy-search.png', 'r');
$response = $imageAnnotatorClient->productSearch($image, $productSearchParams);
于 2021-11-01T07:44:42.217 回答