This tutorial demonstrates using Cloud Run functions, the Vision API, and ImageMagick to detect and blur offensive images that get uploaded to a Cloud Storage bucket.
Objectives
- Deploy a storage-triggered CloudEvent function.
- Use the Vision API to detect violent or adult content.
- Use ImageMagick to blur offensive images.
- Test the function by uploading an image of a flesh-eating zombie.
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Run functions
- Cloud Storage
- Cloud Vision
- Cloud Build
- Pub/Sub
- Artifact Registry
- Eventarc
- Cloud Logging
For details, see Cloud Run functions pricing.
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- 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, Artifact Registry, Eventarc, Cloud Storage, Cloud Vision, Logging, and Pub/Sub 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, Artifact Registry, Eventarc, Cloud Storage, Cloud Vision, Logging, and Pub/Sub APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Visualize the flow of data
The flow of data in the ImageMagick tutorial application involves several steps:
- An image is uploaded to a Cloud Storage bucket.
- The Cloud Run function analyzes the image using the Cloud Vision API.
- If violent or adult content is detected, the Cloud Run function uses ImageMagick to blur the image.
- The blurred image is uploaded to another Cloud Storage bucket for use.
Prepare the application
Create a regional Cloud Storage bucket for uploading images, where
YOUR_INPUT_BUCKET_NAME
is a globally unique bucket name, andREGION
is the region in which you plan to deploy your function:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME --location=REGION
Create a regional Cloud Storage bucket to receive blurred images, where
YOUR_OUTPUT_BUCKET_NAME
is a globally unique bucket name, andREGION
is the region in which you plan to deploy your function:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME --location=REGION
Clone the sample app repository to your local machine:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Cloud Run functions sample code:
Node.js
cd nodejs-docs-samples/functions/v2/imagemagick/
Python
cd python-docs-samples/functions/v2/imagemagick/
Go
cd golang-samples/functions/functionsv2/imagemagick/
Java
cd java-docs-samples/functions/v2/imagemagick/
Understand the code
The ImageMagick sample includes dependencies and two different functions. The first function analyzes the image, and the second function blurs it if it contains violent or adult content.
Import dependencies
The application must import several dependencies in order to interact with Google Cloud services, ImageMagick, and the file system:
ImageMagick and its command-line tool convert
are included by default
within the Cloud Run functions execution environment for most of the
runtimes. For PHP, you may need to do some
manual configuration.
Note that Cloud Run functions does not support installing custom system-level
packages.
Node.js
Python
Go
Java
Analyze images
The following function is invoked when an image is uploaded to the Cloud Storage bucket you created for image input. The function uses the Vision API to detect violent or adult content in uploaded images.
Node.js
Python
Go
Java
Blur images
The following function is called when violent or adult content is detected in an uploaded image. The function downloads the offensive image, uses ImageMagick to blur the image, and then uploads the blurred image to your output bucket.
Node.js
Python
Go
Java
Deploy the function
To deploy your Cloud Run function with a storage trigger, run the following
command in the directory that contains the sample code (or in the case of
Java, the pom.xml
file):
Node.js
gcloud functions deploy nodejs-blur-function \ --gen2 \ --runtime=RUNTIME \ --region=REGION \ --source=. \ --entry-point=blurOffensiveImages \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Python
gcloud functions deploy python-blur-function \ --gen2 \ --runtime=RUNTIME \ --region=REGION \ --source=. \ --entry-point=blur_offensive_images \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Go
gcloud functions deploy go-blur-function \ --gen2 \ --runtime=RUNTIME \ --region=REGION \ --source=. \ --entry-point=blur-offensive-images \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Java
gcloud functions deploy java-blur-function \ --gen2 \ --runtime=RUNTIME \ --region=REGION \ --source=. \ --entry-point=functions.ImageMagick \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Replace the following:
- RUNTIME: a runtime that is based on Ubuntu 18.04 or higher
- REGION: The name of the
Google Cloud region where you want to deploy your function
(for example,
us-west1
). - YOUR_INPUT_BUCKET_NAME: The name of the Cloud Storage bucket for uploading images.
- YOUR_OUTPUT_BUCKET_NAME: The name of the bucket that the blurred images should be saved to.
When deploying Cloud Run functions, specify the bucket name alone without the
leading gs://
; for example, --trigger-event-filters="bucket=my-bucket"
.
Upload an image
Upload an offensive image, such as this image of a flesh-eating zombie:
gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
where
YOUR_INPUT_BUCKET_NAME
is the Cloud Storage bucket you created earlier for uploading images.You should see the analysis of the image in the logs:
gcloud beta functions logs read YOUR_FUNCTION_NAME --gen2 --limit=100
You can view the blurred images in the
YOUR_OUTPUT_BUCKET_NAME
Cloud Storage bucket you created earlier.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the project
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- 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.
Delete the Cloud Run function
Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.
To delete the function you deployed in this tutorial, run the following command:
Node.js
gcloud functions delete nodejs-blur-function --gen2 --region REGION
Python
gcloud functions delete python-blur-function --gen2 --region REGION
Go
gcloud functions delete go-blur-function --gen2 --region REGION
Java
gcloud functions delete java-blur-function --gen2 --region REGION
You can also delete Cloud Run functions from the Google Cloud console.