8000 Audio can be from gs:// or local · janiceblue/python-docs-samples@5f66d30 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f66d30

Browse files
author
Jerjou Cheng
committed
Audio can be from gs:// or local
1 parent a9d914b commit 5f66d30

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

speech/grpc/transcribe.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ def main(input_uri, encoding, sample_rate, language_code='ja-JP'):
5454
# grpc client lib was generated. See:
5555
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1/cloud_speech.proto
5656
start = time.time()
57+
if input_uri.startswith('gs://'):
58+
audio = cloud_speech_pb2.RecognitionAudio(uri=input_uri)
59+
else:
60+
with open(input_uri, 'rb') as f:
61+
audio = cloud_speech_pb2.RecognitionAudio(content=f.read())
5762
response = service.Recognize(cloud_speech_pb2.RecognizeRequest(
5863
config=cloud_speech_pb2.RecognitionConfig(
5964
# There are a bunch of config options you can specify. See
@@ -64,11 +69,10 @@ def main(input_uri, encoding, sample_rate, language_code='ja-JP'):
6469
# supported languages.
6570
language_code=language_code, # a BCP-47 language tag
6671
),
67-
audio=cloud_speech_pb2.RecognitionAudio(
68-
uri=input_uri,
69-
)
72+
audio=audio
7073
), DEADLINE_SECS)
7174
print('Time: %s' % (time.time() - start))
75+
print(response)
7276

7377
# Print the recognition result alternatives and confidence scores.
7478
for result in response.results:
@@ -91,13 +95,13 @@ def _gcs_uri(text):
9195
parser = argparse.ArgumentParser(
9296
description=__doc__,
9397
formatter_class=argparse.RawDescriptionHelpFormatter)
94-
parser.add_argument('input_uri', type=_gcs_uri)
98+
parser.add_argument('filename')
9599
parser.add_argument(
96100
'--encoding', default='LINEAR16', choices=[
97-
'LINEAR16', 'FLAC', 'MULAW', 'AMR', 'AMR_WB', 'SPEEX_WITH_HEADER_BYTE'],
101+
'LINEAR16', 'FLAC', 'MULAW', 'AMR', 'AMR_WB', 'SPEEX_WITH_HEADER_BYTE', 'OGG_OPUS'],
98102
help='How the audio file is encoded. See {}#L67'.format(PROTO_URL))
99103
parser.add_argument('--sample_rate', type=int, default=16000)
100104
parser.add_argument('--lang', default='en-US')
101105

102106
args = parser.parse_args()
103-
main(args.input_uri, args.encoding, args.sample_rate, args.lang)
107+
main(args.filename, args.encoding, args.sample_rate, args.lang)

0 commit comments

Comments
 (0)
0