本文整理匯總了Python中speech_recognition.AudioData方法的典型用法代碼示例。如果您正苦於以下問題:Python speech_recognition.AudioData方法的具體用法?Python speech_recognition.AudioData怎麽用?Python speech_recognition.AudioData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類speech_recognition
的用法示例。
在下文中一共展示了speech_recognition.AudioData方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: convert
# 需要導入模塊: import speech_recognition [as 別名]
# 或者: from speech_recognition import AudioData [as 別名]
def convert(audio_data):
if isinstance(audio_data, types.GeneratorType):
def generate(audio):
yield BingSpeechAPI.get_wav_header()
for a in audio:
yield a
data = generate(audio_data)
else:
data = BingSpeechAPI.to_wav(audio_data)
audio = sr.AudioData(''.join(data), 16000, 2)
return audio
示例2: test_live
# 需要導入模塊: import speech_recognition [as 別名]
# 或者: from speech_recognition import AudioData [as 別名]
def test_live(message):
speech = sr.AudioData(base64.b64decode(message['data']),message['sample_rate'],message['sample_width'])
value = r.recognize_google(speech,language='ms-MY')
emit('speech_update', {'text': value},broadcast=True)
示例3: recognize_from_api
# 需要導入模塊: import speech_recognition [as 別名]
# 或者: from speech_recognition import AudioData [as 別名]
def recognize_from_api(audio, api, name='API', safe=True, **kwargs):
if not isinstance(audio, sr.AudioData):
with sr.AudioFile(audio) as source:
audio = r.record(source)
try:
return api(audio, **kwargs)
except sr.UnknownValueError as e:
if not safe:
raise e
return "\t%s could not understand audio" % name
except sr.RequestError as e:
if not safe:
raise e
return "\tCould not request results from %s \
service; {0}" % (name, e)
示例4: __init__
# 需要導入模塊: import speech_recognition [as 別名]
# 或者: from speech_recognition import AudioData [as 別名]
def __init__(self):
# format of input audio data
self.sample_rate = rospy.get_param("~sample_rate", 16000)
self.sample_width = rospy.get_param("~sample_width", 2L)
# language of STT service
self.language = rospy.get_param("~language", "ja-JP")
# ignore voice input while the robot is speaking
self.self_cancellation = rospy.get_param("~self_cancellation", True)
# time to assume as SPEAKING after tts service is finished
self.tts_tolerance = rospy.Duration.from_sec(
rospy.get_param("~tts_tolerance", 1.0))
self.recognizer = SR.Recognizer()
self.tts_action = None
self.last_tts = None
self.is_canceling = False
if self.self_cancellation:
self.tts_action = actionlib.SimpleActionClient(
"sound_play", SoundRequestAction)
if self.tts_action.wait_for_server(rospy.Duration(5.0)):
self.tts_timer = rospy.Timer(rospy.Duration(0.1), self.tts_timer_cb)
else:
rospy.logerr("action '%s' is not initialized." % rospy.remap_name("sound_play"))
self.tts_action = None
self.pub_speech = rospy.Publisher(
"speech_to_text", SpeechRecognitionCandidates, queue_size=1)
self.sub_audio = rospy.Subscriber("audio", AudioData, self.audio_cb)
示例5: audio_cb
# 需要導入模塊: import speech_recognition [as 別名]
# 或者: from speech_recognition import AudioData [as 別名]
def audio_cb(self, msg):
if self.is_canceling:
rospy.loginfo("Speech is cancelled")
return
data = SR.AudioData(msg.data, self.sample_rate, self.sample_width)
try:
rospy.loginfo("Waiting for result %d" % len(data.get_raw_data()))
result = self.recognizer.recognize_google(
data, language=self.language)
msg = SpeechRecognitionCandidates(transcript=[result])
self.pub_speech.publish(msg)
except SR.UnknownValueError as e:
rospy.logerr("Failed to recognize: %s" % str(e))
except SR.RequestError as e:
rospy.logerr("Failed to recognize: %s" % str(e))