图片批处理

除了流式处理之外,您还可以选择批处理作为从数据中获取信息的方式。这种非流式处理模式可让您构建应用来支持其他媒体类型。

您可以使用选定的模型批量处理存储在 Cloud Storage 中的图片文件。 输出存储在 Cloud Storage 中。

支持的模型

以下模型支持批处理:

  • 标记识别器
  • 商品识别器

支持的数据类型

本页介绍了如何批量处理以下类型的数据:

  • 图片数据

准备工作

用户体验历程

如需批量处理图片数据,请完成以下一般步骤:

  1. 启用 Gemini Enterprise Agent Platform Vision API。

  2. 创建受支持的处理程序(商品识别器或标记识别器)。

  3. 创建应用。

    1. 输入:添加一个通用输入节点,用于指定要在 Cloud Storage 中处理的文件。

    2. 处理:添加模型节点。

    3. 输出:添加输出存储节点,用于指定处理后的数据在 Cloud Storage 中的存储位置。

  4. 创建应用的批处理实例,每个实例对应一个 Cloud Storage 存储桶输入位置。

  5. 部署应用和应用实例。

  6. 批处理实例完成后,检查存储在 Cloud Storage 中的输出。

批量处理图片

发送批处理请求的步骤因模型而异。请按照目标模型的说明批量处理图片。

商品识别器模型

使用这些示例通过商品识别器模型批量处理图片。

控制台

在 Google Cloud 控制台中创建图片批处理请求。

创建新应用

  1. 打开 Gemini Enterprise Agent Platform Vision 信息中心的应用 标签页。

    前往“应用”标签页

  2. 点击 创建 按钮。

  3. 输入应用名称并选择区域。

  4. 点击继续 。

  5. 选择结算方式。如需详细了解按需付费 与按月结算,请参阅价格页面。

  6. 点击创建 。

指定批处理输入

  1. 在图表应用构建器页面中,点击通用输入 节点。

  2. 在侧边通用输入 窗格中,点击选择输入来源 。

  3. 在选择输入来源 页面中,选择 批量预测 。

  4. 点击继续 。

  5. 在来源 窗格中,点击位置选择器字段中的浏览 ,以指明文件在 Cloud Storage 中的位置。

  6. 可选。如需指定更多来源,请点击添加项 ,然后重复上一步。

  7. 点击提交 。

添加模型

  1. 在图表应用构建器页面中,点击专用模型 部分中的商品识别器 模型节点。

  2. 在商品识别器 窗格中,点击选择模型 。

  3. 选择 选择现有商品识别器模型。

    如果您需要创建新模型或索引,请选择相应选项以创建资源。

  4. 从列表中选择模型。

  5. 点击选择 。

  6. 可选。修改置信度阈值 。

  7. 点击应用设置 。

添加输出目标位置

  1. 在图表应用构建器页面中,点击输出 部分中的 Cloud Storage 模型节点。

  2. 在 Cloud Storage 窗格中,点击浏览 以选择 Cloud Storage 中的批量预测输出目标位置。

部署应用

  1. 在图表应用构建器页面中,点击 部署。

REST 和命令行

