简体   繁体   中英

Microphone on windows not working for speech_recognition by using python

I am working on a virtual assistant for desktop in python. the part of it requires to take input of my voice and give corresponding results.

But for some reasons,it is not taking input of my voice. it looks like it does not have the permissions to access my microphone. I tried using code F:\programming\development and other\python programming\projects\jarvis for desktop\project.py (project.py is the name of file i am working with) command in windows power shell which was recommended at https://github.com/MicrosoftDocs/live-share/issues/3254

still no results

Code Made so far:

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)


def wish():
    hour = int(datetime.datetime.now().hour)
    if hour >= 5 and hour <= 12:
        speak("Good Morning Mister Abhinav Agrawal")

    elif hour > 12 and hour < 5:
        speak("good Afternoon Mister Abhinav Agrawal")

    else:
        speak("Good evening Mister Abhinav Agrawal")


def speak(str):

    engine.say(str)
    engine.runAndWait()


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)
        print("Say that again please...")
        return "None"
    return query


if __name__ == "__main__":
    wish()
    leave = False
    while True:
        query = takeCommand().lower()
        if 'quit' in query:
            break
        elif 'wikipedia' in query:
            query = query.replace('wikipedia', "")
            result = wikipedia.summary(query, sentences=2)
            speak(result)
            print(result+"\n")

ok so I finally found the solution to this problem there were two problems:

  1. the energy threshold was high and my laptop's inbuilt mic was not able to catch my audio
  2. the noise from background was also a problem

fix: adding r.adjust_for_ambient_noise(source) and adjusitng energy threshold to 150 fixed my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM