광 문자 인식(OCR) 튜토리얼(2세대)


Google Cloud Platform에서 광 문자 인식(OCR)을 수행하는 방법을 알아보세요. 이 튜토리얼에서는 Cloud Storage에 이미지 파일을 업로드하거나, Cloud Vision을 사용하여 이미지에서 텍스트를 추출하거나, Cloud Translation API를 사용하여 텍스트를 번역하거나, 번역을 Cloud Storage에 다시 저장하는 방법을 보여줍니다. Pub/Sub를 사용하여 여러 태스크를 큐에 추가하고 이를 실행하기 위한 적절한 Cloud Functions를 트리거할 수 있습니다.

텍스트 인식(OCR) 요청 전송에 대한 자세한 내용은 이미지에서 텍스트 인식, 이미지에서 필기 입력 인식, 파일에서 텍스트 인식(PDF/TIFF)을 참조하세요.

목표

  • 여러 이벤트 기반 함수를 작성하고 배포합니다.
  • Cloud Storage에 이미지를 업로드합니다.
  • 업로드된 이미지에서 텍스트를 추출, 번역, 저장합니다.

비용

이 문서에서는 비용이 청구될 수 있는 다음과 같은 Google Cloud 구성요소를 사용합니다.

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

프로젝트 사용량을 기준으로 예상 비용을 산출하려면 가격 계산기를 사용하세요. Google Cloud를 처음 사용하는 사용자는 무료 체험판을 사용할 수 있습니다.

시작하기 전에

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  4. Enable the Cloud Functions, Cloud Build, Cloud Run, Artifact Registry, Eventarc, Logging, Pub/Sub, Cloud Storage, Cloud Translation, and Cloud Vision APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

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

    Go to project selector

  8. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  9. Enable the Cloud Functions, Cloud Build, Cloud Run, Artifact Registry, Eventarc, Logging, Pub/Sub, Cloud Storage, Cloud Translation, and Cloud Vision APIs.

    Enable the APIs

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. gcloud CLI가 이미 설치되어 있으면 다음 명령어를 실행하여 업데이트합니다.

    gcloud components update
  13. 개발 환경을 준비합니다.

데이터 흐름 시각화

OCR 튜토리얼 애플리케이션의 데이터 흐름 단계는 다음과 같습니다.

  1. 언어에 상관없이 텍스트를 포함하는 이미지가 Cloud Storage에 업로드됩니다.
  2. Vision API를 사용하여 텍스트를 추출하고 출발어를 감지하는 Cloud 함수가 트리거됩니다.
  3. 텍스트는 Pub/Sub 주제에 메시지를 게시하여 번역 큐에 추가됩니다. 번역은 출발어와 다른 각 도착어 큐에 추가됩니다.
  4. 도착어가 출발어와 일치하면 번역 큐를 건너뛰고 텍스트가 다른 Pub/Sub 주제인 결과 큐로 전송됩니다.
  5. Cloud 함수는 Translation API를 사용하여 번역 큐의 텍스트를 번역합니다. 번역 결과는 결과 큐로 전송됩니다.
  6. 다른 Cloud 함수가 결과 큐에 있는 번역된 텍스트를 Cloud Storage에 저장합니다.
  7. 결과는 Cloud Storage에서 각 번역의 텍스트 파일로 검색됩니다.

해당 단계를 시각화하면 다음과 같습니다.

애플리케이션 준비

  1. 이미지를 업로드할 Cloud Storage 버킷을 만듭니다. YOUR_IMAGE_BUCKET_NAME은 전역적으로 고유한 버킷 이름입니다.

    gsutil mb gs://YOUR_IMAGE_BUCKET_NAME
  2. 텍스트 번역을 저장할 Cloud Storage 버킷을 만듭니다. YOUR_RESULT_BUCKET_NAME은 전역적으로 고유한 버킷 이름입니다.

    gsutil mb gs://YOUR_RESULT_BUCKET_NAME
  3. 번역 요청을 게시할 Cloud Pub/Sub 주제를 만듭니다. YOUR_TRANSLATE_TOPIC_NAME은 번역 요청 주제의 이름입니다.

    gcloud pubsub topics create YOUR_TRANSLATE_TOPIC_NAME
  4. 완료된 번역 결과를 게시할 Cloud Pub/Sub 주제를 만듭니다. YOUR_RESULT_TOPIC_NAME은 번역 결과 주제의 이름입니다.

    gcloud pubsub topics create YOUR_RESULT_TOPIC_NAME
  5. 샘플 앱 저장소를 로컬 머신에 클론합니다.

    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 파일로 샘플을 다운로드하고 압축을 풀 수 있습니다.

  6. Cloud Functions 샘플 코드가 있는 디렉터리로 변경합니다.

    Node.js

    cd nodejs-docs-samples/functions/v2/ocr/app/

    Python

    cd python-docs-samples/functions/v2/ocr/

    Go

    cd golang-samples/functions/functionsv2/ocr/app/

    Java

    cd java-docs-samples/functions/v2/ocr/ocr-process-image/

코드 이해하기

이 섹션에서는 OCR 샘플을 구성하는 종속 항목과 함수를 설명합니다.

종속 항목 가져오기

애플리케이션은 Google Cloud Platform 서비스와 통신하기 위해 몇 가지 종속성을 가져와야 합니다.

Node.js

// Get a reference to the Pub/Sub component
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();

// Get a reference to the Cloud Storage component
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

// Get a reference to the Cloud Vision API component
const Vision = require('@google-cloud/vision');
const vision = new Vision.ImageAnnotatorClient();

// Get a reference to the Translate API component
const {Translate} = require('@google-cloud/translate').v2;
const translate = new Translate();

const functions = require('@google-cloud/functions-framework');

Python

import base64
import json
import os

from cloudevents.http import CloudEvent

import functions_framework

from google.cloud import pubsub_v1
from google.cloud import storage
from google.cloud import translate_v2 as translate
from google.cloud import vision

vision_client = vision.ImageAnnotatorClient()
translate_client = translate.Client()
publisher = pubsub_v1.PublisherClient()
storage_client = storage.Client()

project_id = os.environ.get("GCP_PROJECT")

Go


// Package ocr contains Go samples for creating OCR
// (Optical Character Recognition) Cloud functions.
package ocr

import (
	"context"
	"fmt"
	"os"
	"strings"

	"cloud.google.com/go/pubsub"
	"cloud.google.com/go/storage"
	"cloud.google.com/go/translate"
	vision "cloud.google.com/go/vision/apiv1"
	"golang.org/x/text/language"
)

type ocrMessage struct {
	Text     string       `json:"text"`
	FileName string       `json:"fileName"`
	Lang     language.Tag `json:"lang"`
	SrcLang  language.Tag `json:"srcLang"`
}

// Eventarc sends a MessagePublishedData object.
// See the documentation for additional fields and more details:
// https://cloud.google.com/eventarc/docs/cloudevents#pubsub_1
type MessagePublishedData struct {
	Message PubSubMessage
}

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for additional fields and more details:
// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
type PubSubMessage struct {
	Data []byte `json:"data"`
}

var (
	visionClient    *vision.ImageAnnotatorClient
	translateClient *translate.Client
	pubsubClient    *pubsub.Client
	storageClient   *storage.Client

	projectID      string
	resultBucket   string
	resultTopic    string
	toLang         []string
	translateTopic string
)

func setup(ctx context.Context) error {
	projectID = os.Getenv("GCP_PROJECT")
	resultBucket = os.Getenv("RESULT_BUCKET")
	resultTopic = os.Getenv("RESULT_TOPIC")
	toLang = strings.Split(os.Getenv("TO_LANG"), ",")
	translateTopic = os.Getenv("TRANSLATE_TOPIC")

	var err error // Prevent shadowing clients with :=.

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

	if translateClient == nil {
		translateClient, err = translate.NewClient(ctx)
		if err != nil {
			return fmt.Errorf("translate.NewClient: %w", err)
		}
	}

	if pubsubClient == nil {
		pubsubClient, err = pubsub.NewClient(ctx, projectID)
		if err != nil {
			return fmt.Errorf("translate.NewClient: %w", err)
		}
	}

	if storageClient == nil {
		storageClient, err = storage.NewClient(ctx)
		if err != nil {
			return fmt.Errorf("storage.NewClient: %w", err)
		}
	}
	return nil
}

Java

public class OcrProcessImage implements CloudEventsFunction {
  // TODO<developer> set these environment variables
  private static final String PROJECT_ID = System.getenv("GCP_PROJECT");
  private static final String TRANSLATE_TOPIC_NAME = System.getenv("TRANSLATE_TOPIC");
  private static final String[] TO_LANGS = System.getenv("TO_LANG") == null ? new String[] { "es" }
      : System.getenv("TO_LANG").split(",");

  private static final Logger logger = Logger.getLogger(OcrProcessImage.class.getName());
  private static final String LOCATION_NAME = LocationName.of(PROJECT_ID, "global").toString();
  private Publisher publisher;

  public OcrProcessImage() throws IOException {
    publisher = Publisher.newBuilder(ProjectTopicName.of(PROJECT_ID, TRANSLATE_TOPIC_NAME)).build();
  }

}

이미지 처리

다음 함수는 Cloud Storage에서 업로드된 이미지 파일을 읽고 이미지에 텍스트가 포함되어 있는지를 감지하는 함수를 호출합니다.

Node.js

/**
 * This function is exported by index.js, and is executed when
 * a file is uploaded to the Cloud Storage bucket you created
 * for uploading images.
 *
 * @param {object} cloudEvent A CloudEvent containing the Cloud Storage File object.
 * https://cloud.google.com/storage/docs/json_api/v1/objects
 */
functions.cloudEvent('processImage', async cloudEvent => {
  const {bucket, name} = cloudEvent.data;

  if (!bucket) {
    throw new Error(
      'Bucket not provided. Make sure you have a "bucket" property in your request'
    );
  }
  if (!name) {
    throw new Error(
      'Filename not provided. Make sure you have a "name" property in your request'
    );
  }

  await detectText(bucket, name);
  console.log(`File ${name} processed.`);
});

Python

@functions_framework.cloud_event
def process_image(cloud_event: CloudEvent) -> None:
    """Cloud Function triggered by Cloud Storage when a file is changed.

    Gets the names of the newly created object and its bucket then calls
    detect_text to find text in that image.

    detect_text finishes by sending PubSub messages requesting another service
    then complete processing those texts by translating them and saving the
    translations.
    """

    # Check that the received event is of the expected type, return error if not
    expected_type = "google.cloud.storage.object.v1.finalized"
    received_type = cloud_event["type"]
    if received_type != expected_type:
        raise ValueError(f"Expected {expected_type} but received {received_type}")

    # Extract the bucket and file names of the uploaded image for processing
    data = cloud_event.data
    bucket = data["bucket"]
    filename = data["name"]

    # Process the information in the new image
    detect_text(bucket, filename)

    print(f"File {filename} processed.")

Go


package ocr

import (
	"context"
	"fmt"
	"log"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/cloud/storagedata"
	"google.golang.org/protobuf/encoding/protojson"
)

func init() {
	functions.CloudEvent("process-image", ProcessImage)
}

// ProcessImage is executed when a file is uploaded to the Cloud Storage bucket you
// created for uploading images. It runs detectText, which processes the image for text.
func ProcessImage(ctx context.Context, cloudevent event.Event) error {
	if err := setup(ctx); err != nil {
		return fmt.Errorf("ProcessImage: %w", err)
	}

	var data storagedata.StorageObjectData
	if err := protojson.Unmarshal(cloudevent.Data(), &data); err != nil {
		return fmt.Errorf("protojson.Unmarshal: Failed to parse CloudEvent data: %w", err)
	}
	if data.GetBucket() == "" {
		return fmt.Errorf("empty file.Bucket")
	}
	if data.GetName() == "" {
		return fmt.Errorf("empty file.Name")
	}
	if err := detectText(ctx, data.GetBucket(), data.GetName()); err != nil {
		return fmt.Errorf("detectText: %w", err)
	}
	log.Printf("File %s processed.", data.GetName())
	return nil
}

Java


import com.google.cloud.functions.CloudEventsFunction;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.cloud.translate.v3.DetectLanguageRequest;
import com.google.cloud.translate.v3.DetectLanguageResponse;
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.TranslationServiceClient;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.events.cloud.storage.v1.StorageObjectData;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.PubsubMessage;
import io.cloudevents.CloudEvent;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    // Unmarshal data from CloudEvent
    String cloudEventData = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
    StorageObjectData.Builder builder = StorageObjectData.newBuilder();
    JsonFormat.parser().merge(cloudEventData, builder);
    StorageObjectData gcsEvent = builder.build();

    String bucket = gcsEvent.getBucket();
    if (bucket.isEmpty()) {
      throw new IllegalArgumentException("Missing bucket parameter");
    }
    String filename = gcsEvent.getName();
    if (filename.isEmpty()) {
      throw new IllegalArgumentException("Missing name parameter");
    }

    detectText(bucket, filename);
  }
}

다음 함수는 Cloud Vision API를 사용하여 이미지에서 텍스트를 추출하고 번역을 위해 큐에 추가합니다.

Node.js

/**
 * Detects the text in an image using the Google Vision API.
 *
 * @param {string} bucketName Cloud Storage bucket name.
 * @param {string} filename Cloud Storage file name.
 * @returns {Promise}
 */
const detectText = async (bucketName, filename) => {
  console.log(`Looking for text in image ${filename}`);
  const [textDetections] = await vision.textDetection(
    `gs://${bucketName}/${filename}`
  );
  const [annotation] = textDetections.textAnnotations;
  const text = annotation ? annotation.description.trim() : '';
  console.log('Extracted text from image:', text);

  let [translateDetection] = await translate.detect(text);
  if (Array.isArray(translateDetection)) {
    [translateDetection] = translateDetection;
  }
  console.log(
    `Detected language "${translateDetection.language}" for ${filename}`
  );

  // Submit a message to the bus for each language we're going to translate to
  const TO_LANGS = process.env.TO_LANG.split(',');
  const topicName = process.env.TRANSLATE_TOPIC;

  const tasks = TO_LANGS.map(lang => {
    const messageData = {
      text: text,
      filename: filename,
      lang: lang,
    };

    // Helper function that publishes translation result to a Pub/Sub topic
    // For more information on publishing Pub/Sub messages, see this page:
    //   https://cloud.google.com/pubsub/docs/publisher
    return publishResult(topicName, messageData);
  });

  return Promise.all(tasks);
};

Python

def detect_text(bucket: str, filename: str) -> None:
    """Extract the text from an image uploaded to Cloud Storage, then
    publish messages requesting subscribing services translate the text
    to each target language and save the result.

    Args:
        bucket: name of GCS bucket in which the file is stored.
        filename: name of the file to be read.
    """

    print(f"Looking for text in image {filename}")

    # Use the Vision API to extract text from the image
    image = vision.Image(
        source=vision.ImageSource(gcs_image_uri=f"gs://{bucket}/{filename}")
    )
    text_detection_response = vision_client.text_detection(image=image)
    annotations = text_detection_response.text_annotations

    if annotations:
        text = annotations[0].description
    else:
        text = ""
    print(f"Extracted text {text} from image ({len(text)} chars).")

    detect_language_response = translate_client.detect_language(text)
    src_lang = detect_language_response["language"]
    print(f"Detected language {src_lang} for text {text}.")

    # Submit a message to the bus for each target language
    futures = []  # Asynchronous publish request statuses

    to_langs = os.environ.get("TO_LANG", "").split(",")
    for target_lang in to_langs:
        topic_name = os.environ.get("TRANSLATE_TOPIC")
        if src_lang == target_lang or src_lang == "und":
            topic_name = os.environ.get("RESULT_TOPIC")

        message = {
            "text": text,
            "filename": filename,
            "lang": target_lang,
            "src_lang": src_lang,
        }

        message_data = json.dumps(message).encode("utf-8")
        topic_path = publisher.topic_path(project_id, topic_name)
        future = publisher.publish(topic_path, data=message_data)
        futures.append(future)

    # Wait for each publish request to be completed before exiting
    for future in futures:
        future.result()

Go


package ocr

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"cloud.google.com/go/pubsub"
	"golang.org/x/text/language"
	visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

