working google recog

This commit is contained in:
2018-08-27 13:45:07 +02:00
parent ae5c3ba538
commit 7d543873a0
4 changed files with 1092 additions and 48 deletions

View File

@@ -24,68 +24,50 @@ Example usage:
import argparse
import io
from gcloud import storage
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# [START speech_transcribe_async]
def transcribe_file(speech_file):
"""Transcribe the given audio file asynchronously."""
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
url = upload_file(speech_file)
print(url)
return transcribe_gcs("gs://"+url)
# [START speech_python_migration_async_request]
with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
def upload_file(filename):
bukket = "ths-speech-audio/"
client = storage.Client()
cb = client.get_bucket("ths-speech-audio")
blob = cb.blob(filename)
blob.upload_from_filename(filename)
return bukket + filename
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US')
# [START speech_python_migration_async_response]
operation = client.long_running_recognize(config, audio)
# [END speech_python_migration_async_request]
print('Waiting for operation to complete...')
response = operation.result(timeout=90)
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
for result in response.results:
# The first alternative is the most likely one for this portion.
print(u'Transcript: {}'.format(result.alternatives[0].transcript))
print('Confidence: {}'.format(result.alternatives[0].confidence))
# [END speech_python_migration_async_response]
# [END speech_transcribe_async]
# [START speech_transcribe_async_gcs]
def transcribe_gcs(gcs_uri):
"""Asynchronously transcribes the audio file specified by the gcs_uri."""
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
audio = types.RecognitionAudio(uri=gcs_uri)
config = types.RecognitionConfig(
#encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
#sample_rate_hertz=16000,
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=32000,
language_code='de-DE')
operation = client.long_running_recognize(config, audio)
print('Waiting for operation to complete...')
response = operation.result(timeout=90)
response = operation.result(timeout=900)
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
ret = ""
for result in response.results:
# The first alternative is the most likely one for this portion.
ret += result.alternatives[0].transcript
print(u'Transcript: {}'.format(result.alternatives[0].transcript))
print('Confidence: {}'.format(result.alternatives[0].confidence))
return ret
# [END speech_transcribe_async_gcs]