|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 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 | +# http://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 | +"""Google Cloud Speech API sample that demonstrates enhanced models |
| 18 | +and recognition metadata. |
| 19 | +
|
| 20 | +Example usage: |
| 21 | + python beta_snippets.py enhanced-model resources/commercial_mono.wav |
| 22 | + python beta_snippets.py metadata resources/commercial_mono.wav |
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | +import io |
| 27 | + |
| 28 | +from google.cloud import speech_v1p1beta1 as speech |
| 29 | + |
| 30 | + |
| 31 | +# [START speech_transcribe_file_with_enhanced_model] |
| 32 | +def transcribe_file_with_enhanced_model(path): |
| 33 | + """Transcribe the given audio file using an enhanced model.""" |
| 34 | + client = speech.SpeechClient() |
| 35 | + |
| 36 | + with io.open(path, 'rb') as audio_file: |
| 37 | + content = audio_file.read() |
| 38 | + |
| 39 | + audio = speech.types.RecognitionAudio(content=content) |
| 40 | + config = speech.types.RecognitionConfig( |
| 41 | + encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, |
| 42 | + sample_rate_hertz=8000, |
| 43 | + language_code='en-US', |
| 44 | + # Enhanced models are only available to projects that |
| 45 | + # opt in for audio data collection. |
| 46 | + use_enhanced=True, |
| 47 | + # A model must be specified to use enhanced model. |
| 48 | + model='phone_call') |
| 49 | + |
| 50 | + response = client.recognize(config, audio) |
| 51 | + |
| 52 | + for i, result in enumerate(response.results): |
| 53 | + alternative = result.alternatives[0] |
| 54 | + print('-' * 20) |
| 55 | + print('First alternative of result {}'.format(i)) |
| 56 | + print('Transcript: {}'.format(alternative.transcript)) |
| 57 | +# [END speech_transcribe_file_with_enhanced_model] |
| 58 | + |
| 59 | + |
| 60 | +# [START speech_transcribe_file_with_metadata] |
| 61 | +def transcribe_file_with_metadata(path): |
| 62 | + """Send a request that includes recognition metadata.""" |
| 63 | + client = speech.SpeechClient() |
| 64 | + |
| 65 | + with io.open(path, 'rb') as audio_file: |
| 66 | + content = audio_file.read() |
| 67 | + |
| 68 | + # Here we construct a recognition metadata object. |
| 69 | + # Most metadata fields are specified as enums that can be found |
| 70 | + # in speech.enums.RecognitionMetadata |
| 71 | + metadata = speech.types.RecognitionMetadata() |
| 72 | + metadata.interaction_type = ( |
| 73 | + speech.enums.RecognitionMetadata.InteractionType.DISCUSSION) |
| 74 | + metadata.microphone_distance = ( |
| 75 | + speech.enums.RecognitionMetadata.MicrophoneDistance.NEARFIELD) |
| 76 | + metadata.recording_device_type = ( |
| 77 | + speech.enums.RecognitionMetadata.RecordingDeviceType.SMARTPHONE) |
| 78 | + # Some metadata fields are free form strings |
| 79 | + metadata.recording_device_name = "Pixel 2 XL" |
| 80 | + # And some are integers, for instance the 6 digit NAICS code |
| 81 | + # https://www.naics.com/search/ |
| 82 | + metadata.industry_naics_code_of_audio = 519190 |
| 83 | + |
| 84 | + audio = speech.types.RecognitionAudio(content=content) |
| 85 | + config = speech.types.RecognitionConfig( |
| 86 | + encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, |
| 87 | + sample_rate_hertz=8000, |
| 88 | + language_code='en-US', |
| 89 | + # Add this in the request to send metadata. |
| 90 | + metadata=metadata) |
| 91 | + |
| 92 | + response = client.recognize(config, audio) |
| 93 | + |
| 94 | + for i, result in enumerate(response.results): |
| 95 | + alternative = result.alternatives[0] |
| 96 | + print('-' * 20) |
| 97 | + print('First alternative of result {}'.format(i)) |
| 98 | + print('Transcript: {}'.format(alternative.transcript)) |
| 99 | +# [END speech_transcribe_file_with_metadata] |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + parser = argparse.ArgumentParser( |
| 104 | + description=__doc__, |
| 105 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 106 | + parser.add_argument('command') |
| 107 | + parser.add_argument( |
| 108 | + 'path', help='File for audio file to be recognized') |
| 109 | + |
| 110 | + args = parser.parse_args() |
| 111 | + |
| 112 | + if args.command == 'enhanced-model': |
| 113 | + transcribe_file_with_enhanced_model(args.path) |
| 114 | + elif args.command == 'metadata': |
| 115 | + transcribe_file_with_metadata(args.path) |
0 commit comments