This tutorial demonstrates using Cloud Run functions, the Cloud Vision API, and ImageMagick to detect and blur offensive images that get uploaded to a Cloud Storage bucket.
Objectives
- Deploy a storage-triggered Background Cloud Run 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
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, 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
- Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Visualizing 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 function analyzes the image using the Vision API.
- If violent or adult content is detected, the function uses ImageMagick to blur the image.
- The blurred image is uploaded to another Cloud Storage bucket for use.
Preparing the application
Create a Cloud Storage bucket for uploading images, where
YOUR_INPUT_BUCKET_NAME
is a globally unique bucket name:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
Create a Cloud Storage bucket to receive blurred images, where
YOUR_OUTPUT_BUCKET_NAME
is a globally unique bucket name:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
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.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
PHP
git clone https://github.com/GoogleCloudPlatform/php-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/imagemagick/
Python
cd python-docs-samples/functions/imagemagick/
Go
cd golang-samples/functions/imagemagick/
Java
cd java-docs-samples/functions/imagemagick/
C#
cd dotnet-docs-samples/functions/imagemagick/
Ruby
cd ruby-docs-samples/functions/imagemagick/
PHP
cd php-docs-samples/functions/imagemagick/
Understanding the code
Importing dependencies
The application must import several dependencies in order to interact with Google Cloud services, ImageMagick, and the file system:
Node.js
Python
Go
Java
C#
Ruby
PHP
Analyzing images
The following function is invoked when an image is uploaded to the Cloud Storage bucket you created for storing images. The function uses the Vision API to detect violent or adult content in uploaded images.
Node.js
Python
Go
Java
C#
Ruby
PHP
Blurring 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 over the original image.
Node.js
Python
Go
Java
C#
Ruby
PHP
Deploying the function
To deploy your 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 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
Replace the following:
RUNTIME
: a runtime that is based on Ubuntu 18.04 (later runtimes don't include support for ImageMagick).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.
For this particular example, do not include gs://
as part of the bucket names
in the deploy
command.
Uploading 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.Watch the logs to be sure the executions have completed:
gcloud functions logs read --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.
Deleting 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.
Deleting the 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 blurOffensiveImages
Python
gcloud functions delete blur_offensive_images
Go
gcloud functions delete BlurOffensiveImages
Java
gcloud functions delete java-blur-function
C#
gcloud functions delete csharp-blur-function
Ruby
gcloud functions delete blur_offensive_images
PHP
gcloud functions delete blurOffensiveImages
You can also delete Cloud Run functions from the Google Cloud console.