Tutorial ImageMagick (generasi ke-2)


Tutorial ini menunjukkan penggunaan Cloud Functions, Google Cloud Vision API, dan ImageMagick untuk mendeteksi dan memburamkan gambar menyinggung yang diupload ke bucket Cloud Storage.

Tujuan

  • Mendeploy fungsi CloudEvent yang dipicu penyimpanan.
  • Gunakan Cloud Vision API untuk mendeteksi konten kekerasan atau khusus dewasa.
  • Gunakan ImageMagick untuk memburamkan gambar yang menyinggung.
  • Uji fungsi dengan mengupload gambar zombie pemakan daging.

Biaya

Dalam dokumen ini, Anda menggunakan komponen Google Cloud yang dapat ditagih berikut:

  • Cloud Functions
  • Cloud Storage
  • Cloud Vision
  • Cloud Build
  • Pub/Sub
  • Artifact Registry
  • Eventarc
  • Cloud Logging

Untuk detailnya, lihat Harga Cloud Functions.

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga. Pengguna baru Google Cloud mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.

    Buka pemilih project

  3. Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.

  4. Aktifkan API Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Cloud Storage, Cloud Vision, Logging, and Pub/Sub.

    Mengaktifkan API

  5. Menginstal Google Cloud CLI.
  6. Untuk initialize gcloud CLI, jalankan perintah berikut:

    gcloud init
  7. Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.

    Buka pemilih project

  8. Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.

  9. Aktifkan API Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Cloud Storage, Cloud Vision, Logging, and Pub/Sub.

    Mengaktifkan API

  10. Menginstal Google Cloud CLI.
  11. Untuk initialize gcloud CLI, jalankan perintah berikut:

    gcloud init
  12. Jika Anda sudah menginstal gcloud CLI, update dengan menjalankan perintah berikut:

    gcloud components update
  13. Siapkan lingkungan pengembangan Anda.

Memvisualisasikan aliran data

Alur data dalam aplikasi tutorial ImageMagick melibatkan beberapa langkah:

  1. Gambar diupload ke bucket Cloud Storage.
  2. Cloud Function menganalisis gambar menggunakan Cloud Vision API.
  3. Jika konten kekerasan atau khusus dewasa terdeteksi, Cloud Function akan menggunakan ImageMagick untuk memburamkan gambar.
  4. Gambar yang diburamkan diupload ke bucket Cloud Storage lain untuk digunakan.

Menyiapkan aplikasi

  1. Buat bucket Cloud Storage regional untuk mengupload gambar, dengan YOUR_INPUT_BUCKET_NAME adalah nama bucket yang unik secara global, dan REGION adalah region tempat Anda berencana men-deploy fungsi:

    gsutil mb -l REGION gs://YOUR_INPUT_BUCKET_NAME
  2. Buat bucket Cloud Storage regional untuk menerima gambar yang diburamkan, dengan YOUR_OUTPUT_BUCKET_NAME sebagai nama bucket yang unik secara global, dan REGION adalah region tempat Anda berencana men-deploy fungsi:

    gsutil mb -l REGION gs://YOUR_OUTPUT_BUCKET_NAME
  3. Clone repositori aplikasi contoh ke komputer lokal Anda:

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    Atau, Anda dapat mendownload sampel sebagai file ZIP dan mengekstraknya.

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    Atau, Anda dapat mendownload sampel sebagai file ZIP dan mengekstraknya.

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git

    Atau, Anda dapat mendownload sampel sebagai file ZIP dan mengekstraknya.

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git

    Atau, Anda dapat mendownload sampel sebagai file ZIP dan mengekstraknya.

  4. Ubah ke direktori yang memuat kode contoh Cloud Functions:

    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/

Memahami kode

Contoh ImageMagick mencakup dependensi dan dua fungsi berbeda. Fungsi pertama menganalisis gambar, dan fungsi kedua memburamkannya jika berisi konten kekerasan atau khusus dewasa.

Mengimpor dependensi

Aplikasi harus mengimpor beberapa dependensi untuk berinteraksi dengan layanan Google Cloud, ImageMagick, dan sistem file:

ImageMagick dan alat command line convert disertakan secara default dalam lingkungan eksekusi Cloud Functions untuk sebagian besar runtime. Untuk PHP, Anda mungkin perlu melakukan beberapa konfigurasi manual. Perlu diperhatikan bahwa Cloud Functions tidak mendukung penginstalan paket kustom level sistem.

Node.js

const functions = require('@google-cloud/functions-framework');
const gm = require('gm').subClass({imageMagick: true});
const fs = require('fs').promises;
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

import functions_framework
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"
	"cloud.google.com/go/vision/v2/apiv1/visionpb"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	cloudevents "github.com/cloudevents/sdk-go/v2"
	"github.com/googleapis/google-cloudevents-go/cloud/storagedata"
	"google.golang.org/protobuf/encoding/protojson"
)

// 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

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

	visionClient, err = vision.NewImageAnnotatorClient(bgctx)
	if err != nil {
		log.Fatalf("vision.NewAnnotatorClient: %v", err)
	}
	functions.CloudEvent("blur-offensive-images", blurOffensiveImages)
}

