为什么SpeechRecognition无法检测到麦克风并产生错误

问题描述

我正在用树莓派3b做一个像Siri这样的gTTS语音助手。谷歌文本到语音的作品,但当我使用SpeechRecognition和Pyaudio作为输入时,我得到了错误。我已经意识到我可以使此代码更简单,并且可以完成相同的操作,但是我不需要更改它。这是代码

import os
from gtts import gTTS
import time
import speech_recognition as sr

text_to_read = "Hello my name is Alex. What's your name? "


language = 'en'
slow_audio_speed = False
filename = '1.mp3'
filename2 = '2.mp3'
filename3 = '3.mp3'
filename4 = '4.mp3'
filename5 = '5.mp3'
filename6 = '6.mp3'
filename7 = '7.mp3'
filename8 = '8.mp3'
 filename9 = '9.mp3'
filename10 = '10.mp3'
filename11 = '11.mp3'
filename12 = '12.mp3'
filename13 = '13.mp3'

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

def reading_from_string():
    audio_created = gTTS(text=text_to_read,lang=language,slow=slow_audio_speed)
    audio_created.save(filename)


    os.system(f'omxplayer --no-keys {filename}')
def reading_from_string2():
    audio_created2 = gTTS(text=text_to_read2,slow=slow_audio_speed)
    audio_created2.save(filename2)

    os.system(f'omxplayer --no-keys {filename2}')
def reading_from_string3():
    audio_created3 = gTTS(text=text_to_read3,slow=slow_audio_speed)
    audio_created3.save(filename3)

    os.system(f'omxplayer --no-keys {filename3}')

def reading_from_string4():
    audio_created4 = gTTS(text=text_to_read4,slow=slow_audio_speed)
    audio_created4.save(filename4)

    os.system(f'omxplayer --no-keys {filename4}')

def reading_from_string5():
    audio_created5 = gTTS(text=text_to_read5,slow=slow_audio_speed)
    audio_created5.save(filename5)

    os.system(f'omxplayer --no-keys {filename5}')

def reading_from_string6():
    audio_created6 = gTTS(text=text_to_read6,slow=slow_audio_speed)
    audio_created6.save(filename6)

    os.system(f'omxplayer --no-keys {filename6}')
def reading_from_string11():
    audio_created11 = gTTS(text=text_to_read11,slow=slow_audio_speed)
    audio_created11.save(filename11)

    os.system(f'omxplayer --no-keys {filename11}')

def reading_from_string12():
    audio_created12 = gTTS(text=text_to_read12,slow=slow_audio_speed)
    audio_created12.save(filename12)

    os.system(f'omxplayer --no-keys {filename12}')
     
def reading_from_string13():
    audio_created13 = gTTS(text=text_to_read13,slow=slow_audio_speed)
    audio_created13.save(filename13)

    os.system(f'omxplayer --no-keys {filename13}')

f=open('name.txt','r+')
x=f.read()
 f.close()
reading_from_string()
answer = get_audio()
if answer == x :
    text_to_read11 = "Hello"
    reading_from_string11()
    f=open('name.txt','r')
    g=f.read()
    text_to_read12 = g
    reading_from_string12()
    f.close()
    text_to_read13 = 'Good to see you again'
    reading_from_string13()

else:
    name=get_audio()
    f=open('name.txt','w')
    f.write(name)
    f.close()
    text_to_read2 = 'Hello'
    reading_from_string2()
    text_to_read3 = (name)
    time.sleep(0.5)
    reading_from_string3()
    text_to_read4= 'What do you like'
    reading_from_string4()
    text_to_read6 = get_audio()
    y=open('likes.txt','w')
    x=text_to_read6
    y.write(x)
    y.close()
    text_to_read5 = 'storing memory'
    reading_from_string5()
    time.sleep(1)
    reading_from_string6()

这是错误

Audio codec mp3float channels 1 samplerate 24000 bitspersample 16
Subtitle count: 0,state: off,index: 1,delay: 0
have a nice day ;)
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm,hwParams,&alsaPeriodFrames,&dir )' Failed in 'src/hostapi/alsa/pa_linux_alsa.c',line: 924
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm,line: 924
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.front.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.side
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround21
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround21
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround40.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround40
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround41
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround50
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround51.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround51
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.surround71.0:CARD=0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM surround71
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM iec958
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM spdif
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'cards.bcm2835_hdmi.pcm.iec958.0:CARD=0,AES3=2'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM spdif
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find deFinition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM bluealsa
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm,line: 924
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
Exception: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
Audio codec mp3float channels 1 samplerate 24000 bitspersample 16
Subtitle count: 0,delay: 0
have a nice day ;)
Traceback (most recent call last):
  File "/home/pi/Desktop/coding/Alex.py",line 111,in <module>
    reading_from_string12()
  File "/home/pi/Desktop/coding/Alex.py",line 88,in reading_from_string12
    slow=slow_audio_speed)
  File "/home/pi/.local/lib/python3.7/site-packages/gtts/tts.py",line 120,in __init__
    assert text,'No text to speak'
AssertionError: No text to speak

我正在使用TKGOU UM6 USB麦克风。 你知道怎么了吗?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...