You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Vision API调用特定图片URL返回空响应的解决方法

解决Google Vision API无法解析juicer.io图片链接的问题

问题原因

juicer.io的图片链接存在防盗链机制:浏览器访问该链接时会携带Cookie、Referer等验证信息,而Google Vision API通过image_uri直接请求时,没有这些验证信息,服务器会拒绝返回图片内容,导致API返回空响应。

解决方案:先下载图片到本地再处理

绕过远程请求限制,先将图片下载到本地,再通过本地文件路径调用Vision API处理,具体步骤如下:

  1. 安装requests库(用于下载图片):
pip install requests
  1. 修改代码,添加下载逻辑并读取本地图片:
from google.cloud import vision
import os
import sys
import requests

def download_image(url, save_path):
    # 模拟浏览器请求头,规避防盗链拦截
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
        "Referer": "https://www.juicer.io/"
    }
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    with open(save_path, "wb") as f:
        f.write(response.content)

if __name__ == '__main__':
    path = os.path.abspath(os.path.dirname(sys.argv[0])) 
    credsFN = os.path.join(path, "creds.json")
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credsFN

    # 目标juicer.io图片链接
    imageURL = "https://www.juicer.io/api/posts/459631754/images.jpg?external_id=CiE264cqZcw&s=199f57b090efba5ef332c3ff09d7b14554f84bfc"
    local_image_path = os.path.join(path, "temp_image.jpg")

    # 下载图片到本地
    try:
        download_image(imageURL, local_image_path)
    except Exception as e:
        print(f"下载图片失败: {e}")
        sys.exit(1)

    # 调用Vision API处理本地图片
    client = vision.ImageAnnotatorClient()
    with open(local_image_path, "rb") as image_file:
        content = image_file.read()
    image = vision.Image(content=content)
    
    features = [vision.Feature(type_=vision.Feature.Type.LABEL_DETECTION)]
    requ = vision.AnnotateImageRequest(image=image, features=features)
    resp = client.annotate_image(request=requ)
    
    # 输出标注结果
    if resp.label_annotations:
        for label in resp.label_annotations:
            print(f"{label.score:4.0%}: {label.description}")
    else:
        print("未检测到标注结果,请检查图片是否有效")
    
    # 可选:删除临时图片
    os.remove(local_image_path)

关键说明

  • 伪装浏览器请求:通过User-Agent和Referer模拟浏览器访问,避免被juicer.io的防盗链机制拦截。
  • 本地文件处理:Vision API读取本地图片时不受远程服务器限制,能稳定获取图片内容进行分析。

内容的提问来源于stack exchange,提问作者Rapid1898

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:47:48