Currently I am trying to work on face detection API provided by Azure. I am currently using Rest call to detect face in an image. More details could be found here: https://learn.microsoft.com/en-us/azure/cognitive-services/face/quickstarts/python
Below is the sample code for detecting face in an image:
import json, os, requests
subscription_key = os.environ['FACE_SUBSCRIPTION_KEY']
assert subscription_key
face_api_url = os.environ['FACE_ENDPOINT'] + '/face/v1.0/detect'
image_url = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {
'detectionModel': 'detection_02',
'returnFaceId': 'true'
}
response = requests.post(face_api_url, params=params,
headers=headers, json={"url": image_url})
print(json.dumps(response.json()))
Now what is want is instead of passing an image from github or any public URL, I want to pass image from my local machine. For ex an image is available on my local machine at "/home/ubuntu/index.png".
Any help on how can I do it would be appreciated.
Set the Content-Type header of the POST request to "application/octet-stream" and add the image to the HTTP body:
import os
import requests
import json
subscription_key = os.environ['FACE_SUBSCRIPTION_KEY']
face_api_url = os.environ['FACE_ENDPOINT'] + '/face/v1.0/detect'
headers = {'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key }
with open('/home/ubuntu/index.png', 'rb') as img:
res = requests.post(face_api_url , headers=headers, data=img)
res.raise_for_status()
faces = res.json()
print(faces)
API reference: https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236