Gap handling fixed

borken-flask
HeNine 3 years ago
parent 8be91d4fd5
commit 11164271b2

@ -43,8 +43,8 @@ def transcribe_segments(segments: list, sample_rate: int, recognizer: BuscribeRe
data = process.stdout.read(16000) data = process.stdout.read(16000)
if len(data) == 0: if len(data) == 0:
break break
if recognizer.AcceptWaveform(data): if recognizer.accept_waveform(data):
result_json = json.loads(recognizer.Result()) result_json = json.loads(recognizer.result())
logging.debug(json.dumps(result_json, indent=2)) logging.debug(json.dumps(result_json, indent=2))
if result_json["text"] == "": if result_json["text"] == "":
@ -92,11 +92,11 @@ def get_end_of_transcript(db_cursor):
def finish_off_recognizer(recognizer: BuscribeRecognizer, db_cursor): def finish_off_recognizer(recognizer: BuscribeRecognizer, db_cursor):
"""Flush the recognizer, commit the final line to the database and reset it.""" """Flush the recognizer, commit the final line to the database and reset it."""
final_result_json = json.loads(recognizer.FinalResult()) # Flush the tubes final_result_json = json.loads(recognizer.final_result()) # Flush the tubes
line_start_time = recognizer.segments_start_time + timedelta(seconds=final_result_json["result"][0]["start"]) line_start_time = recognizer.segments_start_time + timedelta(seconds=final_result_json["result"][0]["start"])
line_end_time = recognizer.segments_start_time + timedelta(seconds=final_result_json["result"][-1]["end"]) line_end_time = recognizer.segments_start_time + timedelta(seconds=final_result_json["result"][-1]["end"])
write_line(final_result_json, line_start_time, line_end_time, db_cursor) write_line(final_result_json, line_start_time, line_end_time, db_cursor)
recognizer.Reset() recognizer.reset()

@ -63,6 +63,7 @@ def main(database="", base_dir=".",
logging.info('Transcribing from {}'.format(start_time)) logging.info('Transcribing from {}'.format(start_time))
# Start priming the recognizer if possible # Start priming the recognizer if possible
start_of_transcription = start_time
start_time -= timedelta(minutes=2) start_time -= timedelta(minutes=2)
stopping = gevent.event.Event() stopping = gevent.event.Event()
@ -85,8 +86,10 @@ def main(database="", base_dir=".",
if recognizer.segments_start_time is None: if recognizer.segments_start_time is None:
recognizer.segments_start_time = segments[0].start recognizer.segments_start_time = segments[0].start
logging.info(f"Starting from: {segments[0].start}")
segments_end_time = transcribe_segments(segments, SAMPLE_RATE, recognizer, start_time, db_cursor, stopping) segments_end_time = transcribe_segments(segments, SAMPLE_RATE, recognizer, start_of_transcription, db_cursor,
stopping)
if end_time is not None and segments_end_time >= end_time \ if end_time is not None and segments_end_time >= end_time \
or stopping.is_set(): or stopping.is_set():

@ -1,7 +1,7 @@
from vosk import Model, SpkModel, KaldiRecognizer from vosk import Model, SpkModel, KaldiRecognizer
class BuscribeRecognizer(KaldiRecognizer): class BuscribeRecognizer():
segments_start_time = None segments_start_time = None
def __init__(self, sample_rate=48000, model_path="model_small", spk_model_path="spk_model"): def __init__(self, sample_rate=48000, model_path="model_small", spk_model_path="spk_model"):
@ -11,13 +11,23 @@ class BuscribeRecognizer(KaldiRecognizer):
Returns a recognizer object. Returns a recognizer object.
""" """
self.sample_rate = sample_rate
self.model = Model(model_path) self.model = Model(model_path)
self.spk_model = SpkModel(spk_model_path) self.spk_model = SpkModel(spk_model_path)
super(BuscribeRecognizer, self).__init__(self.model, sample_rate, self.spk_model) self.recognizer = KaldiRecognizer(self.model, self.sample_rate, self.spk_model)
self.recognizer.SetWords(True)
self.SetWords(True) def reset(self):
self.recognizer = KaldiRecognizer(self.model, self.sample_rate, self.spk_model)
def Reset(self): self.recognizer.SetWords(True)
super(BuscribeRecognizer, self).Reset()
self.segments_start_time = None self.segments_start_time = None
def accept_waveform(self, data):
return self.recognizer.AcceptWaveform(data)
def result(self):
return self.recognizer.Result()
def final_result(self):
return self.recognizer.FinalResult()

Loading…
Cancel
Save