Skip to content

Commit

Permalink
fix for no frames decoded error
Browse files Browse the repository at this point in the history
  • Loading branch information
pskrunner14 committed Dec 28, 2019
1 parent c0cf15e commit 010da74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions python/kaldi_serve/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def chunks_from_mic(secs: int, frame_rate: int, channels: int):
p.terminate()


def chunks_from_file(filename: str, chunk_size=1, raw=False):
def chunks_from_file(filename: str, chunk_size=1, raw=False, pcm=False):
"""
Return wav chunks of given size (in seconds) from the file.
"""

# TODO: Should remove assumptions about audio properties from here
audio = AudioSegment.from_file(filename, format="wav", frame_rate=8000, channels=1, sample_width=2)
return chunks_from_audio_segment(audio, chunk_size=chunk_size, raw=raw)
audio = AudioSegment.from_file(filename, format="s16le" if pcm else "wav", frame_rate=8000, channels=1, sample_width=2)
return chunks_from_audio_segment(audio, chunk_size=chunk_size, raw=True if pcm else raw)

def chunks_from_audio_segment(audio: str, chunk_size=1, raw=False):
"""
Expand Down
14 changes: 8 additions & 6 deletions python/scripts/example_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
Script for testing out ASR server.
Usage:
example_client.py mic [--n-secs=<n-secs>] [--model=<model>] [--lang=<lang>] [--raw]
example_client.py <file>... [--model=<model>] [--lang=<lang>] [--raw]
example_client.py mic [--n-secs=<n-secs>] [--model=<model>] [--lang=<lang>] [--raw] [--pcm]
example_client.py <file>... [--model=<model>] [--lang=<lang>] [--raw] [--pcm]
Options:
--n-secs=<n-secs> Number of seconds to records, ideally there should be a VAD here. [default: 3]
--model=<model> Name of the model to hit [default: general]
--lang=<lang> Language code of the model [default: hi]
--raw Flag that specifies whether to stream raw audio bytes to server.
--pcm Flag for sending raw pcm bytes
"""

import random
Expand All @@ -18,6 +19,7 @@
from pprint import pprint
from typing import List

from pydub import AudioSegment
from docopt import docopt

from kaldi_serve import KaldiServeClient, RecognitionAudio, RecognitionConfig
Expand Down Expand Up @@ -84,12 +86,11 @@ def transcribe_chunks(client, audio_chunks, model: str, language_code: str, raw:
pprint(parse_response(response))


def decode_files(client, audio_paths: List[str], model: str, language_code: str, raw: bool=False):
def decode_files(client, audio_paths: List[str], model: str, language_code: str, raw: bool=False, pcm: bool=False):
"""
Decode files using threaded requests
"""

chunked_audios = [chunks_from_file(x, chunk_size=random.randint(1, 3), raw=raw) for x in audio_paths]
chunked_audios = [chunks_from_file(x, chunk_size=random.randint(1, 3), raw=raw, pcm=pcm) for x in audio_paths]

threads = [
threading.Thread(target=transcribe_chunks, args=(client, chunks, model, language_code, raw))
Expand All @@ -109,8 +110,9 @@ def decode_files(client, audio_paths: List[str], model: str, language_code: str,
model = args["--model"]
language_code = args["--lang"]
raw = args['--raw']
pcm = args['--pcm']

if args["mic"]:
transcribe_chunks(client, chunks_from_mic(int(args["--n-secs"]), SR, 1), model, language_code, raw)
else:
decode_files(client, args["<file>"], model, language_code, raw)
decode_files(client, args["<file>"], model, language_code, raw, pcm)
9 changes: 7 additions & 2 deletions src/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,17 @@ void Decoder::decode_stream_final(kaldi::OnlineNnet2FeaturePipeline &feature_pip
feature_pipeline.InputFinished();
decoder.FinalizeDecoding();

if (decoder.NumFramesDecoded() == 0) {
KALDI_WARN << "audio may be empty :: decoded no frames";
return;
}

kaldi::CompactLattice clat;
try {
decoder.GetLattice(true, &clat);
find_alternatives(word_syms_.get(), clat, n_best, results);
} catch (const std::exception &e) {
std::cout << "ERROR :: client timed out" << ENDL;
} catch (std::exception &e) {
KALDI_ERR << "unexpected error during decoding lattice :: " << e.what();
}
}

Expand Down

0 comments on commit 010da74

Please sign in to comment.