Alex Trottier
Alex Trottier

Reputation: 21

Python speech_recognition not detecting audio from microphone

I'm currently looking into building my own home automation system controllable through voice commands. I've learned about the package SpeechRecognition and decided this would be how I interact with my system.

After reading tutorials, and the reference pages from github, and also looking at sample scripts I have come up with the following code:

import speech_recognition as sr
import pyaudio

r=sr.Recognizer()

    #Microphone(device_index=i, sample_rate=48000)
with sr.Microphone( sample_rate=48000) as source:
    print("Say Something!")
    audio=r.listen(source)

with open("microphone-results.wav", "wb") as f:
    f.write(audio.get_wav_data())

When testing my code it runs and displays "Say Something!" however no matter what I say into my microphone, or how long the code runs for nothing happens and no "microphone-results.wav" file is created.

I know my microphone works because I can test it with the command arecord -D plughw:1,0 test.wav and play back the file using aplay test.wav

I'm using Raspberry Pi 3 with the latest update of Rasbian.

Upvotes: 2

Views: 2472

Answers (1)

Fidahussain Shahani
Fidahussain Shahani

Reputation: 11

i also had same problem but i finally came to the solution here is the lines of code for detecting only one frequency sound after the [print("Say something") just paste

r.pause_threshold=1
r.adjust_for_ambient_noise(source,duration=1) 

then you will get the message whatever you speak after 5 to 10 seconds 

Upvotes: 1

Related Questions