Skip to content

Instantly share code, notes, and snippets.

@setuc
Created March 30, 2020 08:58
Show Gist options
  • Select an option

  • Save setuc/38cafb857a4ba672773edaae665265c6 to your computer and use it in GitHub Desktop.

Select an option

Save setuc/38cafb857a4ba672773edaae665265c6 to your computer and use it in GitHub Desktop.
Azure Functions replacement __init__ for custom vision
import logging
import json
import azure.functions as func
from .predict import predict_url, log_msg
def main(req: func.HttpRequest) -> func.HttpResponse:
image_url = req.params.get('img')
logging.info('Image URL received: ' + image_url)
results = predict_url(image_url)
headers = {
"Content-type": "application/json",
"Access-Control-Allow-Origin": "*"
}
return func.HttpResponse(json.dumps(results), headers = headers)
@setuc
Copy link
Author

setuc commented Mar 30, 2020

Modify the predict file to provide a path for the model weight file and the file containing the labels.

import os

scriptpath = os.path.abspath(__file__)
scriptdir = os.path.dirname(scriptpath)
filename = os.path.join(scriptdir, 'model.pb')

labels_filename = os.path.join(scriptdir,'labels.txt')

@setuc
Copy link
Author

setuc commented Mar 30, 2020

Now fix the predict_url function by adding these two lines for the initialization of weights. The first two lines of the function would look like this:

def predict_url(imageUrl):
    log_msg("Loading the TF Model")
    initialize()

@setuc
Copy link
Author

setuc commented Mar 30, 2020

Your requirements file will look like this

azure-functions
numpy==1.17.3
tensorflow==2.0.0
flask
pillow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment