ImageMagick 教程

本教程演示了如何使用 Cloud Functions 函数、Google Cloud Vision APIImageMagick 检测上传到 Cloud Storage 存储分区的令人反感的图片并对其进行模糊处理。

目标

  • 部署存储触发的后台 Cloud Functions 函数
  • 使用 Cloud Vision API 检测暴力或成人内容。
  • 使用 ImageMagick 对令人反感的图片进行模糊处理。
  • 上传一张肉食僵尸的图片来测试函数。

费用

本教程会使用 Cloud Platform 的如下计费组件:

  • Google Cloud Functions
  • Google Cloud Storage
  • Google Cloud Vision API

您可使用价格计算器根据您的预计使用情况来估算费用。

Cloud Platform 新用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google 帐号。

    如果您还没有 Google 帐号,请注册一个新帐号

  2. 在 Cloud Console 的项目选择器页面上,选择或创建 Cloud 项目。

    转到项目选择器页面

  3. 确保您的 Google Cloud 项目已启用结算功能。 了解如何确认您的项目已启用结算功能

  4. 启用 Cloud Functions, Cloud Storage, and Cloud Vision API。

    启用 API

  5. 安装并初始化 Cloud SDK
  6. 更新 gcloud 组件:
    gcloud components update
  7. 准备开发环境。

直观呈现数据流

ImageMagick 教程应用中的数据流涉及以下几个步骤:

  1. 将图片上传到 Cloud Storage 存储分区。
  2. Cloud Functions 函数使用 Cloud Vision API 分析图片。
  3. 如果检测到暴力或成人内容,Cloud Functions 函数会使用 ImageMagick 对图片进行模糊处理。
  4. 经过模糊处理的图片会上传到其他 Cloud Storage 存储分区以供使用。

准备应用

  1. 创建一个 Cloud Storage 存储分区以上传图片,其中 YOUR_INPUT_BUCKET_NAME 是全局唯一的存储分区名称:

    gsutil mb gs://YOUR_INPUT_BUCKET_NAME
    
  2. 创建一个 Cloud Storage 存储分区以接收经过模糊处理的图片,其中 YOUR_OUTPUT_BUCKET_NAME 是全局唯一的存储分区名称:

    gsutil mb gs://YOUR_OUTPUT_BUCKET_NAME
    
  3. 将示例应用代码库克隆到本地机器:

    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 文件并将其解压缩。

  4. 切换到包含 Cloud 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/

了解代码

导入依赖项

应用必须导入多个依赖项才能与 Google Cloud Platform 服务、ImageMagick 和文件系统进行交互:

Node.js

const gm = require('gm').subClass({imageMagick: true});
const fs = require('fs');
const {promisify} = require('util');
const path = require('path');
const vision = require('@google-cloud/vision');

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const client = new vision.ImageAnnotatorClient();

const {BLURRED_BUCKET_NAME} = process.env;

Python

import os
import tempfile

from google.cloud import storage, vision
from wand.image import Image

storage_client = storage.Client()
vision_client = vision.ImageAnnotatorClient()

Go


// Package imagemagick contains an example of using ImageMagick to process a
// file uploaded to Cloud Storage.
package imagemagick

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"os/exec"

	"cloud.google.com/go/storage"
	vision "cloud.google.com/go/vision/apiv1"
	visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

// Global API clients used across function invocations.
var (
	storageClient *storage.Client
	visionClient  *vision.ImageAnnotatorClient
)

func init() {
	// Declare a separate err variable to avoid shadowing the client variables.
	var err error

	storageClient, err = storage.NewClient(context.Background())
	if err != nil {
		log.Fatalf("storage.NewClient: %v", err)
	}

	visionClient, err = vision.NewImageAnnotatorClient(context.Background())
	if err != nil {
		log.Fatalf("vision.NewAnnotatorClient: %v", err)
	}
}

Java


import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.SafeSearchAnnotation;
import functions.eventpojos.GcsEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ImageMagick implements BackgroundFunction<GcsEvent> {

  private static Storage storage = StorageOptions.getDefaultInstance().getService();
  private static final String BLURRED_BUCKET_NAME = System.getenv("BLURRED_BUCKET_NAME");
  private static final Logger logger = Logger.getLogger(ImageMagick.class.getName());
}

