Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 谷歌语音识别API不监听_Python_Python 3.x_Speech Recognition_Google Speech Api - Fatal编程技术网

Python 谷歌语音识别API不监听

Python 谷歌语音识别API不监听,python,python-3.x,speech-recognition,google-speech-api,Python,Python 3.x,Speech Recognition,Google Speech Api,我正在使用谷歌语音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 re

我正在使用谷歌语音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!
即使我说了些什么,什么也没发生

我没有外置麦克风。我想这个脚本可以与我的笔记本电脑内置的麦克风配合使用

我已经测试了我笔记本电脑的麦克风。它工作得很好


我遗漏了什么吗?

您可以通过运行以下命令来测试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

这将让你看到你正在走多远

接下来,您可能需要找出哪个是默认的音频设备:

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的有效方法,因此请将其删除。看起来您可能需要更改
r.energy\u threshold
或设置
r.adjust\u for\u ambient\u noise(源代码,1)
打印('Say something')
之前,您还可以为
接听
通话添加超时。谢谢steve:)。它现在起作用了。你能把这个也加到答案上吗?它可能会帮助未来的游客。
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))