תרגום טקסט מתוך תמונה

בדף הזה מוסבר איך לזהות טקסט בתמונה, איך להתאים אישית תרגומים ואיך ליצור דיבור סינתטי מטקסט. במדריך הזה נעשה שימוש ב-Cloud Vision כדי לזהות טקסט בקובץ תמונה. בהמשך המדריך מוסבר איך להשתמש ב-Cloud Translation כדי לספק תרגום מותאם אישית של הטקסט שזוהה. בסיום, המדריך הזה משתמש בהמרת טקסט לדיבור כדי לספק הכתבה אוטומטית של הטקסט המתורגם.

מטרות

  1. העברת טקסט שזוהה על ידי Cloud Vision API אל Cloud Translation API.

  2. אפשר ליצור מילוני מונחים של Cloud Translation ולהשתמש בהם כדי להתאים אישית תרגומים של Cloud Translation API.

  3. יצירת ייצוג אודיו של טקסט מתורגם באמצעות Text-to-Speech API.

עלויות

לכל Google Cloud API יש מבנה תמחור נפרד.

פרטים על התמחור מופיעים במדריך התמחור של Cloud Vision, במדריך התמחור של Cloud Translation ובמדריך התמחור של Text-to-Speech.

לפני שמתחילים

חשוב לוודא שיש לכם:

הגדרה של ספריות לקוח

במדריך הזה נעשה שימוש בספריות הלקוח של Vision,‏ Translation ו-Text-to-Speech.

כדי להתקין את ספריות הלקוח הרלוונטיות, מריצים את הפקודות הבאות מהטרמינל.

Python

  pip install --upgrade google-cloud-vision
  pip install --upgrade google-cloud-translate
  pip install --upgrade google-cloud-texttospeech
  

Node.js

  npm install @google-cloud/vision
  npm install @google-cloud/translate
  npm install @google-cloud/text-to-speech
  

הגדרת הרשאות ליצירת מילון מונחים

כדי ליצור מילוני מונחים לתרגום, צריך להשתמש במפתח של חשבון שירות עם הרשאות 'עורך Cloud Translation API'.

כדי להגדיר מפתח של חשבון שירות עם הרשאות עריכה ב-Cloud Translation API:

  1. יוצרים חשבון שירות:

    1. נכנסים לדף Service Accounts במסוף Google Cloud .

      כניסה לדף Service Accounts

    2. בוחרים את הפרויקט הרצוי.

    3. לוחצים על יצירת חשבון שירות.

    4. כותבים שם בשדה Service account name. השדה Service account ID ימולא במסוףGoogle Cloud בהתאם לשם הזה.

    5. אופציונלי: בשדה Service account description, מזינים תיאור של חשבון השירות.

    6. לוחצים על Create and continue.

    7. לוחצים על השדה Select a role (בחירת תפקיד) ובוחרים באפשרות Cloud Translation > Cloud Translation API Editor (עורך Cloud Translation API).

    8. לוחצים על Done כדי לסיים ליצור את חשבון השירות.

      חשוב לא לסגור את החלון של הדפדפן, כי תשתמשו בו גם בשלב הבא.

  2. מורידים מפתח JSON לחשבון השירות שיצרתם:

    1. במסוף Google Cloud , לוחצים על כתובת האימייל של חשבון השירות שיצרתם.
    2. לוחצים על Keys.
    3. לוחצים על Add key ואז על Create new key.
    4. לוחצים על יצירה. למחשב שלכם תתבצע הורדה של קובץ JSON עם המפתח.

      חשוב לאחסן את קובץ המפתח באופן מאובטח כי הוא יכול לשמש לצורך אימות כחשבון השירות שלכם. אפשר להעביר את הקובץ לאן שרוצים ולשנות את השם שלו למה שרוצים.

    5. לוחצים על Close.

  3. בטרמינל, מגדירים את המשתנה GOOGLE_APPLICATION_CREDENTIALS באמצעות הפקודה הבאה. מחליפים את path_to_key בנתיב של קובץ ה-JSON שהורדתם ומכיל את המפתח החדש של חשבון השירות.

    Linux או macOS

    export GOOGLE_APPLICATION_CREDENTIALS=path_to_key

    Windows

    set GOOGLE_APPLICATION_CREDENTIALS=path_to_key

ייבוא ספריות

