我正在尝试在googles API上测试实时流语音转文本。 但是,据我所知,在没有编辑任何内容的情况下,脚本停止了工作,现在出现此错误: 'SpeechClient'对象没有属性'streamingRecognize'
我尝试在Visual Studio 2019中从头开始创建具有新密钥的新服务帐户,设置一个新项目,尝试了类似的代码但存在相同的错误。
key_file = "key.json"
from google.cloud import speech_v1p1beta1 as speech
from google.cloud.speech_v1p1beta1 import enums
from google.cloud.speech_v1p1beta1 import types
import pyaudio
from six.moves import queue
class ResumableMicrophoneStream:
for response in responses:
if get_current_time() - stream.start_time > STREAMING_LIMIT:
stream.start_time = get_current_time()
break
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
result_seconds = 0
result_nanos = 0
if result.result_end_time.seconds:
result_seconds = result.result_end_time.seconds
if result.result_end_time.nanos:
result_nanos = result.result_end_time.nanos
stream.result_end_time = int((result_seconds * 1000)
+ (result_nanos / 1000000))
corrected_time = (stream.result_end_time - stream.bridging_offset
+ (STREAMING_LIMIT * stream.restart_counter))
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
if result.is_final:
sys.stdout.write(GREEN)
sys.stdout.write('\033[K')
sys.stdout.write(str(corrected_time) + ': ' + transcript + '\n')
stream.is_final_end_time = stream.result_end_time
stream.last_transcript_was_final = True
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r'\b(exit|quit)\b', transcript, re.I):
sys.stdout.write(YELLOW)
sys.stdout.write('Exiting...\n')
stream.closed = True
break
else:
sys.stdout.write(RED)
sys.stdout.write('\033[K')
sys.stdout.write(str(corrected_time) + ': ' + transcript + '\r')
stream.last_transcript_was_final = False
def main():
client = speech.SpeechClient(key_file)
print(client)
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=SAMPLE_RATE,
language_code='da-DK',
max_alternatives=1)
streaming_config = speech.types.StreamingRecognitionConfig(
config=config,
interim_results=True)
mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print(mic_manager.chunk_size)
sys.stdout.write(YELLOW)
sys.stdout.write('\nListening, say "Quit" or "Exit" to stop.\n\n')
sys.stdout.write('End (ms) Transcript Results/Status\n')
sys.stdout.write('=====================================================\n')
with mic_manager as stream:
while not stream.closed:
sys.stdout.write(YELLOW)
sys.stdout.write('\n' + str(
STREAMING_LIMIT * stream.restart_counter) + ': NEW REQUEST\n')
stream.audio_input = []
audio_generator = stream.generator()
requests = (speech.types.StreamingRecognizeRequest(
audio_content=content)for content in audio_generator)
# [this is where the error appears, during the call of the client]
responses = client.streamingRecognize(streaming_config,
requests)
# Now, put the transcription responses to use.
listen_print_loop(responses, stream)
if stream.result_end_time > 0:
stream.final_request_end_time = stream.is_final_end_time
stream.result_end_time = 0
stream.last_audio_input = []
stream.last_audio_input = stream.audio_input
stream.audio_input = []
stream.restart_counter = stream.restart_counter + 1
if not stream.last_transcript_was_final:
sys.stdout.write('\n')
stream.new_stream = True
if __name__ == '__main__':
main()
我希望能够连续输出成绩单-这是我以前得到的。但是,我现在收到此错误:'SpeechClient'对象没有属性'streamingRecognize'。
如果我写此错误,则会包含此消息: 呼叫后打印(客户端):
google.cloud.speech_v1p1beta1.SpeechClient对象位于0x000001D302474D08 (对我来说这没有意义)