python - 我们如何使用 python 将输入文件从存储容器提供给 azure 语音 api

标签 python python-3.x azure azure-speech azure-text-translation

下面是代码,

call_name1="test.wav"
blob_client1=blob_service_client.get_blob_client("bucket/audio",call_name1)
print(blob_client1)

streamdownloader=blob_client1.download_blob()
stream = BytesIO()
streamfinal=streamdownloader.download_to_stream(stream)
print(streamfinal)

speech_key, service_region = "12345", "eastus"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

audio_input = speechsdk.audio.AudioConfig(filename=streamfinal)

错误,

TypeError                                 Traceback (most recent call last)
<ipython-input-6-a402ae91606a> in <module>
     44 speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
     45
---> 46 audio_input = speechsdk.audio.AudioConfig(filename=streamfinal)

C:\ProgramData\Anaconda3\lib\site-packages\azure\cognitiveservices\speech\audio.py in __init__(self, use_default_microphone, filename, stream, device_name)
    213
    214         if filename is not None:
--> 215             self._impl = impl.AudioConfig._from_wav_file_input(filename)
    216             return
    217         if stream is not None:

TypeError: in method 'AudioConfig__from_wav_file_input', argument 1 of type 'std::string const &'

请帮助我们从存储容器中读取音频文件作为 Azure 语音 api 中的输入。谢谢!!

最佳答案

正如 ewong 在评论中所说,您需要获取 stream 而不是 String。

download_to_stream 用于将此 blob 的内容下载到流中。但不是 AudioConfig 需要的 azure.cognitiveservices.speech.audio.AudioInputStream

我找不到将流转换为 AudioInputStream 的解决方法。所以,看来只有从Storage Blob下载音频文件到本地,然后通过AudioConfig上传的方式了。

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import azure.cognitiveservices.speech as speechsdk

filename = "test.txt"
container_name="test-container"

blob_service_client = BlobServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=pamelastorage;AccountKey=UOyhItMnWJmB54Jmj8U0YtStNFk0vZyN1+nRem9+JwqNVJEMh5deerdfLbhVQl0ztmg96UZEUtRh2HVp8+ZJWA==;EndpointSuffix=core.windows.net")
container_client=blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(filename)

with open(filename, "wb") as f:
    data = blob_client.download_blob()
    data.readinto(f)

audio_input = speechsdk.audio.AudioConfig(filename=filename)
print(audio_input)

关于python - 我们如何使用 python 将输入文件从存储容器提供给 azure 语音 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63128082/

相关文章:

python 比较 CSV 并找出差异

python - pandas:应用多个过滤器

python - UnicodeEncodeError : 'ascii' codec can't encode character u'\u2013' in position 3 2: ordinal not in range(128)

c# - 在 Azure 网站中附加路径以包含 bin\debug 和 bin\release

python - 从 PyQt 的主窗口打开 Ui_Form

python - Pandas,附加到 Excel 中的下一个可用行

python - 同步 Python 脚本?

mysql - 检查 csv 文件中是否存在数据库记录

azure - 无法连接到远程桌面上的 Azure VM RDP

c# - Redis 缓存仅在异步方法中同步请求超时且异步请求响应缓慢