-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
speech_adaptation_beta.py
99 lines (76 loc) · 3.37 KB
/
speech_adaptation_beta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# DO NOT EDIT! This is a generated sample ("Request", "speech_adaptation_beta")
# To install the latest published package dependency, execute the following:
# pip install google-cloud-speech
# sample-metadata
# title: Speech Adaptation (Cloud Storage)
# description: Transcribe a short audio file with speech adaptation.
# usage: python3 samples/v1p1beta1/speech_adaptation_beta.py [--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"] [--phrase "Brooklyn Bridge"]
# [START speech_adaptation_beta]
from google.cloud import speech_v1p1beta1 as speech
def sample_recognize(storage_uri, phrase):
"""
Transcribe a short audio file with speech adaptation.
Args:
storage_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
phrase Phrase "hints" help recognize the specified phrases from your audio.
"""
client = speech.SpeechClient()
# storage_uri = 'gs://cloud-samples-data/speech/brooklyn_bridge.mp3'
# phrase = 'Brooklyn Bridge'
phrases = [phrase]
# Hint Boost. This value increases the probability that a specific
# phrase will be recognized over other similar sounding phrases.
# The higher the boost, the higher the chance of false positive
# recognition as well. Can accept wide range of positive values.
# Most use cases are best served with values between 0 and 20.
# Using a binary search approach may help you find the optimal value.
boost = 20.0
speech_contexts_element = {"phrases": phrases, "boost": boost}
speech_contexts = [speech_contexts_element]
# Sample rate in Hertz of the audio data sent
sample_rate_hertz = 44100
# The language of the supplied audio
language_code = "en-US"
# Encoding of audio data sent. This sample sets this explicitly.
# This field is optional for FLAC and WAV audio formats
encoding = speech.RecognitionConfig.AudioEncoding.MP3
config = {
"speech_contexts": speech_contexts,
"sample_rate_hertz": sample_rate_hertz,
"language_code": language_code,
"encoding": encoding,
}
audio = {"uri": storage_uri}
response = client.recognize(config=config, audio=audio)
for result in response.results:
# First alternative is the most probable result
alternative = result.alternatives[0]
print("Transcript: {}".format(alternative.transcript))
# [END speech_adaptation_beta]
return response
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--storage_uri",
type=str,
default="gs://cloud-samples-data/speech/brooklyn_bridge.mp3",
)
parser.add_argument("--phrase", type=str, default="Brooklyn Bridge")
args = parser.parse_args()
sample_recognize(args.storage_uri, args.phrase)
if __name__ == "__main__":
main()