Python speech_recognition.AudioData() Examples

The following are 5 code examples of speech_recognition.AudioData(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module speech_recognition , or try the search function .
Example #1
Source File: server.py    From Gather-Deployment with MIT License 6 votes vote down vote up
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) 
Example #2
Source File: apis.py    From asr-study with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: speech_to_text.py    From respeaker_ros with Apache License 2.0 5 votes vote down vote up
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) 
Example #4
Source File: speech_to_text.py    From respeaker_ros with Apache License 2.0 5 votes vote down vote up
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)) 
Example #5
Source File: SpeechRecognition_translator.py    From respeaker_python_library with Apache License 2.0 -8 votes vote down vote up
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