Serverless image classification with Azure Functions and Custom Vision – Part 3
Published Oct 12 2022 04:22 AM 1,734 Views
Senior Member

serverless-ai-part3.png

 

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!

 

FoteiniSavvidou_0-1665244341849.jpeg

 

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:

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

  1. Choose the Azure icon in the left toolbar bar and next to Workspace (local), select the + button. In the dropdown window, choose Create Function.

    1-create-function.png

     

  2. Choose the directory for your project.
  3. In the Select a language window, choose Python and then choose your preferred Python interpreter.

    2-python.png

     

    3-python-interpreter.png
  4. Choose the HTTP trigger template for your project’s first function.

    4-HTTP-trigger-template.png
  5. Provide a name for your function, for example, enter “classify”.
  6. Then, in the Authorization level window, choose Anonymous, which lets anyone call your function.

    5-authorization-level.png
  7. 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 the classify folder.

 

Run the function locally

Before editing the function’s code, let’s run the function locally!

  1. In the left toolbar, select the Run and Debug icon. To start the function locally, select the play icon (Start Debugging) or press F5.

    6-run-function.png
  2. In the Terminal, you can see the endpoint of your local function.

    7-terminal-endpoint.png
  3. Choose the Azure icon, right-click on the classify function and select Execute Function Now.

    8-execute-function.png

     

  4. In the Enter request body window, leave the default values and press Enter.

    9-request-body.png
  5. If the function runs correctly, a notification is raised in Visual Studio Code containing information about the function’s execution.

    10-function-response.png

 

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

 

  1. In the 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.
  2. Copy the predict.py file (which contains code to run the TensorFlow model) into the classify folder.
  3. Open the requirements.txt file and add the following Python libraries:
    opencv-python
    tensorflow==2.8.0
    Pillow
    requests

 

Update the function’s code

 

  1. 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​

     

  2. Replace the contents of the 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}%")​

     

  3. Then, run the function to install the required dependencies and review the function’s response.

    11-function-response.png

     

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:

Co-Authors
Version history
Last update:
‎Oct 12 2022 01:26 AM
Updated by: