Microsoft Cognitive Services - Speaker Recognition API - Verification - error-SpeakerInvalid - python-3.x

I am still facing the error in the verification process
{"error":{"code":"BadRequest","message":"SpeakerInvalid"}}'
My audio is correct as it is getting enrolled easily
##code for API CALL speaker verification
import http.client, urllib.request, urllib.parse, urllib.error, base64
subscription_key = 'XXXXXXXXXXXXXXXXXXXXXXX'
headers = {
# Request headers
"Content-Type": 'multipart/form-data',
"Ocp-Apim-Subscription-Key": subscription_key,
}
params = urllib.parse.urlencode({
'verificationProfileId':'445b849b-6418-4443-961b-77bd88196223',
})
#body = {
#}
try:
conn = http.client.HTTPSConnection('speaker-recognition-api.cognitiveservices.azure.com')
body = open('pp.wav','rb') //pp.wav is my audio file
conn.request("POST", "/spid/v1.0/verify?verificationProfileId=445b849b-6418-4443-961b-77bd88196223?%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))

I could reproduce your problem. You are getting this error cause there is a ? in the end of your url, however behind verify there is already a ?. So if you want to add params to your request url you should use & just like the sample code in this API doc:Speaker Recognition - Verification .
Below is my work code.
try:
conn = http.client.HTTPSConnection('geospeaker.cognitiveservices.azure.com')
body=open("output4.wav","rb")
conn.request("POST", "/spid/v1.0/verify?verificationProfileId=1ae143b0-c301-4345-9295-3e34ad367092?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except OSError as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

Related

Access vault credentials using API

I am trying to access Vault credentials using Python API. I am doing something wrong as I always get access denied. I have used the following code:
def fetch():
url1="http://127.0.0.1:8200/v1/auth/gcp/config"
payload1={}
headers2={
'X-VAULT-TOKEN': 's.DsqQCKCY1JMhSe1k8A5rIyku'
}
try:
response1=requests.request("GET", url1, headers=headers2, data=payload1)
return response1.text
except Exception as err1:
return str(err1)
url="http://127.0.0.1:8200/v1/myengine/data/myspringapplication/staging"
payload={}
headers={
'X-Vault-Token': 'myroot'
}
try:
response=requests.request("GET", url, headers=headers, data=payload)
return response.text
except Exception as err:
return str(err)
when you are sending a request that contains a payload you should use the POST method. I would write it like this. If that doesn't work since the payload is empty maybe it is the GET request method so try removing data=payload.
import requests
payload = {}
# https://www.vaultproject.io/api/auth/gcp
url = "http://127.0.0.1:8200/v1/sys/auth"
ses = requests.session()
ses.headers.update({'X-VAULT-TOKEN': 's.Ezr0s63KQhW72knZHCIkjMHh'})
try:
r = ses.get(url, timeout=60)
if r.status_code == 200:
print(r.json())
else:
print("bad status code", r.status_code)
except requests.exceptions.Timeout:
pass
Edit: changed the code with default URL for testing, and it doesn't need a payload. So GET request is O.K. and this returns data for me.

how to add content_type explicitly for json and file in post request

I would like to add content type explicity for multipart form data before sending post request
Below is my sample code i managed to add conten type for file data but couldn't figure out how to add content type correctly for json data, i would like to add "application/json; charset=utf-8" for json data
import requests
import json
import traceback
def uploadLogs(fileName):
f = open(fileName, 'rb')
payload = { "var1":"this", "var2" : "that"
}
files = {'file': ('current', f, "text/plain; charset=us-ascii")}
data = {'info': json.dumps(payload)}
headers = {'type': 'myReport', "Keep-Alive": "timeout=100"}
try:
url = "http://localhost:8009/upload"
response = requests.post(url, data=data, files=files, headers=headers)
print(response.request.body)
print(response.request.headers)
print(response.status_code)
if (response != None and (response.status_code == 200 or
response.status_code == 201)):
return True
except:
traceback.print_exc()
return False
filename = "C:\\sample.txt"
print(uploadLogs(filename))
If someone knows how to do please suggest

How to upload video as a multipart/form body content in Python for Video Indexer API?

I am trying to upload a video file from local machine using VideoIndexer API in Python. I am expected to upload video as a multipart/form body content. Following is my code (Variable values like accountID, name, location are not pasted below),
# Request headers
headers = {
'x-ms-client-request-id': '',
'Content-Type': 'multipart/form-data',
'Ocp-Apim-Subscription-Key': 'xxxxxxxxx',
}
# Request parameters
params = urllib.parse.urlencode({
'privacy': 'Private',
'description': 'testing',
})
try:
conn = http.client.HTTPSConnection('api.videoindexer.ai')
conn.request("POST", "/" + location + "/Accounts/" + accountId + "/Videos?name=" + name + "&accessToken=" + accessToken + "&%s" % params, body = open('TestAudio1.mp4', 'rb').read(), headers = headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
I am getting the following error,
{"ErrorType":"INVALID_INPUT","Message":"Input is invalid. Input must specify either a video url, an asset id or provide a multipart content body."}
How can I fix this issue?

How to upload a video file using python in video indexer api?

I am trying to upload a video in Video Indexer API using Python:
import http.client, urllib.request, urllib.parse, urllib.error, base64
headers = {
# Request headers
'Content-Type': 'multipart/form-data',
'Ocp-Apim-Subscription-Key': '******************',
}
params = urllib.parse.urlencode({
# Request parameters
'name': 'xxxx',
'privacy': 'Private',
'language': 'English',
})
try:
conn = http.client.HTTPSConnection('videobreakdown.azure-api.net')
conn.request("POST", "/Breakdowns/Api/Partner/Breakdowns?%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))
But I am not able to specify how to give the video file in the {body} section.
Kindly help me.
This works for me:
import requests
import urllib.parse
import json
headers = {
'Ocp-Apim-Subscription-Key': 'YOUR-API-KEY',
}
form_data = {'file': open('YOUR-VIDEO.mp4', 'rb')}
params = urllib.parse.urlencode({
'name': 'video.mp4',
'privacy': 'Private',
'language': 'English',
})
try:
url = 'https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?'
r = requests.post(url, params=params, files=form_data, headers=headers)
print(r.url)
print(json.dumps(r.json(), indent=2))
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

error message in microsoft emotion api video python3

Im trying to get the emotions from a video
Below is my code,
Always when I run this code i get this error
b'{"error":{"code":"BadArgument","message":"Failed to deserialize JSON request."}}' any idea why?
import http.client, urllib.request, urllib.parse, urllib.error, base64, sys
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxx',
}
params = urllib.parse.urlencode({
})
body = "{ 'url': 'http://www.dropbox.com/s/zfmaswf8s9c58om/blog2.mp4' }"
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, "
{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(e.args)
You forgot to substitute the placeholder {body} with the real thing.
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, body, headers)

Resources