如何在Python中实现Microsoft语音识别/验证API?

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

我想为演讲者验证项目实施Microsoft认知服务提供的演讲者识别API。我已经有一个说话者识别API密钥。我直接从文档中获得了示例Python代码(在文档底部):

https://westus.dev.cognitive.microsoft.com/docs/services/563309b6778daf02acc0a508/operations/563309b7778daf06340c9652

    ########### Python 3.2 #############
    import http.client, urllib.request, urllib.parse, urllib.error, base64

    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '{subscription key}',
    }

    params = urllib.parse.urlencode({
    })

    try:
        conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
        conn.request("POST", "/spid/v1.0/verificationProfiles?%s" % params, "{body}", headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

    ####################################

这是第一步的代码示例,创建并保存语音配置文件。

要进行演讲者验证,我们需要执行3个步骤:1)创建个人资料2)创建注册3)验证

我现在已经停留在第一步了。通常,这是我第一次使用API​​,因此我不确定是否需要更改Python代码的哪些部分。我知道我需要在'Ocp-Apim-Subscription-Key'中插入我的API密钥,但除此之外,还有什么呢?例如,如果我在该特定字段中添加API密钥并运行代码,则会收到此错误消息。

b'{"error":{"code":"BadRequest","message":"locale is not specified"}}'

例如,我需要在哪里插入语言环境(“ en-us”)?从文档中我并不清楚我需要编辑什么。如果您能指导我我需要在API调用中插入/添加的内容,我将非常感激。

非常感谢!

azure speech-recognition microsoft-cognitive voice-recognition azure-cognitive-services
1个回答
1
投票

创建发言人识别配置文件时,必须将其与语言环境链接,并在请求正文中指定此语言环境。主体应为JSON对象,如下所示:

{
  "locale":"en-us",
}

为了使示例正常工作,您需要将“ {body}”替换为实际的主体值,如下所示:

conn.request("POST", "/spid/v1.0/verificationProfiles?%s" % params, "{\"locale\":\"en-US\"}", headers)
© www.soinside.com 2019 - 2024. All rights reserved.