演示如何将图片文件上传到 Cloud Storage,以及如何使用 Vision API 和 Translation API 提取并翻译图片中的文本。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
// Package ocr contains Go samples for creating OCR
// (Optical Character Recognition) Cloud functions.
package ocr
import (
"context"
"fmt"
"os"
"strings"
"time"
"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"`
}
// GCSEvent is the payload of a GCS event.
type GCSEvent struct {
Bucket string `json:"bucket"`
Name string `json:"name"`
Metageneration string `json:"metageneration"`
ResourceState string `json:"resourceState"`
TimeCreated time.Time `json:"timeCreated"`
Updated time.Time `json:"updated"`
}
// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for 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: %v", err)
}
}
if translateClient == nil {
translateClient, err = translate.NewClient(ctx)
if err != nil {
return fmt.Errorf("translate.NewClient: %v", err)
}
}
if pubsubClient == nil {
pubsubClient, err = pubsub.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("translate.NewClient: %v", err)
}
}
if storageClient == nil {
storageClient, err = storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
}
return nil
}
Java
public class OcrProcessImage implements BackgroundFunction<GcsEvent> {
// 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").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();
}
}
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();
Python
import base64
import json
import os
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["GCP_PROJECT"]
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。