Welcome to the third part of the learning series “Serverless image classification with Azure Functions and Custom Vision”!
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.
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:
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:
To complete the exercise, you will need:
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 the classify
folder.
Before editing the function’s code, let’s run the function locally!
classify
function and select Execute Function Now.
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.
classify
folder, copy the model files into a new folder named model
. Verify that the model folder contains these files: model.pb and labels.txt.classify
folder.opencv-python
tensorflow==2.8.0
Pillow
requests
import
statements, import the helper function for the predict.py script.from .predict import predict_image_from_url
main
function with the following code, which retrieves the submitted image URL, calls the predict_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}%")
Info: You can also test the function in your browser using: http://localhost:7071/api/classify?img=<URL>
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.