示例#1
0
    def __init__(self,
                 notifier,
                 rate=16000,
                 wav_dir=None,
                 model=os.path.join(_MODEL_DIR, 'model')):
        """
        @see AudioInput.__init__()

        :type  rate:
        :param rate:
            The override for the rate, if not the model's one.
        :type  wav_dir:
        :param wav_dir:
            Where to save the wave files, if anywhere.
        :type  model:
        :param model:
            The path to the Vosk model file.
        """
        # Load in and configure the model.
        if not os.path.exists(model):
            raise IOError("Not found: %s" % (model, ))
        LOG.info("Loading model from %s, this could take a while", model)
        SetLogLevel(1 if LOG.getLogger().getEffectiveLevel() >= 20 else 2)
        self._model = Model(model)
        self._recognizer = KaldiRecognizer(self._model, rate)
        LOG.info("Model loaded")

        # Wen can now init the superclass
        super(VoskInput, self).__init__(notifier,
                                        format=pyaudio.paInt16,
                                        channels=1,
                                        rate=rate,
                                        wav_dir=wav_dir)

        # Where we put the results
        self._results = []
示例#2
0
文件: dexter.py 项目: iamsrp/dexter
def main(log_level=None, config=None):
    """
    Dexter is a personal assistant which responds to natural language for its
    commands.
    """
    # Set the log level, if supplied
    if log_level is not None:
        try:
            LOG.getLogger().setLevel(int(log_level))
        except:
            LOG.getLogger().setLevel(log_level.upper())

    # Load in any configuration
    if config is not None:
        try:
            with open(config) as fh:
                configuration = json.load(fh)
        except Exception as e:
            LOG.fatal("Failed to parse config file '%s': %s" % (config, e))
            sys.exit(1)
    else:
        configuration = CONFIG

    # Handle environment variables in the component kwargs, in the form of
    # "${VARNAME}". This isn't overly pretty, but it works.
    for typ in configuration['components']:
        # We might have kwargs for all the components
        for component in configuration['components'][typ]:
            (which, kwargs) = component
            if kwargs is None:
                continue

            # Look at all the kwargs which we have and check for environment
            # variables in the value names.
            for name in kwargs:
                value = kwargs[name]
                try:
                    while True:
                        # Pull out the variable name, if it exists
                        start = value.index('${')
                        end = value.index('}', start)
                        varname = value[start + 2:end]
                        varvalue = os.environ.get(varname, '')

                        # Special handling for some variables
                        if not varvalue:
                            # These are not always set in the environment but
                            # people tend to expect it to be, so we are nice and
                            # provide them
                            if varname == "HOSTNAME":
                                varvalue = socket.gethostname()
                            elif varname == "USER":
                                varvalue = getpass.getuser()

                        # And replace it
                        value = (value[:start] + varvalue + value[end + 1:])
                except:
                    # This means we failed to find the opening or closing
                    # varname container in the string, so we're done
                    pass

                # If we changed it then save it back in
                if value != kwargs[name]:
                    LOG.info(
                        "Expanded component %s:%s:%s argument from '%s' to '%s'"
                        % (typ, which, name, kwargs[name], value))
                    kwargs[name] = value

    # And spawn it
    dexter = Dexter(configuration)
    dexter.run()