分析图片

当图片上传到您为了存储图片而创建的 Cloud Storage 存储分区时,系统会调用以下函数。该函数使用 Cloud Vision API 检测上传的图片中的暴力或成人内容。

Node.js

// Blurs uploaded images that are flagged as Adult or Violence.
exports.blurOffensiveImages = async (event) => {
  // This event represents the triggering Cloud Storage object.
  const object = event;

  const file = storage.bucket(object.bucket).file(object.name);
  const filePath = `gs://${object.bucket}/${object.name}`;

  console.log(`Analyzing ${file.name}.`);

  try {
    const [result] = await client.safeSearchDetection(filePath);
    const detections = result.safeSearchAnnotation || {};

    if (
      // Levels are defined in https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse#likelihood
      detections.adult === 'VERY_LIKELY' ||
      detections.violence === 'VERY_LIKELY'
    ) {
      console.log(`Detected ${file.name} as inappropriate.`);
      return await blurImage(file, BLURRED_BUCKET_NAME);
    } else {
      console.log(`Detected ${file.name} as OK.`);
    }
  } catch (err) {
    console.error(`Failed to analyze ${file.name}.`, err);
    throw err;
  }
};

Python

# Blurs uploaded images that are flagged as Adult or Violence.
def blur_offensive_images(data, context):
    file_data = data

    file_name = file_data['name']
    bucket_name = file_data['bucket']

    blob = storage_client.bucket(bucket_name).get_blob(file_name)
    blob_uri = f'gs://{bucket_name}/{file_name}'
    blob_source = {'source': {'image_uri': blob_uri}}
    # Ignore already-blurred files
    if file_name.startswith('blurred-'):
        print(f'The image {file_name} is already blurred.')
        return

    print(f'Analyzing {file_name}.')

    result = vision_client.safe_search_detection(blob_source)
    detected = result.safe_search_annotation

    # Process image
    if detected.adult == 5 or detected.violence == 5:
        print(f'The image {file_name} was detected as inappropriate.')
        return __blur_image(blob)
    else:
        print(f'The image {file_name} was detected as OK.')

Go


// GCSEvent is the payload of a GCS event.
type GCSEvent struct {
	Bucket string `json:"bucket"`
	Name   string `json:"name"`
}

// BlurOffensiveImages blurs offensive images uploaded to GCS.
func BlurOffensiveImages(ctx context.Context, e GCSEvent) error {
	outputBucket := os.Getenv("BLURRED_BUCKET_NAME")
	if outputBucket == "" {
		return errors.New("BLURRED_BUCKET_NAME must be set")
	}

	img := vision.NewImageFromURI(fmt.Sprintf("gs://%s/%s", e.Bucket, e.Name))

	resp, err := visionClient.DetectSafeSearch(ctx, img, nil)
	if err != nil {
		return fmt.Errorf("AnnotateImage: %v", err)
	}

	if resp.GetAdult() == visionpb.Likelihood_VERY_LIKELY ||
		resp.GetViolence() == visionpb.Likelihood_VERY_LIKELY {
		return blur(ctx, e.Bucket, outputBucket, e.Name)
	}
	log.Printf("The image %q was detected as OK.", e.Name)
	return nil
}

Java

@Override
// Blurs uploaded images that are flagged as Adult or Violence.
public void accept(GcsEvent gcsEvent, Context context) {
  // Validate parameters
  if (gcsEvent.getBucket() == null || gcsEvent.getName() == null) {
    logger.severe("Error: Malformed GCS event.");
    return;
  }

  BlobInfo blobInfo = BlobInfo.newBuilder(gcsEvent.getBucket(), gcsEvent.getName()).build();

  // Construct URI to GCS bucket and file.
  String gcsPath = String.format("gs://%s/%s", gcsEvent.getBucket(), gcsEvent.getName());
  logger.info(String.format("Analyzing %s", gcsEvent.getName()));

  // Construct request.
  ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
  List<AnnotateImageRequest> requests = List.of(request);

  // Send request to the Vision API.
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        logger.info(String.format("Error: %s", res.getError().getMessage()));
        return;
      }
      // Get Safe Search Annotations
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
        logger.info(String.format("Detected %s as inappropriate.", gcsEvent.getName()));
        blur(blobInfo);
      } else {
        logger.info(String.format("Detected %s as OK.", gcsEvent.getName()));
      }
    }
  } catch (IOException e) {
    logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
  }
}

对图片进行模糊处理

当在上传的图片中检测到暴力或成人内容时,系统将调用以下函数。该函数会下载令人反感的图片,使用 ImageMagick 对图片进行模糊处理,然后上传经过模糊处理后的图片来覆盖原始图片。

Node.js

// Blurs the given file using ImageMagick, and uploads it to another bucket.
const blurImage = async (file, blurredBucketName) => {
  const tempLocalPath = `/tmp/${path.parse(file.name).base}`;

  // Download file from bucket.
  try {
    await file.download({destination: tempLocalPath});

    console.log(`Downloaded ${file.name} to ${tempLocalPath}.`);
  } catch (err) {
    throw new Error(`File download failed: ${err}`);
  }

  await new Promise((resolve, reject) => {
    gm(tempLocalPath)
      .blur(0, 16)
      .write(tempLocalPath, (err, stdout) => {
        if (err) {
          console.error('Failed to blur image.', err);
          reject(err);
        } else {
          console.log(`Blurred image: ${file.name}`);
          resolve(stdout);
        }
      });
  });

  // Upload result to a different bucket, to avoid re-triggering this function.
  const blurredBucket = storage.bucket(blurredBucketName);

  // Upload the Blurred image back into the bucket.
  const gcsPath = `gs://${blurredBucketName}/${file.name}`;
  try {
    await blurredBucket.upload(tempLocalPath, {destination: file.name});
    console.log(`Uploaded blurred image to: ${gcsPath}`);
  } catch (err) {
    throw new Error(`Unable to upload blurred image to ${gcsPath}: ${err}`);
  }

  // Delete the temporary file.
  const unlink = promisify(fs.unlink);
  return unlink(tempLocalPath);
};

Python

# Blurs the given file using ImageMagick.
def __blur_image(current_blob):
    file_name = current_blob.name
    _, temp_local_filename = tempfile.mkstemp()

    # Download file from bucket.
    current_blob.download_to_filename(temp_local_filename)
    print(f'Image {file_name} was downloaded to {temp_local_filename}.')

    # Blur the image using ImageMagick.
    with Image(filename=temp_local_filename) as image:
        image.resize(*image.size, blur=16, filter='hamming')
        image.save(filename=temp_local_filename)

    print(f'Image {file_name} was blurred.')

    # Upload result to a second bucket, to avoid re-triggering the function.
    # You could instead re-upload it to the same bucket + tell your function
    # to ignore files marked as blurred (e.g. those with a "blurred" prefix)
    blur_bucket_name = os.getenv('BLURRED_BUCKET_NAME')
    blur_bucket = storage_client.bucket(blur_bucket_name)
    new_blob = blur_bucket.blob(file_name)
    new_blob.upload_from_filename(temp_local_filename)
    print(f'Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}')

    # Delete the temporary file.
    os.remove(temp_local_filename)

Go


// blur blurs the image stored at gs://inputBucket/name and stores the result in
// gs://outputBucket/name.
func blur(ctx context.Context, inputBucket, outputBucket, name string) error {
	inputBlob := storageClient.Bucket(inputBucket).Object(name)
	r, err := inputBlob.NewReader(ctx)
	if err != nil {
		return fmt.Errorf("NewReader: %v", err)
	}

	outputBlob := storageClient.Bucket(outputBucket).Object(name)
	w := outputBlob.NewWriter(ctx)
	defer w.Close()

	// Use - as input and output to use stdin and stdout.
	cmd := exec.Command("convert", "-", "-blur", "0x8", "-")
	cmd.Stdin = r
	cmd.Stdout = w

	if err := cmd.Run(); err != nil {
		return fmt.Errorf("cmd.Run: %v", err)
	}

	log.Printf("Blurred image uploaded to gs://%s/%s", outputBlob.BucketName(), outputBlob.ObjectName())

	return nil
}

Java

