SpeechRecognition Documentation

repository·master·Indexed 26 days ago

https://github.com/uberi/speech_recognition

A Python library for performing speech recognition with support for both online and offline engines. It integrates with various APIs and engines including Google Speech Recognition, OpenAI Whisper, Vosk, CMU Sphinx, Microsoft Azure Speech, and IBM Speech to Text. The library provides tools for microphone input via PyAudio, audio file transcription, and ambient noise calibration.

Tokens
8.1K
Snippets
27
Records
56
Agent score
94%

What's inside SpeechRecognition

  1. Supported speech recognition engines and APIs

    master

    SpeechRecognition supports a wide variety of online and offline engines:

    Offline Engines:

    • CMU Sphinx
    • Snowboy Hotword Detection
    • Tensorflow
    • Vosk API
    • OpenAI whisper

    Online APIs:

    • Google Speech Recognition
    • Google Cloud Speech API
    • Wit.ai
    • Microsoft Azure Speech
    • Houndify API
    • IBM Speech to Text
    • OpenAI Whisper API (including OpenAI compatible self-hosted endpoints like vLLM or Ollama)
    • Groq Whisper API
    • Cohere Transcribe API
  2. Rebuild FLAC executables for Linux

    master

    The library includes flac-linux-x86 and flac-linux-x86_64 binaries built from FLAC 1.3.2 source code using Manylinux to ensure wide distribution compatibility. To rebuild them on a Debian-like system, use Docker to build static binaries inside Manylinux images.

    # build FLAC inside the Manylinux i686 Docker image
    tar xf flac-1.3.2.tar.xz
    sudo docker run --tty --interactive --rm --volume "$(pwd):/root" quay.io/pypa/manylinux1_i686:latest bash
        cd /root/flac-1.3.2
        ./configure LDFLAGS=-static # compiler flags to make a static build
        make
    exit
    cp flac-1.3.2/src/flac/flac ../speech_recognition/flac-linux-x86 && sudo rm -rf flac-1.3.2/
    
    # build FLAC inside the Manylinux x86_64 Docker image
    tar xf flac-1.3.2.tar.xz
    sudo docker run --tty --interactive --rm --volume "$(pwd):/root" quay.io/pypa/manylinux1_x86_64:latest bash
        cd /root/flac-1.3.2
        ./configure LDFLAGS=-static # compiler flags to make a static build
        make
    exit
    cp flac-1.3.2/src/flac/flac ../speech_recognition/flac-linux-x86_64 && sudo rm -r flac-1.3.2/
  3. Install SpeechRecognition

    master

    Install the SpeechRecognition library using pip. Ensure you have met all system requirements before installation.

    To install via pip:

    pip install SpeechRecognition

    Alternatively, you can install from the source distribution:

    1. Download the source distribution from PyPI.
    2. Extract the archive.
    3. Run the following command in the extracted folder:
    python -m pip install .
  4. Build high-accuracy French language data

    master

    To improve French recognition accuracy in PocketSphinx (at the cost of higher RAM and disk usage), replace the default files in /speech_recognition/pocketsphinx-data/fr-FR/ with a converted version of fr.lm.gmp.

    Steps:

    1. Download fr.lm.gmp from the Sphinx French language model source.
    2. Convert from DMP format to ARPA format using sphinx_lm_convert: sphinx_lm_convert -i fr.lm.gmp -o french.lm.bin (Note: The documentation implies this command produces an ARPA file despite the .bin extension in the example snippet).
    3. Replace the existing /speech_recognition/pocketsphinx-data/fr-FR/language-model.lm.bin with your new file.
    sphinx_lm_convert -i fr.lm.gmp -o french.lm.bin
  5. Build PocketSphinx-Python from source

    master

    If you need to build pocketsphinx from source, follow the instructions for your operating system:

    Debian-based Linux (Ubuntu, Mint, etc.)

    1. Install dependencies: sudo apt-get install python3 python3-all-dev python3-pip build-essential swig git libpulse-dev libasound2-dev
    2. Install via pip: pip3 install pocketsphinx

    macOS

    1. Install dependencies via Homebrew: brew install swig git python3
    2. Install via pip: pip install pocketsphinx Note: If import errors occur, try brew link --overwrite python.

    Other POSIX systems

    1. Install Python, Pip, SWIG, and Git using your package manager.
    2. Install via pip: pip install pocketsphinx

    Windows

    1. Install Python, Pip, SWIG, and Git.
    2. Add the folders containing the Python, SWIG, and Git binaries to your PATH environment variable.
    3. Reboot your system.
    4. Clone the repository (do not use ZIP downloads): git clone --recursive --depth 1 https://github.com/cmusphinx/pocketsphinx-python
    5. Install: python setup.py install (run this inside the cloned folder)
  6. Install PyAudio for microphone input

    master

    To use the Microphone class, you must install PyAudio (version 0.2.11+). If not installed, attempting to instantiate a Microphone object will raise an AttributeError.

    # Windows
    pip install SpeechRecognition[audio]
    
    # Debian-derived Linux (Ubuntu, Mint)
    sudo apt-get install python-pyaudio python3-pyaudio
    
    # If repository version is too old on Linux
    sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install SpeechRecognition[audio]
    
    # OS X
    brew install portaudio
    pip install SpeechRecognition[audio]
    
    # Other POSIX
    sudo apt-get install portaudio19-dev python-all-dev
    pip install SpeechRecognition[audio]
  7. Install additional language packs for PocketSphinx

    master

    By default, PocketSphinx in SpeechRecognition only supports US English (en-US). To use other languages like French (fr-FR), Mandarin Chinese (zh-CN), or Italian (it-IT), you must download their language pack ZIP archives and extract them directly into the speech_recognition module install directory.

    You can find your module install directory by running:

    python -c "import speech_recognition as sr, os.path as p; print(p.dirname(sr.__file__))"

    Once extracted, you can use the language by passing the IETF language tag to the language parameter of recognizer_instance.recognize_sphinx().

    #!/usr/bin/env bash
    SR_LIB=$(python -c "import speech_recognition as sr, os.path as p; print(p.dirname(sr.__file__))")
    sudo apt-get install --yes unzip
    sudo unzip -o fr-FR.zip -d "$SR_LIB"
    sudo chmod --recursive a+r "$SR_LIB/pocketsphinx-data/fr-FR/"
    sudo unzip -o zh-CN.zip -d "$SR_LIB"
    sudo chmod --recursive a+r "$SR_LIB/pocketsphinx-data/zh-CN/"
    sudo unzip -o it-IT.zip -d "$SR_LIB"
    sudo chmod --recursive a+r "$SR_LIB/pocketsphinx-data/it-IT/"
  8. Install PocketSphinx for Sphinx recognition

    master

    Required for recognizer_instance.recognize_sphinx. It is recommended to use bundled wheel packages or build from source rather than using outdated package repository versions.

    pip install SpeechRecognition[pocketsphinx]
  9. Build high-accuracy Mandarin Chinese language data

    master

    To achieve better Mandarin Chinese recognition accuracy, follow these steps to prepare the language model from zh_broadcastnews_64000_utf8.DMP. To maximize accuracy, skip the pruning step.

    Steps:

    1. Download zh_broadcastnews_64000_utf8.DMP from the Sphinx Mandarin language model source.
    2. Convert from DMP to ARPA format: sphinx_lm_convert -i zh_broadcastnews_64000_utf8.DMP -o chinese.lm -ofmt arpa
    3. (Optional) Prune the model (skip this for maximum accuracy): prune-lm --threshold=4e-8 chinese.lm chinese.lm
    4. Convert from ARPA format to Sphinx binary format: sphinx_lm_convert -i chinese.lm -o chinese.lm.bin
    5. Replace /speech_recognition/pocketsphinx-data/zh-CN/language-model.lm.bin with chinese.lm.bin.
    sphinx_lm_convert -i zh_broadcastnews_64000_utf8.DMP -o chinese.lm -ofmt arpa
    # Skip pruning for better accuracy
    sphinx_lm_convert -i chinese.lm -o chinese.lm.bin