|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# DO NOT EDIT! This is a generated sample ("Request", "speech_adaptation_beta") |
| 18 | + |
| 19 | +# To install the latest published package dependency, execute the following: |
| 20 | +# pip install google-cloud-speech |
| 21 | + |
| 22 | +# sample-metadata |
| 23 | +# title: |
| 24 | +# description: Performs synchronous speech recognition with speech adaptation. |
| 25 | +# usage: python3 samples/v1p1beta1/speech_adaptation_beta.py [--sample_rate_hertz 44100] [--language_code "en-US"] [--phrase "Brooklyn Bridge"] [--boost 20] [--uri_path "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"] |
| 26 | +import sys |
| 27 | + |
| 28 | +# [START speech_adaptation_beta] |
| 29 | + |
| 30 | +from google.cloud import speech_v1p1beta1 |
| 31 | +from google.cloud.speech_v1p1beta1 import enums |
| 32 | +import six |
| 33 | + |
| 34 | + |
| 35 | +def sample_recognize(sample_rate_hertz, language_code, phrase, boost, uri_path): |
| 36 | + """ |
| 37 | + Performs synchronous speech recognition with speech adaptation. |
| 38 | +
|
| 39 | + Args: |
| 40 | + sample_rate_hertz Sample rate in Hertz of the audio data sent in all |
| 41 | + `RecognitionAudio` messages. Valid values are: 8000-48000. |
| 42 | + language_code The language of the supplied audio. |
| 43 | + phrase Phrase "hints" help Speech-to-Text API recognize the specified phrases |
| 44 | + from your audio data. |
| 45 | + boost Positive value will increase the probability that a specific phrase will |
| 46 | + be recognized over other similar sounding phrases. |
| 47 | + uri_path Path to the audio file stored on GCS. |
| 48 | + """ |
| 49 | + # [START speech_adaptation_beta_core] |
| 50 | + |
| 51 | + client = speech_v1p1beta1.SpeechClient() |
| 52 | + |
| 53 | + # sample_rate_hertz = 44100 |
| 54 | + # language_code = 'en-US' |
| 55 | + # phrase = 'Brooklyn Bridge' |
| 56 | + # boost = 20 |
| 57 | + # uri_path = 'gs://cloud-samples-data/speech/brooklyn_bridge.mp3' |
| 58 | + |
| 59 | + if isinstance(language_code, six.binary_type): |
| 60 | + language_code = language_code.decode("utf-8") |
| 61 | + if isinstance(phrase, six.binary_type): |
| 62 | + phrase = phrase.decode("utf-8") |
| 63 | + |
| 64 | + if isinstance(uri_path, six.binary_type): |
| 65 | + uri_path = uri_path.decode("utf-8") |
| 66 | + encoding = enums.RecognitionConfig.AudioEncoding.MP3 |
| 67 | + phrases = [phrase] |
| 68 | + speech_contexts_element = {"phrases": phrases, "boost": boost} |
| 69 | + speech_contexts = [speech_contexts_element] |
| 70 | + config = { |
| 71 | + "encoding": encoding, |
| 72 | + "sample_rate_hertz": sample_rate_hertz, |
| 73 | + "language_code": language_code, |
| 74 | + "speech_contexts": speech_contexts, |
| 75 | + } |
| 76 | + audio = {"uri": uri_path} |
| 77 | + |
| 78 | + response = client.recognize(config, audio) |
| 79 | + for result in response.results: |
| 80 | + # First alternative is the most probable result |
| 81 | + alternative = result.alternatives[0] |
| 82 | + print("Transcript: {}".format(alternative.transcript)) |
| 83 | + |
| 84 | + # [END speech_adaptation_beta_core] |
| 85 | + |
| 86 | + |
| 87 | +# [END speech_adaptation_beta] |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + import argparse |
| 92 | + |
| 93 | + parser = argparse.ArgumentParser() |
| 94 | + parser.add_argument("--sample_rate_hertz", type=int, default=44100) |
| 95 | + parser.add_argument("--language_code", type=str, default="en-US") |
| 96 | + parser.add_argument("--phrase", type=str, default="Brooklyn Bridge") |
| 97 | + parser.add_argument("--boost", type=float, default=20) |
| 98 | + parser.add_argument( |
| 99 | + "--uri_path", |
| 100 | + type=str, |
| 101 | + default="gs://cloud-samples-data/speech/brooklyn_bridge.mp3", |
| 102 | + ) |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + sample_recognize( |
| 106 | + args.sample_rate_hertz, |
| 107 | + args.language_code, |
| 108 | + args.phrase, |
| 109 | + args.boost, |
| 110 | + args.uri_path, |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments