首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义短语/单词被Google语音到文本忽略。

自定义短语/单词被Google语音到文本忽略。
EN

Stack Overflow用户
提问于 2021-11-20 18:59:06
回答 1查看 773关注 0票数 1

我正在使用python3通过提供的python包( Google )转录一个带有的音频文件。

有一个选项可以定义自定义短语,如docs:https://cloud.google.com/speech-to-text/docs/speech-adaptation中所述,这些短语应该用于转录。

为了测试目的,我使用了一个包含文本的小型音频文件:

。。在这节课中,我们将讨论布伦斯惠勒变换和调频指数。

如果我想用正确的表示法来识别一个特定的名字,我会给出下面的短语来查看效果。在本例中,我希望将钻孔更改为barrows。

代码语言:javascript
复制
config = speech.RecognitionConfig(dict(
    encoding=speech.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
    sample_rate_hertz=24000,
    language_code="en-US",
    enable_word_time_offsets=True,
    speech_contexts=[
        speech.SpeechContext(dict(
            phrases=["barrows", "barrows wheeler", "barrows wheeler transform"]
        ))
    ]
))

不幸的是,这似乎没有任何影响,因为输出仍然是相同的,没有上下文短语。

我是不是用错了短语,或者它是否很有信心,它听到的单词确实是 burrows ,这样它就会忽略我的短语?

PS:我也尝试使用speech_v1p1beta1.AdaptationClientspeech_v1p1beta1.SpeechAdaptation,而不是将短语放入配置中,但这只会给出一个内部服务器错误,而不需要提供更多的错误信息。https://cloud.google.com/speech-to-text/docs/adaptation

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-26 18:51:58

我已经创建了一个音频文件,以重新创建您的场景,我能够提高识别使用模型自适应。为了实现这个特性,我建议查看这个示例和这个帖子,以更好地理解适应模型。

现在,为了提高对您短语的认知度,我执行了以下操作:

  1. 我用下面提到的短语使用下面的页面创建了一个新的音频文件。

在这节课中,我们将讨论布伦斯惠勒变换和调频指数。

  1. 我的测试是基于这个代码样本。这段代码创建了一个PhraseSetCustomClass,其中包含了您想要改进的单词,在本例中是"barrows“。还可以使用语音文本图形用户界面创建/更新/删除短语集和自定义类。下面是我用于改进的代码。
代码语言:javascript
复制
from os import pathconf_names
from google.cloud import speech_v1p1beta1 as speech
import argparse


def transcribe_with_model_adaptation(
    project_id="[PROJECT-ID]", location="global", speech_file=None, custom_class_id="[CUSTOM-CLASS-ID]", phrase_set_id="[PHRASE-SET-ID]"
):
    """
    Create`PhraseSet` and `CustomClasses` to create custom lists of similar
    items that are likely to occur in your input data.
    """
    import io

    # Create the adaptation client
    adaptation_client = speech.AdaptationClient()

    # The parent resource where the custom class and phrase set will be created.
    parent = f"projects/{project_id}/locations/{location}"

    # Create the custom class resource
    adaptation_client.create_custom_class(
        {
            "parent": parent,
            "custom_class_id": custom_class_id,
            "custom_class": {
                "items": [
                    {"value": "barrows"}
                ]
            },
        }
    )
    custom_class_name = (
        f"projects/{project_id}/locations/{location}/customClasses/{custom_class_id}"
    )
    # Create the phrase set resource
    phrase_set_response = adaptation_client.create_phrase_set(
        {
            "parent": parent,
            "phrase_set_id": phrase_set_id,
            "phrase_set": {
                "boost": 0,
                "phrases": [
                    {"value": f"${{{custom_class_name}}}", "boost": 10},
                    {"value": f"talk about the ${{{custom_class_name}}} wheeler transform", "boost": 15}
                ],
            },
        }
    )
    phrase_set_name = phrase_set_response.name
    # print(u"Phrase set name: {}".format(phrase_set_name))
 
    # The next section shows how to use the newly created custom
    # class and phrase set to send a transcription request with speech adaptation

    # Speech adaptation configuration
    speech_adaptation = speech.SpeechAdaptation(
        phrase_set_references=[phrase_set_name])

    # speech configuration object
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
        sample_rate_hertz=24000,
        language_code="en-US",
        adaptation=speech_adaptation,
        enable_word_time_offsets=True,
        model="phone_call",
        use_enhanced=True
    )

    # The name of the audio file to transcribe
    # storage_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
    with io.open(speech_file, "rb") as audio_file:
        content = audio_file.read()

    audio = speech.RecognitionAudio(content=content)
    # audio = speech.RecognitionAudio(uri="gs://biasing-resources-test-audio/call_me_fionity_and_ionity.wav")

    # Create the speech client
    speech_client = speech.SpeechClient()

    response = speech_client.recognize(config=config, audio=audio)

    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print(u"Transcript: {}".format(result.alternatives[0].transcript))

    # [END speech_transcribe_with_model_adaptation]


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument("path", help="Path for audio file to be recognized")
    args = parser.parse_args()

    transcribe_with_model_adaptation(speech_file=args.path)
  1. 一旦它运行,您将得到以下改进的识别;但是,考虑到代码在运行时尝试创建一个新的自定义类和一个新的短语集,如果试图重新创建自定义类和短语集,它可能会抛出带有element already exists消息的错误。
  • 使用无适应性的识别
代码语言:javascript
复制
(python_speech2text) user@penguin:~/replication/python_speech2text$ python speech_model_adaptation_beta.py audio.flac
Transcript: in this lecture will talk about the Burrows wheeler transform and the FM index

  • 在适应中使用识别
代码语言:javascript
复制
(python_speech2text) user@penguin:~/replication/python_speech2text$ python speech_model_adaptation_beta.py audio.flac
Transcript: in this lecture will talk about the barrows wheeler transform and the FM index

最后,我想添加一些关于改进和我执行的代码的说明:

  • 我使用了一个flac音频文件,因为它是推荐的最佳结果。
  • 我使用了model="phone_call"use_enhanced=True,因为这是Cloud使用我自己的音频文件识别的模型。此外,增强的模型可以提供更好的结果,您可以看到文档的更多细节。注意,此配置可能与您的音频文件不同。
  • 考虑让数据录井谷歌从您的音频转录请求收集数据。然后,Google使用这些数据来改进用于识别语音音频的机器学习模型。
  • 一旦我创建了自定义类和短语集,您就可以使用语音到文本用户界面来更新和快速执行测试。只包含
  • 我在短语设置中使用了参数boost,当您使用boost时,可以为PhraseSet资源中的短语项分配一个加权值。在为音频数据中的单词选择可能的转录时,语音到文本是指这个加权值。价值越高,从可能的备选方案中选择单词或短语的可能性就越高。

我希望这些信息能帮助你提高认识。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70048973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档