Radwan Ashraf
Radwan Ashraf

Reputation: 11

how to get Recognized text only?

I used Azure speech to text in python

import azure.cognitiveservices.speech as speechsdk



var = lambda evt: print('ss: {}'.format(evt))
speech_recognizer.recognizing.connect(var)

then after trying to get result actual recognizer text it end with this:

ss: SpeechRecognitionEventArgs(session_id=0aea5e8b80e544b48414f2d27585b6c4, result=SpeechRecognitionResult(result_id=86c7de30436f4db1b064121bd617f24b, text="Hello.", reason=ResultReason.RecognizedSpeech))

I want to just print Hello ?

Upvotes: 1

Views: 455

Answers (2)

Drop17
Drop17

Reputation: 31

To get the text from the event:

import azure.cognitiveservices.speech as speechsdk


var = lambda evt: print('ss: {}'.format(evt.result.text))
speech_recognizer.recognizing.connect(var)

Upvotes: 2

Mohit Verma
Mohit Verma

Reputation: 5296

If you are using simple mic to recognize the text, here is something which you can use to get the text:

def speech_recognize_once_from_mic():
    """performs one-shot speech recognition from the default microphone"""
    # <SpeechRecognitionWithMicrophone>
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    # Creates a speech recognizer using microphone as audio input.
    # The default language is "en-us".
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

    # Starts speech recognition, and returns after a single utterance is recognized. The end of a
    # single utterance is determined by listening for silence at the end or until a maximum of 15
    # seconds of audio is processed. It returns the recognition text as result.
    # Note: Since recognize_once() returns only a single utterance, it is suitable only for single
    # shot recognition like command or query.
    # For long-running multi-utterance recognition, use start_continuous_recognition() instead.
    result = speech_recognizer.recognize_once()

    # Check the result
    if result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(result.text))
    elif result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized")
    elif result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = result.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
    # </SpeechRecognitionWithMicrophone>

Check this repo for further reference. Hope it helps.

Upvotes: 0

Related Questions