Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding save_dir argument to decode_speech function, fix issue #117 #118

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions quail/decode_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
pass


def decode_speech(path, keypath=None, save=False, speech_context=None,
def decode_speech(path, keypath=None, save=False, save_dir=None, speech_context=None,
sample_rate=44100, max_alternatives=1, language_code='en-US',
enable_word_time_offsets=True, return_raw=False):
"""
Expand Down Expand Up @@ -49,6 +49,10 @@ def decode_speech(path, keypath=None, save=False, speech_context=None,
False by default, but if set to true, will save a pickle with the results
object from google speech, and a text file with the decoded words.

save_dir : str
Path to desired output folder for pickled results object and text
file. Default is the folder containing the decoded wav file(s).

speech_context : list of str
This allows you to give some context to the speech decoding algorithm.
For example, this could be the words studied on a given list, or all
Expand Down Expand Up @@ -95,7 +99,8 @@ def recognize(chunk, file_path):
Subfunction that loops over audio segments to recognize speech
"""
# export as flac
chunk.export(file_path + ".flac", format = "flac", bitrate="44.1k")
if not os.path.isfile(file_path + ".flac"):
chunk.export(file_path + ".flac", format = "flac", bitrate="44.1k")

# open flac file
with open(file_path + ".flac", 'rb') as sc:
Expand Down Expand Up @@ -196,12 +201,16 @@ def parse_response(results):
else:
client = speech.SpeechClient()

# make a list of files
# make a list of files and set save_dir if not passed
files = []
if path.endswith(".wav"):
files = [path]
if save_dir is None:
save_dir = os.path.split(path)[0]
else:
listdirectory = os.listdir(path)
if save_dir is None:
save_dir = path
for filename in listdirectory:
if filename.endswith(".wav"):
files.append(path + filename)
Expand Down Expand Up @@ -235,16 +244,19 @@ def parse_response(results):
raw.append(results)

if save:

f_name = os.path.split(f)[1]
save_path = os.path.abspath(os.path.join(save_dir,f_name))

# save the raw response in a pickle
pickle.dump(results, open(f + ".p", "wb" ) )
pickle.dump(results, open(save_path + ".p", "wb" ))

# save a text file with just the words
pd.DataFrame(parsed_results).to_csv(f + '.txt', header=False,
index=False)
pd.DataFrame(parsed_results).to_csv(save_path + '.txt', header=False, index=False)

# print when finished
print('Finished file ' + str(i+1) + ' of ' + str(len(files)) + ' in ' +
str(round(time.time()-start,2)) + ' seconds.')
print('Finished file ' + str(i+1) + ' of ' + str(len(files)) + ' in '
+ str(round(time.time()-start,2)) + ' seconds.')

# handle when something goes wrong
except ValueError as e:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
REQUIREMENTS = f.read().splitlines()

EXTRAS_REQUIRE={
'speech-decoding': ["pydub", "google-cloud-speech<0.31dev,>=0.30.0", "google-cloud>=0.32.0,<0.34.0"],
'speech-decoding': ["pydub", "google-cloud-speech", "google-auth"],
'efficient-learning': ["sqlalchemy"],
}

Expand Down