除了流处理之外,您还可以选择批处理作为从数据中获取信息的方法。通过这种非流式处理模式,您可以构建应用来支持其他媒体类型。
您可以使用部分模型批量处理存储在 Cloud Storage 中的图片文件。 输出存储在 Cloud Storage 中。
支持的模型
以下模型支持批处理:
- 标记识别器
- 商品识别器
支持的数据类型
本页面介绍了如何批量处理以下类型的数据:
- 图片数据
准备工作
- 查看本页面上介绍的批处理支持的模型和支持的数据类型。
- 如果您使用商品识别器模型,请创建
Catalog和ProductRecognitionIndex,以便在创建商品识别器模型进行批量处理时使用。或者,确定要使用的现有商品识别器索引或商品识别器模型。 - 如果您使用标记识别器模型,请按照标记识别器模型指南创建模型。
- 如果您使用商品识别器模型,请创建
- 创建一个或多个 Cloud Storage 存储桶,用于批处理输入和输出。
- 找到支持的文件,并将其上传到 Cloud Storage 输入存储桶进行处理。
用户体验历程
如需批量处理图片数据,请完成以下常规步骤:
启用 Gemini Enterprise Agent Platform Vision API。
创建受支持的处理器(商品识别器或标签识别器)。
创建应用。
输入:添加一个通用输入节点,用于指定要在 Cloud Storage 中处理的文件。
正在处理:添加模型节点。
输出:添加输出存储节点,指定处理后的数据在 Cloud Storage 中的存储位置。
创建应用的批处理实例,每个实例对应一个 Cloud Storage 存储桶输入位置。
部署应用和应用实例。
批处理实例完成后,检查存储在 Cloud Storage 中的输出。
批量处理图片
发送批量处理请求的步骤因模型而异。按照目标模型的说明批量处理图片。
商品识别器模型
使用这些示例,通过商品识别器模型批量处理图片。
控制台
在 Google Cloud 控制台中创建图片批处理请求。
创建新应用
打开 Gemini Enterprise Agent Platform Vision 信息中心的应用标签页。
点击创建按钮。
输入应用名称并选择您所在的区域。
点击继续。
选择结算方式。如需详细了解随用随付与按月结算,请参阅价格页面。
点击创建。
指定批处理输入
在图表 App Builder 页面中,点击通用输入节点。
在侧边通用输入窗格中,点击选择输入源。
在选择输入来源页面中,选择 批量预测。
点击继续。
在来源窗格中,点击位置选择器字段中的浏览,以指明文件在 Cloud Storage 中的位置。
可选。如需指定更多来源,请点击添加项,然后重复上一步。
点击提交。
添加模型
在图表 App Builder 页面中,点击专用模型部分中的商品识别器模型节点。
在商品识别器窗格中,点击选择模型。
选择选择现有商品识别器模型。
如果您需要创建新模型或索引,请选择相应的选项来创建资源。
从列表中选择模型。
点击选择。
可选。修改置信度阈值。
点击应用设置。
添加输出目标位置
在图表 App Builder 页面中,点击输出部分中的 Cloud Storage 模型节点。
在 Cloud Storage 窗格中,点击浏览,在 Cloud Storage 中选择批量预测输出目标位置。
部署应用
- 在图表 App Builder 页面中,点击 部署。
REST 和命令行
完成以下步骤,发送图片批量处理请求。
使用
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", } } }'使用新创建的商品识别器模型创建应用。 此请求使用
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'使用
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'部署应用。
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'获取应用实例。此信息会告知您批处理何时完成。
具体而言,
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 和命令行
完成以下步骤,发送图片批量处理请求。
使用
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" } }'使用新创建的标记识别器模型创建应用。 此请求使用
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'使用
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'部署应用。
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'获取应用实例。此信息会告知您批处理何时完成。
具体而言,
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" } }