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

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
import base64
import os.path
from pydub import AudioSegment
import audiosegment_wrapper as AudioSegment
def save_audio(filename, base64_string):
decoded = None
@@ -15,7 +15,9 @@ def save_audio(filename, base64_string):
# return b"ERROR_FILE_EXISTS"
with open(orig_filename,"wb") as f:
f.write(decoded)
AudioSegment.from_file(orig_filename).export(filename,format="wav")
seg = AudioSegment.from_file(orig_filename)
seg = seg.resample(sample_rate_Hz=32000, sample_width=2, channels=1)
seg.export(filename,format="wav")
return b"SUCCESS"
def save_audio_chain(file_str_tupels):
@@ -37,6 +39,7 @@ def save_audio_chain(file_str_tupels):
if not completeAudio:
return b"ERROR_AUDIO_CONCAT_FAILED"
else:
completeAudio = completeAudio.resample(sample_rate_Hz=32000, sample_width=2, channels=1)
completeAudio.export(file_str_tupels[0][0],format="wav")
return b"SUCCESS"

View File

@@ -3,6 +3,7 @@ import multiprocessing as mp
import os.path
import filesystem
import log
import transcribe_async
USE_FREE=False
USE_PAID=True
@@ -17,26 +18,24 @@ def create_and_save_transcript(filename):
def analyse(filename):
''' returns the transcripted audio, or None if the analysis fails '''
try:
if USE_FREE:
recognizer = spr.Recognizer()
with spr.AudioFile(filename) as source:
audio = recognizer.record(source)
try:
if USE_FREE:
string = free_google_backend(recognizer, audio)
elif USE_PAID:
string = paid_google_backend(recognizer,audio)
string = paid_google_backend(filename)
except spr.UnknownValueError:
log.log("Audio file is broken or not an audio file")
return "ERROR_AUDIO_FILE_INVALID"
except spr.RequestError as e:
log.log("Could not connect to google API: {}".format(e))
return "ERROR_API_FAILURE"
return string
def free_google_backend(recognizer, audio):
return recognizer.recognize_google(audio,language="de-DE")
def paid_google_backend(recognizer, audio):
pass
def paid_google_backend(filename):
return transcribe_async.transcribe_file(filename)

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]