במדריך הזה נעשה שימוש בייבוא המערכת ובייבוא ספריית הלקוח הבאים.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

import html
import os

# Imports the Google Cloud client libraries
from google.api_core.exceptions import AlreadyExists
from google.cloud import texttospeech
from google.cloud import translate_v3beta1 as translate
from google.cloud import vision

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech');
const translate = require('@google-cloud/translate').v3beta1;
const vision = require('@google-cloud/vision');

// Import other required libraries
const fs = require('fs');
//const escape = require('escape-html');
const util = require('util');

הגדרת מזהה הפרויקט

צריך לשייך Google Cloud פרויקט לכל בקשה שנשלחת אל Google Cloud API. מגדירים את משתנה הסביבה GCLOUD_PROJECT מהטרמינל כדי לציין את Google Cloud הפרויקט.

בפקודה הבאה, מחליפים את project-id במזהה הפרויקט. Google Cloud מריצים את הפקודה הבאה מהטרמינל.

Linux או macOS

export GCLOUD_PROJECT=project-id

Windows

set GCLOUD_PROJECT=project-id

שימוש ב-Vision כדי לזהות טקסט מתמונה

שימוש ב-Vision API כדי לזהות ולחלץ טקסט מתמונה. ‫Vision API משתמש בזיהוי תווים אופטי (OCR) כדי לתמוך בשני פיצ'רים של זיהוי טקסט: זיהוי של טקסט צפוף, או DOCUMENT_TEXT_DETECTION, וזיהוי של טקסט דליל, או TEXT_DETECTION.

בדוגמה הבאה של קוד אפשר לראות איך משתמשים בתכונה DOCUMENT_TEXT_DETECTION של Vision API כדי לזהות טקסט בתמונה עם טקסט צפוף.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def pic_to_text(infile: str) -> str:
    """Detects text in an image file

    Args:
    infile: path to image file

    Returns:
    String of text detected in image
    """

    # Instantiates a client
    client = vision.ImageAnnotatorClient()

    # Opens the input image file
    with open(infile, "rb") as image_file:
        content = image_file.read()

    image = vision.Image(content=content)

    # For dense text, use document_text_detection
    # For less dense text, use text_detection
    response = client.document_text_detection(image=image)
    text = response.full_text_annotation.text
    print(f"Detected text: {text}")

    return text

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * Detects text in an image file
 *
 * ARGS
 * inputFile: path to image file
 * RETURNS
 * string of text detected in the input image
 **/
async function picToText(inputFile) {
  // Creates a client
  const client = new vision.ImageAnnotatorClient();

  // Performs text detection on the local file
  const [result] = await client.textDetection(inputFile);
  return result.fullTextAnnotation.text;
}

שימוש בתרגום עם מילוני מונחים

אחרי שחולצים טקסט מתמונה, אפשר להשתמש במילוני מונחים לתרגום כדי להתאים אישית את התרגום של הטקסט שחולץ. מילוני מונחים מספקים תרגומים מוגדרים מראש שמבטלים את התרגומים של מונחים ייעודיים ב-Cloud Translation API.

תרחישי שימוש במילון המונחים:

  • שמות מוצרים: לדוגמה, 'Google Home' צריך להיות מתורגם ל-'Google Home'.

  • מילים דו-משמעיות: לדוגמה, המילה 'bat' יכולה להתייחס למחבט ספורט או לעטלף. אם אתם יודעים שאתם מתרגמים מילים שקשורות לספורט, יכול להיות שתרצו להשתמש במילון מונחים כדי להזין ל-Cloud Translation API את התרגום של המילה bat בהקשר של ספורט, ולא בהקשר של בעל חיים.

  • מילים שאולות: לדוגמה, המילה bouillabaisse בצרפתית מתורגמת ל-bouillabaisse באנגלית. השפה האנגלית שאלה את המילה bouillabaisse מהשפה הצרפתית. דובר אנגלית שלא מכיר את ההקשר התרבותי הצרפתי אולי לא יידע שבוילבאס היא מנת תבשיל דגים צרפתית. אפשר להשתמש במילוני מונחים כדי לשנות תרגום. לדוגמה, אפשר להגדיר ש-bouillabaisse בצרפתית יתורגם ל-fish stew באנגלית.

יצירת קובץ מילון מונחים

‫Cloud Translation API מקבל קובצי מילון מונחים בפורמטים TSV,‏ CSV או TMX. במדריך הזה נשתמש בקובץ CSV שהועלה ל-Cloud Storage כדי להגדיר קבוצות של מונחים שווי ערך.

כדי ליצור קובץ CSV של מילון מונחים:

  1. מציינים את השפה של עמודה באמצעות קודי שפה בתקן ISO-639 או בתקן BCP-47 בשורה הראשונה של קובץ ה-CSV.

    fr,en,

  2. מפרטים זוגות של מונחים מקבילים בכל שורה בקובץ ה-CSV. מפרידים בין התנאים באמצעות פסיקים. בדוגמה הבאה מוגדר תרגום לאנגלית של כמה מילים צרפתיות שקשורות לקולינריה.

    fr,en,
    chèvre,goat cheese,
    crème brulée,crème brulée,
    bouillabaisse,fish stew,
    steak frites,steak with french fries,
    

  3. הגדרת וריאנטים של מילה. ‫Cloud Translation API הוא תלוי אותיות רישיות ורגיש לתווים מיוחדים כמו מילים עם סימני הטעמה. חשוב לוודא שהמילון מטפל בווריאציות של מילה על ידי הגדרה מפורשת של איותים שונים של המילה.

    fr,en,
    chevre,goat cheese,
    Chevre,Goat cheese,
    chèvre,goat cheese,
    Chèvre,Goat cheese,
    crème brulée,crème brulée,
    Crème brulée,Crème brulée,
    Crème Brulée,Crème Brulée,
    bouillabaisse,fish stew,
    Bouillabaisse,Fish stew,
    steak frites,steak with french fries,
    Steak frites,Steak with french fries,
    Steak Frites,Steak with French Fries,
    

  4. מעלים את מילון המונחים לקטגוריה של Cloud Storage. לצורך המדריך הזה, לא צריך להעלות קובץ מונחים לקטגוריה של Cloud Storage ולא צריך ליצור קטגוריה של Cloud Storage. במקום זאת, כדי להימנע מעלויות של Cloud Storage, אפשר להשתמש בקובץ המילון המונחים שזמין לכולם ונוצר לצורך המדריך הזה. שולחים את ה-URI של קובץ מילון מונחים ב-Cloud Storage אל Cloud Translation API כדי ליצור משאב מילון מונחים. ה-URI של קובץ המילון המונחים שזמין לכולם במדריך הזה הוא gs://cloud-samples-data/translation/bistro_glossary.csv. כדי להוריד את המילון, לוחצים על קישור ה-URI שלמעלה, אבל לא פותחים אותו בכרטיסייה חדשה.

יצירת משאב של מילון מונחים

כדי להשתמש במילון מונחים, צריך ליצור משאב מילון מונחים באמצעות Cloud Translation API. כדי ליצור משאב מילון מונחים, שולחים את ה-URI של קובץ מילון מונחים ב-Cloud Storage אל Cloud Translation API.

חשוב לוודא שאתם משתמשים במפתח של חשבון שירות עם הרשאות "עריכה של Cloud Translation API", ולוודא שהגדרתם את מזהה הפרויקט מהטרמינל.

הפונקציה הבאה יוצרת משאב של מילון מונחים. בעזרת המילון הזה, תוכלו להתאים אישית את בקשת התרגום בשלב הבא של המדריך הזה.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def create_glossary(
    languages: list,
    project_id: str,
    glossary_name: str,
    glossary_uri: str,
) -> str:
    """Creates a GCP glossary resource
    Assumes you've already manually uploaded a glossary to Cloud Storage

    Args:
    languages: list of languages in the glossary
    project_id: GCP project id
    glossary_name: name you want to give this glossary resource
    glossary_uri: the uri of the glossary you uploaded to Cloud Storage

    Returns:
    name of the created or existing glossary
    """

    # Instantiates a client
    client = translate.TranslationServiceClient()

    # Designates the data center location that you want to use
    location = "us-central1"

    # Set glossary resource name
    name = client.glossary_path(project_id, location, glossary_name)

    # Set language codes
    language_codes_set = translate.Glossary.LanguageCodesSet(language_codes=languages)

    gcs_source = translate.GcsSource(input_uri=glossary_uri)

    input_config = translate.GlossaryInputConfig(gcs_source=gcs_source)

    # Set glossary resource information
    glossary = translate.Glossary(
        name=name, language_codes_set=language_codes_set, input_config=input_config
    )

    parent = f"projects/{project_id}/locations/{location}"

    # Create glossary resource
    # Handle exception for case in which a glossary
    #  with glossary_name already exists
    try:
        operation = client.create_glossary(parent=parent, glossary=glossary)
        operation.result(timeout=90)
        print("Created glossary " + glossary_name + ".")
    except AlreadyExists:
        print(
            "The glossary "
            + glossary_name
            + " already exists. No new glossary was created."
        )

    return glossary_name

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/** Creates a GCP glossary resource
 * Assumes you've already manually uploaded a glossary to Cloud Storage
 *
 * ARGS
 * languages: list of languages in the glossary
 * projectId: GCP project id
 * glossaryName: name you want to give this glossary resource
 * glossaryUri: the uri of the glossary you uploaded to Cloud Storage
 * RETURNS
 * nothing
 **/
async function createGlossary(
  languages,
  projectId,
  glossaryName,
  glossaryUri
) {
  // Instantiates a client
  const translationClient = await new translate.TranslationServiceClient();

  // Construct glossary
  const glossary = {
    languageCodesSet: {
      languageCodes: languages,
    },
    inputConfig: {
      gcsSource: {
        inputUri: glossaryUri,
      },
    },
    name: translationClient.glossaryPath(
      projectId,
      'us-central1',
      glossaryName
    ),
  };

  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, 'us-central1'),
    glossary: glossary,
  };

  // Create glossary using a long-running operation.
  try {
    const [operation] = await translationClient.createGlossary(request);
    // Wait for operation to complete.
    await operation.promise();
    console.log('Created glossary ' + glossaryName + '.');
  } catch (AlreadyExists) {
    console.log(
      'The glossary ' +
        glossaryName +
        ' already exists. No new glossary was created.'
    );
  }
}

תרגום באמצעות מילונים

אחרי שיוצרים משאב מילון מונחים, אפשר להשתמש בו כדי להתאים אישית את התרגומים של הטקסט ששולחים אל Cloud Translation API.

הפונקציה הבאה משתמשת במשאב המילון המונחים שיצרתם קודם כדי להתאים אישית את תרגום הטקסט.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def translate_text(
    text: str,
    source_language_code: str,
    target_language_code: str,
    project_id: str,
    glossary_name: str,
) -> str:
    """Translates text to a given language using a glossary

    Args:
    text: String of text to translate
    source_language_code: language of input text
    target_language_code: language of output text
    project_id: GCP project id
    glossary_name: name you gave your project's glossary
        resource when you created it

    Return:
    String of translated text
    """

    # Instantiates a client
    client = translate.TranslationServiceClient()

    # Designates the data center location that you want to use
    location = "us-central1"

    glossary = client.glossary_path(project_id, location, glossary_name)

    glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary)

    parent = f"projects/{project_id}/locations/{location}"

    result = client.translate_text(
        request={
            "parent": parent,
            "contents": [text],
            "mime_type": "text/plain",  # mime types: text/plain, text/html
            "source_language_code": source_language_code,
            "target_language_code": target_language_code,
            "glossary_config": glossary_config,
        }
    )

    # Extract translated text from API response
    return result.glossary_translations[0].translated_text

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * Translates text to a given language using a glossary
 *
 * ARGS
 * text: String of text to translate
 * sourceLanguageCode: language of input text
 * targetLanguageCode: language of output text
 * projectId: GCP project id
 * glossaryName: name you gave your project's glossary
 *     resource when you created it
 * RETURNS
 * String of translated text
 **/
async function translateText(
  text,
  sourceLanguageCode,
  targetLanguageCode,
  projectId,
  glossaryName
) {
  // Instantiates a client
  const translationClient = new translate.TranslationServiceClient();
  const glossary = translationClient.glossaryPath(
    projectId,
    'us-central1',
    glossaryName
  );
  const glossaryConfig = {
    glossary: glossary,
  };
  // Construct request
  const request = {
    parent: translationClient.locationPath(projectId, 'us-central1'),
    contents: [text],
    mimeType: 'text/plain', // mime types: text/plain, text/html
    sourceLanguageCode: sourceLanguageCode,
    targetLanguageCode: targetLanguageCode,
    glossaryConfig: glossaryConfig,
  };

  // Run request
  const [response] = await translationClient.translateText(request);
  // Extract the string of translated text
  return response.glossaryTranslations[0].translatedText;
}

