When I started writing the code to create my voice assistant, I had a problem using the Speech Recognition
library for Python.
Error: IndentationError: unindent does not match any outer indentation level
Here is the part of the code where the error occurs: r = sr.Recognizer()
Here is the complete code for now.
import os
import sys
import time
import speech_recognition as sr
from fuzzywuzzy import fuzz
import pyttsx3
import datetime
opts = {
"alias": ('jarvis', 'jervis', 'jirves', 'jurves', 'jervas'
'jalvis', 'jelvis', 'jilves', 'julves', 'jelvas'),
"tbr": ('say', 'tell', 'show', 'how much', 'will', 'would',
'could', 'may', 'can', 'must', 'the'),
"cmds": {
"ctime": ('current time', 'time', 'what time is it'),
"radio": ('turn on the music', 'turn on the radio',
'play radio'),
"jokes": ('tell a joke', 'do you know any jokes',
'make me laugh')
}
}
# functions
def speak(what):
print(what)
speak_engine.say(what)
speak_engine.runAndWait()
speak_engine.stop()
#start
r = sr.Recognizer()
m = sr.Microphone(device_index=1)
with m as source:
r.adjust_for_ambient_noise(source)
speak_engine = pyttsx3.init()
speak("Good day my master")
speak("Jarvis is listening")
# stop_listening = r.listen_in_background(m, callback)
# while True:
# time.sleep(0.1) # infinite loop
all libraries are installed ok with pip, all libraries are tested
Answer 1
IndentationError
– an error that occurs when the indentation is incorrect in the code.
Here’s the corrected code:
import os
import sys
import time
import speech_recognition as sr
from fuzzywuzzy import fuzz
import pyttsx3
import datetime
opts = {
"alias": ('jarvis', 'jervis', 'jirves', 'jurves', 'jervas'
'jalvis', 'jelvis', 'jilves', 'julves', 'jelvas'),
"tbr": ('say', 'tell', 'show', 'how much', 'will', 'would',
'could', 'may', 'can', 'must', 'the'),
"cmds": {
"ctime": ('current time', 'time', 'what time is it'),
"radio": ('turn on the music', 'turn on the radio',
'play radio'),
"jokes": ('tell a joke', 'do you know any jokes',
'make me laugh')
}
}
# functions
def speak(what):
print(what)
speak_engine.say(what)
speak_engine.runAndWait()
speak_engine.stop()
#start
r = sr.Recognizer()
m = sr.Microphone(device_index=1)
with m as source:
r.adjust_for_ambient_noise(source)
speak_engine = pyttsx3.init()
speak("Good day my master")
speak("Jarvis is listening")
#stop_listening = r.listen_in_background(m, callback)
#while True:
# time.sleep(0.1) # infinite loop