// Blurs the file described by blobInfo using ImageMagick,
// and uploads it to the blurred bucket.
private static void blur(BlobInfo blobInfo) throws IOException {
  String bucketName = blobInfo.getBucket();
  String fileName = blobInfo.getName();

  // Download image
  Blob blob = storage.get(BlobId.of(bucketName, fileName));
  Path download = Paths.get("/tmp/", fileName);
  blob.downloadTo(download);

  // Construct the command.
  Path upload = Paths.get("/tmp/", "blurred-" + fileName);
  List<String> args = List.of("convert", download.toString(), "-blur", "0x8", upload.toString());
  try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process process = pb.start();
    process.waitFor();
  } catch (Exception e) {
    logger.info(String.format("Error: %s", e.getMessage()));
  }

  // Upload image to blurred bucket.
  BlobId blurredBlobId = BlobId.of(BLURRED_BUCKET_NAME, fileName);
  BlobInfo blurredBlobInfo =
      BlobInfo.newBuilder(blurredBlobId).setContentType(blob.getContentType()).build();

  byte[] blurredFile = Files.readAllBytes(upload);
  storage.create(blurredBlobInfo, blurredFile);
  logger.info(
      String.format("Blurred image uploaded to: gs://%s/%s", BLURRED_BUCKET_NAME, fileName));

  // Remove images from fileSystem
  Files.delete(download);
  Files.delete(upload);
}

部署函数

  1. 如需使用存储触发器部署 Cloud Functions 函数,请在包含示例代码(如果是 Java,则为 pom.xml 文件)的目录中运行以下命令:

    Node.js

    gcloud functions deploy blurOffensiveImages \
    --runtime nodejs10 \
    --trigger-bucket YOUR_INPUT_BUCKET_NAME \
    --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
    您可以使用 --runtime 标志的以下值来指定偏好的 Node.js 版本:
    • nodejs10
    • nodejs12

    Python

    gcloud functions deploy blur_offensive_images \
    --runtime python37 \
    --trigger-bucket YOUR_INPUT_BUCKET_NAME \
    --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
    您可以使用 --runtime 标志的以下值来指定偏好的 Python 版本:
    • python37
    • python38

    Go

    gcloud functions deploy BlurOffensiveImages \
    --runtime go111 \
    --trigger-bucket YOUR_INPUT_BUCKET_NAME \
    --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
    您可以使用 --runtime 标志的以下值来指定偏好的 Go 版本:
    • go111
    • go113

    Java

    gcloud functions deploy java-blur-function \
    --entry-point functions.ImageMagick \
    --runtime java11 \
    --memory 512MB \
    --trigger-bucket YOUR_INPUT_BUCKET_NAME \
    --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME

    其中 YOUR_INPUT_BUCKET_NAME 是用于上传图片的 Cloud Storage 存储分区的名称,YOUR_OUTPUT_BUCKET_NAME 是用于保存经过模糊处理的图片的存储分区名称。

    通过 --allow-unauthenticated 标志,您可以在不进行身份验证的情况下访问函数。如需进行身份验证,请省略此标志。

上传图片

  1. 上传一张令人反感的图片,比如这张肉食僵尸图片:

    gsutil cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
    

    其中 YOUR_INPUT_BUCKET_NAME 是您之前为了上传图片而创建的 Cloud Storage 存储分区。

  2. 查看日志以确保执行已完成:

    gcloud functions logs read --limit 100
    
  3. 您可以在之前创建的 YOUR_OUTPUT_BUCKET_NAME Cloud Storage 存储分区中查看经过模糊处理的图片。

清理

为避免因本教程中使用的资源而导致您的 Google Cloud Platform 帐号产生费用,请执行以下操作:

删除项目

为了避免产生费用,最简单的方法是删除您为本教程创建的项目。

要删除项目,请执行以下操作:

  1. 在 Cloud Console 中,转到管理资源页面。

    转到“管理资源”页面

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

删除 Cloud Functions 函数

删除 Cloud Functions 函数不会移除存储在 Cloud Storage 中的任何资源。

如需删除您在本教程中部署的 Cloud Functions 函数,请运行以下命令:

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 

您也可以通过 Google Cloud Console 删除 Cloud Functions 函数。