forked from Om-Jaiswal/ArtificialMind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechtranslate.py
30 lines (24 loc) · 1.73 KB
/
speechtranslate.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
import azure.cognitiveservices.speech as speechsdk
def recognize_from_microphone():
speech_translation_config = speechsdk.translation.SpeechTranslationConfig(subscription="2846df0e5f93450e845f23971140feed", region="eastus")
speech_translation_config.speech_recognition_language="en-US"
target_language="hi"
speech_translation_config.add_target_language(target_language)
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
translation_recognizer = speechsdk.translation.TranslationRecognizer(translation_config=speech_translation_config, audio_config=audio_config)
print("Speak into your microphone.")
translation_recognition_result = translation_recognizer.recognize_once_async().get()
if translation_recognition_result.reason == speechsdk.ResultReason.TranslatedSpeech:
print("Recognized: {}".format(translation_recognition_result.text))
print("""Translated into '{}': {}""".format(
target_language,
translation_recognition_result.translations[target_language]))
elif translation_recognition_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(translation_recognition_result.no_match_details))
elif translation_recognition_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = translation_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?")
recognize_from_microphone()