简体   繁体   English

windows 上的麦克风无法使用 python 进行语音识别

[英]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.我正在python做一个桌面虚拟助手。它的一部分需要输入我的声音并给出相应的结果。

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我尝试在 windows power shell 中使用code F:\programming\development and other\python programming\projects\jarvis for desktop\project.py (project.py 是我正在使用的文件的名称)命令,在 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修复:添加r.adjust_for_ambient_noise(source)并将能量阈值调整为 150 解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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