// detectText detects the text in an image using the Google Vision API.
func detectText(ctx context.Context, bucketName, fileName string) error {
	log.Printf("Looking for text in image %v", fileName)
	maxResults := 1
	image := &visionpb.Image{
		Source: &visionpb.ImageSource{
			GcsImageUri: fmt.Sprintf("gs://%s/%s", bucketName, fileName),
		},
	}
	annotations, err := visionClient.DetectTexts(ctx, image, &visionpb.ImageContext{}, maxResults)
	if err != nil {
		return fmt.Errorf("DetectTexts: %w", err)
	}
	text := ""
	if len(annotations) > 0 {
		text = annotations[0].Description
	}
	if len(annotations) == 0 || len(text) == 0 {
		log.Printf("No text detected in image %q. Returning early.", fileName)
		return nil
	}
	log.Printf("Extracted text %q from image (%d chars).", text, len(text))

	detectResponse, err := translateClient.DetectLanguage(ctx, []string{text})
	if err != nil {
		return fmt.Errorf("DetectLanguage: %w", err)
	}
	if len(detectResponse) == 0 || len(detectResponse[0]) == 0 {
		return fmt.Errorf("DetectLanguage gave empty response")
	}
	srcLang := detectResponse[0][0].Language.String()
	log.Printf("Detected language %q for text %q.", srcLang, text)

	// Submit a message to the bus for each target language
	for _, targetLang := range toLang {
		topicName := translateTopic
		if srcLang == targetLang || srcLang == "und" { // detection returns "und" for undefined language
			topicName = resultTopic
		}
		targetTag, err := language.Parse(targetLang)
		if err != nil {
			return fmt.Errorf("language.Parse: %w", err)
		}
		srcTag, err := language.Parse(srcLang)
		if err != nil {
			return fmt.Errorf("language.Parse: %w", err)
		}
		message, err := json.Marshal(ocrMessage{
			Text:     text,
			FileName: fileName,
			Lang:     targetTag,
			SrcLang:  srcTag,
		})
		if err != nil {
			return fmt.Errorf("json.Marshal: %w", err)
		}
		topic := pubsubClient.Topic(topicName)
		ok, err := topic.Exists(ctx)
		if err != nil {
			return fmt.Errorf("Exists: %w", err)
		}
		if !ok {
			topic, err = pubsubClient.CreateTopic(ctx, topicName)
			if err != nil {
				return fmt.Errorf("CreateTopic: %w", err)
			}
		}
		msg := &pubsub.Message{
			Data: []byte(message),
		}
		log.Printf("Sending pubsub message: %s", message)
		if _, err = topic.Publish(ctx, msg).Get(ctx); err != nil {
			return fmt.Errorf("Get: %w", err)
		}
	}
	return nil
}

Java

private void detectText(String bucket, String filename) {
  logger.info("Looking for text in image " + filename);

  List<AnnotateImageRequest> visionRequests = new ArrayList<>();
  String gcsPath = String.format("gs://%s/%s", bucket, filename);

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();

  Feature textFeature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
  AnnotateImageRequest visionRequest = AnnotateImageRequest.newBuilder()
      .addFeatures(textFeature).setImage(img)
      .build();
  visionRequests.add(visionRequest);

  // Detect text in an image using the Cloud Vision API
  AnnotateImageResponse visionResponse;
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    visionResponse = client.batchAnnotateImages(visionRequests).getResponses(0);
    if (visionResponse == null || !visionResponse.hasFullTextAnnotation()) {
      logger.info(String.format("Image %s contains no text", filename));
      return;
    }

    if (visionResponse.hasError()) {
      // Log error
      logger.log(
          Level.SEVERE, "Error in vision API call: " + visionResponse.getError().getMessage());
      return;
    }
  } catch (IOException e) {
    // Log error (since IOException cannot be thrown by a Cloud Function)
    logger.log(Level.SEVERE, "Error detecting text: " + e.getMessage(), e);
    return;
  }

  String text = visionResponse.getFullTextAnnotation().getText();
  logger.info("Extracted text from image: " + text);

  // Detect language using the Cloud Translation API
  DetectLanguageRequest languageRequest = DetectLanguageRequest.newBuilder()
      .setParent(LOCATION_NAME)
      .setMimeType("text/plain")
      .setContent(text)
      .build();
  DetectLanguageResponse languageResponse;
  try (TranslationServiceClient client = TranslationServiceClient.create()) {
    languageResponse = client.detectLanguage(languageRequest);
  } catch (IOException e) {
    // Log error (since IOException cannot be thrown by a function)
    logger.log(Level.SEVERE, "Error detecting language: " + e.getMessage(), e);
    return;
  }

  if (languageResponse.getLanguagesCount() == 0) {
    logger.info("No languages were detected for text: " + text);
    return;
  }

  String languageCode = languageResponse.getLanguages(0).getLanguageCode();
  logger.info(String.format("Detected language %s for file %s", languageCode, filename));

  // Send a Pub/Sub translation request for every language we're going to
  // translate to
  for (String targetLanguage : TO_LANGS) {
    logger.info("Sending translation request for language " + targetLanguage);
    OcrTranslateApiMessage message = new OcrTranslateApiMessage(text, filename, targetLanguage);
    ByteString byteStr = ByteString.copyFrom(message.toPubsubData());
    PubsubMessage pubsubApiMessage = PubsubMessage.newBuilder().setData(byteStr).build();
    try {
      publisher.publish(pubsubApiMessage).get();
    } catch (InterruptedException | ExecutionException e) {
      // Log error
      logger.log(Level.SEVERE, "Error publishing translation request: " + e.getMessage(), e);
      return;
    }
  }
}

텍스트 번역

다음 함수는 추출된 텍스트를 번역하고 이를 Cloud Storage에 다시 저장하기 위해 대기열에 추가합니다.

