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

View File

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

View File

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