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

问题描述 投票:-1回答:1

我在使用Azure语音到Python中的文本Api REST连接天蓝色斑点时遇到问题。如果有人在Python中提供了示例代码,如果您能帮助我,我将不胜感激。

python azure text blob speech
1个回答
0
投票

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

  1. 为您的音频文件生成SAS令牌,并通过SAS令牌生成blob url,该音频文件通过适用于Python的Azure存储SDK通过命令pip install azure-storage安装。

    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 samples/csharp/sharedcontent/console/whatstheweatherlike.wavAzure-Samples/cognitive-services-speech-sdk中获得该样本。

    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)
    
  3. 最后,得到的结果如下。

{"RecognitionStatus":"Success","DisplayText":"What's the weather like?","Offset":300000,"Duration":13600000}
© www.soinside.com 2019 - 2024. All rights reserved.