Skip to content

Instantly share code, notes, and snippets.

@samuelcouch
Created October 10, 2017 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelcouch/117546c4254f0f3096cc79effc79ac2c to your computer and use it in GitHub Desktop.
Save samuelcouch/117546c4254f0f3096cc79effc79ac2c to your computer and use it in GitHub Desktop.
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
# Use this method to send the audio to Watson and save the response
def get_and_save_transcript(audio_file, transcript_file):
with open(audio_file, 'rb') as af:
resp = stt.recognize(af,
content_type='audio/wav',
timestamps=True,
continuous=True,
word_confidence=True,
profanity_filter=False,
word_alternatives_threshold=0.4)
with open(transcript_file, 'w') as tf:
tf.write(json.dumps(resp))
stt = SpeechToText(
username='<YOUR SERVICE USERNAME>',
password='<YOUR SERVICE PASSWORD>')
# Make a directory to save our transcripts
if not os.path.exists(join(project_dir, 'split_transcripts')):
os.makedirs(join(project_dir, 'split_transcripts'))
trancription_jobs = []
transcript_files = []
# Create a new job for each audio segment
for chunk in segment_files:
transcript_file = '{}.json'.format(splitext(basename(chunk))[0])
full_transcript_file = join(project_dir, 'split_transcripts', transcript_file)
transcript_files.append(full_transcript_file)
job = Process(target=get_and_save_transcript,
args=(chunk,full_transcript_file))
job.start()
trancription_jobs.append(job)
for job in trancription_jobs:
job.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment