【问题标题】:Microphone on windows not working for speech_recognition by using pythonwindows上的麦克风无法使用python进行speech_recognition
【发布时间】:2021-08-24 14:11:29
【问题描述】:

我正在用 python 开发一个桌面虚拟助手。 它的部分需要输入我的声音并给出相应的结果。

但由于某些原因,它没有接受我的声音输入。 看起来它没有访问我的麦克风的权限。我试过了 在 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 推荐的

还是没有结果

到目前为止的代码:

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")

【问题讨论】:

  • 在终端运行代码会发生什么?因为这就是 VSCode 所做的一切。运行 code script.py 只会在编辑器中打开文件,不会运行与你的麦克风相关的任何内容。
  • 由于某种原因它甚至没有打开我的文件。相反,它创建了几个路径中使用的文件夹名称的新文件和一个新的 python 文件
  • 因为你需要在文件路径周围加上引号,因为它有空格
  • 这也不行
  • 我尝试了其中一些,但同样的事情发生在他们身上

标签: python speech-recognition


【解决方案1】:

好的,我终于找到了解决这个问题的方法 有两个问题:

  1. 能量阈值很高,我的笔记本电脑的内置麦克风无法捕捉到我的音频
  2. 背景噪音也是个问题

修复: 添加r.adjust_for_ambient_noise(source) 并将能量阈值调整为 150 解决了我的问题

【讨论】:

    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多