簡體   English   中英

Google語音識別API無法收聽

[英]Google speech recognition API not listening

我正在嘗試使用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!

即使我說了什么,也沒有任何反應。

我沒有外接麥克風。 我認為此腳本可以與筆記本電腦的內置麥克風配合使用。

在這里測試了筆記本電腦的麥克風。 一切正常。

我有什么想念的嗎?

您可以通過運行以下命令來測試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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM