【问题标题】:Google speech recognition API not listening谷歌语音识别 API 不听
【发布时间】:2017-06-30 07:21:15
【问题描述】:

我正在使用 Google Speech API 尝试以下语音识别代码。

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr

# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# Speech recognition using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e)) 

但我只得到这个。

jobin@jobin-Satellite-A665:~/scr$ python3 scr.py 
Say something!

即使我说了什么,什么也没有发生。

我没有外接麦克风。我认为这个脚本可以与我笔记本电脑的内置麦克风一起使用。

我已经测试了我笔记本电脑的麦克风here。它工作正常。

我错过了什么吗?

【问题讨论】:

    标签: python python-3.x speech-recognition google-speech-api


    【解决方案1】:

    您可以通过运行以下命令来测试 pyAudio 是否正在找到您的麦克风:

    """PyAudio example: Record a few seconds of audio and save to a WAVE file."""
    
    import pyaudio
    import wave
    
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = 5
    WAVE_OUTPUT_FILENAME = "output.wav"
    
    p = pyaudio.PyAudio()
    
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    
    print("* recording")
    
    frames = []
    
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
    
    print("* done recording")
    
    stream.stop_stream()
    stream.close()
    p.terminate()
    
    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    

    并播放生成的output.wav 文件。

    一旦你确信你得到了一些音频,我会在原始代码中添加几个打印语句来定位你得到了多远,即:

    print("Audio captured!") # before trying to recognise see if you have something
    

    print('Recognition Ended')  # at the end of the script
    

    这会让你看到你已经走了多远。

    接下来您可能需要找出默认音频设备:

    import pyaudio
    print(pyaudio.pa.get_default_input_device())
    

    这应该告诉你默认输入设备,这是我机器上的一个,所以用这个:

    with sr.Microphone(1) as source: # Specify which input device to use
        r.adjust_for_ambient_noise(source, 1) # Adjust for ambient
        print("Say something!")
        audio = r.listen(source, 2)  # 2 Second time out
    print('Done Listening sample size =', len(audio.frame_data))
    

    【讨论】:

    • 我试过这个,我可以听到 output.wav 文件。所以 pyaudio 检测到我的麦克风。那我的代码可能有什么问题?
    • @jophab 添加了更多建议。
    • 什么是len(音频)?以前没有使用过音频变量。
    • 好的,所以 len 不是 AudioData 的有效方法,所以摆脱它。看起来您可能需要在 print('Say something') 之前更改 r.energy_threshold 或设置 r.adjust_for_ambient_noise(source, 1) 您还可以为 listen 调用添加超时。
    • 谢谢史蒂夫 :)。现在它的作品。你也可以添加这个来回答吗?它可能对未来的访问者有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多