大佬教程收集整理的这篇文章主要介绍了谷歌云语音到文本 python 示例不起作用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
from __future__ import division
import re,os
import sys
from Google.cloud import speech
import pyaudio
from six.moves import queue
os.environ['Google_APPliCATION_CREDENTIALS'] = '/home/davID/Desktop/python/gtest/GoogleserviceKey.Json'
# Audio recording parameters
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
class Microphonestream(object):
"""Opens a recording stream as a generator yIElding the audio chunks."""
def __init__(self,rate,chunk):
self._rate = rate
self._chunk = chunk
# Create a thread-safe buffer of audio data
self._buff = queue.Queue()
self.closed = True
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,# The API currently only supports 1-chAnnel (mono) audio
chAnnels=1,rate=self._rate,input=True,frames_per_buffer=self._chunk,# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network @R_618_10613@ests,etc.
stream_callBACk=self._fill_buffer,)
self.closed = false
return self
def __exit__(self,type,value,traceBACk):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the clIEnt's
# streaming_recognize method will not block the process termination.
self._buff.put(NonE)
self._audio_interface.terminate()
def _fill_buffer(self,in_data,frame_count,time_info,status_flags):
"""ConTinuously collect data from the audio stream,into the buffer."""
self._buff.put(in_data)
return None,pyaudio.paConTinue
def generator(self):
while not self.closed:
# Use a blocking get() to ensure there's at least one chunk of
# data,and stop iteration if the chunk is None,inDicaTing the
# end of the audio stream.
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=falsE)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yIEld b"".join(data)
def Listen_print_loop(responses):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provIDed by the server.
Each response may contain multiple results,and each result may contain
multiple alternatives; for details,see . Here we
print only the transcription for the top alternative of the top result.
In this case,responses are provIDed for interim results as well. If the
response is an interim one,print a line Feed at the end of it,to allow
the next result to overwrite it,until the response is a final one. For the
final one,print a newline to preserve the finalized transcription.
"""
num_chars_printed = 0
for response in responses:
if not response.results:
conTinue
# The `results` List is consecutive. For streaming,we only care about
# the first result being consIDered,since once it's `is_final`,it
# moves on to consIDering the next utterance.
result = response.results[0]
if not result.alternatives:
conTinue
# display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
# display interim results,but with a carriage return at the end of the
# line,so subsequent lines will overwrite them.
#
# If the prevIoUs result was longer than this one,we need to print
# some extra spaces to overwrite the prevIoUs result
overwrite_chars = " " * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + "\r")
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
# 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):
print("ExiTing..")
break
num_chars_printed = 0
def main():
# See http://g.co/cloud/speech/docs/languages
# for a List of supported languages.
language_code = "en-US" # a BCP-47 language tag
clIEnt = speech.SpeechClIEnt()
config = speech.RecognitionConfig(
enCoding=speech.RecognitionConfig.AudioEnCoding.liNEAR16,sample_rate_hertz=RATE,language_code=language_code,)
streaming_config = speech.StreamingRecognitionConfig(
config=config,interim_results=True
)
with Microphonestream(RATE,CHUNK) as stream:
audio_generator = stream.generator()
@R_618_10613@ests = (
speech.StreamingRecognize@R_618_10613@est(audio_content=content)
for content in audio_generator
)
responses = clIEnt.streaming_recognize(streaming_config,@R_618_10613@ests)
# Now,put the transcription responses to use.
Listen_print_loop(responses)
if __name__ == "__main__":
main()
昨天它起作用了,但由于某种原因现在不起作用,检查后我意识到问题出在线路上
responses = clIEnt.streaming_recognize(streaming_config,@R_618_10613@ests)
主要是,但我不知道出了什么问题。
我也试过 node Js 的例子,它工作得很好,所以问题是因为 python 实现。
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的谷歌云语音到文本 python 示例不起作用全部内容,希望文章能够帮你解决谷歌云语音到文本 python 示例不起作用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。