视觉问答 (VQA)

Imagen for Captioning & VQA (imagetext) 是支持视觉问答的模型的名称。Imagen for Captioning & VQA 可以基于给定的图片回答问题,即便模型之前未曾见过该图片。

如需在控制台中探索此模型,请参阅 Model Garden 中的 Imagen for Captioning & VQ 模型卡片。

前往 Model Garden

使用场景

图片问题和解答的一些常见用例包括:

  • 通过问答功能帮助用户与视觉内容互动。
  • 让客户能够与零售应用和网站上显示的产品图片互动。
  • 为视障用户提供无障碍功能选项。

HTTP 请求

POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/imagetext:predict

请求正文

{
  "instances": [
    {
      "prompt": string,
      "image": {
        // Union field can be only one of the following:
        "bytesBase64Encoded": string,
        "gcsUri": string,
        // End of list of possible types for union field.
        "mimeType": string
      }
    }
  ],
  "parameters": {
    "sampleCount": integer,
    "seed": integer
  }
}

对视觉问答生成模型 imagetext 使用以下参数。如需了解详情,请参阅使用视觉问答 (VQA)

参数 说明 可接受的值
instances 一个数组,包含要获取其相关信息的对象以及提示和图片详细信息。 数组(允许 1 个图片对象
prompt 您要解决的图片问题。 字符串(最多 80 个词元)
bytesBase64Encoded 要获取其相关信息的图片。 采用 Base64 编码的图片字符串(PNG 或 JPEG,最大 20 MB)
gcsUri 要获取其相关信息的图片的 Cloud Storage URI。 Cloud Storage 中图片文件的字符串 URI(PNG 或 JPEG,最大 20 MB)
mimeType 可选。您指定的图片的 MIME 类型。 字符串(image/jpegimage/png
sampleCount 生成的文本字符串数。 整数值:1-3
seed 可选。随机数生成器 (RNG) 的种子。如果包含输入的请求的 RNG 种子相同,则预测结果将相同。 整数

示例请求

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Google Cloud 项目 ID
  • LOCATION:您的项目的区域。 例如 us-central1europe-west2asia-northeast3。如需查看可用区域的列表,请参阅 Vertex AI 上的生成式 AI 位置
  • VQA_PROMPT:有关图片的问题,您希望获得该问题的回答。
    • 这只鞋是什么颜色的?
    • 衬衫上用的是哪种类型的袖套?
  • B64_IMAGE:要获取其说明的图片。图片必须指定为 base64 编码的字节字符串。大小上限:10 MB。
  • RESPONSE_COUNT:您要生成的回答数量。接受的整数值:1-3。

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagetext:predict

请求 JSON 正文:

{
  "instances": [
    {
      "prompt": "VQA_PROMPT",
      "image": {
          "bytesBase64Encoded": "B64_IMAGE"
      }
    }
  ],
  "parameters": {
    "sampleCount": RESPONSE_COUNT
  }
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagetext:predict"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagetext:predict" | Select-Object -Expand Content
以下示例响应适用于包含 "sampleCount": 2"prompt": "What is this?" 的请求。该响应会返回两个预测字符串答案。
{
  "predictions": [
    "cappuccino",
    "coffee"
  ]
}

响应正文


{
  "predictions": [
    string
  ]
}
响应元素 说明
predictions 表示 VQA 答案的文本字符串列表,按置信度排序。

示例响应

以下示例响应适用于包含 "sampleCount": 2"prompt": "What is this?" 的请求。该响应会返回两个预测字符串答案。

{
  "predictions": [
    "cappuccino",
    "coffee"
  ],
  "deployedModelId": "DEPLOYED_MODEL_ID",
  "model": "projects/PROJECT_ID/locations/us-central1/models/MODEL_ID",
  "modelDisplayName": "MODEL_DISPLAYNAME",
  "modelVersionId": "1"
}