Running Python Speech Recognition in background

Implementing the listen_in_background() function

Ecnivs
Nov 12, 2022

Introduction

Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an implementation for the listen_in_background() function of python’s speech recognition module.

Installing SpeechRecognition

SpeechRecognition is compatible with Python 2.6, 2.7 and 3.3+, but requires some additional installation steps for Python 2. For this article, I’ll assume you are using Python 3.3+.

You can install SpeechRecognition from the terminal with pip:

pip install SpeechRecognition

Down below is the Implementation

import speech_recognition as sr
from time import sleep

def callback(recognizer, audio):
try:
print(recognizer.recognize_google(audio)) #Output
except sr.UnknownValueError:
pass

recognizer = sr.Recognizer()
mic = sr.Microphone(0)

with mic as source:
recognizer.adjust_for_ambient_noise(source)

stop_listening = recognizer.listen_in_background(mic, callback)


while True:
sleep(0.1)

--

--

Ecnivs
Ecnivs

Written by Ecnivs

Speaks fluent code and sarcasm.

No responses yet