【问题标题】:Something wrong with ALSA configurations cant figure out whatALSA 配置有问题无法弄清楚是什么
【发布时间】:2021-07-10 10:06:12
【问题描述】:

我使用的是 Ubuntu 20。我输入了 sudo nano /usr/share/alsa/alsa.conf 并得到以下输出:output of the above command 我不知道它是否有问题,但是每当我尝试从 Pycharm 用 Python 运行语音识别程序时,都会出现以下错误: errors while running the code

我的代码如下:

import wikipedia
import speech_recognition as sr
import tkinter.messagebox
n=0
window= tkinter.Tk()
while True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print('Speak Anything...')
        audio = r.listen(source)

        try:
            text = r.recognize_google(audio,language= 'En', show_all=True)
            if text=="Stop":
                break
            else:
                window.geometry("700x600")
                answer = wikipedia.summary(text)
                label1 = window.Label(window, justify="LEFT",compound="CENTER",padx=10,text=answer, font='times 15 bold')
                label1.pack()
                window.after(50000, lambda: window.destroy())
                window.mainloop()
        finally:
            answer = 'Sorry we cannot hear you.'
            print(answer)

【问题讨论】:

  • 您是否尝试过使用文档中的示例代码,但仍然报错??
  • 是的,我有。代码应该可以工作;它只是由于某种原因无法获得音频输入......
  • 已经试过了。它没有帮助......
  • python版本??问题可能出在 alsa 设备上

标签: python ubuntu pycharm speech-recognition alsa


【解决方案1】:

即使有这些错误,代码也能正常工作。问题是它有点太慢了。 我对代码做了一些小的改动。所以,这是有效的代码:

import wikipedia
from tkinter import *
import speech_recognition as sr

while True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening.....")
        audio = r.listen(source)
        try:
            print("Recognizing....")
            text = r.recognize_google(audio)
            print('You said: ' + text)
            if text == "stop":
                print("Program will exit.")
                break
            else:
                window = Tk()
                window.geometry("700x600")
                answer = wikipedia.summary(text, sentences = 5)
                print("Answer from Wikipedia:")
                print(answer)
                label1 = Label(window, justify=LEFT, wraplength=650, compound=CENTER, padx=10, text=answer, font='times 15 bold')
                label1.pack()
                window.after(500000, lambda: window.destroy())
                mainloop()

        except Exception as e:
            print(e)
            answer = "Sorry we can't hear you"
            print(answer)

谢谢!!:)

【讨论】:

  • 使用下面的 r.adjust_for_ambient_noise(source) 声明它会顺利运行!!
  • 谢谢你,Bhavya!!
猜你喜欢
  • 1970-01-01
  • 2018-05-16
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多