Instructivo de ImageMagick (1st gen)


En este instructivo, se muestra cómo usar Cloud Run Functions, la API de Cloud Vision e ImageMagick para detectar y difuminar imágenes ofensivas que se suben a un bucket de Cloud Storage.

Objetivos

  • Implementa una Cloud Run Function en segundo plano activada por Storage
  • Usar la API de Vision para detectar contenido violento o destinado para adultos
  • Usar ImageMagick para difuminar imágenes ofensivas
  • Probar la función con solo subir una imagen de un zombi que come carne humana

Costos

En este documento, usarás los siguientes componentes facturables de Google Cloud:

  • Cloud Run functions
  • Cloud Storage
  • Cloud Vision

Para generar una estimación de costos en función del uso previsto, usa la calculadora de precios.

Es posible que los usuarios de Google Cloud nuevos cumplan con los requisitos para acceder a una prueba gratuita.

Antes de comenzar

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. Si usas un proveedor de identidad externo (IdP), primero debes acceder a gcloud CLI con tu identidad federada.

  7. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. Si usas un proveedor de identidad externo (IdP), primero debes acceder a gcloud CLI con tu identidad federada.

  13. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  14. Si ya tienes instalado gcloud CLI, ejecuta el siguiente comando para actualizarla:

    gcloud components update
  15. Prepara tu entorno de desarrollo.
  16. Visualiza el flujo de datos

    El flujo de datos en la aplicación de instructivo de ImageMagick incluye varios pasos como se muestra a continuación:

    1. Se sube una imagen a un bucket de Cloud Storage.
    2. La función analiza la imagen con la API de Vision.
    3. Si se detecta contenido violento o destinado a adultos, la función usa ImageMagick para difuminar la imagen.
    4. La imagen difuminada se sube a otro bucket de Cloud Storage para su utilización.

    Prepara la aplicación

    1. Crea un bucket de Cloud Storage para subir imágenes, en el que YOUR_INPUT_BUCKET_NAME es un nombre de bucket único a nivel global:

      gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
    2. Crea un bucket de Cloud Storage para recibir las imágenes difuminadas, donde YOUR_OUTPUT_BUCKET_NAME es un nombre de bucket único a nivel global:

      gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
    3. Clona el repositorio de la app de muestra en tu máquina local:

      Node.js

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

      De manera opcional, puedes descargar la muestra como un archivo zip y extraerla.

      Python

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

      De manera opcional, puedes descargar la muestra como un archivo zip y extraerla.

      Go

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

      De manera opcional, puedes descargar la muestra como un archivo ZIP y extraerla.

      Java

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

      De manera opcional, puedes descargar la muestra como un archivo ZIP y extraerla.

      Ruby

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

      De manera opcional, puedes descargar la muestra como un archivo zip y extraerla.

    4. Ve al directorio que contiene el código de muestra de Cloud Run 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/

      Ruby

      cd ruby-docs-samples/functions/imagemagick/

    Comprende el código

    Importa dependencias

    La aplicación debe importar varias dependencias para interactuar con los servicios deGoogle Cloud , ImageMagick y el sistema de archivos:

    Node.js

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

    Ruby

    require "functions_framework"
    
    FunctionsFramework.on_startup do
      set_global :storage_client do
        require "google/cloud/storage"
        Google::Cloud::Storage.new
      end
    
      set_global :vision_client do
        require "google/cloud/vision"
        Google::Cloud::Vision.image_annotator
      end
    end

    Analiza imágenes

    Se invoca la siguiente función cuando una imagen se sube al bucket de Cloud Storage que creaste para almacenar imágenes. La función usa la API de Vision para detectar contenido violento o destinado para adultos en imágenes que se suben.

    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 = 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
        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: %w", 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 event, Context context) {
      // Validate parameters
      if (event.getBucket() == null || event.getName() == null) {
        logger.severe("Error: Malformed GCS event.");
        return;
      }
    
      BlobInfo blobInfo = BlobInfo.newBuilder(event.getBucket(), event.getName()).build();
    
      // Construct URI to GCS bucket and file.
      String gcsPath = String.format("gs://%s/%s", event.getBucket(), event.getName());
      logger.info(String.format("Analyzing %s", event.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.", event.getName()));
            blur(blobInfo);
          } else {
            logger.info(String.format("Detected %s as OK.", event.getName()));
          }
        }
      } catch (IOException e) {
        logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
      }
    }

    Ruby

    # Blurs uploaded images that are flagged as Adult or Violence.
    FunctionsFramework.cloud_event "blur_offensive_images" do |event|
      # Event-triggered Ruby functions receive a CloudEvents::Event::V1 object.
      # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html
      # The storage event payload can be obtained from the event data.
      payload = event.data
      file_name = payload["name"]
      bucket_name = payload["bucket"]
    
      # Ignore already-blurred files
      if file_name.start_with? "blurred-"
        logger.info "The image #{file_name} is already blurred."
        return
      end
    
      # Get image annotations from the Vision service
      logger.info "Analyzing #{file_name}."
      gs_uri = "gs://#{bucket_name}/#{file_name}"
      result = global(:vision_client).safe_search_detection image: gs_uri
      annotation = result.responses.first.safe_search_annotation
    
      # Respond to annotations by possibly blurring the image
      if annotation.adult == :VERY_LIKELY || annotation.violence == :VERY_LIKELY
        logger.info "The image #{file_name} was detected as inappropriate."
        blur_image bucket_name, file_name
      else
        logger.info "The image #{file_name} was detected as OK."
      end
    end

    Difumina imágenes

    La siguiente función recibe una llamada cuando se detecta contenido violento o destinado para adultos en una imagen que se sube. La función descarga la imagen ofensiva, usa ImageMagick para difuminarla y, luego, sube la imagen difuminada sobre la imagen original.

    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.blur(radius=0, sigma=16)
            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: %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)
    	}
    
    	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);
    }

    Ruby

    require "tempfile"
    require "mini_magick"
    
    # Blurs the given file using ImageMagick.
    def blur_image bucket_name, file_name
      tempfile = Tempfile.new
      begin
        # Download the image file
        bucket = global(:storage_client).bucket bucket_name
        file = bucket.file file_name
        file.download tempfile
        tempfile.close
    
        # Blur the image using ImageMagick
        MiniMagick::Image.new tempfile.path do |image|
          image.blur "0x16"
        end
        logger.info "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 and tell your function
        # to ignore files marked as blurred (e.g. those with a "blurred" prefix.)
        blur_bucket_name = ENV["BLURRED_BUCKET_NAME"]
        blur_bucket = global(:storage_client).bucket blur_bucket_name
        blur_bucket.create_file tempfile.path, file_name
        logger.info "Blurred image uploaded to gs://#{blur_bucket_name}/#{file_name}"
      ensure
        # Ruby will remove the temp file when garbage collecting the object,
        # but it is good practice to remove it explicitly.
        tempfile.unlink
      end
    end

    Implementa la función

    Para implementar tu función con un activador de almacenamiento, ejecuta el siguiente comando en el directorio que contiene el código de muestra (o en el caso de Java, el archivo pom.xml):

    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
    

    Reemplaza lo siguiente:

    • RUNTIME: Es un entorno de ejecución que se basa en Ubuntu 18.04 (los entornos de ejecución posteriores no incluyen compatibilidad con ImageMagick).
    • YOUR_INPUT_BUCKET_NAME: Es el nombre del bucket de Cloud Storage para subir imágenes.
    • YOUR_OUTPUT_BUCKET_NAME: El nombre del bucket en el que se deberían guardar las imágenes desenfocadas.

    Para este ejemplo en particular, no incluyas gs:// como parte de los nombres de bucket en el comando deploy.

    Sube una imagen

    1. Sube una imagen ofensiva, como la imagen de un zombi que come carne humana:

      gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME

      donde YOUR_INPUT_BUCKET_NAME es el bucket de Cloud Storage que creaste previamente para subir imágenes.

    2. Revisa los registros para asegurarte de que las ejecuciones se completaron:

      gcloud functions logs read --limit 100
    3. Puedes ver las imágenes desenfocadas en el bucket de Cloud Storage YOUR_OUTPUT_BUCKET_NAME que creaste antes.

    Limpia

    Para evitar que se apliquen cargos a tu cuenta de Google Cloud por los recursos usados en este instructivo, borra el proyecto que contiene los recursos o conserva el proyecto y borra los recursos individuales.

    Borra el proyecto

    La manera más fácil de eliminar la facturación es borrar el proyecto que creaste para el instructivo.

    Para borrar el proyecto, sigue estos pasos:

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    Borra la función

    Borrar las funciones de Cloud Run no quita ningún recurso almacenado en Cloud Storage.

    Para borrar la función que implementaste en este instructivo, ejecuta el siguiente comando:

    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 

    Ruby

    gcloud functions delete blur_offensive_images 

    También puedes borrar funciones de Cloud Run en la consola deGoogle Cloud .