我创建了一个音频文件来重新创建您的场景,并且我能够使用model adaptation 提高识别率。要使用此功能实现此目的,我建议您查看此example 和此post 以更好地了解适应模型。
现在,为了提高您的短语的识别率,我执行了以下操作:
- 我使用以下 page 和提到的短语创建了一个新的音频文件。
在本次讲座中,我们将讨论 Burrows Wheeler 变换和 FM 索引
- 我的测试基于此code sample。这段代码创建了一个
PhraseSet 和CustomClass,其中包含您想要改进的单词,在本例中是单词“barrows”。您还可以使用Speech-To-Text GUI 创建/更新/删除短语集和自定义类。以下是我用于改进的代码。
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)
- 一旦运行,您将收到如下改进的识别;但是,考虑到代码在运行时会尝试创建一个新的自定义类和一个新的短语集,如果尝试重新创建自定义类和短语集,它可能会抛出带有
element already exists 消息的错误。李>
(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
(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 音频文件,因为它是recommended 以获得最佳效果。
-
我使用了 model="phone_call" 和 use_enhanced=True,因为这是 Cloud Speech-To-Text 使用我自己的音频文件识别的模型。此外,增强模型可以提供更好的结果,您可以查看documentation 了解更多详细信息。请注意,此配置可能与您的音频文件不同。
-
考虑向 Google 启用 data logging 以从您的音频转录请求中收集数据。然后,Google 会使用这些数据来改进其用于识别语音音频的机器学习模型。
-
创建自定义类和短语集后,您可以使用 Speech-to-Text UI 快速更新和执行测试。只包含
-
我在短语集中使用了参数 boost,当您使用 boost 时,您为 PhraseSet 资源中的短语项目分配了一个加权值。在为音频数据中的单词选择可能的转录时,Speech-to-Text 指的是这个加权值。该值越高,Speech-to-Text 从可能的备选方案中选择该词或短语的可能性就越高。
希望这些信息能帮助您提高知名度。