用于 Python 的 Amaz SDK on Polly 示例 (Boto3) - AWS SDK 代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

用于 Python 的 Amaz SDK on Polly 示例 (Boto3)

以下代码示例向您展示了如何在 Amazon Polly 中使用来执行操作和实现常见场景。 AWS SDK for Python (Boto3)

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

操作

以下代码示例演示如何使用 DescribeVoices

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def describe_voices(self): """ Gets metadata about available voices. :return: The list of voice metadata. """ try: response = self.polly_client.describe_voices() self.voice_metadata = response["Voices"] logger.info("Got metadata about %s voices.", len(self.voice_metadata)) except ClientError: logger.exception("Couldn't get voice metadata.") raise else: return self.voice_metadata
  • 有关API详细信息,请参阅DescribeVoices中的 AWS SDKPython (Boto3) API 参考。

以下代码示例演示如何使用 GetLexicon

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def get_lexicon(self, name): """ Gets metadata and contents of an existing lexicon. :param name: The name of the lexicon to retrieve. :return: The retrieved lexicon. """ try: response = self.polly_client.get_lexicon(Name=name) logger.info("Got lexicon %s.", name) except ClientError: logger.exception("Couldn't get lexicon %s.", name) raise else: return response
  • 有关API详细信息,请参阅GetLexicon中的 AWS SDKPython (Boto3) API 参考。

以下代码示例演示如何使用 GetSpeechSynthesisTask

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def get_speech_synthesis_task(self, task_id): """ Gets metadata about an asynchronous speech synthesis task, such as its status. :param task_id: The ID of the task to retrieve. :return: Metadata about the task. """ try: response = self.polly_client.get_speech_synthesis_task(TaskId=task_id) task = response["SynthesisTask"] logger.info("Got synthesis task. Status is %s.", task["TaskStatus"]) except ClientError: logger.exception("Couldn't get synthesis task %s.", task_id) raise else: return task

以下代码示例演示如何使用 ListLexicons

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def list_lexicons(self): """ Lists lexicons in the current account. :return: The list of lexicons. """ try: response = self.polly_client.list_lexicons() lexicons = response["Lexicons"] logger.info("Got %s lexicons.", len(lexicons)) except ClientError: logger.exception( "Couldn't get %s.", ) raise else: return lexicons
  • 有关API详细信息,请参阅ListLexicons中的 AWS SDKPython (Boto3) API 参考。

以下代码示例演示如何使用 PutLexicon

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def create_lexicon(self, name, content): """ Creates a lexicon with the specified content. A lexicon contains custom pronunciations. :param name: The name of the lexicon. :param content: The content of the lexicon. """ try: self.polly_client.put_lexicon(Name=name, Content=content) logger.info("Created lexicon %s.", name) except ClientError: logger.exception("Couldn't create lexicon %s.") raise
  • 有关API详细信息,请参阅PutLexicon中的 AWS SDKPython (Boto3) API 参考。

以下代码示例演示如何使用 StartSpeechSynthesisTask

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def do_synthesis_task( self, text, engine, voice, audio_format, s3_bucket, lang_code=None, include_visemes=False, wait_callback=None, ): """ Start an asynchronous task to synthesize speech or speech marks, wait for the task to complete, retrieve the output from Amazon S3, and return the data. An asynchronous task is required when the text is too long for near-real time synthesis. :param text: The text to synthesize. :param engine: The kind of engine used. Can be standard or neural. :param voice: The ID of the voice to use. :param audio_format: The audio format to return for synthesized speech. When speech marks are synthesized, the output format is JSON. :param s3_bucket: The name of an existing Amazon S3 bucket that you have write access to. Synthesis output is written to this bucket. :param lang_code: The language code of the voice to use. This has an effect only when a bilingual voice is selected. :param include_visemes: When True, a second request is made to Amazon Polly to synthesize a list of visemes, using the specified text and voice. A viseme represents the visual position of the face and mouth when saying part of a word. :param wait_callback: A callback function that is called periodically during task processing, to give the caller an opportunity to take action, such as to display status. :return: The audio stream that contains the synthesized speech and a list of visemes that are associated with the speech audio. """ try: kwargs = { "Engine": engine, "OutputFormat": audio_format, "OutputS3BucketName": s3_bucket, "Text": text, "VoiceId": voice, } if lang_code is not None: kwargs["LanguageCode"] = lang_code response = self.polly_client.start_speech_synthesis_task(**kwargs) speech_task = response["SynthesisTask"] logger.info("Started speech synthesis task %s.", speech_task["TaskId"]) viseme_task = None if include_visemes: kwargs["OutputFormat"] = "json" kwargs["SpeechMarkTypes"] = ["viseme"] response = self.polly_client.start_speech_synthesis_task(**kwargs) viseme_task = response["SynthesisTask"] logger.info("Started viseme synthesis task %s.", viseme_task["TaskId"]) except ClientError: logger.exception("Couldn't start synthesis task.") raise else: bucket = self.s3_resource.Bucket(s3_bucket) audio_stream = self._wait_for_task( 10, speech_task["TaskId"], "speech", wait_callback, bucket ) visemes = None if include_visemes: viseme_data = self._wait_for_task( 10, viseme_task["TaskId"], "viseme", wait_callback, bucket ) visemes = [ json.loads(v) for v in viseme_data.read().decode().split() if v ] return audio_stream, visemes

以下代码示例演示如何使用 SynthesizeSpeech

SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def synthesize( self, text, engine, voice, audio_format, lang_code=None, include_visemes=False ): """ Synthesizes speech or speech marks from text, using the specified voice. :param text: The text to synthesize. :param engine: The kind of engine used. Can be standard or neural. :param voice: The ID of the voice to use. :param audio_format: The audio format to return for synthesized speech. When speech marks are synthesized, the output format is JSON. :param lang_code: The language code of the voice to use. This has an effect only when a bilingual voice is selected. :param include_visemes: When True, a second request is made to Amazon Polly to synthesize a list of visemes, using the specified text and voice. A viseme represents the visual position of the face and mouth when saying part of a word. :return: The audio stream that contains the synthesized speech and a list of visemes that are associated with the speech audio. """ try: kwargs = { "Engine": engine, "OutputFormat": audio_format, "Text": text, "VoiceId": voice, } if lang_code is not None: kwargs["LanguageCode"] = lang_code response = self.polly_client.synthesize_speech(**kwargs) audio_stream = response["AudioStream"] logger.info("Got audio stream spoken by %s.", voice) visemes = None if include_visemes: kwargs["OutputFormat"] = "json" kwargs["SpeechMarkTypes"] = ["viseme"] response = self.polly_client.synthesize_speech(**kwargs) visemes = [ json.loads(v) for v in response["AudioStream"].read().decode().split() if v ] logger.info("Got %s visemes.", len(visemes)) except ClientError: logger.exception("Couldn't get audio stream.") raise else: return audio_stream, visemes
  • 有关API详细信息,请参阅SynthesizeSpeech中的 AWS SDKPython (Boto3) API 参考。

场景

以下代码示例演示了如何通过 Amazon Polly 创建口型同步应用程序。

SDK适用于 Python (Boto3)

演示如何使用 Amazon Polly 和 Tkinter 创建口型同步应用程序,该应用程序显示正在说话的动画表情以及由 Amazon Polly 合成的语音。口型同步是通过向 Amazon Polly 申请与合成语音匹配的视素 (viseme) 列表来实现的。

  • 从 Amazon Polly 获取语音元数据并将其显示在 Tkinter 应用程序中。

  • 从 Amazon Polly 获取合成语音音频和匹配的视素语音标记。

  • 在播放音频时,同步播放动画表情中的嘴部动作。

  • 提交异步合成任务以获取长文本,并从 Amazon Simple Storage Service (Amazon S3) 存储桶检索输出。

有关如何设置和运行的完整源代码和说明,请参阅上的完整示例GitHub

本示例中使用的服务
  • Amazon Polly