Skip to content

Instantly share code, notes, and snippets.

@Lord-V15
Created April 8, 2022 13:18
Show Gist options
  • Save Lord-V15/508817e6dd5fc22763a14810a1fb1b42 to your computer and use it in GitHub Desktop.
Save Lord-V15/508817e6dd5fc22763a14810a1fb1b42 to your computer and use it in GitHub Desktop.
Azure Cognitive - Speech Recognition Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "f87e24de",
"metadata": {},
"outputs": [],
"source": [
"import http.client\n",
"import json"
]
},
{
"cell_type": "markdown",
"id": "392a5695",
"metadata": {},
"source": [
"# Create Profile"
]
},
{
"cell_type": "raw",
"id": "02e4fa96",
"metadata": {},
"source": [
"POST {Endpoint}/speaker/verification/v2.0/text-dependent/profiles"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5ef33de4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"remainingEnrollmentsCount\":3,\"locale\":\"en-us\",\"createdDateTime\":\"2021-08-12T09:35:09.465Z\",\"enrollmentStatus\":\"Enrolling\",\"modelVersion\":null,\"profileId\":\"506b52f6-8fe9-44b9-a420-be200880e2a1\",\"lastUpdatedDateTime\":null,\"enrollmentsCount\":0,\"enrollmentsLength\":0.0,\"enrollmentsSpeechLength\":0.0}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0',\n",
" 'Content-type': 'application/json'}\n",
"\n",
"foo = {'locale': 'en-us'}\n",
"json_data = json.dumps(foo)\n",
"\n",
"conn.request(method='POST', url='/speaker/verification/v2.0/text-dependent/profiles', \n",
" body=json_data, headers=headers)\n",
"response = conn.getresponse().read().decode()\n",
"conn.close()\n",
"\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1691c361",
"metadata": {},
"outputs": [],
"source": [
"profileId = json.loads(response)[\"profileId\"]"
]
},
{
"cell_type": "markdown",
"id": "5164d435",
"metadata": {},
"source": [
"# ENROLL PROFILE\n",
"In current version of text-dependent speaker verification API, we provide 10 English phrases for the speakers to choose from.\n",
"- I am going to make him an offer he cannot refuse.\n",
"Houston we have had a problem.\n",
"- My voice is my passport verify me.\n",
"- Apple juice tastes funny after toothpaste.\n",
"- You can get in without your password.\n",
"- You can activate security system now.\n",
"- My voice is stronger than passwords.\n",
"- My password is not your business.\n",
"- My name is unknown to you.\n",
"- Be yourself everyone else is already taken"
]
},
{
"cell_type": "raw",
"id": "e2f1871c",
"metadata": {},
"source": [
"POST {Endpoint}/speaker/verification/v2.0/text-dependent/profiles/{profileId}/enrollments"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9154bf01",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'Speech Services - Quickstart.ipynb' phrase-record1.wav settings.xml\r\n",
"'Text Dependent Recognition.ipynb' phrase-record2.wav verify.wav\r\n",
"'Text Independent Recognition.ipynb' phrase-record3.wav\r\n",
" audio\t\t\t\t record.wav\r\n"
]
}
],
"source": [
"!ls"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c4265b1c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"remainingEnrollmentsCount\":2,\"passPhrase\":\"my voice is stronger than passwords\",\"profileId\":\"506b52f6-8fe9-44b9-a420-be200880e2a1\",\"enrollmentStatus\":\"Enrolling\",\"enrollmentsCount\":1,\"enrollmentsLength\":4.21,\"enrollmentsSpeechLength\":2.87,\"audioLength\":4.21,\"audioSpeechLength\":2.87}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"with open('phrase-record1.wav', 'rb') as data:\n",
" conn.request(method='POST', \n",
" url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}/enrollments?ignoreMinLength=true', \n",
" body=data, headers=headers)\n",
" response = conn.getresponse().read().decode()\n",
" conn.close()\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2bb4cc4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"remainingEnrollmentsCount\":1,\"passPhrase\":\"my voice is stronger than passwords\",\"profileId\":\"506b52f6-8fe9-44b9-a420-be200880e2a1\",\"enrollmentStatus\":\"Enrolling\",\"enrollmentsCount\":2,\"enrollmentsLength\":7.65,\"enrollmentsSpeechLength\":5.3,\"audioLength\":3.44,\"audioSpeechLength\":2.43}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"with open('phrase-record2.wav', 'rb') as data:\n",
" conn.request(method='POST', \n",
" url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}/enrollments?ignoreMinLength=true', \n",
" body=data, headers=headers)\n",
" response = conn.getresponse().read().decode()\n",
" conn.close()\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0f7744d9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"remainingEnrollmentsCount\":0,\"passPhrase\":\"my voice is stronger than passwords\",\"profileId\":\"506b52f6-8fe9-44b9-a420-be200880e2a1\",\"enrollmentStatus\":\"Enrolled\",\"enrollmentsCount\":3,\"enrollmentsLength\":11.49,\"enrollmentsSpeechLength\":7.6,\"audioLength\":3.84,\"audioSpeechLength\":2.3}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"with open('phrase-record3.wav', 'rb') as data:\n",
" conn.request(method='POST', \n",
" url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}/enrollments?ignoreMinLength=true', \n",
" body=data, headers=headers)\n",
" response = conn.getresponse().read().decode()\n",
" conn.close()\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "a7847bc1",
"metadata": {},
"source": [
"# LIST PROFILES"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9b585660",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"profiles\":[{\"remainingEnrollmentsCount\":3,\"locale\":\"en-us\",\"createdDateTime\":\"2021-08-10T15:26:47.191Z\",\"enrollmentStatus\":\"Enrolling\",\"modelVersion\":null,\"profileId\":\"0ae74e3c-3c1b-433b-a6fb-11826f8f69f9\",\"lastUpdatedDateTime\":null,\"enrollmentsCount\":0,\"enrollmentsLength\":0.0,\"enrollmentsSpeechLength\":0.0},{\"remainingEnrollmentsCount\":0,\"locale\":\"en-us\",\"createdDateTime\":\"2021-08-12T09:35:09.465Z\",\"enrollmentStatus\":\"Enrolled\",\"modelVersion\":\"2019-11-01\",\"profileId\":\"506b52f6-8fe9-44b9-a420-be200880e2a1\",\"lastUpdatedDateTime\":\"08/12/2021 09:49:07\",\"enrollmentsCount\":3,\"enrollmentsLength\":11.49,\"enrollmentsSpeechLength\":0.0}],\"@nextLink\":\"\"}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"conn.request(method='GET', url='/speaker/verification/v2.0/text-dependent/profiles?$top=10', \n",
" headers=headers)\n",
"response = conn.getresponse().read().decode()\n",
"conn.close()\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d8b09aad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(json.loads(response)[\"profiles\"])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "01a23e8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'506b52f6-8fe9-44b9-a420-be200880e2a1'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"profileId"
]
},
{
"cell_type": "markdown",
"id": "4a4ec125",
"metadata": {},
"source": [
"# VERIFY PROFILE"
]
},
{
"cell_type": "raw",
"id": "6df6cb28",
"metadata": {},
"source": [
"POST {Endpoint}/speaker/verification/v2.0/text-dependent/profiles/{profileId}/verify"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d9f29b76",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"recognitionResult\":\"Accept\",\"score\":0.5509841442108154}\n"
]
}
],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"with open('phrase-verify.wav', 'rb') as data:\n",
" conn.request(method='POST', \n",
" url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}/verify', \n",
" body=data, headers=headers)\n",
" response = conn.getresponse().read().decode()\n",
" conn.close()\n",
" print(response)"
]
},
{
"cell_type": "markdown",
"id": "3774b422",
"metadata": {},
"source": [
"# DELETE PROFILE"
]
},
{
"cell_type": "raw",
"id": "12795b0d",
"metadata": {},
"source": [
"DELETE {Endpoint}/speaker/verification/v2.0/text-dependent/profiles/{profileId}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2abfe4a3",
"metadata": {},
"outputs": [],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"conn.request(method='DELETE', url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}', \n",
" headers=headers)\n",
"response = conn.getresponse().read().decode()\n",
"conn.close()\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "95bca90a",
"metadata": {},
"source": [
"# RESET PROFILE"
]
},
{
"cell_type": "raw",
"id": "f57d8c6b",
"metadata": {},
"source": [
"POST {Endpoint}/speaker/verification/v2.0/text-dependent/profiles/{profileId}/reset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7db2350",
"metadata": {},
"outputs": [],
"source": [
"conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
"\n",
"headers = {'Ocp-Apim-Subscription-Key': 'f92e7479ee9f4e1ca9ba46c32e5644a0'}\n",
"\n",
"conn.request(method='DELETE', url=f'/speaker/verification/v2.0/text-dependent/profiles/{profileId}/reset', \n",
" headers=headers)\n",
"response = conn.getresponse().read().decode()\n",
"conn.close()\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7027b08d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3af57a9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment