Skip to Content

Set Up Computer Vision Package for SAP AI Core

Requires Customer/Partner License
Set up Python and install computer vision package and its dependencies like PyTorch and Detectron2. Install the SAP AI Core SDK to interact with SAP AI Core using Python.
You will learn
  • How to install Python packages for computer vision AI pipelines
  • How to execute Python code in Jupyter
  • How to connect to SAP AI Core using the SAP AI Core SDK
kannankumarKannan Presanna KumarJanuary 26, 2023
Created by
kannankumar
July 14, 2022
Contributors
maximilianone
jmmargo
kannankumar

Pre-read

The computer vision package provides a fast and easy way to train computer vision models and set up a serving infrastructure. The computer vision package works with the SAP AI Core SDK (ai-core-sdk) to provide a command line interface to create SAP AI Core templates for training and serving.
The package bundles capabilities of detectron2 (library for object detection and image segmentation) with extensions such as image classification, feature extraction and model serving.

In this tutorial, you first create a Python virtual environment and install Python packages in the Python virtual environment. You then link the virtual environment to a Jupyter notebook so that you can run computer vision pipelines on SAP AI Core from a Jupyter notebook.

After set up, you can train and serve an AI model for object detection. The aim of the model is to recognise objects (numbers) on electricity meter readings. See the related computer vision tutorials for training and serving an object detection model.

  • Step 1

    Python package detectron2 currently only supports Linux or MacOS. For this reason, it’s recommended that you set up a Linux Virtual Machine (VM) in your Windows OS to use the computer vision package and complete the tutorial. For more information about how to install Linux VM, see guide to install Ubuntu VM on VirtualBox here.


    Log in to complete tutorial
  • Step 2

    Download and install Python 3.9.X from python.org v3.9.13.

    CAUTION: Python version 3.9 is required for the computer vision package.

    pip is the Python package installer. It is required to install sap-computer-vision-package and other required packages. pip is installed along with Python.

    Check pip availability.

    BASH
    Copy
    pip --version
    
    Log in to complete tutorial
  • Step 3

    The Python virtual environment helps you install Python packages inside a sandbox-like environment. You use the environment to maintain required versions of the packages for your project.

    Create and activate a virtual Python environment using the following snippet. Note, sap_cv_env is the name of your environment.

    BASH
    Copy
    $ python -m venv sap_cv_env
    $ source sap_cv_env/bin/activate
    

    Check which Python is used by your virtual environment.

    Shell
    Copy
    $ which python
    

    You can see the path of the Python executable in your virtual environment.

    Shell
    Copy
    /Users/<username>/Documents/tutorials/sap_cv_env/bin/python
    
    Log in to complete tutorial
  • Step 4

    The ai-core-sdk Python package allows us to interact with SAP AI Core. It also provides a command line interface to content packages like the computer vision package.

    Install the SAP AI Core SDK package by running the snippet below.

    (The command pip install <package_name> downloads the package from public python repository. and installs it in your virtual environment.)

    BASH
    Copy
    pip install "ai-core-sdk[aicore-content]"
    
    image
    Log in to complete tutorial
  • Step 5

    You must install Torch and detectron2 packages.

    INFORMATION For detailed Torch installation instructions, see here. After installing Torch, the matching version of detectron2 must be installed. Check the detectron2 installation guide to select the required version. Test the package with detectron2=0.6.

    Log in to complete tutorial
  • Step 6

    Install the computer vision package.

    BASH
    Copy
    pip install sap-computer-vision-package
    

    Which three of the following packages must be installed before installing SAP Computer Vision Package?

    Log in to complete tutorial
  • Step 7

    The computer vision package uses Metaflow library for creating pipelines on SAP AI Core. Metaflow is installed along with sap-computer-vision-package. You need to configure Metaflow on your system and set your AWS connection to it.

    Paste and run the snippet. When prompted, provide the credentials for your AWS object store (example: S3).

    CAUTION For Windows users, run the snippet within your Linux VM.

    BASH
    Copy
    metaflow configure aws
    

    You should now have a Metaflow configuration file (~/.metaflowconfig/config.json) in the home directory of your user. The contents of the configuration file should look something like this.

    (Template for Metaflow configuration file)

    JSON
    Copy
    {
        "METAFLOW_DEFAULT_DATASTORE": "s3",
        "METAFLOW_DATASTORE_SYSROOT_S3": "s3://<your_object_store_bucket_name>/path/in/your/bucket"
    }
    

    Alternatively, you can manually create the configuration file (~/.metaflowconfig/config.json) for Metaflow and use your S3 bucket data from the example above.

    Log in to complete tutorial
  • Step 8

    You must ensure your AWS CLI is configured for the same bucket that you used for Metaflow

    If you don’t have awscli installed, you can install AWS CLI, and then run the snippet to configure AWS CLI.

    BASH
    Copy
    aws configure
    

    When prompted, provide account details for your AWS object store (S3). It’s similar to the example below.

    image

    For more details about configuring AWS CLI, see Official AWS user guide for configuring awscli.

    Log in to complete tutorial
  • Step 9

    Jupyter is used to execute Python code pieces in cells. You’ll paste and run Python code in the cells in your own Jupyter notebook.

    Run the snippet.

    BASH
    Copy
    pip install notebook
    
    image

    You can confirm the Jupyter installation using the following snippet.

    BASH
    Copy
    jupyter --version
    

    Example output.

    BASH
    Copy
    $ jupyter --version
    
    jupyter core     : 4.6.1
    jupyter-notebook : 6.0.3
    qtconsole        : 4.6.0
    ipython          : 7.12.0
    ipykernel        : 5.1.4
    jupyter client   : 5.3.4
    jupyter lab      : 1.2.6
    nbconvert        : 5.6.1
    ipywidgets       : 7.5.1
    nbformat         : 5.0.4
    traitlets        : 4.3.3
    

    Add the sap_cv_env virtual environment to your Jupyter installation.

    BASH
    Copy
    python -m ipykernel install --name=sap_cv_env
    

    You should see an output that indicates that the kernel is installed.

    Shell
    Copy
    Installed kernelspec sap_cv_env in /usr/local/share/jupyter/kernels/sap_cv_env
    
    Log in to complete tutorial
  • Step 10

    Navigate to the folder that you are using for this the tutorial. The folder must not be GitHub synced, as the folder may store sensitive information.

    In this tutorial, the folder is ~/tutorial.

    Run the snippet to start the Jupyter notebook.

    BASH
    Copy
    jupyter notebook
    

    This starts the Jupyter notebook, like the example shown below, and automatically opens the notebook in your default browser.

    image

    IMPORTANT: DO NOT close the terminal which started the Jupyter notebook while using the notebook. The webpage is an interface to this terminal.

    The Jupyter notebook should look similar to the example below.

    image
    Log in to complete tutorial
  • Step 11

    To create a notebook, choose New > sap_cv_env (ipykernel) in the open Jupyter session. A new notebook starts with sap_cv_env environment as the kernel.

    image

    To change the notebook name, click Untitled on the header bar.

    In the dialog box that appears, enter Meter Reading using SAP CV Package. Then choose Rename.

    image

    You’ll see the renamed file in the folder for your Jupyter session.

    image
    Log in to complete tutorial
  • Step 12

    Enter your Python code inside your Jupyter notebook.

    Python
    Copy
    print("Hi, printed using python")
    
    image
    1. Enter your Python code in the grey box. The grey box is a Jupyter notebook cell.

    2. Choose Run to execute the cell. You can also use the shortcut (CTRL + Enter).

    Log in to complete tutorial
  • Step 13

    Get the service key for your SAP AI Core. Check how to create a service key for SAP AI Core.

    If you have not provisioned SAP AI Core, check the steps to provision SAP AI Core service.

    Here’s an example service key file. Confirm that your SAP AI Core service key file has the same properties.

    JSON
    Copy
    {
        "clientid": "<YourClientID>",
        "clientsecret": "<YourClientSecret>",
        "url": "https://tutorial.authentication.sap.hana.ondemand.com",
        "identityzone": "tutorial",
        "identityzoneid": "5555a-a2c-4444-2222",
        "appname": "example-cdefg-111-12233!h7777|aicore!1111",
        "serviceurls": {
            "AI_API_URL": "https://api.ai.ml.hana.ondemand.com"
        }
    }
    

    Now, download the service key file in JSON format.

    Save the aic_service_key.json file in the folder relative to your Jupyter notebook. You’ll use the saved location to load the service key and create the AI API client, so ensure the path is correct.

    After you’ve added the aic_service_key.json file to the correct folder, it should look like this example in your Jupyter file browser.

    image
    Log in to complete tutorial
  • Step 14

    To connect the AI API client with SAP AI Core, first paste the code snippet in a new Jupyter cell.

    PYTHON
    Copy
    from ai_core_sdk.ai_core_v2_client import AICoreV2Client
    import json
    
    # Your service key JSON file relative to this notebook
    aic_service_key_path = 'aic_service_key.json'
    
    # Loads the service key file
    with open(aic_service_key_path) as ask:
        aic_service_key = json.load(ask)
    
    # Creating an AI API client instance
    aicore_client = AICoreV2Client(
        base_url = aic_service_key["serviceurls"]["AI_API_URL"] + "/v2", # The present AI API version is 2
        auth_url=  aic_service_key["url"] + "/oauth/token",
        client_id = aic_service_key['clientid'],
        client_secret = aic_service_key['clientsecret']
    )
    

    Then, execute the cell. You can either click the arrow beside the cell, press Shift + Enter on keyboard, or *alternatively: Run > Run Cells*.

    The code first loads file aic_service_key.json. Then, it creates a connection to your SAP AI Core instance via AI API client SDK. Finally, it stores the connection to the AI API client instance variable aicore_client.

    Note, you’ll use the aicore_client variable throughout the tutorial to refer to your SAP AI Core connection.

    Log in to complete tutorial
  • Step 15

    Check that the AI API is correctly set up.

    PYTHON
    Copy
    aicore_client.scenario.query('default').count
    

    You should see a numeric value based on the number of scenarios in your AI Core instance (most likely 0).

    image
    Log in to complete tutorial
  • Step 16

    Let’s assume you have the following file snippet in your SAP AI Core service key (JSON file).

    {
        ...
        "serviceurls": {
          "AI_API_URL": "https://api.ai.ml.hana.ondemand.com"
    }
    

    And given that at present the API version for AI API is v2.


    What would be the value for the parameter `base_url` to create connection to SAP AI Core using AI API client SDK? Stuck? See Step 13 for a hint.

    Log in to complete tutorial
Back to top