0
votes

Speech Recognition not working in python

r = sr.Recognizer()
with sr.Recognizer() as source:
    print("Listining...")
    r.pause_threshold = 1
    audio = r.listen(source)

error :

File "f:/F.R.I.D.A.Y/main.py", line 36, in takeCommand with sr.Recognizer() as source:File "C:\Users\kakra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\speech_recognition_init_.py", line 51, in enter raise NotImplementedError("this is an abstract class") NotImplementedError: this is an abstract class

2

2 Answers

0
votes

Source should be set to some input device/file:

Instead of doing with sr.Recognizer() as source you need to do either of these:

  1. Get input from a audio file
audio_file = sr.AudioFile('audio.wav')
with audio_file as source:

Reference

  1. Get input from specific microphone
mic_list = sr.Microphone.list_microphone_names()
  
# Iterate and select whichever mic you want from system
mic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"
for i, microphone_name in enumerate(mic_list):
    if microphone_name == mic_name:
        device_id = i

# Or Simply for testing, use any hardcoded value 
# (verify value according to total microphones present in system)
device_id = 0 
  
with sr.Microphone(device_index = device_id) as source:

Reference

  1. Get input from default microphone
with sr.Microphone() as source:
0
votes

You can try this method

This is a simple example of converting audio to text

import speech_recognition as sr

r = sr.Recognizer()  

while True:    
    with sr.Microphone() as source:

        r.adjust_for_ambient_noise(source, duration=0.2)

        audio = r.listen(source)

        Text = r.recognize_google(audio)
        Text = Text.lower()

        print(Text)

source should be a source for getting information like Microphone

You can see more examples here