Skip to content

Print out transcripts directly. #605

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 1 commit into from
Oct 21, 2016
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
20 changes: 12 additions & 8 deletions speech/grpc/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from grpc.beta import implementations

# Keep the request alive for this many seconds
DEADLINE_SECS = 10
DEADLINE_SECS = 60
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'


Expand All @@ -48,7 +48,7 @@ def make_channel(host, port):
return implementations.secure_channel(host, port, composite_channel)


def main(input_uri, encoding, sample_rate):
def main(input_uri, encoding, sample_rate, language_code='en-US'):
service = cloud_speech.beta_create_Speech_stub(
make_channel('speech.googleapis.com', 443))
# The method and parameters can be inferred from the proto from which the
Expand All @@ -60,17 +60,21 @@ def main(input_uri, encoding, sample_rate):
# https://goo.gl/KPZn97 for the full list.
encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
sample_rate=sample_rate, # the rate in hertz
# See
# https://g.co/cloud/speech/docs/best-practices#language_support
# for a list of supported languages.
language_code='en-US', # a BCP-47 language tag
# See https://g.co/cloud/speech/docs/languages for a list of
# supported languages.
language_code=language_code, # a BCP-47 language tag
),
audio=cloud_speech.RecognitionAudio(
uri=input_uri,
)
), DEADLINE_SECS)
# Print the recognition results.
print(response.results)

# Print the recognition result alternatives and confidence scores.
for result in response.results:
print('Result:')
for alternative in result.alternatives:
print(u' ({}): {}'.format(
alternative.confidence, alternative.transcript))


def _gcs_uri(text):
Expand Down
32 changes: 17 additions & 15 deletions speech/grpc/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,54 @@ def make_channel(host, port):
return implementations.secure_channel(host, port, composite_channel)


def main(input_uri, encoding, sample_rate):
def main(input_uri, encoding, sample_rate, language_code='en-US'):
channel = make_channel('speech.googleapis.com', 443)
service = cloud_speech_pb2.beta_create_Speech_stub(channel)
# The method and parameters can be inferred from the proto from which the
# grpc client lib was generated. See:
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto
response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest(
operation = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest(
config=cloud_speech_pb2.RecognitionConfig(
# There are a bunch of config options you can specify. See
# https://goo.gl/KPZn97 for the full list.
encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
sample_rate=sample_rate, # the rate in hertz
# See
# https://g.co/cloud/speech/docs/best-practices#language_support
# for a list of supported languages.
language_code='en-US', # a BCP-47 language tag
# See https://g.co/cloud/speech/docs/languages for a list of
# supported languages.
language_code=language_code, # a BCP-47 language tag
),
audio=cloud_speech_pb2.RecognitionAudio(
uri=input_uri,
)
), DEADLINE_SECS)

# Print the longrunning operation handle.
print(response)
print(operation)

# Construct a long running operation endpoint.
service = operations_grpc_pb2.beta_create_Operations_stub(channel)

name = response.name
name = operation.name

while True:
# Give the server a few seconds to process.
print('Waiting for server processing...')
time.sleep(1)
# Get the long running operation with response.
response = service.GetOperation(
operation = service.GetOperation(
operations_grpc_pb2.GetOperationRequest(name=name),
DEADLINE_SECS)

if response.done:
if operation.done:
break

# Print the recognition results.
results = cloud_speech_pb2.AsyncRecognizeResponse()
response.response.Unpack(results)
print(results)
response = cloud_speech_pb2.AsyncRecognizeResponse()
operation.response.Unpack(response)
# Print the recognition result alternatives and confidence scores.
for result in response.results:
print('Result:')
for alternative in result.alternatives:
print(u' ({}): {}'.format(
alternative.confidence, alternative.transcript))


def _gcs_uri(text):
Expand Down