图片批处理

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

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

支持的模型

以下模型支持批处理:

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

支持的数据类型

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

  • 图片数据

准备工作

用户体验历程

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

  1. 启用 Vertex AI Vision API。

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

  3. 创建应用。

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

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

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

  4. 创建应用的批量实例,每个实例对应于一个 Cloud Storage 存储分区输入位置。

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

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

批量处理图片

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

商品识别器模型

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

控制台

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

创建新应用

  1. 打开 Vertex AI Vision 信息中心的应用标签页。

    前往“应用”标签页

  2. 点击 Create 按钮。

  3. 输入应用名称,然后选择您所在的地区。

  4. 点击继续

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

  6. 点击创建

指定批量处理输入

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

  2. 在侧边的 Universal 输入窗格中,点击选择输入源

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

  4. 点击继续

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

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

  7. 点击提交

添加模型

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

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

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

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

  4. 从列表中选择相应型号。

  5. 点击选择

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

  7. 点击应用设置

添加输出目标

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

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

部署应用

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

REST 和命令行

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

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

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

    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 方法跟踪实例。同样,如需从此列表中移除实例,请使用 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 方法跟踪实例。同样,如需从此列表中移除实例,请使用 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"
         }
     }