Skip to main content

Speech recognition extension library

Project description

Build Status Docs Status Join Gitter chat Join Matrix chat

Introduction

Dragonfly is a speech recognition framework for Python that makes it convenient to create custom commands to use with speech recognition software. It was written to make it very easy for Python macros, scripts, and applications to interface with speech recognition engines. Its design allows speech commands and grammar objects to be treated as first-class Python objects.

Dragonfly can be used for general programming by voice. It is flexible enough to allow programming in any language, not just Python. It can also be used for speech-enabling applications, automating computer activities and dictating prose.

Dragonfly contains its own powerful framework for defining and executing actions. It includes actions for text input and key-stroke simulation. This framework is cross-platform, working on Windows, macOS and Linux (X11 only). See the actions sub-package documentation for more information, including code examples.

This project is a fork of the original t4ngo/dragonfly project.

Dragonfly currently supports the following speech recognition engines:

  • Dragon, a product of Nuance. All versions up to 15 (the latest) should be supported. Home, Professional Individual and previous similar editions of Dragon are supported. Other editions may work too

  • Windows Speech Recognition (WSR), included with Microsoft Windows Vista, Windows 7+, and freely available for Windows XP

  • Kaldi, open source (AGPL) and multi-platform.

  • CMU Pocket Sphinx, open source and multi-platform.

Documentation and FAQ

Dragonfly’s documentation is available online at Read the Docs. The changes in each release are listed in the project’s changelog. Dragonfly’s FAQ is available in the documentation here. There are also a number of Dragonfly-related questions on Stackoverflow, although many of them are related to issues resolved in the latest version of Dragonfly.

CompoundRule Usage example

A very simple example of Dragonfly usage is to create a static voice command with a callback that will be called when the command is spoken. This is done as follows:

from dragonfly import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print("Voice command spoken.")

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()                                      # Load the grammar.

To use this example, save it in a command module in your module loader directory or Natlink user directory, load it and then say do something computer. If the speech recognition engine recognized the command, then Voice command spoken. will be printed in the Natlink messages window. If you’re not using Dragon, then it will be printed into the console window.

MappingRule usage example

A more common use of Dragonfly is the MappingRule class, which allows defining multiple voice commands. The following example is a simple grammar to be used when Notepad is the foreground window:

from dragonfly import (Grammar, AppContext, MappingRule, Dictation,
                       Key, Text)

# Voice command rule combining spoken forms and action execution.
class NotepadRule(MappingRule):
    # Define the commands and the actions they execute.
    mapping = {
        "save [file]":            Key("c-s"),
        "save [file] as":         Key("a-f, a/20"),
        "save [file] as <text>":  Key("a-f, a/20") + Text("%(text)s"),
        "find <text>":            Key("c-f/20") + Text("%(text)s\n"),
    }

    # Define the extras list of Dragonfly elements which are available
    # to be used in mapping specs and actions.
    extras = [
        Dictation("text")
    ]


# Create the grammar and the context under which it'll be active.
context = AppContext(executable="notepad")
grammar = Grammar("Notepad example", context=context)

# Add the command rule to the grammar and load it.
grammar.add_rule(NotepadRule())
grammar.load()

To use this example, save it in a command module in your module loader directory or Natlink user directory, load it, open a Notepad window and then say one of mapping commands. For example, saying save or save file will cause the control and S keys to be pressed.

The example aboves don’t show any of Dragonfly’s exciting features, such as dynamic speech elements. To learn more about these, please take a look at Dragonfly’s online docs.

Installation

Dragonfly is a Python package. It can be installed as dragonfly2 using pip:

pip install dragonfly2

The distribution name has been changed to dragonfly2 in order to upload releases to PyPI.org, but everything can still be imported using dragonfly. If you use any grammar modules that include something like pkg_resources.require("dragonfly >= 0.6.5"), you will need to either replace dragonfly with dragonfly2 or remove lines like this altogether.

If you are installing this on Linux, you will also need to install the wmctrl, xdotool and xsel programs. You may also need to manually set the XDG_SESSION_TYPE environment variable to x11.

Please note that Dragonfly is only fully functional in an X11 session on Linux. Input action classes, application contexts and the Window class will not be functional under Wayland. It is recommended that Wayland users switch to X11.

If you have dragonfly installed under the original dragonfly distribution name, you’ll need to remove the old version using:

pip uninstall dragonfly

Dragonfly can also be installed by cloning this repository or downloading it from the releases page and running the following (or similar) command in the project’s root directory:

python setup.py install

If pip fails to install dragonfly2 or any of its required or extra dependencies, then you may need to upgrade pip with the following command:

pip install --upgrade pip

SR engine back-ends

Each Dragonfly speech recognition engine back-end and its requirements are documented separately:

Existing command modules

The related resources page of Dragonfly’s documentation has a section on command modules which lists various sources.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dragonfly2-0.35.0.tar.gz (373.8 kB view details)

Uploaded Source

Built Distribution

dragonfly2-0.35.0-py2.py3-none-any.whl (455.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dragonfly2-0.35.0.tar.gz.

File metadata

  • Download URL: dragonfly2-0.35.0.tar.gz
  • Upload date:
  • Size: 373.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.2 importlib-metadata/4.8.1 keyring/23.1.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for dragonfly2-0.35.0.tar.gz
Algorithm Hash digest
SHA256 71353479142df37d60287d42559f912d0d238affa6cecc3db65364e1f2032c52
MD5 2e06a93d225870c504d9a4e9deff3e4d
BLAKE2b-256 bb02fbd2db370161e765594bec66246185f5c32fac68e6f0f13059df7bd12879

See more details on using hashes here.

File details

Details for the file dragonfly2-0.35.0-py2.py3-none-any.whl.

File metadata

  • Download URL: dragonfly2-0.35.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 455.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.2 importlib-metadata/4.8.1 keyring/23.1.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for dragonfly2-0.35.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dc7b882c9743dc0a3c2a6098a58be46d7fd4608123998aeb6b089e7a2d528ee8
MD5 64287307ec3097aaffb27226785719a4
BLAKE2b-256 0fe0f7bef7133e011043e73477ea3c8620a478d68062b6831d4f055574175ff6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page