שימוש בהמרת טקסט לדיבור (TTS) עם שפת סימון לסינתזת דיבור (SSML)

עכשיו, אחרי שהתאמתם אישית תרגום של טקסט שזוהה בתמונה, אתם מוכנים להשתמש ב-Text-to-Speech API. באמצעות Text-to-Speech API אפשר ליצור אודיו סינתטי של הטקסט המתורגם.

‫Text-to-Speech API יוצר אודיו סינתטי ממחרוזת של טקסט פשוט או ממחרוזת של טקסט שמסומן באמצעות Speech Synthesis Markup Language (SSML). ‫SSML היא שפת תגי עיצוב שתומכת בהוספת הערות לטקסט באמצעות תגי SSML. אפשר להשתמש בתגי SSML כדי להשפיע על האופן שבו Text-to-Speech API מעצב את יצירת הדיבור הסינתטי.

הפונקציה הבאה ממירה מחרוזת של SSML לקובץ MP3 של דיבור סינתטי.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def text_to_speech(text: str, outfile: str) -> str:
    """Converts plaintext to SSML and
    generates synthetic audio from SSML

    Args:

    text: text to synthesize
    outfile: filename to use to store synthetic audio

    Returns:
    String of synthesized audio
    """

    # Replace special characters with HTML Ampersand Character Codes
    # These Codes prevent the API from confusing text with
    # SSML commands
    # For example, '<' --> '&lt;' and '&' --> '&amp;'
    escaped_lines = html.escape(text)

    # Convert plaintext to SSML in order to wait two seconds
    #   between each line in synthetic speech
    ssml = "<speak>{}</speak>".format(
        escaped_lines.replace("\n", '\n<break time="2s"/>')
    )

    # Instantiates a client
    client = texttospeech.TextToSpeechClient()

    # Sets the text input to be synthesized
    synthesis_input = texttospeech.SynthesisInput(ssml=ssml)

    # Builds the voice request, selects the language code ("en-US") and
    # the SSML voice gender ("MALE")
    voice = texttospeech.VoiceSelectionParams(
        language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE
    )

    # Selects the type of audio file to return
    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.MP3
    )

    # Performs the text-to-speech request on the text input with the selected
    # voice parameters and audio file type

    request = texttospeech.SynthesizeSpeechRequest(
        input=synthesis_input, voice=voice, audio_config=audio_config
    )

    response = client.synthesize_speech(request=request)

    # Writes the synthetic audio to the output file.
    with open(outfile, "wb") as out:
        out.write(response.audio_content)
        print("Audio content written to file " + outfile)

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * Generates synthetic audio from plaintext tagged with SSML.
 *
 * Given the name of a text file and an output file name, this function
 * tags the text in the text file with SSML. This function then
 * calls the Text-to-Speech API. The API returns a synthetic audio
 * version of the text, formatted according to the SSML commands. This
 * function saves the synthetic audio to the designated output file.
 *
 * ARGS
 * text: String of plaintext
 * outFile: String name of file under which to save audio output
 * RETURNS
 * nothing
 *
 */
async function syntheticAudio(text, outFile) {
  // Replace special characters with HTML Ampersand Character Codes
  // These codes prevent the API from confusing text with SSML tags
  // For example, '<' --> '&lt;' and '&' --> '&amp;'
  let escapedLines = text.replace(/&/g, '&amp;');
  escapedLines = escapedLines.replace(/"/g, '&quot;');
  escapedLines = escapedLines.replace(/</g, '&lt;');
  escapedLines = escapedLines.replace(/>/g, '&gt;');

  // Convert plaintext to SSML
  // Tag SSML so that there is a 2 second pause between each address
  const expandedNewline = escapedLines.replace(/\n/g, '\n<break time="2s"/>');
  const ssmlText = '<speak>' + expandedNewline + '</speak>';

  // Creates a client
  const client = new textToSpeech.TextToSpeechClient();

  // Constructs the request
  const request = {
    // Select the text to synthesize
    input: {ssml: ssmlText},
    // Select the language and SSML Voice Gender (optional)
    voice: {languageCode: 'en-US', ssmlGender: 'MALE'},
    // Select the type of audio encoding
    audioConfig: {audioEncoding: 'MP3'},
  };

  // Performs the Text-to-Speech request
  const [response] = await client.synthesizeSpeech(request);
  // Write the binary audio content to a local file
  const writeFile = util.promisify(fs.writeFile);
  await writeFile(outFile, response.audioContent, 'binary');
  console.log('Audio content written to file ' + outFile);
}

איך הכל משתלב יחד

בשלבים הקודמים הגדרתם פונקציות ב-hybrid_glossaries.py שמשתמשות ב-Vision, בתרגום ובהמרת טקסט לדיבור. עכשיו אפשר להשתמש בפונקציות האלה כדי ליצור דיבור סינתטי של טקסט מתורגם מהתמונה הבאה.

הקוד הבא קורא לפונקציות שמוגדרות ב-hybrid_glossaries.py כדי:

  • יצירת משאב מילון מונחים של Cloud Translation API

  • תשתמש ב-Vision API כדי לזהות טקסט בתמונה שלמעלה

  • לבצע תרגום של הטקסט שזוהה באמצעות מילון מונחים של Cloud Translation API

  • יצירת דיבור סינתטי של הטקסט המתורגם באמצעות המרת טקסט לדיבור (TTS)

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Pythonההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Python API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

def main() -> None:
    """This method is called when the tutorial is run in the Google Cloud
    Translation API. It creates a glossary, translates text to
    French, and speaks the translated text.

    Args:
    None

    Returns:
    None
    """
    # Photo from which to extract text
    infile = "resources/example.png"
    # Name of file that will hold synthetic speech
    outfile = "resources/example.mp3"

    # Defines the languages in the glossary
    # This list must match the languages in the glossary
    #   Here, the glossary includes French and English
    glossary_langs = ["fr", "en"]
    # Name that will be assigned to your project's glossary resource
    glossary_name = "bistro-glossary"
    # uri of .csv file uploaded to Cloud Storage
    glossary_uri = "gs://cloud-samples-data/translation/bistro_glossary.csv"

    created_glossary_name = create_glossary(
        glossary_langs, PROJECT_ID, glossary_name, glossary_uri
    )

    # photo -> detected text
    text_to_translate = pic_to_text(infile)
    # detected text -> translated text
    text_to_speak = translate_text(
        text_to_translate, "fr", "en", PROJECT_ID, created_glossary_name
    )
    # translated text -> synthetic audio
    text_to_speech(text_to_speak, outfile)

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsההוראות להגדרה במדריך למתחילים בנושא Cloud Translation באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Cloud Translation Node.js API.

כדי לבצע אימות ב-Cloud Translation, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

await createGlossary(glossaryLangs, projectId, glossaryName, glossaryUri);
const text = await picToText(inFile);
const translatedText = await translateText(
  text,
  'fr',
  'en',
  projectId,
  glossaryName
);
syntheticAudio(translatedText, outFile);

הרצת הקוד

כדי להריץ את הקוד, מזינים את הפקודה הבאה בטרמינל בספרייה שבה הקוד נמצא:

Python

python hybrid_tutorial.py
  

Node.js

  node hybridGlossaries.js
  

הפלט הבא מופיע:

Created glossary bistro-glossary.
Audio content written to file resources/example.mp3

אחרי שמריצים את הקוד, עוברים לספרייה resources מתוך הספרייה hybrid_glossaries. בודקים אם יש קובץ example.mp3 בספריית המשאבים.

מאזינים לקטע האודיו הבא כדי לוודא שקובץ example.mp3 נשמע זהה.


פתרון בעיות שמקפיצות הודעות שגיאה

סידור וארגון

אם לא צריך את הפרויקט, אפשר להשתמש בGoogle Cloud מסוף כדי למחוק אותו. מחיקת הפרויקט תמנע חיובים נוספים בחשבון לחיוב ב-Cloud על המשאבים שבהם השתמשתם במדריך הזה.

מחיקת הפרויקט

  1. במסוףGoogle Cloud , עוברים לדף Projects.
  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על מחיקה.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים

כל הכבוד! הרגע השתמשת ב-Vision OCR כדי לזהות טקסט בתמונה. לאחר מכן, יצרתם מילון מונחים לתרגום וביצעתם תרגום באמצעות המילון הזה. לאחר מכן, השתמשת בהמרת טקסט לדיבור כדי ליצור אודיו סינתטי של הטקסט המתורגם.

כדי להרחיב את הידע שלכם בנושא Vision,‏ Translation ו-Text-to-Speech: