‎2019 Oct 17 3:35 PM - edited ‎2024 Feb 04 6:52 AM
Could someone please share an python example of how can we call the Object Detection API? It would help me a lot!
‎2019 Oct 18 12:15 PM
Hello Renan, Here is some Python code that uses the face feature extractor of MLF. It shows how to create and use the Bearer token and how to pass a file and the header to MLF. So this might give you a start for object detection.
Greetings, Andreas
## Get logon credentials from service key
# Path to service key
service_key_location = "./Service Keys/Servicekey_beta.txt"
file_read = open(service_key_location, "r")
jsonRead = json.loads(file_read.read())
username = jsonRead["clientid"]
password = jsonRead["clientsecret"]
## Get Bearer token
import json
import requests
api_url = 'https://[yoururl].authentication.us10.hana.ondemand.com/oauth/token?grant_type=client_credentials'
auth_values = (username, password)
response = requests.get(api_url, auth=auth_values)
# Extract token from response
json_response = response.json()
api_token = "Bearer " + json_response["access_token"]
print(api_token)
## Identify face on image
# Obtain feature vector of face
nugget_file = "./image.jpg"
api_url = 'https://mlfproduction-face-feature-extractor.cfapps.us10.hana.ondemand.com/api/v2alpha1/image/face-feature-extraction'
files = {'files': open(nugget_file, 'rb')}
headers = {'Authorization': api_token}
response_nugget = requests.post(api_url, files=files, headers=headers)
jsonRead = json.loads(response_nugget.content.decode('utf-8'))
# Extract the detected face and display
bottom = jsonRead['predictions'][0]['faces'][0]['face_location']['bottom']
left = jsonRead['predictions'][0]['faces'][0]['face_location']['left']
right = jsonRead['predictions'][0]['faces'][0]['face_location']['right']
top = jsonRead['predictions'][0]['faces'][0]['face_location']['top']
from PIL import Image
img = Image.open(nugget_file)
area = (left, top, right, bottom)
cropped_img = img.crop(area)
cropped_img.show()
‎2019 Oct 18 12:15 PM
Hello Renan, Here is some Python code that uses the face feature extractor of MLF. It shows how to create and use the Bearer token and how to pass a file and the header to MLF. So this might give you a start for object detection.
Greetings, Andreas
## Get logon credentials from service key
# Path to service key
service_key_location = "./Service Keys/Servicekey_beta.txt"
file_read = open(service_key_location, "r")
jsonRead = json.loads(file_read.read())
username = jsonRead["clientid"]
password = jsonRead["clientsecret"]
## Get Bearer token
import json
import requests
api_url = 'https://[yoururl].authentication.us10.hana.ondemand.com/oauth/token?grant_type=client_credentials'
auth_values = (username, password)
response = requests.get(api_url, auth=auth_values)
# Extract token from response
json_response = response.json()
api_token = "Bearer " + json_response["access_token"]
print(api_token)
## Identify face on image
# Obtain feature vector of face
nugget_file = "./image.jpg"
api_url = 'https://mlfproduction-face-feature-extractor.cfapps.us10.hana.ondemand.com/api/v2alpha1/image/face-feature-extraction'
files = {'files': open(nugget_file, 'rb')}
headers = {'Authorization': api_token}
response_nugget = requests.post(api_url, files=files, headers=headers)
jsonRead = json.loads(response_nugget.content.decode('utf-8'))
# Extract the detected face and display
bottom = jsonRead['predictions'][0]['faces'][0]['face_location']['bottom']
left = jsonRead['predictions'][0]['faces'][0]['face_location']['left']
right = jsonRead['predictions'][0]['faces'][0]['face_location']['right']
top = jsonRead['predictions'][0]['faces'][0]['face_location']['top']
from PIL import Image
img = Image.open(nugget_file)
area = (left, top, right, bottom)
cropped_img = img.crop(area)
cropped_img.show()
‎2019 Oct 18 7:49 PM
Thank you for the quick answer, Andreas. You've been very helpful!