1. Objectives
Overview
This codelab will focus on creating a Vertex AI Vision application end-to-end to monitor queue size using retail video footage. We will use the pretrained Specialized model Occupancy analytics inbuilt features to capture the following things:
- Count the number of people standing in the queue.
- Count the number of people getting served at the counter.
What you'll learn
- How to create an application in Vertex AI Vision and deploy it
- How to set up an RTSP stream using a video file and ingest the stream into Vertex AI Vision using vaictl from a Jupyter Notebook.
- How to use the Occupancy Analytics model and its different features.
- How to search for videos in your storage Vertex AI Vision's Media Warehouse.
- How to connect output to BigQuery, write SQL query to extract insights from the model's json output and use the output to label and annotate the original video.
Cost:
The total cost to run this lab on Google Cloud is about $2.
2. Before You Begin
Create a project and enable APIs:
- In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project. Go to project selector
- Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
- Enable the Compute Engine, Vertex API, Notebook API and Vision AI API. Enable the APIs
Create a service account:
- In the Google Cloud console, go to the Create service account page. Go to Create service account
- Select your project.
- In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name. In the Service account description field, enter a description. For example, Service account for quickstart.
- Click Create and continue.
- To provide access to your project, grant the following role(s) to your service account:
- Vision AI > Vision AI Editor
- Compute Engine > Compute Instance Admin (beta)
- BigQuery > BigQuery Admin .
In the Select a role list, select a role. For additional roles, click Add another role and add each additional role.
- Click Continue.
- Click Done to finish creating the service account. Do not close your browser window. You will use it in the next step.
3. Set up Jupyter Notebook
Before creating an App in Occupancy Analytics, you must register a stream which can be used later by the App.
In this tutorial you create a Jupyter Notebook instance that hosts a video, and you send that streaming video data from the notebook. We are using jupyter notebook as it offers us flexibility to do execute shell commands as well as run custom pre/post processing code in a single place that is very good for rapid experimentation. We wiil use this notebook to:
- Run rtsp server as a background process
- Run vaictl command as background process
- Run queries and processing code to analyze occupancy analytics output
Create a Jupyter Notebook
The first step in sending video from a Jupyter Notebook instance is creating the notebook with our service account created in the previous step.
- In the console, go to the Vertex AI page. Go to Vertex AI Workbench
- Click User-Managed Notebooks

- Click on New Notebook > Tensorflow Enterprise 2.6 (with LTS) > Without GPUs

- Enter the name for jupyter notebook. For more information, see Resource naming convention.

- Click on ADVANCED OPTIONS
- Scroll down to Permissions Sections
- Uncheck Use Compute Engine default service account option
- Add the Service account email created in the previous step. And click on Create.

- Once the instance has been created click on OPEN JUPYTERLAB.
4. Set up a Notebook to stream video
Before creating an App in Occupancy Analytics, you must register a stream which can be used later by the App.
In this tutorial we will use our Jupyter Notebook instance to host a video, and you send that streaming video data from the Notebook terminal.
Download the vaictl command-line tool
- In the opened Jupyterlab instance, Open a Notebook from the launcher.

- Download the Vertex AI Vision (vaictl) command line tool, rtsp server command line tool, open-cv tool using the following command in the notebook cell:
!wget -q https://github.com/aler9/rtsp-simple-server/releases/download/v0.20.4/rtsp-simple-server_v0.20.4_linux_amd64.tar.gz
!wget -q https://github.com/google/visionai/releases/download/v0.0.4/visionai_0.0-4_amd64.deb
!tar -xf rtsp-simple-server_v0.20.4_linux_amd64.tar.gz
!pip install opencv-python --quiet
!sudo apt-get -qq remove -y visionai
!sudo apt-get -qq install -y ./visionai_0.0-4_amd64.deb
!sudo apt-get -qq install -y ffmpeg
5. Ingest a video file for streaming
After you set up your notebook environment with required command line tools, you can copy a sample video file and then use vaictl to stream the video data to your occupancy analytics app.
Register a new stream
- Click streams tab on the left panel of Vertex AI Vision.
- Click on Register Button at the top

- In the Stream name enter ‘queue-stream'
- In region, choose the same region selected during Notebook creation in the previous step.
- Click Register
Copy a sample video to your VM
- In your notebook, copy a sample video with the following wget command.
!wget -q https://github.com/vagrantism/interesting-datasets/raw/main/video/collective_activity/seq25_h264.mp4
Stream video from VM and ingest data into your stream
- To send this local video file to the app input stream, use the following command in your notebook cell. You must make the following variable substitutions:
- PROJECT_ID: Your Google Cloud project ID.
- LOCATION: Your location ID. For example, us-central1. For more information, see Cloud locations.
- LOCAL_FILE: The filename of a local video file. For example,
seq25_h264.mp4.
PROJECT_ID='<Your Google Cloud project ID>'
LOCATION='<Your stream location>'
LOCAL_FILE='seq25_h264.mp4'
STREAM_NAME='queue-stream'
- Start a rtsp-simple-server where we stream the video file with rtsp protocol
import os
import time
import subprocess
subprocess.Popen(["nohup", "./rtsp-simple-server"], stdout=open('rtsp_out.log', 'a'), stderr=open('rtsp_err.log', 'a'), preexec_fn=os.setpgrp)
time.sleep(5)
- Use ffmpeg command line tool to loop the video in rtsp stream
subprocess.Popen(["nohup", "ffmpeg", "-re", "-stream_loop", "-1", "-i", LOCAL_FILE, "-c", "copy", "-f", "rtsp", f"rtsp://localhost:8554/{LOCAL_FILE.split('.')[0]}"], stdout=open('ffmpeg_out.log', 'a'), stderr=open('ffmpeg_err.log', 'a'), preexec_fn=os.setpgrp)
time.sleep(5)
- Use the vaictl command line tool to stream the video from rtsp server uri to our Vertex AI Vision stream ‘queue-stream' created in previous step.
subprocess.Popen(["nohup", "vaictl", "-p", PROJECT_ID, "-l", LOCATION, "-c", "application-cluster-0", "--service-endpoint", "visionai.googleapis.com", "send", "rtsp", "to", "streams", "queue-stream", "--rtsp-uri", f"rtsp://localhost:8554/{LOCAL_FILE.split('.')[0]}"], stdout=open('vaictl_out.log', 'a'), stderr=open('vaictl_err.log', 'a'), preexec_fn=os.setpgrp)
It might take ~100 seconds between starting the vaictl ingest operation and the video appearing in the dashboard.
After the stream ingestion is available, you can see the video feed in the Streams tab of the Vertex AI Vision dashboard by selecting the queue-stream stream.

6. Create an application
The first step is to create an app that processes your data. An app can be thought of as an automated pipeline that connects the following:
- Data ingestion: A video feed is ingested into a stream.
- Data analysis: An AI(Computer Vision) model can be added after the ingestion.
- Data storage: The two versions of the video feed (the original stream and the stream processed by the AI model) can be stored in a media warehouse.
In the Google Cloud console an app is represented as a graph.
Create an empty app
Before you can populate the app graph, you must first create an empty app.
Create an app in the Google Cloud console.
- Go to Google Cloud console.
- Open the Applications tab of the Vertex AI Vision dashboard. Go to the Applications tab
- Click the Create button.

- Enter ‘queue-app' as the app name and choose your region.
- Click Create.
Add app component nodes
After you have created the empty application, you can then add the three nodes to the app graph:
- Ingestion node: The stream resource that ingests data sent from a rtsp video server you created in notebook.
- Processing node: The occupancy analytics model that acts on ingested data.
- Storage node: The media warehouse that stores processed videos, and serves as a metadata store. The metadata stores include analytics information about ingested video data, and inferred information by the AI models.
Add component nodes to your app in the console.
- Open the Applications tab of the Vertex AI Vision dashboard. Go to the Applications tab
This takes you to the graph visualization of the processing pipeline.
Add a data ingestion node
- To add an input stream node, select the Streams option in the Connectors section of the side menu.
- In the Source section of the Stream menu that opens, select Add streams.
- In the Add streams menu, choose queue-stream.
- To add the stream to the app graph, click Add streams.
Add a data processing node
- To add the occupancy count model node, select the occupancy analytics option in the Specialized models section of the side menu.
- Leave the default selections People. Uncheck Vehicles if it is already selected.

- In the Advanced Options section, Click on Create Active Zones/Lines

- Draw the active zones using the Polygon tool to count people in that zone. Label the zone accordingly

- Click on Back Arrow at top.

- Add settings for dwell time to detect congestion by clicking on Checkbox.

Add a data storage node
- To add the output destination (storage) node, select the VIsion AI Warehouse option in the Connectors section of the side menu.
- Click on Vertex AI Warehouse Connector to open its menu, click Connect warehouse.
- In the Connect warehouse menu, select Create new warehouse. Name the warehouse queue-warehouse, and leave the TTL duration at 14 days.
- Click the Create button to add the warehouse.