How to refer a local file for Azure emotion api - python

I am trying to refer a local jpg file for using in Azure Emotion API.
To do this, I refer my file through "file:///" like below.
body = "{'url': 'file:///Users/jonghkim/dev_jhk/Research/Crowdfunding/Face_Analysis/me.jpg'}"
But the response says "Invalid image URL." How could I fix it?
{"error":{"code":"InvalidUrl","message":"Invalid image URL."}}
Whole code looks like below.
########### Python 2.7 #############
import httplib, urllib, base64
headers = {
# Request headers. Replace the placeholder key below with your subscription key.
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '***********************',
}
params = urllib.urlencode({
})
# Replace the example URL below with the URL of the image you want to analyze.
body = "{'url': 'file:///Users/jonghkim/dev_jhk/Research/Crowdfunding/Face_Analysis/me.jpg'}"
try:
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%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 solved this problem. The true reason was two fold. At first, when we refer local file, we should use 'Content-Type': 'application/octet-stream' in a header.
The second problem is that the image should satisfy the condition of Azure (learn.microsoft.com/ko-kr/azure/cognitive-services/emotion/fa‌​q).
Full code is here:
########### Python 2.7 #############
import httplib, urllib, base64
headers = {
# Request headers. Replace the placeholder key below with your subscription key.
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '**************************',
}
params = urllib.urlencode({
})
# Replace the example URL below with the URL of the image you want to analyze.
body = open('test.jpg','rb').read()
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()

You're executing the emotion API within Cognitive Services - just take a look at the URI. This code is not being executed locally. It's a service. Being run somewhere else.
So, when the service gets the URL (via url in the body), it then needs to reach out to that resource, which is impossible to do if the resource is on your computer. And file:// is going to be an invalid scheme because the service won't be reading from its own file system.
You'll need to have your resource in an accessible place (e.g. a public or SAS-signed blob, an image link from a website, etc).

Related

How do you push files to a github repo in python

This bounty has ended. Answers to this question are eligible for a +50 reputation bounty. Bounty grace period ends in 19 hours.
user19926715 is looking for an up-to-date answer to this question:
I would like an example that shows the before and after of the packet before it gets sent and the response after and showing proof of it going into a repository and explain what was different from the code given by me.
I have a file called w.txt that holds 4 random numbers
I am trying to push it to a git repo and I have the following code:
aa=open(fileName,'r').read()
print(aa)
text= base64.b64encode(aa.encode("utf-8"))
api="https://api.github.com/repos/Ixonblitz-MatOS/Worm/contents/w.txt"
headers = {
"Authorization": f'''Bearer {token}''',
"Content-type": "application/vnd.github+json"
}
data = {
"message": msg, # Put your commit message here.
"content": text.decode("utf-8")
}
r = requests.put(api, headers=headers, json=data)
print(r.text)
In this case fileName is "w.txt" and the api is the api for my repo and the correct token is being used. What am I missing? It is leaving the file on my repo empty instead of putting the numbers there.
You need to provide the SHA of the file as the error message says.
To get the SHA, you can get the file like this:
import requests
url = "https://api.github.com/repos/Ixonblitz-MatOS/Worm/contents/w.txt"
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer <token>',
}
response = requests.request("GET", url, headers=headers)
data = response.json()
The SHA will be at data['sha'].
Then you need to modify your request to include the SHA:
data = {
"message": msg, # Put your commit message here.
"content": text.decode("utf-8"),
"sha": data['sha']
}
The /repos/{owner}/{repo}/contents/{path} API route to fetch files has some size limits. See below:
If the requested file's size is:
1 MB or smaller: All features of this endpoint are supported.
Between
1-100 MB: Only the raw or object custom media types are supported.
Both will work as normal, except that when using the object media
type, the content field will be an empty string and the encoding field
will be "none". To get the contents of these larger files, use the raw
media type.
Greater than 100 MB: This endpoint is not supported.
For larger files, you will have to clone the repository and then calculate the SHA locally.
Why don't you do it this way.
from git import Repo
PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git' # make sure .git folder is properly configured
COMMIT_MESSAGE = 'comment from python script'
def git_push():
try:
repo = Repo(PATH_OF_GIT_REPO)
repo.git.add(update=True)
repo.index.commit(COMMIT_MESSAGE)
origin = repo.remote(name='origin')
origin.push()
except:
print('Some error occured while pushing the code')
git_push()
Based on the code you've provided, it seems that you are encoding the content of the file as base64 before pushing it to your Git repository. While this is a valid approach, you need to make sure that you are decoding the content before writing it to the file on the Git repository. Here's a modified version of your code that should work:
import base64
import requests
fileName = "w.txt"
msg = "Updated w.txt with new numbers"
token = "<your GitHub access token>"
with open(fileName, 'r') as f:
content = f.read()
encoded_content = base64.b64encode(content.encode("utf- 8")).decode("utf-8")
api_url = "https://api.github.com/repos/Ixonblitz-MatOS/Worm/contents/w.txt"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"Accept": "application/vnd.github+json"
}
data = {
"message": msg,
"content": encoded_content
}
response = requests.put(api_url, headers=headers, json=data)
if response.status_code == 200:
print("Successfully pushed the file to Git repository.")
else:
print(f"Error pushing file to Git repository. Response status code: {response.status_code}")
The above code reads the content of the file into the content variable and then encodes it as base64 using the base64.b64encode method. It then sends a PUT request to the GitHub API to create or update the file in your Git repository. The data payload contains the encoded content of the file along with a commit message. The response status code is then checked to verify whether the operation was successful.
Note that you may also want to make sure that the headers dictionary contains the correct User-Agent field as per GitHub API requirements.

