dictation-toolbox / dragonfly

Speech recognition framework allowing powerful Python-based scripting and extension of Dragon NaturallySpeaking (DNS), Windows Speech Recognition (WSR), Kaldi and CMU Pocket Sphinx
GNU Lesser General Public License v3.0
388 stars 75 forks source link

Issues with restarting Kaldi `subprocess.Popen` and/or `disconnect()`. #309

Closed LexiconCode closed 3 years ago

LexiconCode commented 4 years ago

Test Rule without Caster. Test with subprocess.Popen and without just "disconnect()".

import subprocess, sys, time

from dragonfly import (Choice, Dictation, Function, Grammar, IntegerRef, Key,
                       MappingRule, Text, get_current_engine)

def reboot():
    engine = get_current_engine()
    if engine.name == 'kaldi':
        engine.disconnect()
        # time.sleep(3)
        subprocess.Popen([sys.executable, '-m', 'dragonfly', 'load', '_*.py', '--engine', 'kaldi',  '--no-recobs-messages'])

class TestDragonflyRule(MappingRule):
    mapping = {
        "test reboot":
            Function(reboot),
    } 

    extras = [

    ]

    defaults = {

    }

grammar = Grammar('TestExamples')
grammar.add_rule(TestDragonflyRule())
grammar.load()

However there seems to be issues calling Popen on Linux after disconnect.

This problem is semi-intermittent with subprocess.Popen.

INFO:engine:Loading grammar TestExamples
INFO:engine:Listening...
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/main/.local/lib/python3.8/site-packages/dragonfly/engines/backend_kaldi/audio.py", line 110, in _reader_thread
    in_data, overflowed = self.stream.read(self.stream.blocksize)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 1196, in read
    _check(err)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 2653, in _check
    raise PortAudioError(errormsg, err)

semi-intermittent Stream is stopped [PaErrorCode -9983] with subprocess.Popen and without.

INFO:engine:Listening...
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/main/.local/lib/python3.8/site-packages/dragonfly/engines/backend_kaldi/audio.py", line 110, in _reader_thread
    in_data, overflowed = self.stream.read(self.stream.blocksize)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 1196, in read
    _check(err)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 2653, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Stream is stopped [PaErrorCode -9983]
main@pop-os:~/Caster$ Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/main/.local/lib/python3.8/site-packages/dragonfly/engines/backend_kaldi/audio.py", line 110, in _reader_thread
    in_data, overflowed = self.stream.read(self.stream.blocksize)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 1196, in read
    _check(err)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 2653, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Stream is stopped [PaErrorCode -9983]
INFO:engine:Loading Kaldi-Active-Grammar v1.8.1 in process 8286.
INFO:engine:Kaldi options: {'model_dir': None, 'tmp_dir': None, 'audio_input_device': None, 'audio_self_threaded': True, 'audio_auto_reconnect': True, 'audio_reconnect_callback': None, 'retain_dir': None, 'retain_audio': False, 'retain_metadata': False, 'retain_approval_func': None, 'vad_aggressiveness': 3, 'vad_padding_start_ms': 150, 'vad_padding_end_ms': 200, 'vad_complex_padding_end_ms': 600, 'auto_add_to_user_lexicon': True, 'lazy_compilation': True, 'invalidate_cache': False, 'expected_error_rate_threshold': None, 'alternative_dictation': None, 'cloud_dictation_lang': 'en-US', 'decoder_init_config': None}
Kaldi-Active-Grammar v1.8.1: 
    If this free, open source engine is valuable to you, please consider donating 
    https://github.com/daanzu/kaldi-active-grammar 
    Disable message by calling `kaldi_active_grammar.disable_donation_message()`
INFO:engine:Loading Kaldi-Active-Grammar v1.8.1 in process 8303.
INFO:engine:Kaldi options: {'model_dir': None, 'tmp_dir': None, 'audio_input_device': None, 'audio_self_threaded': True, 'audio_auto_reconnect': True, 'audio_reconnect_callback': None, 'retain_dir': None, 'retain_audio': False, 'retain_metadata': False, 'retain_approval_func': None, 'vad_aggressiveness': 3, 'vad_padding_start_ms': 150, 'vad_padding_end_ms': 200, 'vad_complex_padding_end_ms': 600, 'auto_add_to_user_lexicon': True, 'lazy_compilation': True, 'invalidate_cache': False, 'expected_error_rate_threshold': None, 'alternative_dictation': None, 'cloud_dictation_lang': 'en-US', 'decoder_init_config': None}
Kaldi-Active-Grammar v1.8.1: 
    If this free, open source engine is valuable to you, please consider donating 
    https://github.com/daanzu/kaldi-active-grammar 
    Disable message by calling `kaldi_active_grammar.disable_donation_message()`
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms
INFO:module:CommandModule('_DragonflyExamplesRule.py'): Loading module: '/home/main/Caster/_DragonflyExamplesRule.py'
INFO:engine:Loading grammar TestExamples
INFO:engine:Listening...
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms
INFO:module:CommandModule('_DragonflyExamplesRule.py'): Loading module: '/home/main/Caster/_DragonflyExamplesRule.py'
INFO:engine:Loading grammar TestExamples
INFO:engine:Listening...

Sometimes initializes twice with subprocess.Popen and (without only once).

main@pop-os:~/Caster$ python3 -m dragonfly load _*.py --engine kaldi  --no-recobs-messages
INFO:engine:Loading Kaldi-Active-Grammar v1.8.1 in process 11548.
INFO:engine:Kaldi options: {'model_dir': None, 'tmp_dir': None, 'audio_input_device': None, 'audio_self_threaded': True, 'audio_auto_reconnect': True, 'audio_reconnect_callback': None, 'retain_dir': None, 'retain_audio': False, 'retain_metadata': False, 'retain_approval_func': None, 'vad_aggressiveness': 3, 'vad_padding_start_ms': 150, 'vad_padding_end_ms': 200, 'vad_complex_padding_end_ms': 600, 'auto_add_to_user_lexicon': True, 'lazy_compilation': True, 'invalidate_cache': False, 'expected_error_rate_threshold': None, 'alternative_dictation': None, 'cloud_dictation_lang': 'en-US', 'decoder_init_config': None}
Kaldi-Active-Grammar v1.8.1: 
    If this free, open source engine is valuable to you, please consider donating 
    https://github.com/daanzu/kaldi-active-grammar 
    Disable message by calling `kaldi_active_grammar.disable_donation_message()`
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms
INFO:module:CommandModule('_DragonflyExamplesRule.py'): Loading module: '/home/main/Caster/_DragonflyExamplesRule.py'
INFO:engine:Loading grammar TestExamples
INFO:engine:Listening...
^CException in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/main/.local/lib/python3.8/site-packages/dragonfly/engines/backend_kaldi/audio.py", line 110, in _reader_thread
    in_data, overflowed = self.stream.read(self.stream.blocksize)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 1196, in read
    _check(err)
  File "/home/main/.local/lib/python3.8/site-packages/sounddevice.py", line 2653, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Stream is stopped [PaErrorCode -9983]
main@pop-os:~/Caster$ INFO:engine:Loading Kaldi-Active-Grammar v1.8.1 in process 12113.
INFO:engine:Kaldi options: {'model_dir': None, 'tmp_dir': None, 'audio_input_device': None, 'audio_self_threaded': True, 'audio_auto_reconnect': True, 'audio_reconnect_callback': None, 'retain_dir': None, 'retain_audio': False, 'retain_metadata': False, 'retain_approval_func': None, 'vad_aggressiveness': 3, 'vad_padding_start_ms': 150, 'vad_padding_end_ms': 200, 'vad_complex_padding_end_ms': 600, 'auto_add_to_user_lexicon': True, 'lazy_compilation': True, 'invalidate_cache': False, 'expected_error_rate_threshold': None, 'alternative_dictation': None, 'cloud_dictation_lang': 'en-US', 'decoder_init_config': None}
Kaldi-Active-Grammar v1.8.1: 
    If this free, open source engine is valuable to you, please consider donating 
    https://github.com/daanzu/kaldi-active-grammar 
    Disable message by calling `kaldi_active_grammar.disable_donation_message()`
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms
INFO:module:CommandModule('_DragonflyExamplesRule.py'): Loading module: '/home/main/Caster/_DragonflyExamplesRule.py'
INFO:engine:Loading grammar TestExamples
INFO:engine:Listening...
WARNING:engine:<dragonfly.engines.backend_kaldi.audio.VADAudio object at 0x7fe9bdac3040>: no good block received recently, so reconnecting audio
WARNING:engine:<dragonfly.engines.backend_kaldi.audio.VADAudio object at 0x7f5355cad100>: no good block received recently, so reconnecting audio
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms
INFO:engine:streaming audio from 'default' using ALSA: 16000 sample_rate, 10 block_duration_ms, 40 latency_ms

The last bit is keyboard interrupt ctrl-c for ^C to end some times after for a short bit i can stlil execute commands.

Originally posted by @LexiconCode in https://github.com/dictation-toolbox/dragonfly/pull/302#issuecomment-707725057

drmfinlay commented 3 years ago

I think this bug may have been fixed a while ago. Do you still get it @LexiconCode?

LexiconCode commented 3 years ago

I think this bug may have been fixed a while ago. Do you still get it @LexiconCode?

Yes the issue looks like it's been resolved.