溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Azure Cognitive Services- Spee

發(fā)布時(shí)間:2020-03-01 10:57:13 來(lái)源:網(wǎng)絡(luò) 閱讀:383 作者:13188888544 欄目:云計(jì)算



Speech 服務(wù)是認(rèn)知服務(wù)的一種,提供了語(yǔ)音轉(zhuǎn)文本,文本轉(zhuǎn)語(yǔ)音, 語(yǔ)音翻譯等,今天我們實(shí)戰(zhàn)的是語(yǔ)音轉(zhuǎn)文本(Speech To Text)。


STT支持兩種訪問(wèn)方式,1.是SDK,2.是REST API。


其中:

SDK方式支持?識(shí)別麥克風(fēng)的語(yǔ)音流 和 語(yǔ)音文件;

REST API方式僅支持語(yǔ)音文件;


準(zhǔn)備工作:創(chuàng)建 認(rèn)知服務(wù)之Speech服務(wù):


Azure Cognitive Services- Speecdn.nlark.com/yuque/0/2020/png/741540/1580645460436-abeba30d-7098-41ba-a3bf-e932146333c1.png">

創(chuàng)建完成后,兩個(gè)重要的參數(shù)可以在頁(yè)面查看:

Azure Cognitive Services- Spee



一. REST API方式將語(yǔ)音文件轉(zhuǎn)換成文本:


Azure global的 Speech API 終結(jié)點(diǎn)請(qǐng)參考:

https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/rest-speech-to-text#regions-and-endpoints



Azure 中國(guó)區(qū)?的 Speech API 終結(jié)點(diǎn):

截至到2020.2月,僅中國(guó)東部2區(qū)域已開(kāi)通Speech服務(wù),服務(wù)終結(jié)點(diǎn)為:

https://chinaeast2.stt.speech.azure.cn/speech/recognition/conversation/cognitiveservices/v1


對(duì)于Speech To Text來(lái)說(shuō),有兩種身份驗(yàn)證方式:

其中Authorization? Token有效期為10分鐘。

Azure Cognitive Services- Spee

為了簡(jiǎn)便,本文使用了Ocp-Apim-Subscription-Key的方式。

注意:如果要實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音,按照上表,則必須使用 Authorization Token形式進(jìn)行身份驗(yàn)證。


構(gòu)建請(qǐng)求的其他注意事項(xiàng):

  1. 文件格式:

    Azure Cognitive Services- Spee

  2. 請(qǐng)求頭:

    Azure Cognitive Services- Spee
    需要注意的是,Key或者Authorization是二選一的關(guān)系。

  3. 請(qǐng)求參數(shù):

    Azure Cognitive Services- Spee

在Postman中的示例如下:


Azure Cognitive Services- Spee


Azure Cognitive Services- Spee


Azure Cognitive Services- Spee


如果要在REST API中使用 Authorization Token,則需要先獲得Token:

Global 獲取Token的終結(jié)點(diǎn):

https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/rest-speech-to-text#authentication

中國(guó)區(qū)獲取Token的終結(jié)點(diǎn):

截至2020.02,只有中國(guó)東部2有Speech服務(wù),其Token終結(jié)點(diǎn)為:

https://chinaeast2.api.cognitive.azure.cn/sts/v1.0/issuetoken


Postman獲取Token 參考如下:

Azure Cognitive Services- Spee



二. SDK方式將語(yǔ)音文件轉(zhuǎn)換成文本(Python示例):


在官網(wǎng)可以看到類似的代碼,但需要注意的是,該代碼僅在Azure Global的Speech服務(wù)中正常工作,針對(duì)中國(guó)區(qū),需要做特定的修改(見(jiàn)下文)。

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., "chinaeast2"). 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 = "whatstheweatherlike.wav" audio_input = speechsdk.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))



代碼提供頁(yè)面:

https://docs.azure.cn/zh-cn/cognitive-services/speech-service/quickstarts/speech-to-text-from-file?tabs=linux&pivots=programming-language-python#create-a-python-application-that-uses-the-speech-sdk


針對(duì)中國(guó)區(qū),需要使用自定義終結(jié)點(diǎn)的方式,才能正常使用SDK:

speech_key,?service_region?=?"Your?Key",?"chinaeast2"
template?=?"wss://{}.stt.speech.azure.cn/speech/recognition"?\
???????????????"/conversation/cognitiveservices/v1?initialSilenceTimeoutMs={:d}&language=zh-CN"
speech_config?=?speechsdk.SpeechConfig(subscription=speech_key,
endpoint=template.format(service_region,?int(initial_silence_timeout_ms)))


中國(guó)區(qū)完整代碼為:

#!/usr/bin/env?python
#?coding:?utf-8

#?Copyright?(c)?Microsoft.?All?rights?reserved.
#?Licensed?under?the?MIT?license.?See?LICENSE.md?file?in?the?project?root?for?full?license?information.
"""
Speech?recognition?samples?for?the?Microsoft?Cognitive?Services?Speech?SDK
"""

import?time
import?wave

try:
????import?azure.cognitiveservices.speech?as?speechsdk
except?ImportError:
????print("""
????Importing?the?Speech?SDK?for?Python?failed.
????Refer?to
????https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstart-python?for
????installation?instructions.
????""")
????import?sys
????sys.exit(1)


#?Set?up?the?subscription?info?for?the?Speech?Service:
#?Replace?with?your?own?subscription?key?and?service?region?(e.g.,?"westus").
speech_key,?service_region?=?"your?key",?"chinaeast2"

#?Specify?the?path?to?an?audio?file?containing?speech?(mono?WAV?/?PCM?with?a?sampling?rate?of?16
#?kHz).
filename?=?"D:\FFOutput\speechtotext.wav"

def?speech_recognize_once_from_file_with_custom_endpoint_parameters():
????"""performs?one-shot?speech?recognition?with?input?from?an?audio?file,?specifying?an
????endpoint?with?custom?parameters"""
????initial_silence_timeout_ms?=?15?*?1e3
????template?=?"wss://{}.stt.speech.azure.cn/speech/recognition/conversation/cognitiveservices/v1?initialSilenceTimeoutMs={:d}&language=zh-CN"
????speech_config?=?speechsdk.SpeechConfig(subscription=speech_key,
????????????endpoint=template.format(service_region,?int(initial_silence_timeout_ms)))
????print("Using?endpoint",?speech_config.get_property(speechsdk.PropertyId.SpeechServiceConnection_Endpoint))
????audio_config?=?speechsdk.audio.AudioConfig(filename=filename)
????#?Creates?a?speech?recognizer?using?a?file?as?audio?input.
????#?The?default?language?is?"en-us".
????speech_recognizer?=?speechsdk.SpeechRecognizer(speech_config=speech_config,?audio_config=audio_config)
????
????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:?{}".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))


speech_recognize_once_from_file_with_custom_endpoint_parameters()


需要注意的是,如果我們使用SDK識(shí)別麥克風(fēng)中的語(yǔ)音,則將

speech_recognizer?=?speechsdk.SpeechRecognizer(speech_config=speech_config,?audio_config=audio_config)

修改為如下即可(去掉audio_config參數(shù)):

speech_recognizer?=?speechsdk.SpeechRecognizer(speech_config=speech_config)



公眾號(hào)鏈接:https://mp.weixin.qq.com/s/NA9kQsVDfzTXEqHMTdDExA


語(yǔ)雀地址:https://www.yuque.com/seanyu/azure/blwb5i



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI