Serverless image classification with Azure Functions and Custom Vision – Part 3

Welcome to the third part of the learning series “Serverless image classification with Azure Functions and Custom Vision”! 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.

    Create an Azure Function in Visual Studio Code
  2. Choose the directory for your project.

  3. In the Select a language window, choose Python and then choose your preferred Python interpreter.

    Select the Python language Select the Python interpreter
  4. Choose the HTTP trigger template for your project’s first function.

    Select the HTTP trigger template
  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.

    Select the Anonymous Authorization level
  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.
  • 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.

    Run the function locally
  2. In the Terminal, you can see the endpoint of your local function.

    Endpoint of the local function
  3. Choose the Azure icon, right-click on the classify function and select Execute Function Now.

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

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

    Function's response

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 and the 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 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:


Update the function’s code

  1. Open the file and after the existing import statements, import the helper function for the 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:
                req_body = req.get_json()
            except ValueError:
                image_url = req_body.get('img')'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.

    Function's response
You can 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:

You May Also Like