How to get temporary AWS credentials via Cognito Identity Pool

I am looking to get temporary AWS credentials through a Cognito Identity Pool to send data to a Kinesis stream through API Gateway. One thing to note is that I am trying to do this without an AWS SDK because the language it will be written in eventually does not have a compatible SDK. I am testing the solution in Python and seem to be missing something as I keep getting a 400 error. Any ideas of what I am doing wrong here?
import requests
url = 'https://cognito-identity.us-east-1.amazonaws.com' #cognito regional endpoint
headers = {
'X-AMZ-TARGET': 'com.amazonaws.cognito.identity.model.AWSCognitoIdentityService.GetCredentialsForIdentity',
'X-AMZ-DATE': '20151020T232759Z'
}
body = {
'IdentityId': 'us-east-1:123456789' #identity id
}
response = requests.post(url = url, data = body, headers = headers)
print(response)
I was able to resolve the issue by adding a content type header, converting the single quotes in the body to double quotes, and wrapping the body dictionary in quotes as well.
import requests
url = 'https://cognito-identity.us-east-1.amazonaws.com' #cognito regional endpoint
headers = {
"CONTENT-TYPE": "application/x-amz-json-1.1",
"X-AMZ-TARGET": "com.amazonaws.cognito.identity.model.AWSCognitoIdentityService.GetCredentialsForIdentity",
"X-AMZ-DATE": "20151020T232759Z"
}
body = '''{
"IdentityId": "123456789"
}'''
response = requests.post(url = url, data = body, headers = headers)
print(response.text)

Python - Microsoft Cognitive Verify API (params)

I'm trying to use the Microsoft Cognitive Verify API with python 2.7: https://dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a
The code is:
import httplib, urllib, base64
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'my key',
}
params = '{\'faceId1\': \'URL.jpg\',\'faceId2\': \'URL.jpg.jpg\'}'
try:
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/face/v1.0/verify?%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 tried letting the conn.request line like this:
conn.request("POST", "/face/v1.0/verify?%s" % params, "", headers)
The error is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>
I alrealy tried to follow and make work the following codes:
https://github.com/Microsoft/Cognitive-Emotion-Python/blob/master/Jupyter%20Notebook/Emotion%20Analysis%20Example.ipynb
Using Project Oxford's Emotion API
However I just can't make this one work. I guess there is something wrong with the params or body argument.
Any help is very appreciated.
You can refer to this question.
Obviously you did not understand the code. "{body}" means you should replace it with your body which contains your request url, just like the site says:
So you can use this api this way:
body = {
"url": "http://example.com/1.jpg"
}
…………
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/face/v1.0/detect?%s" % params, str(body), headers)
Dawid's comment looks like it should fix it (double quoting), try this for python 2.7:
import requests
url = "https://api.projectoxford.ai/face/v1.0/verify"
payload = "{\n \"faceId1\":\"A Face ID\",\n \"faceId2\":\"A Face ID\"\n}"
headers = {
'ocp-apim-subscription-key': "KEY_HERE",
'content-type': "application/json"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
for python 3:
import http.client
conn = http.client.HTTPSConnection("api.projectoxford.ai")
payload = "{\n\"faceId1\": \"A Face ID\",\n\"faceId2\": \"Another Face ID\"\n}"
headers = {
'ocp-apim-subscription-key': "keyHere",
'content-type': "application/json"
}
conn.request("POST", "/face/v1.0/verify", payload, headers)
res = conn.getresponse()
data = res.read()
There are a couple issues with your script:
You must pass face Ids and not URLs or file objects to the REST API.
You must correctly formulate the HTTP request.
However, you may find it easier to use the Python API and not the REST API. For example, once you have the face ids, you can just run result = CF.face.verify(faceid1, another_face_id=faceid2) instead of worrying about setting up the correct POST request.
You will probably need to install cognitive_face with pip. I use this API to get the face Ids for some bonus instruction.
To make this simpler, let's assume you have img1.jpg and img2.jpg on disk.
Here is an example using the REST API:
import cognitive_face as CF
from io import BytesIO
import json
import http.client
# Setup
KEY = "your subscription key"
# Get Face Ids
def get_face_id(img_file):
f = open(img_file, 'rb')
data = f.read()
f.close()
faces = CF.face.detect(BytesIO(data))
if len(faces) != 1:
raise RuntimeError('Too many faces!')
face_id = faces[0]['faceId']
return face_id
# Initialize API
CF.Key.set(KEY)
faceId1 = get_face_id('img1.jpg')
faceId2 = get_face_id('img2.jpg')
# Now that we have face ids, we can setup our request
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': KEY
}
params = {
'faceId1': faceId1,
'faceId2': faceId2
}
# The Content-Type in the header specifies that the body will
# be json so convert params to json
json_body = json.dumps(params)
try:
conn = httplib.HTTPSConnection('https://eastus.api.cognitive.microsoft.com')
conn.request("POST", "/face/v1.0/verify", body=json_body, headers=headers)
response = conn.getresponse()
data = response.read()
data = json.loads(data)
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

Microsoft Emotion API for video with Python

I'm using the following code to send my video and apparently I got no error. But the response is coming blank. How can I read the response?
########### Python 2.7 #############
import httplib, urllib, base64, json
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxxxxxxxxxxx',
'Content-Type': 'application/json'
}
video_filename = {"url":"https://fsarquivoeastus.blob.core.windows.net/public0/WhatsApp-Video-20160727.mp4"}
params = urllib.urlencode({})
try:
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, json.dumps(video_filename), headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
The Cognitive Service Video APIs, including Emotion, operate asynchronously, and by design return an empty response body when the POST succeeds. What you must do instead is retrieve the operation URL from the headers, as shown here:
response = conn.getresponse()
location = response.getheader('operation-location');
print(location);
You call GET on that location URL to check on the status of the operation. More on that here.
#FelipeSouzaLima, To getting the operation result from Emotion Recognition in Video, you need to do two steps as below.
Call the REST API for Emotion Recognition in Video, then you will get the blank response body and the operation-location header which will be called at the next step as #cthrash said.
Call the url of the value of operation-location header above with the same Ocp-Apim-Subscription-Key as request header, then you can get the json response body which includes the recognition job status. If the value of status field in the json response is Succeeded, you will get the operation result as processingResult field in the json result, please refer to the REST API of Get Recognition in Video Operation Result.

How to access the list of Azure AD Groups using python?

I'm new to python. I found the following sample code to retrieve the Azure AD groups from https://msdn.microsoft.com/en-us/Library/Azure/Ad/Graph/api/groups-operations#BasicoperationsongroupsGetgroups
the code is:
########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64
# OAuth2 is required to access this API. For more information visit: https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks
headers = {}
params = urllib.parse.urlencode({
# Specify values for the following required parameters
'api-version': '1.5',
})
try:
conn = http.client.HTTPSConnection('graph.windows.net')
#Specify values for path parameters (shown as {...}) and request body if needed
conn.request("GET", "/myorganization/groups?%s" % params, "", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
####################################
everything is fine but i don't know what would be the value of "headers = {}".
I need help i spent a lot of hours on this but no luck yet.
Base on my understanding, you need to write the Oauth Authorization information into headers, like the code below:
headers = {
#set your access token
'Authorization':'your access token'
}
Before you accessing the Graph API, you need to get the access token form AD. You can add the authorization information into your headers and request.
About how to get the access token, I suggest that you can refer to this page:
Active Directory -Authentication Scenarios

Categories

Resources