-
Notifications
You must be signed in to change notification settings - Fork 0
/
stt.py
33 lines (25 loc) · 1.65 KB
/
stt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import azure.cognitiveservices.speech as speechsdk
SPEECH_KEY = 'YOUR_AZURE_SPEECH_STUDIO_KEY'
SPEECH_REGION = 'eastus'
# filename = 'D:\\GPT\\GPT\\reading_evalaution\\test_paragraph_cars_correct.wav'
def recognize_from_microphone(filename):
# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
speech_config = speechsdk.SpeechConfig(SPEECH_KEY, SPEECH_REGION)
speech_config.speech_recognition_language="en-US"
audio_config = speechsdk.audio.AudioConfig(filename=filename)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
# print("Speak into your microphone.")
speech_recognition_result = speech_recognizer.recognize_once_async().get()
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(speech_recognition_result.text))
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_recognition_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))
print("Did you set the speech resource key and region values?")
return speech_recognition_result.text
# recognize_from_microphone(filename)