mirror of
https://github.com/FAUSheppy/ths-speech
synced 2025-12-07 17:51:37 +01:00
refactor
This commit is contained in:
@@ -1,43 +1,23 @@
|
||||
import speech_recognition as spr
|
||||
import multiprocessing as mp
|
||||
import os.path
|
||||
import filesystem
|
||||
import log
|
||||
import transcribe_async
|
||||
import os
|
||||
import transcribe
|
||||
|
||||
USE_FREE=False
|
||||
USE_PAID=True
|
||||
|
||||
def async_create_transcript(filename):
|
||||
print("Creating transcript..")
|
||||
mp.Process(target=create_and_save_transcript,args=(filename,)).start()
|
||||
def asyncCreateTranscript(filename):
|
||||
mp.Process(target=createAndSaveTranscript, args=(filename,)).start()
|
||||
|
||||
def create_and_save_transcript(filename):
|
||||
transcript = analyse(filename)
|
||||
filesystem.save_transcript(filename, transcript)
|
||||
#os.system("../permissions.sh")
|
||||
|
||||
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)
|
||||
string = free_google_backend(recognizer, audio)
|
||||
elif USE_PAID:
|
||||
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(filename):
|
||||
return transcribe_async.transcribe_file(filename)
|
||||
def createAndSaveTranscript(filename):
|
||||
if no hasTranscript(filename):
|
||||
try:
|
||||
string = transcribe.transcribeFile(filename)
|
||||
except spr.UnknownValueError:
|
||||
log.log("Audio file is broken or not an audio file.")
|
||||
raise e
|
||||
except spr.RequestError as e:
|
||||
log.log("Could not connect to google API: {}".format(e))
|
||||
raise e
|
||||
filesystem.saveTranscript(filename, string)
|
||||
|
||||
Reference in New Issue
Block a user