2017-02-12 103 views
1

我正在使用Google Speech API嘗試以下語音識別代碼。Google語音識別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)) 

但我得到的只是這個。

[email protected]:~/scr$ python3 scr.py 
Say something! 

即使我說了些什麼,沒有任何反應。

我沒有外接麥克風。我認爲這個腳本將與我的筆記本電腦的內置麥克風一起工作。

我測試了我的筆記本電腦的麥克風here。它工作正常。

我錯過了什麼?

+1

不錯的標題...;) – BlackBear

回答

2

你可以測試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)) 
+0

我試過這個,我可以聽到output.wav文件。所以pyaudio檢測我的麥克風。那麼我的代碼可能是什麼問題? – jophab

+0

@jophab添加了一些更多建議。 –

+0

len(音頻)是什麼?音頻變量以前不用。 – jophab