Google Cloud Vision 運算子

先決條件任務

若要使用這些運算子,您必須執行幾件事

CloudVisionAddProductToProductSetOperator

建立新的 ReferenceImage 資源。

如需參數定義,請參閱 CloudVisionAddProductToProductSetOperator

使用運算子

我們正在使用 Google 程式庫中的 ProductProductSetRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ProductSet  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import Product  # isort:skip

如果 product_set_idproduct_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

add_product_to_product_set = CloudVisionAddProductToProductSetOperator(
    location=LOCATION,
    product_set_id=product_set_create_output,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="add_product_to_product_set",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

add_product_to_product_set_2 = CloudVisionAddProductToProductSetOperator(
    location=LOCATION,
    product_set_id=GCP_VISION_PRODUCT_SET_ID,
    product_id=GCP_VISION_PRODUCT_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="add_product_to_product_set_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "product_set_id",
    "product_id",
    "project_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionImageAnnotateOperator

針對影像執行影像偵測和註解。

如需參數定義,請參閱 CloudVisionImageAnnotateOperator

使用運算子

我們正在使用 Google 程式庫中的 enumsRetry 物件

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.cloud.vision_v1 import Feature  # isort:skip
from providers.tests.system.google import DEFAULT_GCP_SYSTEM_TEST_PROJECT_ID

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

annotate_image = CloudVisionImageAnnotateOperator(
    request=annotate_image_request,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="annotate_image",
)

結果可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

annotate_image_result = BashOperator(
    bash_command="echo {{ task_instance.xcom_pull('annotate_image')"
    "['logoAnnotations'][0]['description'] }}",
    task_id="annotate_image_result",
)

範本

template_fields: Sequence[str] = (
    "request",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionCreateProductOperator

建立並傳回新的產品資源。

關於提供的 Product 物件的可能錯誤

  • 如果 display_name 遺失或超過 4096 個字元,則傳回 INVALID_ARGUMENT。

  • 如果 description 超過 4096 個字元,則傳回 INVALID_ARGUMENT。

  • 如果 product_category 遺失或無效,則傳回 INVALID_ARGUMENT。

如需參數定義,請參閱 CloudVisionCreateProductOperator

使用運算子

我們正在使用 Google 程式庫中的 ProductRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import Product  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product = Product(display_name="My Product 1", product_category="toys")

可以省略 product_id 引數(它將由 API 產生)

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_create = CloudVisionCreateProductOperator(
    location=LOCATION,
    product=product,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="product_create",
)

或者可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_create_2 = CloudVisionCreateProductOperator(
    product_id=GCP_VISION_PRODUCT_ID,
    location=LOCATION,
    product=product,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="product_create_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionDeleteProductOperator

永久刪除產品及其參考圖片。

產品及其所有圖片的中繼資料將立即刪除,但針對包含該產品的 ProductSets 進行的搜尋查詢可能仍然有效,直到所有相關快取都重新整理為止。

可能錯誤

  • 如果產品不存在,則傳回 NOT_FOUND。

如需參數定義,請參閱 CloudVisionDeleteProductOperator

使用運算子

如果 product_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_delete = CloudVisionDeleteProductOperator(
    location=LOCATION,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    task_id="product_delete",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_delete_2 = CloudVisionDeleteProductOperator(
    location=LOCATION, product_id=GCP_VISION_PRODUCT_ID, task_id="product_delete_2"
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionGetProductOperator

取得與 Product 相關聯的資訊。

可能錯誤

  • 如果 Product 不存在,則傳回 NOT_FOUND。

如需參數定義,請參閱 CloudVisionGetProductOperator

使用運算子

如果 product_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_get = CloudVisionGetProductOperator(
    location=LOCATION,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    task_id="product_get",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_get_2 = CloudVisionGetProductOperator(
    location=LOCATION, product_id=GCP_VISION_PRODUCT_ID, task_id="product_get_2"
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_id",
    "gcp_conn_id",
    "impersonation_chain",
)

更多資訊

請參閱 Google Cloud Vision Product get 文件

CloudVisionProductSetCreateOperator

建立新的 ProductSet 資源。

如需參數定義,請參閱 CloudVisionCreateProductSetOperator

使用運算子

我們正在使用 Google 程式庫中的 ProductSetRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ProductSet  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set = ProductSet(display_name="My Product Set")

可以省略 product_set_id 引數(它將由 API 產生)

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set_create = CloudVisionCreateProductSetOperator(
    location=LOCATION,
    product_set=product_set,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="product_set_create",
)

或者可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_set_create_2 = CloudVisionCreateProductSetOperator(
    product_set_id=GCP_VISION_PRODUCT_SET_ID,
    location=LOCATION,
    product_set=product_set,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="product_set_create_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_set_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionDeleteProductSetOperator

永久刪除 ProductSetProductSet 中的 ProductsReferenceImages 不會被刪除。實際的影像檔案不會從 Google Cloud Storage 中刪除。

如需參數定義,請參閱 CloudVisionDeleteProductSetOperator

使用運算子

如果 product_set_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set_delete = CloudVisionDeleteProductSetOperator(
    location=LOCATION,
    product_set_id=product_set_create_output,
    task_id="product_set_delete",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_set_delete_2 = CloudVisionDeleteProductSetOperator(
    location=LOCATION, product_set_id=GCP_VISION_PRODUCT_SET_ID, task_id="product_set_delete_2"
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_set_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionGetProductSetOperator

取得與 ProductSet 相關聯的資訊。

如需參數定義,請參閱 CloudVisionGetProductSetOperator

使用運算子

如果 product_set_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set_get = CloudVisionGetProductSetOperator(
    location=LOCATION,
    product_set_id=product_set_create_output,
    task_id="product_set_get",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_set_get_2 = CloudVisionGetProductSetOperator(
    location=LOCATION, product_set_id=GCP_VISION_PRODUCT_SET_ID, task_id="product_set_get_2"
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_set_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionUpdateProductSetOperator

變更 ProductSet 資源。目前只能更新 display_name

注意

若要找到 ProductSet 資源,需要其名稱,格式為 projects/PROJECT_ID/locations/LOC_ID/productSets/PRODUCT_SET_ID

您可以直接將 name 作為 product_set 物件的屬性提供。但是,您可以將其留空,並改為提供 locationproduct_set_id(以及可選的 project_id - 如果不存在,將使用連線預設值),名稱將由運算子本身建立。

此機制的存在是為了您的方便,允許將 project_id 留空,並讓 Airflow 使用連線預設的 project_id

如需參數定義,請參閱 CloudVisionUpdateProductSetOperator

使用運算子

我們正在使用 Google Cloud Vision 程式庫中的 ProductSet 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ProductSet  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set = ProductSet(display_name="My Product Set")

任務初始化

如果 product_set_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_set_update = CloudVisionUpdateProductSetOperator(
    location=LOCATION,
    product_set_id=product_set_create_output,
    product_set=ProductSet(display_name="My Product Set 2"),
    task_id="product_set_update",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_set_update_2 = CloudVisionUpdateProductSetOperator(
    location=LOCATION,
    product_set_id=GCP_VISION_PRODUCT_SET_ID,
    product_set=ProductSet(display_name="My Product Set 2"),
    task_id="product_set_update_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_set_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionUpdateProductOperator

變更 Product 資源。目前只能更新 display_namedescriptionlabels 欄位。如果更新了標籤,則變更將在下次索引時間才會反映在查詢中。

注意

若要找到 Product 資源,需要其名稱,格式為 projects/PROJECT_ID/locations/LOC_ID/products/PRODUCT_ID

您可以直接將 name 作為 product 物件的屬性提供。但是,您可以將其留空,並改為提供 locationproduct_id(以及可選的 project_id - 如果不存在,將使用連線預設值),名稱將由運算子本身建立。

此機制的存在是為了您的方便,允許將 project_id 留空,並讓 Airflow 使用連線預設的 project_id

可能錯誤

  • 如果 Product 不存在,則傳回 NOT_FOUND。

  • 如果 display_name 出現在 update_mask 中,但請求中遺失或超過 4096 個字元,則傳回 INVALID_ARGUMENT。

  • 如果 description 出現在 update_mask 中,但超過 4096 個字元,則傳回 INVALID_ARGUMENT。

  • 如果 product_category 出現在 update_mask 中,則傳回 INVALID_ARGUMENT。

如需參數定義,請參閱 CloudVisionUpdateProductOperator

使用運算子

我們正在使用 Google Cloud Vision 程式庫中的 Product 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import Product  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product = Product(display_name="My Product 1", product_category="toys")

如果 product_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

product_update = CloudVisionUpdateProductOperator(
    location=LOCATION,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    product=Product(display_name="My Product 2", description="My updated description"),
    task_id="product_update",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

product_update_2 = CloudVisionUpdateProductOperator(
    location=LOCATION,
    product_id=GCP_VISION_PRODUCT_ID,
    product=Product(display_name="My Product 2", description="My updated description"),
    task_id="product_update_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "project_id",
    "product_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionCreateReferenceImageOperator

建立新的 ReferenceImage 資源。

如需參數定義,請參閱 CloudVisionCreateReferenceImageOperator

使用運算子

我們正在使用 Google 程式庫中的 ReferenceImageRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ReferenceImage  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

reference_image = ReferenceImage(uri=VISION_IMAGE_URL)

可以省略 product_set_id 引數(它將由 API 產生)

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

reference_image_create = CloudVisionCreateReferenceImageOperator(
    location=LOCATION,
    reference_image=reference_image,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    reference_image_id=GCP_VISION_REFERENCE_IMAGE_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="reference_image_create",
)

或者可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

reference_image_create_2 = CloudVisionCreateReferenceImageOperator(
    location=LOCATION,
    reference_image=reference_image,
    product_id=GCP_VISION_PRODUCT_ID,
    reference_image_id=GCP_VISION_REFERENCE_IMAGE_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="reference_image_create_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "reference_image",
    "product_id",
    "reference_image_id",
    "project_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionDeleteReferenceImageOperator

刪除 ReferenceImage 資源。

如需參數定義,請參閱 CloudVisionDeleteReferenceImageOperator

使用運算子

我們正在使用 Google 程式庫中的 ReferenceImageRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ReferenceImage  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

reference_image = ReferenceImage(uri=VISION_IMAGE_URL)

可以省略 product_set_id 引數(它將由 API 產生)

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

reference_image_delete = CloudVisionDeleteReferenceImageOperator(
    location=LOCATION,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    reference_image_id=GCP_VISION_REFERENCE_IMAGE_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="reference_image_delete",
)

或者可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

reference_image_delete_2 = CloudVisionDeleteReferenceImageOperator(
    location=LOCATION,
    reference_image_id=GCP_VISION_REFERENCE_IMAGE_ID,
    product_id=GCP_VISION_PRODUCT_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="reference_image_delete_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "reference_image",
    "product_id",
    "reference_image_id",
    "project_id",
    "gcp_conn_id",
    "impersonation_chain",
)

CloudVisionRemoveProductFromProductSetOperator

建立新的 ReferenceImage 資源。

如需參數定義,請參閱 CloudVisionRemoveProductFromProductSetOperator

使用運算子

我們正在使用 Google 程式庫中的 ProductProductSetRetry 物件

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import ProductSet  # isort:skip

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

from google.cloud.vision_v1.types import Product  # isort:skip

如果 product_set_idproduct_id 是由 API 產生,則可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_autogenerated.py[來源]

remove_product_from_product_set = CloudVisionRemoveProductFromProductSetOperator(
    location=LOCATION,
    product_set_id=product_set_create_output,
    product_id="{{ task_instance.xcom_pull('product_create') }}",
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="remove_product_from_product_set",
)

否則可以明確指定

tests/system/google/cloud/vision/example_vision_explicit.py[來源]

remove_product_from_product_set_2 = CloudVisionRemoveProductFromProductSetOperator(
    location=LOCATION,
    product_set_id=GCP_VISION_PRODUCT_SET_ID,
    product_id=GCP_VISION_PRODUCT_ID,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="remove_product_from_product_set_2",
)

範本

template_fields: Sequence[str] = (
    "location",
    "product_set_id",
    "product_id",
    "project_id",
    "gcp_conn_id",
    "impersonation_chain",
)

更多資訊

請參閱 Google Cloud Vision Remove Product From Product Set 文件

CloudVisionDetectTextOperator

針對影像執行文字偵測。

如需參數定義,請參閱 CloudVisionDetectTextOperator

使用運算子

我們正在使用 Google 程式庫中的 Retry 物件

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_text = CloudVisionDetectTextOperator(
    image=DETECT_IMAGE,
    retry=Retry(maximum=10.0),
    timeout=5,
    task_id="detect_text",
    language_hints="en",
    web_detection_params={"include_geo_results": True},
)

結果可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_text_result = BashOperator(
    bash_command="echo {{ task_instance.xcom_pull('detect_text')['textAnnotations'][0] }}",
    task_id="detect_text_result",
)

範本

template_fields: Sequence[str] = (
    "image",
    "max_results",
    "timeout",
    "gcp_conn_id",
    "impersonation_chain",
)

更多資訊

請參閱 Google Cloud Vision Text Detection 文件

CloudVisionTextDetectOperator

針對影像執行文件文字偵測。

如需參數定義,請參閱 CloudVisionTextDetectOperator

使用運算子

我們正在使用 Google 程式庫中的 Retry 物件

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

document_detect_text = CloudVisionTextDetectOperator(
    image=DETECT_IMAGE, retry=Retry(maximum=10.0), timeout=5, task_id="document_detect_text"
)

結果可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

document_detect_text_result = BashOperator(
    bash_command="echo {{ task_instance.xcom_pull('document_detect_text')['textAnnotations'][0] }}",
    task_id="document_detect_text_result",
)

範本

template_fields: Sequence[str] = (
    "image",
    "max_results",
    "timeout",
    "gcp_conn_id",
    "impersonation_chain",
)  # Iterable[str]

更多資訊

請參閱 Google Cloud Vision Document Text Detection 文件

CloudVisionDetectImageLabelsOperator

針對影像執行影像標籤偵測。

如需參數定義,請參閱 CloudVisionDetectImageLabelsOperator

使用運算子

我們正在使用 Google 程式庫中的 Retry 物件

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_labels = CloudVisionDetectImageLabelsOperator(
    image=DETECT_IMAGE, retry=Retry(maximum=10.0), timeout=5, task_id="detect_labels"
)

結果可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_labels_result = BashOperator(
    bash_command="echo {{ task_instance.xcom_pull('detect_labels')['labelAnnotations'][0] }}",
    task_id="detect_labels_result",
)

範本

template_fields: Sequence[str] = (
    "image",
    "max_results",
    "timeout",
    "gcp_conn_id",
    "impersonation_chain",
)

更多資訊

請參閱 Google Cloud Vision Label Detection 文件

CloudVisionDetectImageSafeSearchOperator

針對影像執行影像標籤偵測。

如需參數定義,請參閱 CloudVisionDetectImageSafeSearchOperator

使用運算子

我們正在使用 Google 程式庫中的 Retry 物件

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

from google.api_core.retry import Retry  # isort:skip

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_safe_search = CloudVisionDetectImageSafeSearchOperator(
    image=DETECT_IMAGE, retry=Retry(maximum=10.0), timeout=5, task_id="detect_safe_search"
)

結果可以從 XCOM 中提取

tests/system/google/cloud/vision/example_vision_annotate_image.py[來源]

detect_safe_search_result = BashOperator(
    bash_command=f"echo {detect_safe_search.output}",
    task_id="detect_safe_search_result",
)

範本

template_fields: Sequence[str] = (
    "image",
    "max_results",
    "timeout",
    "gcp_conn_id",
    "impersonation_chain",
)

參考

如需更多資訊,請參閱

此條目是否有幫助?