Skip to content

Speech model selection #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added speech/cloud-client/resources/Google_Gnome.wav
Binary file not shown.
102 changes: 102 additions & 0 deletions speech/cloud-client/transcribe_model_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python

# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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
#
# http://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.

"""Google Cloud Speech API sample that demonstrates how to select the model
used for speech recognition.

Example usage:
python transcribe_model_selection.py \
resources/Google_Gnome.wav --model video
python transcribe_model_selection.py \
gs://cloud-samples-tests/speech/Google_Gnome.wav --model video
"""

import argparse


# [START speech_transcribe_model_selection]
def transcribe_model_selection(speech_file, model):
"""Transcribe the given audio file synchronously with
the selected model."""
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()

with open(speech_file, 'rb') as audio_file:
content = audio_file.read()

audio = speech.types.RecognitionAudio(content=content)

config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
model=model)

response = client.recognize(config, audio)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print('Transcript: {}'.format(alternative.transcript))
# [END speech_transcribe_model_selection]


# [START speech_transcribe_model_selection_gcs]
def transcribe_model_selection_gcs(gcs_uri, model):
"""Transcribe the given audio file asynchronously with
the selected model."""
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()

audio = speech.types.RecognitionAudio(uri=gcs_uri)

config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
model=model)

operation = client.long_running_recognize(config, audio)

print('Waiting for operation to complete...')
response = operation.result(timeout=90)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print('Transcript: {}'.format(alternative.transcript))
# [END speech_transcribe_model_selection_gcs]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'path', help='File or GCS path for audio file to be recognized')
parser.add_argument(
'--model', help='The speech recognition model to use',
choices=['command_and_search', 'phone_call', 'video', 'default'],
default='default')

args = parser.parse_args()

if args.path.startswith('gs://'):
transcribe_model_selection_gcs(args.path, args.model)
else:
transcribe_model_selection(args.path, args.model)
35 changes: 35 additions & 0 deletions speech/cloud-client/transcribe_model_selection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2016, Google, Inc.
# 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
#
# http://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.

import os
import re

import transcribe_model_selection

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')


def test_transcribe_model_selection_file(capsys):
transcribe_model_selection.transcribe_model_selection(
os.path.join(RESOURCES, 'Google_Gnome.wav'), 'video')
out, err = capsys.readouterr()

assert re.search(r'the weather outside is sunny', out, re.DOTALL | re.I)


def test_transcribe_model_selection_gcs(capsys):
transcribe_model_selection.transcribe_model_selection_gcs(
'gs://cloud-samples-tests/speech/Google_Gnome.wav', 'video')
out, err = capsys.readouterr()

assert re.search(r'the weather outside is sunny', out, re.DOTALL | re.I)