|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +""" Google Cloud Speech API sample application using the REST API for batch |
| 15 | +processing. |
| 16 | +
|
| 17 | +Example usage: |
| 18 | + python transcribe.py gs://cloud-samples-tests/speech/brooklyn.flac |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | + |
| 23 | + |
| 24 | +# [START speech_recognize_with_profanity_filter_gcs] |
| 25 | +def sync_recognize_with_profanity_filter_gcs(storage_uri): |
| 26 | + |
| 27 | + from google.cloud import speech |
| 28 | + |
| 29 | + client = speech.SpeechClient() |
| 30 | + |
| 31 | + audio = {"uri": storage_uri} |
| 32 | + |
| 33 | + config = speech.RecognitionConfig( |
| 34 | + encoding=speech.RecognitionConfig.AudioEncoding.FLAC, |
| 35 | + sample_rate_hertz=16000, |
| 36 | + language_code="en-US", |
| 37 | + profanity_filter=True, |
| 38 | + ) |
| 39 | + |
| 40 | + response = client.recognize(config=config, audio=audio) |
| 41 | + |
| 42 | + for i, result in enumerate(response.results): |
| 43 | + alternative = result.alternatives[0] |
| 44 | + print(u"Transcript: {}".format(alternative.transcript)) |
| 45 | + |
| 46 | + |
| 47 | +# [END speech_recognize_with_profanity_filter_gcs] |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + parser = argparse.ArgumentParser( |
| 51 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 52 | + ) |
| 53 | + parser.add_argument("path", help="GCS path for audio file to be recognized") |
| 54 | + args = parser.parse_args() |
| 55 | + sync_recognize_with_profanity_filter_gcs(args.path) |
0 commit comments