Node.js

/**
 * This function is exported by index.js, and is executed when
 * a message is published to the Cloud Pub/Sub topic specified
 * by the TRANSLATE_TOPIC environment variable. The function
 * translates text using the Google Translate API.
 *
 * @param {object} cloudEvent The CloudEvent containing the Pub/Sub Message object
 * https://cloud.google.com/storage/docs/json_api/v1/objects
 */
functions.cloudEvent('translateText', async cloudEvent => {
  const pubsubData = cloudEvent.data;
  const jsonStr = Buffer.from(pubsubData.message, 'base64').toString();
  const {text, filename, lang} = JSON.parse(jsonStr);

  if (!text) {
    throw new Error(
      'Text not provided. Make sure you have a "text" property in your request'
    );
  }
  if (!filename) {
    throw new Error(
      'Filename not provided. Make sure you have a "filename" property in your request'
    );
  }
  if (!lang) {
    throw new Error(
      'Language not provided. Make sure you have a "lang" property in your request'
    );
  }

  console.log(`Translating text into ${lang}`);
  const [translation] = await translate.translate(text, lang);

  console.log('Translated text:', translation);

  const messageData = {
    text: translation,
    filename: filename,
    lang: lang,
  };

  await publishResult(process.env.RESULT_TOPIC, messageData);
  console.log(`Text translated to ${lang}`);
});

Python

@functions_framework.cloud_event
def translate_text(cloud_event: CloudEvent) -> None:
    """Cloud Function triggered by PubSub when a message is received from
    a subscription.

    Translates the text in the message from the specified source language
    to the requested target language, then sends a message requesting another
    service save the result.
    """

    # Check that the received event is of the expected type, return error if not
    expected_type = "google.cloud.pubsub.topic.v1.messagePublished"
    received_type = cloud_event["type"]
    if received_type != expected_type:
        raise ValueError(f"Expected {expected_type} but received {received_type}")

    # Extract the message body, expected to be a JSON representation of a
    # dictionary, and extract the fields from that dictionary.
    data = cloud_event.data["message"]["data"]
    try:
        message_data = base64.b64decode(data)
        message = json.loads(message_data)

        text = message["text"]
        filename = message["filename"]
        target_lang = message["lang"]
        src_lang = message["src_lang"]
    except Exception as e:
        raise ValueError(f"Missing or malformed PubSub message {data}: {e}.")

    # Translate the text and publish a message with the translation
    print(f"Translating text into {target_lang}.")

    translated_text = translate_client.translate(
        text, target_language=target_lang, source_language=src_lang
    )

    topic_name = os.environ["RESULT_TOPIC"]
    message = {
        "text": translated_text["translatedText"],
        "filename": filename,
        "lang": target_lang,
    }
    message_data = json.dumps(message).encode("utf-8")
    topic_path = publisher.topic_path(project_id, topic_name)
    future = publisher.publish(topic_path, data=message_data)
    future.result()  # Wait for operation to complete

Go


package ocr

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"cloud.google.com/go/pubsub"
	"cloud.google.com/go/translate"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
)

func init() {
	functions.CloudEvent("translate-text", TranslateText)
}

// TranslateText is executed when a message is published to the Cloud Pub/Sub
// topic specified by the TRANSLATE_TOPIC environment variable, and translates
// the text using the Google Translate API.
func TranslateText(ctx context.Context, cloudevent event.Event) error {
	var event MessagePublishedData
	if err := setup(ctx); err != nil {
		return fmt.Errorf("setup: %w", err)
	}
	if err := cloudevent.DataAs(&event); err != nil {
		return fmt.Errorf("Failed to parse CloudEvent data: %w", err)
	}
	if event.Message.Data == nil {
		log.Printf("event: %s", event)
		return fmt.Errorf("empty data")
	}
	var message ocrMessage
	if err := json.Unmarshal(event.Message.Data, &message); err != nil {
		return fmt.Errorf("json.Unmarshal: %w", err)
	}

	log.Printf("Translating text into %s.", message.Lang.String())
	opts := translate.Options{
		Source: message.SrcLang,
	}
	translateResponse, err := translateClient.Translate(ctx, []string{message.Text}, message.Lang, &opts)
	if err != nil {
		return fmt.Errorf("Translate: %w", err)
	}
	if len(translateResponse) == 0 {
		return fmt.Errorf("Empty Translate response")
	}
	translatedText := translateResponse[0]

	messageData, err := json.Marshal(ocrMessage{
		Text:     translatedText.Text,
		FileName: message.FileName,
		Lang:     message.Lang,
		SrcLang:  message.SrcLang,
	})
	if err != nil {
		return fmt.Errorf("json.Marshal: %w", err)
	}

	topic := pubsubClient.Topic(resultTopic)
	ok, err := topic.Exists(ctx)
	if err != nil {
		return fmt.Errorf("Exists: %w", err)
	}
	if !ok {
		topic, err = pubsubClient.CreateTopic(ctx, resultTopic)
		if err != nil {
			return fmt.Errorf("CreateTopic: %w", err)
		}
	}
	msg := &pubsub.Message{
		Data: messageData,
	}
	if _, err = topic.Publish(ctx, msg).Get(ctx); err != nil {
		return fmt.Errorf("Get: %w", err)
	}
	log.Printf("Sent translation: %q", translatedText.Text)
	return nil
}

Java


import com.google.cloud.functions.CloudEventsFunction;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.TranslateTextRequest;
import com.google.cloud.translate.v3.TranslateTextResponse;
import com.google.cloud.translate.v3.TranslationServiceClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.PubsubMessage;
import functions.eventpojos.MessagePublishedData;
import io.cloudevents.CloudEvent;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OcrTranslateText implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(OcrTranslateText.class.getName());

  // TODO<developer> set these environment variables
  private static final String PROJECT_ID = getenv("GCP_PROJECT");
  private static final String RESULTS_TOPIC_NAME = getenv("RESULT_TOPIC");
  private static final String LOCATION_NAME = LocationName.of(PROJECT_ID, "global").toString();

  private Publisher publisher;

  public OcrTranslateText() throws IOException {
    publisher = Publisher.newBuilder(ProjectTopicName.of(PROJECT_ID, RESULTS_TOPIC_NAME)).build();
  }

  // Create custom deserializer to handle timestamps in event data
  class DateDeserializer implements JsonDeserializer<OffsetDateTime> {
    @Override
    public OffsetDateTime deserialize(
        JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return OffsetDateTime.parse(json.getAsString());
    }
  }

  Gson gson =
      new GsonBuilder().registerTypeAdapter(OffsetDateTime.class, new DateDeserializer()).create();

  @Override
  public void accept(CloudEvent event) throws InterruptedException, IOException {
    MessagePublishedData data =
        gson.fromJson(
            new String(event.getData().toBytes(), StandardCharsets.UTF_8),
            MessagePublishedData.class);
    OcrTranslateApiMessage ocrMessage =
        OcrTranslateApiMessage.fromPubsubData(
            data.getMessage().getData().getBytes(StandardCharsets.UTF_8));

    String targetLang = ocrMessage.getLang();
    logger.info("Translating text into " + targetLang);

    // Translate text to target language
    String text = ocrMessage.getText();
    TranslateTextRequest request =
        TranslateTextRequest.newBuilder()
            .setParent(LOCATION_NAME)
            .setMimeType("text/plain")
            .setTargetLanguageCode(targetLang)
            .addContents(text)
            .build();

    TranslateTextResponse response;
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
      response = client.translateText(request);
    } catch (IOException e) {
      // Log error (since IOException cannot be thrown by a function)
      logger.log(Level.SEVERE, "Error translating text: " + e.getMessage(), e);
      return;
    }
    if (response.getTranslationsCount() == 0) {
      return;
    }

    String translatedText = response.getTranslations(0).getTranslatedText();
    logger.info("Translated text: " + translatedText);

    // Send translated text to (subsequent) Pub/Sub topic
    String filename = ocrMessage.getFilename();
    OcrTranslateApiMessage translateMessage =
        new OcrTranslateApiMessage(translatedText, filename, targetLang);
    try {
      ByteString byteStr = ByteString.copyFrom(translateMessage.toPubsubData());
      PubsubMessage pubsubApiMessage = PubsubMessage.newBuilder().setData(byteStr).build();
      publisher.publish(pubsubApiMessage).get();
      logger.info("Text translated to " + targetLang);
    } catch (InterruptedException | ExecutionException e) {
      // Log error (since these exception types cannot be thrown by a function)
      logger.log(Level.SEVERE, "Error publishing translation save request: " + e.getMessage(), e);
    }
  }

  // Avoid ungraceful deployment failures due to unset environment variables.
  // If you get this warning you should redeploy with the variable set.
  private static String getenv(String name) {
    String value = System.getenv(name);
    if (value == null) {
      logger.warning("Environment variable " + name + " was not set");
      value = "MISSING";
    }
    return value;
  }
}

번역 저장

마지막으로 다음 함수는 번역된 텍스트를 받아 Cloud Storage에 다시 저장합니다.

Node.js

/**
 * This function is exported by index.js, and is executed when
 * a message is published to the Cloud Pub/Sub topic specified
 * by the RESULT_TOPIC environment variable. The function saves
 * the data packet to a file in GCS.
 *
 * @param {object} cloudEvent The CloudEvent containing the Pub/Sub Message object.
 * https://cloud.google.com/storage/docs/json_api/v1/objects
 */
functions.cloudEvent('saveResult', async cloudEvent => {
  const pubsubData = cloudEvent.data;
  const jsonStr = Buffer.from(pubsubData.message, 'base64').toString();
  const {text, filename, lang} = JSON.parse(jsonStr);

  if (!text) {
    throw new Error(
      'Text not provided. Make sure you have a "text" property in your request'
    );
  }
  if (!filename) {
    throw new Error(
      'Filename not provided. Make sure you have a "filename" property in your request'
    );
  }
  if (!lang) {
    throw new Error(
      'Language not provided. Make sure you have a "lang" property in your request'
    );
  }

  console.log(`Received request to save file ${filename}`);

  const bucketName = process.env.RESULT_BUCKET;
  const newFilename = renameImageForSave(filename, lang);
  const file = storage.bucket(bucketName).file(newFilename);

  console.log(`Saving result to ${newFilename} in bucket ${bucketName}`);

  await file.save(text);
  console.log('File saved.');
});

Python

