DeepSpeech/examples/vad_transcriber/audioTranscript_cmd.py
bhargav-ak 89620bc448 Transcribing longer audio clips
Prerequisites
-------------
~/Deepspeech$ sudo apt install virtualenv
~/Deepspeech$ cd examples/vad_transcriber
~/Deepspeech/examples/vad_transcriber$ virtualenv -p python3 venv
~/Deepspeech/examples/vad_transcriber$ source venv/bin/activate
(venv) ~/Deepspeech/examples/vad_transcriber$ pip3 install -r requirements.txt

Command line tool
-----------------
The command line tool processes a wav file of any duration and returns a trancript
which will the saved in the same directory as the input audio file.

(venv) ~/Deepspeech/examples/vad_transcriber
$ python3 audioTranscript_cmd.py --aggressive 1 --audio ./audio/guido-van-rossum.wav --model ./models/0.2.0/

Minimalistic GUI
----------------
The GUI tool does the same job as the CLI tool. The VAD is fixed at an aggressiveness of 1.
The output is displayed in the transcription window and saved into the directory as the input
audio file as well.

(venv) ~/Deepspeech/examples/vad_transcriber
$ python3 audioTranscript_gui.py

Changes(v1):
1. Using Deepspeech python module instead of subprocess
2. Moved VAD code to a module
3. Moved all files to bin/ and renamed README.md to Audio_Transcription.md

Changes(v2):
Renamed files

Changes (v2.1):
1. Refactoring between CMD and GUI code
2. Documenting pre-requisites with a virtualenv
3. Loading model only once per long wav file
4. CMD and GUI tool do the same job, perform VAD and consolidate the output.
5. Chunks are not saved in the disk. Using a numpy interger array to store them.

Changes (v2.2):
1. Argparse module for command line arguments
2. Everything in virtualenv, with a requirements.txt
3. Older APIs aligned with 0.2.0 release
4. Moved all files into examples/vad_transcriber

Changes (v2.3)
1. Updated requirements.txt
2018-10-03 02:29:26 +05:30

68 lines
3.0 KiB
Python

import sys
import os
import logging
import argparse
import numpy as np
import wavTranscriber
# Debug helpers
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def main(args):
parser = argparse.ArgumentParser(description='Transcribe long audio files using webRTC VAD')
parser.add_argument('--aggressive', type=int, choices=range(4), required=True,
help='Determines how aggressive filtering out non-speech is. (Interger between 0-3)')
parser.add_argument('--audio', required=True,
help='Path to the audio file to run (WAV format)')
parser.add_argument('--model', required=True,
help='Path to directory that contains all model files (output_graph, lm, trie and alphabet)')
if len(sys.argv[1:])<6:
parser.print_help()
parser.exit()
args = parser.parse_args()
# Point to a path containing the pre-trained models & resolve ~ if used
dirName = os.path.expanduser(args.model)
title_names = ['Filename', 'Duration(s)', 'Inference Time(s)', 'Model Load Time(s)', 'LM Load Time(s)']
print("\n%-30s %-20s %-20s %-20s %s" % (title_names[0], title_names[1], title_names[2], title_names[3], title_names[4]))
# Resolve all the paths of model files
output_graph, alphabet, lm, trie = wavTranscriber.resolve_models(dirName)
# Load output_graph, alpahbet, lm and trie
model_retval = wavTranscriber.load_model(output_graph, alphabet, lm, trie)
inference_time = 0.0
# Run VAD on the input file
waveFile = args.audio
segments, sample_rate, audio_length = wavTranscriber.vad_segment_generator(waveFile, args.aggressive)
f = open(waveFile.rstrip(".wav") + ".txt", 'w')
logging.debug("Saving Transcript @: %s" % waveFile.rstrip(".wav") + ".txt")
for i, segment in enumerate(segments):
# Run deepspeech on the chunk that just completed VAD
logging.debug("Processing chunk %002d" % (i,))
audio = np.frombuffer(segment, dtype=np.int16)
output = wavTranscriber.stt(model_retval[0], audio, sample_rate)
inference_time += output[1]
logging.debug("Transcript: %s" % output[0])
f.write(output[0] + " ")
# Summary of the files processed
f.close()
# Extract filename from the full file path
filename, ext = os.path.split(os.path.basename(waveFile))
logging.debug("************************************************************************************************************")
logging.debug("%-30s %-20s %-20s %-20s %s" % (title_names[0], title_names[1], title_names[2], title_names[3], title_names[4]))
logging.debug("%-30s %-20.3f %-20.3f %-20.3f %-0.3f" % (filename + ext, audio_length, inference_time, model_retval[1], model_retval[2]))
logging.debug("************************************************************************************************************")
print("%-30s %-20.3f %-20.3f %-20.3f %-0.3f" % (filename + ext, audio_length, inference_time, model_retval[1], model_retval[2]))
if __name__ == '__main__':
main(sys.argv[1:])