Welcome to the third part of the learning series “Serverless image classification with Azure Functions and Custom Vision”!
About me
Hi, I am Foteini Savvidou!
I am an undergraduate Electrical and Computer Engineering student at Aristotle University of Thessaloniki (Greece) interested in wireless communications, IoT, AI, cloud technologies and biomedical engineering. I am also Microsoft MVP and Gold Microsoft Learn Student Ambassador. Always passionate about teaching and learning new things, I love helping people expand their technical skills through organizing workshops and sharing articles on my blog.
Introduction
In the previous articles, you built an image classification model that predicts whether a photo contains a dog or a cat and exported the Custom AI model to a TensorFlow file to run offline.
To find out more about previous posts, check out the links below:
- Serverless image classification with Azure Functions and Custom Vision – Part 1
- Serverless image classification with Azure Functions and Custom Vision – Part 2
In this article, you are going to create an “intelligent” Python function that responds to HTTP requests and classifies images. You will learn how to:
- Create a Python function using Visual Studio Code and Azure Functions Core Tools.
- Run the function on your local development environment.
- Import a custom TensorFlow model into a function app.
To complete the exercise, you will need:
- A TensorFlow machine learning model for image classification exported from Azure Custom Vision.
- The Azure Functions Core Tools.
- Python 3.9.13 (or Python versions that are supported by Azure Functions).
- Visual Studio Code, the Python extension, and the Azure Functions extension for Visual Studio Code.
Create a local Azure Functions project
- Choose the Azure icon in the left toolbar bar and next to Workspace (local), select the + button. In the dropdown window, choose Create Function.
- Choose the directory for your project.
- In the Select a language window, choose Python and then choose your preferred Python interpreter.
- Choose the HTTP trigger template for your project’s first function.
- Provide a name for your function, for example, enter “classify”.
- Then, in the Authorization level window, choose Anonymous, which lets anyone call your function.
- Wait for your Azure Functions project to be generated. Visual Studio Code will create a function in your chosen language using the HTTP trigger template.
Your function project contains several files. In the following sections of this tutorial, we will edit these files:
requirements.txt
: A project-level file that lists the Python packages required by Azure Functions.__init__.py
: A Python file that contains the function’s code and is in theclassify
folder.
Run the function locally
Before editing the function’s code, let’s run the function locally!
- In the left toolbar, select the Run and Debug icon. To start the function locally, select the play icon (Start Debugging) or press F5.
- In the Terminal, you can see the endpoint of your local function.
- Choose the Azure icon, right-click on the
classify
function and select Execute Function Now.
- In the Enter request body window, leave the default values and press Enter.
- If the function runs correctly, a notification is raised in Visual Studio Code containing information about the function’s execution.
Import the TensorFlow model and modify the function’s code
To modify the classify
function to predict whether an image contains a cat or a dog, you will use the custom TensorFlow model exported from Azure Custom Vision the predict.py script that you created in the previous article.
Import the TensorFlow model and helper code
- In the
classify
folder, copy the model files into a new folder namedmodel
. Verify that the model folder contains these files: model.pb and labels.txt. - Copy the predict.py file (which contains code to run the TensorFlow model) into the
classify
folder. - Open the requirements.txt file and add the following Python libraries:
opencv-python tensorflow==2.8.0 Pillow requests
Update the function’s code
- Open the __init__.py file and after the existing
import
statements, import the helper function for the predict.py script.from .predict import predict_image_from_url
- Replace the contents of the
main
function with the following code, which retrieves the submitted image URL, calls thepredict_image_from_url()
function to classify the image using the TensorFlow model, and returns an HTTP response with the predicted label and its probability.def main(req: func.HttpRequest) -> func.HttpResponse: image_url = req.params.get('img') if not image_url: try: req_body = req.get_json() except ValueError: pass else: image_url = req_body.get('img') logging.info('Image URL received: ' + image_url) prediction = predict_image_from_url(image_url) return func.HttpResponse(f"Classified as {prediction['label']} with probability {prediction['probability']*100 :.3f}%")
- Then, run the function to install the required dependencies and review the function’s response.
Info: You can also test the function in your browser using: http://localhost:7071/api/classify?img=<URL>
Summary and next steps
In this article, you learned how to use Python, TensorFlow, and Visual Studio Code to build a function that predicts whether an image contains a cat or a dog. In the next part of this series, you will learn how to publish your Azure Functions project to Azure to build a serverless HTTP API.
If you are interested in learning more about Azure Functions, you may check out the following resources: