Artificial Intelligence Forum
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Python code to call ML Object Detection API

Former Member
0 Likes
1,266

Could someone please share an python example of how can we call the Object Detection API? It would help me a lot!

1 ACCEPTED SOLUTION
Read only

AndreasForster
Product and Topic Expert
Product and Topic Expert
1,096

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()

View solution in original post

2 REPLIES 2
Read only

AndreasForster
Product and Topic Expert
Product and Topic Expert
1,097

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()

Read only

Former Member
0 Likes
1,096

Thank you for the quick answer, Andreas. You've been very helpful!