如何批量使用Azure语音转文本转录-Python代码

时间:2019-10-02 16:24:06

标签: python azure text blob speech

我在使用Azure语音到Python中的文本Api REST连接天蓝色斑点时遇到问题。如果有人在Python中提供了示例代码,请允许我帮忙。

1 个答案:

答案 0 :(得分:0)

这是我满足您需求的示例代码。

  1. 使用SAS令牌为通过命令pip install azure-storage安装的适用于Python的Azure存储SDK为存储在Azure Blob存储中的音频文件生成带有SAS令牌的blob URL。

    from azure.storage.blob.baseblobservice import BaseBlobService
    import numpy as np
    
    account_name = '<your account name>'
    account_key = '<your account key>'
    container_name = '<your container name>' # for example, `test`
    blob_name = '<your blob name>' # for example, `whatstheweatherlike.wav`
    
    blob_service = BaseBlobService(
        account_name=account_name,
        account_key=account_key
    )
    
    from azure.storage.blob import BlobPermissions
    from datetime import datetime, timedelta
    import requests
    
    sas_token = blob_service.generate_blob_shared_access_signature(container_name, blob_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1))
    print(sas_token)
    url_with_sas = blob_service.make_blob_url(container_name, blob_name, sas_token=sas_token)
    print(url_with_sas)
    
  2. 读取音频文件的Blob URL的内容,然后调用Azure语音到文本REST API,请参考正式文档Speech-to-text REST API。在这里,我使用了一个名为whatstheweatherlike.wav的官方音频样本,您可以从GitHub Repo Azure-Samples/cognitive-services-speech-sdk的{​​{3}}那里获得

    import requests
    
    subscription_key = '<your subscription key>'
    service_region = '<your service_region>' # for example, `eastasia`
    
    def get_token(subscription_key, service_region):
        fetch_token_url = f"https://{service_region}.api.cognitive.microsoft.com/sts/v1.0/issuetoken"
        headers = {
            'Ocp-Apim-Subscription-Key': subscription_key
        }
        response = requests.post(fetch_token_url, headers=headers)
        access_token = str(response.text)
        return access_token
    
    access_token = get_token(subscription_key, service_region)
    #print(access_token)
    
    endpoint = f"https://{service_region}.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US"
    
    audio_blob_url_with_sas = url_with_sas # it's from STEP 1.
    r = requests.get(audio_blob_url_with_sas)
    
    headers = {
        'Accept': 'application/json;text/xml',
        'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
        'Authorization': f"Bearer {access_token}" # Or just use `'Ocp-Apim-Subscription-Key': subscription_key` instead of `'Authorization'`
    }
    
    res = requests.post(endpoint, headers = headers, data = r.content)
    print(res.text)
    

最后,我得到了如下结果。

{"RecognitionStatus":"Success","DisplayText":"What's the weather like?","Offset":300000,"Duration":13600000}