本教程演示了如何使用 Cloud Vision API 和 ImageMagick 检测上传到 Cloud Storage 存储桶的令人反感的图片并对其进行模糊处理。
目标
- 部署存储触发的后台 Cloud Run functions 函数。
- 使用 Vision API 检测暴力或成人内容。
- 使用 ImageMagick 对令人反感的图片进行模糊处理。
- 上传一张肉食僵尸的图片来测试函数。
费用
在本文档中,您将使用 Google Cloud 的以下收费组件:
- Cloud Run functions
- Cloud Storage
- Cloud Vision
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
准备开发环境。
Node.js
Python
Go
Java
Ruby
PHP
如果您已经安装 gcloud CLI,请运行以下命令进行更新:
gcloud components update
直观呈现数据流
ImageMagick 教程应用中的数据流涉及以下几个步骤:
- 将图片上传到 Cloud Storage 存储桶。
- 该函数使用 Vision API 分析图片。
- 如果检测到暴力或成人内容,Cloud Run functions 函数会使用 ImageMagick 对图片进行模糊处理。
- 经过模糊处理的图片会上传到其他 Cloud Storage 存储桶以供使用。
准备应用
创建一个 Cloud Storage 存储分区以上传图片,其中
YOUR_INPUT_BUCKET_NAME
是全局唯一的存储分区名称:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
创建一个 Cloud Storage 存储分区以接收经过模糊处理的图片,其中
YOUR_OUTPUT_BUCKET_NAME
是全局唯一的存储分区名称:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
将示例应用代码库克隆到本地机器:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
或者,您也可以下载该示例的 zip 文件并将其解压缩。
切换到包含 Cloud Run functions 示例代码的目录:
Node.js
cd nodejs-docs-samples/functions/imagemagick/
Python
cd python-docs-samples/functions/imagemagick/
Go
cd golang-samples/functions/imagemagick/
Java
cd java-docs-samples/functions/imagemagick/
Ruby
cd ruby-docs-samples/functions/imagemagick/
PHP
cd php-docs-samples/functions/imagemagick/
了解代码
导入依赖项
应用必须导入多个依赖项才能与Google Cloud 服务、ImageMagick 和文件系统进行交互:
Node.js
Python
Go
Java
Ruby
PHP
分析图片
当图片上传到您为了存储图片而创建的 Cloud Storage 存储分区时,系统会调用以下函数。该函数使用 Vision API 检测上传的图片中的暴力或成人内容。
Node.js
Python
Go
Java
Ruby
PHP
对图片进行模糊处理
当在上传的图片中检测到暴力或成人内容时,系统将调用以下函数。该函数会下载令人反感的图片,使用 ImageMagick 对图片进行模糊处理,然后上传经过模糊处理后的图片来覆盖原始图片。
Node.js
Python
Go
Java
Ruby
PHP
部署函数
如需使用存储触发器部署 Cloud Run functions 函数,请在包含示例代码(如果是 Java,则为 pom.xml
文件)的目录中运行以下命令:
Node.js
gcloud functions deploy blurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Python
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Go
gcloud functions deploy BlurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Java
gcloud functions deploy java-blur-function \ --no-gen2 \ --entry-point=functions.ImageMagick \ --runtime=RUNTIME \ --memory 512MB \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
C#
gcloud functions deploy csharp-blur-function \ --no-gen2 \ --entry-point=ImageMagick.Function \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Ruby
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
PHP
gcloud functions deploy blurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
请替换以下内容:
RUNTIME
:基于 Ubuntu 18.04 的运行时(更高版本的运行时暂不支持 ImageMagick)。YOUR_INPUT_BUCKET_NAME
:用于上传图片的 Cloud Storage 存储桶的名称。YOUR_OUTPUT_BUCKET_NAME
:经过模糊处理的图片应保存到的存储桶的名称。
对于此特定示例,在 deploy
命令中,请不要在存储桶名称中添加 gs://
。
上传图片
上传一张令人反感的图片,比如这张肉食僵尸图片:
gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
其中
YOUR_INPUT_BUCKET_NAME
是您之前为了上传图片而创建的 Cloud Storage 存储分区。查看日志以确保执行已完成:
gcloud functions logs read --limit 100
您可以在之前创建的
YOUR_OUTPUT_BUCKET_NAME
Cloud Storage 存储桶中查看经过模糊处理的图片。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
为了避免产生费用,最简单的方法是删除您为本教程创建的项目。
要删除项目,请执行以下操作:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
删除函数
删除 Cloud Run functions 不会移除存储在 Cloud Storage 中的任何资源。
如需删除您在本教程中部署的函数,请运行以下命令:
Node.js
gcloud functions delete blurOffensiveImages
Python
gcloud functions delete blur_offensive_images
Go
gcloud functions delete BlurOffensiveImages
Java
gcloud functions delete java-blur-function
Ruby
gcloud functions delete blur_offensive_images
PHP
gcloud functions delete blurOffensiveImages
您也可以通过 Google Cloud 控制台删除 Cloud Run functions。