pythonaudiospeech-recognitionspeaker

Python: Get system audio in speech recognition instead of microphone


I am working on speech recognition in python, but it is only getting the input from Micropohone. How is it possible to give the audio from speakers as input to the speech recognition library?

The piece of code is given below:

import speech_recognition as sr
with sr.Microphone() as source:   # using microphone here, would like to use speaker instead
    print("Talk Something...")
    audio=r.listen(source)
    print("Time Over...")

import time
try:
    t1=time.time()
    print("Text: "+r.recognize_google(audio))   # prints after converting speech to text
    t2=time.time()
    print("T2-T1: ", t2-t1)
except:
    print("Didn't understand the audio")

I have been struggling here for so long and any help will be much appreciated. Thanks!


Solution

  • You can configure device index as in docs:

    import speech_recognition as sr
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
    

    If LINEIN is not available as a separate input, you might just configure it as a recording source in audio properties.