如需发送图片批处理请求,请完成以下步骤。

  1. 使用 projects.locations.processors.create 方法创建商品识别器模型。

    此请求包含对 Catalog 和 ProductRecognitionIndex 资源的引用。如需了解如何创建 Catalog和 ProductRecognitionIndex 资源,请参阅商品识别器模型指南。

    curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json" \
     https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/processors?processor_id=product-recognition-model1 \
     -d '{
       "display_name": "DISPLAY_NAME",
       "model_type": PRODUCT_RECOGNIZER,
       "custom_processor_source_info": {
         "source_type": PRODUCT_RECOGNIZER,
         "product_recognizer_artifact": {
           "retail_product_recognition_index":"projects/PROJECT_ID/locations/LOCATION_ID/retailCatalogs/CATALOG_ID/retailProductRecognitionIndexes/INDEX_ID",
         }
       }
     }'
    
  2. 使用新创建的商品识别器模型创建应用。 此请求使用 projects.locations.applications.create 方法。

    请求正文 (app.json):

    {
      "applicationConfigs": {
        "nodes": [
          {
            "displayName": "Universal Input",
            "name": "universal-input-0",
            "processor": "builtin:universal-input"
          },
          {
            "displayName": "Product Recognition",
            "name": "product-recognition",
            "nodeConfig": {
              "product_recognizer_config": {
                "recognition_confidence_threshold": 0.75
              }
            },
            "parents": [
              {
                "parentNode": "universal-input-0"
              }
            ],
            "processor": "projects/PROJECT_ID/locations/LOCATION_ID/processors/product-recognition-model1"
          },
          {
            "displayName": "Storage output",
            "name": "gcs-output",
            "nodeConfig": {
              "gcs_output_config": {
                "gcs_path":"gs://product_recognizer_app_output"
              }
            },
            "parents": [
              {
                "parentNode": "product-recognition"
              }
            ],
            "processor": "builtin:gcs-output"
          }
        ]
      }
    }
    

    请求:

    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      -d @app.json  \
      'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications?application_id=product-recognition-app'
    
  3. 使用 projects.locations.applications.createApplicationInstances 方法创建应用实例。

    请求正文 (instances.json):

     {
       "applicationInstances": [
         {
           "instance": { 
             "instanceType": "BATCH_PREDICTION",
             "inputResources": [
               {
                 "consumerNode": "universal-input-0",
                 "dataType": "IMAGE",
                 "inputResource": "gs://product_recognition_input"
               }
             ]
           },
           "instanceId": "instance1"
         },
         {
           "instance": { 
             "instanceType":"BATCH_PREDICTION",
             "inputResources": [
               {
                 "consumerNode": "universal-input-0",
                 "dataType": "IMAGE",
                 "inputResource": "gs://product_recognition_input2"
               }
             ]
           },
           "instanceId": "instance2"
         }
       ]
     }
    

    请求:

    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      -d @instances.json \
      'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/product-recognition-app:createApplicationInstances'
    
  4. 部署应用。

     curl -X POST \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json" \
       -d '{}' \
       'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/product-recognition-app:deploy'
    
  5. 获取应用实例。此信息会告知您批处理何时完成。

    具体而言,state 字段会显示处理何时 完成:"state": "FINISHED"。实例完成后,您无法对其进行修改。

    您可以使用 projects.locations.applications.instances.list方法 跟踪实例。同样,如需从此列表中移除实例,请使用 the projects.locations.applications.deleteApplicationInstances 方法。

    请求:

     curl -X GET \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json"  \
       'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/product-recognition-app/instances'
    

    示例响应:

     {
       "instances": [
         {
           "name": "projects/PROJECT_ID/locations/LOCATION_ID/applications/product-recognition-app/instances/instance1",
           "createTime": "2023-03-30T18:30:51.991004265Z",
           "inputResources": [
             {
               "inputResource": "gs://product_recognition_input",
               "consumerNode": "universal-input-0",
               "dataType": "IMAGE"
             }
           ],
           "outputResources": [
             {
               "outputResource": "gs://product_recognition_output/instance1",
               "producerNode": "product-recognition",
               "autogen": true
             }
           ],
           "updateTime": "2023-04-18T04:58:18.592092259Z",
           "state": "FINISHED",
           "instanceType": "BATCH_PREDICTION"
         }
     }
    

标记识别器模型

使用这些示例通过标记识别器模型批量处理图片。

REST 和命令行

如需发送图片批处理请求,请完成以下步骤。

  1. 使用 projects.locations.processors.create方法创建标记识别器模型。

    这需要您指定托管在 Vertex AI 平台中的原始模型的资源名称 (vertex_model)。

    curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json" \
     https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/processors?processor_id=tag-recognition-model1 \
     -d '{
       "display_name": "DISPLAY_NAME",
       "model_type": TAG_RECOGNIZER,
       "custom_processor_source_info": {
         "source_type": VERTEX_AUTOML,
         "vertex_model": "projects/PROJECT_ID/locations/LOCATION_ID/models/MODEL_ID"
       }
     }'
    
  2. 使用新创建的标记识别器模型创建应用。 此请求使用 projects.locations.applications.create 方法。

    请求正文 (app.json):

     {
       "applicationConfigs": {
         "nodes": [
           {
             "displayName": "Universal Input",
             "name": "universal-input-0",
             "processor": "builtin:universal-input"
           },
           {
             "displayName": "Tag Recognition",
             "name": "tag-recognition",
             "nodeConfig": {
               "tag_recognizer_config": {
                 "tag_parsing_config": {
                   "entity_parsing_configs": [
                     {
                       "entity_class": "price",
                       "regex": "\\$\\d+\\.\\d{2}",
                       "entity_matching_strategy": "MAX_OVERLAP_AREA"
                     }
                   ]
                 },
                 "entity_detection_confidence_threshold": 0.0
               }
             },
             "parents": [
               {
                 "parentNode": "universal-input-0"
               }
             ],
             "processor": "projects/PROJECT_ID/locations/LOCATION_ID/processors/tag-recognition-model1"
           },
           {
             "displayName": "Storage output",
             "name": "gcs-output",
             "nodeConfig": {
               "gcs_output_config": {
                 "gcs_path": "gs://tag_recognizer_app_output"
               }
             },
             "parents": [
               {
                 "parentNode": "tag-recognition"
               }
             ],
             "processor": "builtin:gcs-output"
           }
         ]
       }
     }
    

    请求:

    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      -d @app.json  \
      'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications?application_id=tag-recognition-app'
    
  3. 使用 projects.locations.applications.createApplicationInstances 方法创建应用实例。

    请求正文 (instances.json):

     {
       "applicationInstances": [
         {
           "instance": { 
             "instanceType": "BATCH_PREDICTION",
             "inputResources": [
               {
                 "consumerNode": "universal-input-0",
                 "dataType": "IMAGE",
                 "inputResource": "gs://tag_recognition_input"
               }
             ]
           },
           "instanceId": "instance1"
         },
         {
           "instance": { 
             "instanceType":"BATCH_PREDICTION",
             "inputResources": [
               {
                 "consumerNode": "universal-input-0",
                 "dataType": "IMAGE",
                 "inputResource": "gs://tag_recognition_input2"
               }
             ]
           },
           "instanceId": "instance2"
         }
       ]
     }
    

    请求:

    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      -d @instances.json \
      'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/tag-recognition-app:createApplicationInstances'
    
  4. 部署应用。

     curl -X POST \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json" \
       -d '{}' \
       'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/tag-recognition-app:deploy'
    
  5. 获取应用实例。此信息会告知您批处理何时完成。

    具体而言,state 字段会显示处理何时 完成:"state": "FINISHED"。实例完成后,您无法对其进行修改。

    您可以使用 projects.locations.applications.instances.list方法 跟踪实例。同样,如需从此列表中移除实例,请使用 the projects.locations.applications.deleteApplicationInstances 方法。

    请求:

     curl -X GET \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json"  \
       'https://visionai.googleapis.com/v1alpha1/projects/PROJECT_ID/locations/LOCATION_ID/applications/tag-recognition-app/instances'
    

    示例响应:

     {
       "instances": [
         {
           "name": "projects/PROJECT_ID/locations/LOCATION_ID/applications/tag-recognition-app/instances/instance1",
           "createTime": "2023-03-30T18:30:51.991004265Z",
           "inputResources": [
             {
               "inputResource": "gs://tag_recognition_input",
               "consumerNode": "universal-input-0",
               "dataType": "IMAGE"
             }
           ],
           "outputResources": [
             {
               "outputResource": "gs://tag_recognition_output/instance1",
               "producerNode": "tag-recognition",
               "autogen": true
             }
           ],
           "updateTime": "2023-04-18T04:58:18.592092259Z",
           "state": "FINISHED",
           "instanceType": "BATCH_PREDICTION"
         }
     }