Testing MULTIPLE Microsoft Azure Custom Vision image recognition using Met Museum subject keywords

This is a test of using the Azure Custom Vision API with a machine learning model that has been trained with tens of thousands of Met Museum images, each of which have been associated with up to five subject keyword tags from a custom art vocabulary determined by the Met (roughly 1,000 unique terms).

The API can take any JPEG URL and provide its predictions in JSON format.

# Query MS Azure CustomVision API for tag predictions, which has been trained 
# with Met subject keywords and tens of thousands of Met images

import json
import requests
import pandas as pd
import matplotlib.pyplot as plot
from IPython.display import Image

# Load Azure Custom Vision API endpoint URL and API key
with open('azurecredentials.json') as f:
    data = json.load(f)
    PREDICTION_KEY = data['predictionkey']
    AZURE_CV_URL   = data['azure_cv_url']
# Provide image URL - string like 'https://foo.org/bar.jpg'

image = '''

Send a POST request to the Azure Custom Vision API

The Azure Custom Vision API takes a HTTP POST request and returns a JSON object with the probabilities of what matched, using a prediction model. There are roughly 1,000 art vocabulary terms determined by the Met used as the tags.

# Set POST request header to JSON and our secret key
headers = {'Content-Type' : 'application/json', 'Prediction-Key' : PREDICTION_KEY }
# Set image URL as payload - string like {"url": "https://foo.org/bar.jpg"}
payload = '{"url":"' + image.strip() + '"}'
# Send POST request to our Azure Custom Vision API
r = requests.post(AZURE_CV_URL, headers=headers, data=payload).json()

Use a pandas Dataframe to ingest and plot prediction results

# Turn JSON into a dataframe
df = pd.DataFrame(r['predictions'],columns=['tagName','probability'])

# Show image and horizontal plot anything with a probability greater than a given threshold

We are currently working on Met subject keyword to Wikidata mappings, so that these predictions can be used to generate recommended "depicts" statements for Wikidata items.