@functions_framework.cloud_event
def save_result(cloud_event: CloudEvent) -> None:
    """Cloud Function triggered by PubSub when a message is received from
    a subscription.

    Saves translated text to a Cloud Storage object as requested.
    """
    # Check that the received event is of the expected type, return error if not
    expected_type = "google.cloud.pubsub.topic.v1.messagePublished"
    received_type = cloud_event["type"]
    if received_type != expected_type:
        raise ValueError(f"Expected {expected_type} but received {received_type}")

    # Extract the message body, expected to be a JSON representation of a
    # dictionary, and extract the fields from that dictionary.
    data = cloud_event.data["message"]["data"]
    try:
        message_data = base64.b64decode(data)
        message = json.loads(message_data)

        text = message["text"]
        filename = message["filename"]
        lang = message["lang"]
    except Exception as e:
        raise ValueError(f"Missing or malformed PubSub message {data}: {e}.")

    print(f"Received request to save file {filename}.")

    # Save the translation in RESULT_BUCKET
    bucket_name = os.environ["RESULT_BUCKET"]
    result_filename = f"{filename}_{lang}.txt"
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(result_filename)

    print(f"Saving result to {result_filename} in bucket {bucket_name}.")

    blob.upload_from_string(text)

    print("File saved.")

Go


package ocr

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
)

func init() {
	functions.CloudEvent("save-result", SaveResult)
}

// SaveResult is executed when a message is published to the Cloud Pub/Sub topic
// specified by the RESULT_TOPIC environment vairable, and saves the data packet
// to a file in GCS.
func SaveResult(ctx context.Context, cloudevent event.Event) error {
	var event MessagePublishedData
	if err := setup(ctx); err != nil {
		return fmt.Errorf("ProcessImage: %w", err)
	}
	if err := cloudevent.DataAs(&event); err != nil {
		return fmt.Errorf("Failed to parse CloudEvent data: %w", err)
	}
	var message ocrMessage
	if event.Message.Data == nil {
		return fmt.Errorf("Empty data")
	}
	if err := json.Unmarshal(event.Message.Data, &message); err != nil {
		return fmt.Errorf("json.Unmarshal: %w", err)
	}
	log.Printf("Received request to save file %q.", message.FileName)

	resultFilename := fmt.Sprintf("%s_%s.txt", message.FileName, message.Lang)
	bucket := storageClient.Bucket(resultBucket)

	log.Printf("Saving result to %q in bucket %q.", resultFilename, resultBucket)

	w := bucket.Object(resultFilename).NewWriter(ctx)
	defer w.Close()
	fmt.Fprint(w, message.Text)

	log.Printf("File saved.")
	return nil
}

Java


import com.google.cloud.functions.CloudEventsFunction;
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.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import functions.eventpojos.MessagePublishedData;
import io.cloudevents.CloudEvent;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.logging.Logger;

public class OcrSaveResult implements CloudEventsFunction {
  // TODO<developer> set this environment variable
  private static final String RESULT_BUCKET = System.getenv("RESULT_BUCKET");

  private static final Storage STORAGE = StorageOptions.getDefaultInstance().getService();
  private static final Logger logger = Logger.getLogger(OcrSaveResult.class.getName());

  // Configure Gson with custom deserializer to handle timestamps in event data
  class DateDeserializer implements JsonDeserializer<OffsetDateTime> {
    @Override
    public OffsetDateTime deserialize(
        JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return OffsetDateTime.parse(json.getAsString());
    }
  }

  Gson gson =
      new GsonBuilder().registerTypeAdapter(OffsetDateTime.class, new DateDeserializer()).create();

  @Override
  public void accept(CloudEvent event) {
    // Unmarshal data from CloudEvent
    MessagePublishedData data =
        gson.fromJson(
            new String(event.getData().toBytes(), StandardCharsets.UTF_8),
            MessagePublishedData.class);
    OcrTranslateApiMessage ocrMessage =
        OcrTranslateApiMessage.fromPubsubData(
            data.getMessage().getData().getBytes(StandardCharsets.UTF_8));

    logger.info("Received request to save file " + ocrMessage.getFilename());

    String newFileName =
        String.format("%s_to_%s.txt", ocrMessage.getFilename(), ocrMessage.getLang());

    // Save file to RESULT_BUCKET with name newFileName
    logger.info(String.format("Saving result to %s in bucket %s", newFileName, RESULT_BUCKET));
    BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(RESULT_BUCKET, newFileName)).build();
    STORAGE.create(blobInfo, ocrMessage.getText().getBytes(StandardCharsets.UTF_8));
    logger.info("File saved");
  }
}

함수 배포

  1. Cloud Storage 트리거를 사용하여 이미지 처리 함수를 배포하려면 샘플 코드(또는 Java의 경우 pom.xml 파일)가 포함된 디렉터리에서 다음 명령어를 실행합니다.

    // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version) // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version)

    Node.js

    gcloud functions deploy ocr-extract \
    --gen2 \
    --runtime=nodejs20 \
    --region=REGION \
    --source=. \
    --entry-point=processImage \
    --trigger-bucket YOUR_IMAGE_BUCKET_NAME \
    --set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Node.js 버전의 런타임 ID를 지정합니다.

    Python

    gcloud functions deploy ocr-extract \
    --gen2 \
    --runtime=python312 \
    --region=REGION \
    --source=. \
    --entry-point=process_image \
    --trigger-bucket YOUR_IMAGE_BUCKET_NAME \
    --set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Python 버전의 런타임 ID를 지정합니다.

    Go

    gcloud functions deploy ocr-extract \
    --gen2 \
    --runtime=go121 \
    --region=REGION \
    --source=. \
    --entry-point=process-image \
    --trigger-bucket YOUR_IMAGE_BUCKET_NAME \
    --set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Go 버전의 런타임 ID를 지정합니다.

    Java

    gcloud functions deploy ocr-extract \
    --gen2 \
    --runtime=java17 \
    --region=REGION \
    --source=. \
    --entry-point=functions.OcrProcessImage \
    --memory=512MB \
    --trigger-bucket YOUR_IMAGE_BUCKET_NAME \
    --set-env-vars "^:^GCP_PROJECT=YOUR_GCP_PROJECT_ID:TRANSLATE_TOPIC=YOUR_TRANSLATE_TOPIC_NAME:RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME:TO_LANG=es,en,fr,ja"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Java 버전의 런타임 ID를 지정합니다.

    다음을 바꿉니다.

    • REGION: 함수를 배포할 Google Cloud 리전의 이름(예: us-west1)
    • YOUR_IMAGE_BUCKET_NAME: 이미지를 업로드할 Cloud Storage 버킷 이름. 2세대 함수를 배포할 때는 선행 gs:// 없이 버킷 이름만 지정합니다(예: --trigger-event-filters="bucket=my-bucket").
  2. Cloud Pub/Sub 트리거를 사용하여 텍스트 번역 함수를 배포하려면 샘플 코드(또는 Java의 경우 pom.xml 파일)가 포함된 디렉터리에서 다음 명령어를 실행합니다.

    // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version) // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version)

    Node.js

    gcloud functions deploy ocr-translate \
    --gen2 \
    --runtime=nodejs20 \
    --region=REGION \
    --source=. \
    --entry-point=translateText \
    --trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Node.js 버전의 런타임 ID를 지정합니다.

    Python

    gcloud functions deploy ocr-translate \
    --gen2 \
    --runtime=python312 \
    --region=REGION \
    --source=. \
    --entry-point=translate_text \
    --trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Python 버전의 런타임 ID를 지정합니다.

    Go

    gcloud functions deploy ocr-translate \
    --gen2 \
    --runtime=go121 \
    --region=REGION \
    --source=. \
    --entry-point=translate-text \
    --trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Go 버전의 런타임 ID를 지정합니다.

    Java

    gcloud functions deploy ocr-translate \
    --gen2 \
    --runtime=java17 \
    --region=REGION \
    --source=. \
    --entry-point=functions.OcrTranslateText \
    --memory=512MB \
    --trigger-topic YOUR_TRANSLATE_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_TOPIC=YOUR_RESULT_TOPIC_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Java 버전의 런타임 ID를 지정합니다.

  3. Cloud Pub/Sub 트리거를 사용하여 결과를 Cloud Storage에 저장하는 함수를 배포하려면 샘플 코드(또는 Java의 경우 pom.xml 파일)가 있는 디렉터리에서 다음 명령어를 실행합니다.

    // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version) // LINT.IfChange(nodejs_version) // LINT.ThenChange(:nodejs_version_console_text) // LINT.IfChange(nodejs_version_console_text) // LINT.ThenChange(:nodejs_version)

    Node.js

    gcloud functions deploy ocr-save \
    --gen2 \
    --runtime=nodejs20 \
    --region=REGION \
    --source=. \
    --entry-point=saveResult \
    --trigger-topic YOUR_RESULT_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Node.js 버전의 런타임 ID를 지정합니다.

    Python

    gcloud functions deploy ocr-save \
    --gen2 \
    --runtime=python312 \
    --region=REGION \
    --source=. \
    --entry-point=save_result \
    --trigger-topic YOUR_RESULT_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Python 버전의 런타임 ID를 지정합니다.

    Go

    gcloud functions deploy ocr-save \
    --gen2 \
    --runtime=go121 \
    --region=REGION \
    --source=. \
    --entry-point=save-result \
    --trigger-topic YOUR_RESULT_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Go 버전의 런타임 ID를 지정합니다.

    Java

    gcloud functions deploy ocr-save \
    --gen2 \
    --runtime=java17 \
    --region=REGION \
    --source=. \
    --entry-point=functions.OcrSaveResult \
    --memory=512MB \
    --trigger-topic YOUR_RESULT_TOPIC_NAME \
    --set-env-vars "GCP_PROJECT=YOUR_GCP_PROJECT_ID,RESULT_BUCKET=YOUR_RESULT_BUCKET_NAME"

    --runtime 플래그를 사용하여 함수를 실행할 지원되는 Java 버전의 런타임 ID를 지정합니다.

이미지 업로드

  1. 이미지를 이미지 Cloud Storage 버킷에 업로드합니다.

    gsutil cp PATH_TO_IMAGE gs://YOUR_IMAGE_BUCKET_NAME

    각 항목의 의미는 다음과 같습니다.

    • PATH_TO_IMAGE는 로컬 시스템의 이미지 파일(텍스트 포함) 경로입니다.
    • YOUR_IMAGE_BUCKET_NAME은 이미지를 업로드할 버킷의 이름입니다.

    샘플 프로젝트에서 이미지 중 하나를 다운로드할 수 있습니다.

  2. 로그를 확인하여 실행이 완료되었는지 확인합니다.

    gcloud functions logs read --limit 100
  3. YOUR_RESULT_BUCKET_NAME에 사용한 Cloud Storage 버킷에서 저장된 번역을 볼 수 있습니다.

삭제

이 튜토리얼에서 사용된 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하세요.

프로젝트 삭제

비용이 청구되지 않도록 하는 가장 쉬운 방법은 튜토리얼에서 만든 프로젝트를 삭제하는 것입니다.

프로젝트를 삭제하는 방법은 다음과 같습니다.

  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.

Cloud Functions 삭제

Cloud Functions를 삭제해도 Cloud Storage에 저장된 리소스는 삭제되지 않습니다.

이 가이드에서 만든 Cloud Functions를 삭제하려면 다음 명령어를 실행합니다.

gcloud functions delete ocr-extract
gcloud functions delete ocr-translate
gcloud functions delete ocr-save

Google Cloud Console에서 Cloud Functions를 삭제할 수도 있습니다.