| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 84c8ce3 commit a6f61d2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ | |||
| 22 | 22 | from grpc.beta import implementations | |
| 23 | 23 | ||
| 24 | 24 | # Keep the request alive for this many seconds | |
| 25 | - DEADLINE_SECS = 10 | ||
| 25 | + DEADLINE_SECS = 60 | ||
| 26 | 26 | SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform' | |
| 27 | 27 | ||
| 28 | 28 | ||
@@ -48,7 +48,7 @@ def make_channel(host, port): | |||
| 48 | 48 | return implementations.secure_channel(host, port, composite_channel) | |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | - def main(input_uri, encoding, sample_rate): | ||
| 51 | + def main(input_uri, encoding, sample_rate, language_code='en-US'): | ||
| 52 | 52 | service = cloud_speech.beta_create_Speech_stub( | |
| 53 | 53 | make_channel('speech.googleapis.com', 443)) | |
| 54 | 54 | # The method and parameters can be inferred from the proto from which the | |
@@ -60,17 +60,21 @@ def main(input_uri, encoding, sample_rate): | |||
| 60 | 60 | # https://goo.gl/KPZn97 for the full list. | |
| 61 | 61 | encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB | |
| 62 | 62 | sample_rate=sample_rate, # the rate in hertz | |
| 63 | - # See | ||
| 64 | - # https://g.co/cloud/speech/docs/best-practices#language_support | ||
| 65 | - # for a list of supported languages. | ||
| 66 | - language_code='en-US', # a BCP-47 language tag | ||
| 63 | + # See https://g.co/cloud/speech/docs/languages for a list of | ||
| 64 | + # supported languages. | ||
| 65 | + language_code=language_code, # a BCP-47 language tag | ||
| 67 | 66 | ), | |
| 68 | 67 | audio=cloud_speech.RecognitionAudio( | |
| 69 | 68 | uri=input_uri, | |
| 70 | 69 | ) | |
| 71 | 70 | ), DEADLINE_SECS) | |
| 72 | - # Print the recognition results. | ||
| 73 | - print(response.results) | ||
| 71 | + | ||
| 72 | + # Print the recognition result alternatives and confidence scores. | ||
| 73 | + for result in response.results: | ||
| 74 | + print('Result:') | ||
| 75 | + for alternative in result.alternatives: | ||
| 76 | + print(u' ({}): {}'.format( | ||
| 77 | + alternative.confidence, alternative.transcript)) | ||
| 74 | 78 | ||
| 75 | 79 | ||
| 76 | 80 | def _gcs_uri(text): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,52 +51,54 @@ def make_channel(host, port): | |||
| 51 | 51 | return implementations.secure_channel(host, port, composite_channel) | |
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | - def main(input_uri, encoding, sample_rate): | ||
| 54 | + def main(input_uri, encoding, sample_rate, language_code='en-US'): | ||
| 55 | 55 | channel = make_channel('speech.googleapis.com', 443) | |
| 56 | 56 | service = cloud_speech_pb2.beta_create_Speech_stub(channel) | |
| 57 | 57 | # The method and parameters can be inferred from the proto from which the | |
| 58 | 58 | # grpc client lib was generated. See: | |
| 59 | 59 | # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto | |
| 60 | - response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( | ||
| 60 | + operation = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( | ||
| 61 | 61 | config=cloud_speech_pb2.RecognitionConfig( | |
| 62 | 62 | # There are a bunch of config options you can specify. See | |
| 63 | 63 | # https://goo.gl/KPZn97 for the full list. | |
| 64 | 64 | encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB | |
| 65 | 65 | sample_rate=sample_rate, # the rate in hertz | |
| 66 | - # See | ||
| 67 | - # https://g.co/cloud/speech/docs/best-practices#language_support | ||
| 68 | - # for a list of supported languages. | ||
| 69 | - language_code='en-US', # a BCP-47 language tag | ||
| 66 | + # See https://g.co/cloud/speech/docs/languages for a list of | ||
| 67 | + # supported languages. | ||
| 68 | + language_code=language_code, # a BCP-47 language tag | ||
| 70 | 69 | ), | |
| 71 | 70 | audio=cloud_speech_pb2.RecognitionAudio( | |
| 72 | 71 | uri=input_uri, | |
| 73 | 72 | ) | |
| 74 | 73 | ), DEADLINE_SECS) | |
| 75 | 74 | ||
| 76 | 75 | # Print the longrunning operation handle. | |
| 77 | - print(response) | ||
| 76 | + print(operation) | ||
| 78 | 77 | ||
| 79 | 78 | # Construct a long running operation endpoint. | |
| 80 | 79 | service = operations_grpc_pb2.beta_create_Operations_stub(channel) | |
| 81 | 80 | ||
| 82 | - name = response.name | ||
| 81 | + name = operation.name | ||
| 83 | 82 | ||
| 84 | 83 | while True: | |
| 85 | 84 | # Give the server a few seconds to process. | |
| 86 | 85 | print('Waiting for server processing...') | |
| 87 | 86 | time.sleep(1) | |
| 88 | - # Get the long running operation with response. | ||
| 89 | - response = service.GetOperation( | ||
| 87 | + operation = service.GetOperation( | ||
| 90 | 88 | operations_grpc_pb2.GetOperationRequest(name=name), | |
| 91 | 89 | DEADLINE_SECS) | |
| 92 | 90 | ||
| 93 | - if response.done: | ||
| 91 | + if operation.done: | ||
| 94 | 92 | break | |
| 95 | 93 | ||
| 96 | - # Print the recognition results. | ||
| 97 | - results = cloud_speech_pb2.AsyncRecognizeResponse() | ||
| 98 | - response.response.Unpack(results) | ||
| 99 | - print(results) | ||
| 94 | + response = cloud_speech_pb2.AsyncRecognizeResponse() | ||
| 95 | + operation.response.Unpack(response) | ||
| 96 | + # Print the recognition result alternatives and confidence scores. | ||
| 97 | + for result in response.results: | ||
| 98 | + print('Result:') | ||
| 99 | + for alternative in result.alternatives: | ||
| 100 | + print(u' ({}): {}'.format( | ||
| 101 | + alternative.confidence, alternative.transcript)) | ||
| 100 | 102 | ||
| 101 | 103 | ||
| 102 | 104 | def _gcs_uri(text): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments