Lassen Sie uns mithilfe des Azure Speech SDK Sprache in Text konvertieren.
Gehen Sie zu der von Ihnen erstellten Ressource und erstellen Sie eine Kopie des Schlüssels und des Speicherorts.
conda create -n py36 python=3.6
conda activate py36
Vier. Installieren Sie die Bibliothek.
pip install azure-cognitiveservices-speech
Fünf. Erstellen Sie ein Programm.
Es ist ein Programm, das das Erkennungsergebnis durch einmalige Spracheingabe anzeigt. Fügen Sie den zuvor kopierten Schlüssel in "YourSubscriptionKey" und den zuvor kopierten Speicherort in "YourServiceRegion" ein. Ich möchte Japanisch erkennen, also setze die Sprache auf "ja-JP".
import azure.cognitiveservices.speech as speechsdk
Creates an instance of a speech config with specified subscription key and service region.
Replace with your own subscription key and service region (e.g., "westus").
speech_key, service_region, language = "YourSubscriptionKey", "YourServiceRegion", "ja-JP"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
print("Say something...")
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. The task 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()
Checks result.
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(result.no_match_details))
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))
Dies ist ein Programm, das kontinuierlich Sprache eingibt und das Erkennungsergebnis anzeigt. In ähnlicher Weise stellen Sie bitte den Schlüssel, den Ort und die Sprache ein.
import azure.cognitiveservices.speech as speechsdk
import time
Creates an instance of a speech config with specified subscription key and service region.
Replace with your own subscription key and service region (e.g., "westus").
speech_key, service_region, language = "YourSubscriptionKey", "YourServiceRegion", "ja-JP"
speech_config = speechsdk.SpeechConfig(
subscription=speech_key, region=service_region, speech_recognition_language=language)
Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
print("Say something...")
def recognized(evt):
print('「{}」'.format(evt.result.text))
# do something
def start(evt):
print('SESSION STARTED: {}'.format(evt))
def stop(evt):
print('SESSION STOPPED {}'.format(evt))
speech_recognizer.recognized.connect(recognized)
speech_recognizer.session_started.connect(start)
speech_recognizer.session_stopped.connect(stop)
try:
speech_recognizer.start_continuous_recognition()
time.sleep(60)
except KeyboardInterrupt:
print("bye.")
speech_recognizer.recognized.disconnect_all()
speech_recognizer.session_started.disconnect_all()
speech_recognizer.session_stopped.disconnect_all()
python stt.py
Das Erkennungsergebnis wird wie folgt angezeigt.
Erstellen Sie ein Programm.
Ein Programm, das WAV-Dateien liest und Spracherkennungsergebnisse anzeigt. Stellen Sie den Schlüssel und den Ort ein.
import azure.cognitiveservices.speech as speechsdk
Creates an instance of a speech config with specified subscription key and service region.
Replace with your own subscription key and region identifier from here: https://aka.ms/speech/sdkregion
speech_key, service_region = "YourSubscriptionKey", "YourServiceRegion"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
Creates an audio configuration that points to an audio file.
Replace with your own audio filename.
audio_filename = "aboutSpeechSdk.wav"
audio_input = speechsdk.audio.AudioConfig(filename=audio_filename)
Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input)
print("Recognizing first result...")
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. The task 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()
Checks result.
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(result.no_match_details))
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))
Verwenden Sie für Audiodateien sampledata \ audiofiles \ aboutSpeechSdk.wav in kognitive-Dienste-Sprache-SDK.
python stt_from_file.py
Wenn der Schlüssel und der Speicherort falsch sind, wird der folgende Fehler angezeigt.
(py36) C:\Users\good_\Documents\PythonProjects\AzureSpeech>python stt_from_file.py
Recognizing first result...
Speech Recognition canceled: CancellationReason.Error
Error details: Connection failed (no connection to the remote host). Internal error: 1. Error details: 11001. Please check network connection, firewall setting, and the region name used to create speech factory. SessionId: 77ad7686a9d94b7882398ae8b855d903
Das Ergebnis ist wie folgt.
Es hat 52 Sekunden, aber es scheint zu enden, wenn es die erste Zeile erkennt.
Vier. Gehen Sie wie folgt vor, um kontinuierlich zu lesen und die Stimme zu erkennen.
import azure.cognitiveservices.speech as speechsdk
import time
Creates an instance of a speech config with specified subscription key and service region.
Replace with your own subscription key and region identifier from here: https://aka.ms/speech/sdkregion
speech_key, service_region = "YourSubscriptionKey", "YourServiceRegion"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
Creates an audio configuration that points to an audio file.
Replace with your own audio filename.
audio_filename = "aboutSpeechSdk.wav"
audio_input = speechsdk.audio.AudioConfig(filename=audio_filename)
Creates a recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input)
print("Recognizing...")
def recognized(evt):
print('「{}」'.format(evt.result.text))
# do something
def start(evt):
print('SESSION STARTED: {}'.format(evt))
def stop(evt):
print('SESSION STOPPED {}'.format(evt))
speech_recognizer.recognized.connect(recognized)
speech_recognizer.session_started.connect(start)
speech_recognizer.session_stopped.connect(stop)
try:
speech_recognizer.start_continuous_recognition()
time.sleep(60)
except KeyboardInterrupt:
print("bye.")
speech_recognizer.recognized.disconnect_all()
speech_recognizer.session_started.disconnect_all()
speech_recognizer.session_stopped.disconnect_all()
Fünf. Lass es uns erneut versuchen.
Es scheint, dass die Spracherkennung kontinuierlich möglich ist, wie unten gezeigt!
Danke für deine harte Arbeit.
Recommended Posts