Java


import com.google.cloud.functions.CloudEventsFunction;
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 com.google.events.cloud.storage.v1.StorageObjectData;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import io.cloudevents.CloudEvent;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ImageMagick implements CloudEventsFunction {

  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());
}

Menganalisis gambar

Fungsi berikut dipanggil saat gambar diupload ke bucket Cloud Storage yang Anda buat untuk input gambar. Fungsi ini menggunakan Cloud Vision API untuk mendeteksi konten kekerasan atau khusus dewasa dalam gambar yang diupload.

Node.js

// Blurs uploaded images that are flagged as Adult or Violence.
functions.cloudEvent('blurOffensiveImages', async cloudEvent => {
  // This event represents the triggering Cloud Storage object.
  const bucket = cloudEvent.data.bucket;
  const name = cloudEvent.data.name;
  const file = storage.bucket(bucket).file(name);
  const filePath = `gs://${bucket}/${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 Violent imagery.
@functions_framework.cloud_event
def blur_offensive_images(cloud_event):
    file_data = cloud_event.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 = vision.Image(source=vision.ImageSource(gcs_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(image=blob_source)
    detected = result.safe_search_annotation

    # Process image
    # 5 maps to VERY_LIKELY
    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


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

	var gcsEvent storagedata.StorageObjectData
	if err := protojson.Unmarshal(e.Data(), &gcsEvent); err != nil {
		return fmt.Errorf("protojson.Unmarshal: failed to decode event data: %w", err)
	}
	img := vision.NewImageFromURI(fmt.Sprintf("gs://%s/%s", gcsEvent.GetBucket(), gcsEvent.GetName()))

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

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

Java

@Override
// Blurs uploaded images that are flagged as Adult or Violence.
public void accept(CloudEvent event) throws InvalidProtocolBufferException {
  // Extract the GCS Event data from the CloudEvent's data payload.
  StorageObjectData data = getEventData(event);
  // Validate parameters
  if (data == null) {
    logger.severe("Error: Malformed GCS event.");
    return;
  }

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

  // Construct URI to GCS bucket and file.
  String gcsPath = String.format("gs://%s/%s", data.getBucket(), data.getName());
  logger.info(String.format("Analyzing %s", data.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.", data.getName()));
        blur(blobInfo);
      } else {
        logger.info(String.format("Detected %s as OK.", data.getName()));
      }
    }
  } catch (IOException e) {
    logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
  }
}

Memburamkan gambar

Fungsi berikut dipanggil ketika terdeteksi konten kekerasan atau khusus dewasa dalam gambar yang diupload. Fungsi ini akan mendownload gambar yang menyinggung, menggunakan ImageMagick untuk memburamkan gambar, lalu mengupload gambar yang diburamkan ke bucket output.

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.
  return fs.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("inputBlob.NewReader: %w", 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: %w", err)
	}

	if err := w.Close(); err != nil {
		return fmt.Errorf("failed to write output file: %w", 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);
}

Menerapkan fungsi

Untuk men-deploy Cloud Function dengan pemicu penyimpanan, jalankan perintah berikut di direktori yang berisi kode contoh (atau untuk Java, file pom.xml):

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

Ganti kode berikut:

  • RUNTIME: runtime yang didasarkan pada Ubuntu 18.04 atau yang lebih tinggi
  • REGION: Nama region Google Cloud tempat Anda ingin men-deploy fungsi (misalnya, us-west1).
  • YOUR_INPUT_BUCKET_NAME: Nama bucket Cloud Storage untuk mengupload gambar.
  • YOUR_OUTPUT_BUCKET_NAME: Nama bucket tempat menyimpan gambar yang diburamkan.

Saat men-deploy fungsi generasi ke-2, tentukan nama bucket saja tanpa awalan gs://; misalnya, --trigger-event-filters="bucket=my-bucket".

Upload gambar

  1. Upload gambar yang menyinggung, seperti gambar zombie pemakan daging ini:

    gsutil cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME

    pada YOUR_INPUT_BUCKET_NAME bucket Cloud Storage yang Anda buat sebelumnya untuk mengupload gambar.

  2. Anda akan melihat analisis gambar di log:

    gcloud beta functions logs read YOUR_FUNCTION_NAME --gen2 --limit=100
  3. Anda dapat melihat gambar yang diburamkan di bucket Cloud Storage YOUR_OUTPUT_BUCKET_NAME yang dibuat sebelumnya.

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

Menghapus project

Cara termudah untuk menghilangkan penagihan adalah dengan menghapus project yang Anda buat untuk tutorial.

Untuk menghapus project:

  1. Di konsol Google Cloud, buka halaman Manage resource.

    Buka Manage resource

  2. Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
  3. Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.

Menghapus Cloud Function

Menghapus Cloud Functions tidak akan menghapus resource apa pun yang tersimpan di Cloud Storage.

Untuk menghapus fungsi yang Anda deploy dalam tutorial ini, jalankan perintah berikut:

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 

Anda juga dapat menghapus Cloud Functions dari Konsol Google Cloud.