Skip to content

Instantly share code, notes, and snippets.

@wermarter
Created April 25, 2017 09:25
Show Gist options
  • Select an option

  • Save wermarter/fd39ca748122fce796d7115944895c84 to your computer and use it in GitHub Desktop.

Select an option

Save wermarter/fd39ca748122fce796d7115944895c84 to your computer and use it in GitHub Desktop.
audio transcriber for Vietcap using wit.ai
import speech_recognition as sr
import os
class Listenner():
def __init__(self, api_key):
self.api_key = api_key
self.R = sr.Recognizer()
def recognize(self, AUDIO_FILE):
with sr.AudioFile(AUDIO_FILE) as source:
audio = self.R.record(source)
try:
return str(self.R.recognize_wit(audio, key=self.api_key))
except sr.UnknownValueError:
print("Wit.ai could not understand audio")
except sr.RequestError as e:
print("Could not request results from Wit.ai service; {0}".format(e))
return "<Error>"
listenner = Listenner("VIHOT4QYQDGSQKNNEQXRTDQGBKDSP5FW")
def write_txt(fn, txt):
with open(fn, 'w', encoding="UTF-8") as f:
f.write(txt)
def file_lst(directory):
return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
def fold_lst(directory):
return [f for f in os.listdir(directory) if not os.path.isfile(os.path.join(directory, f))]
def main(parent_dir):
for i, _dir in enumerate(fold_lst(parent_dir)):
print(i, ':', _dir)
for _file in file_lst(os.path.join(parent_dir, _dir)):
file_name, file_ext = os.path.splitext(_file)
new_file = os.path.join(parent_dir, _dir, file_name) + '.txt'
if os.path.isfile(new_file):
continue
print(_file)
file_path = os.path.join(parent_dir, _dir, _file)
txt = listenner.recognize(file_path)
write_txt(new_file, txt.lower())
main(os.path.abspath(r'C:/Users/Werma/Downloads/NA161115AM1/waves'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment