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.
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.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-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/
Ruby
cd ruby-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
Ruby
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
Ruby
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
Ruby
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
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, don't 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.