Learn more
How To

Integrating Vertex AI with Google BigQuery for Continuous Image Analysis in a Streamlit Application

Integrate Google BigQuery and Vertex AI within a Streamlit application for continuous image classification. This blog post guides you through setting up your environment, creating a Streamlit app, and leveraging BigQuery and Vertex AI for real-time image analysis. Learn how to build a powerful workflow for uploading images, storing paths in BigQuery, and using Vertex AI for image classification, all within an interactive Streamlit interface.

Britton Stamper
July 22, 2024
Integrating Vertex AI with Google BigQuery for Continuous Image Analysis in a Streamlit Application
Table of Contents

Google BigQuery and Vertex AI together provide a powerful combination for advanced image analysis tasks. By leveraging these tools within a Streamlit application, you can create a seamless workflow for continuous image classification. This blog post guides you through setting up this integration.

Step 1: Set Up Your Environment

Install Required Libraries

First, ensure you have the necessary libraries installed:

pip install streamlit google-cloud-bigquery google-cloud-aiplatform google-auth

Set Up Google Cloud Credentials

Make sure your Google Cloud credentials are configured:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credentials.json"

Step 2: Create the Streamlit Application

Import Required Libraries

Start by importing the required libraries and initializing the clients:

import streamlit as st
from google.cloud import bigquery
from google.cloud import aiplatform

# Initialize clients
bq_client = bigquery.Client()
aiplatform.init()

Upload Images and Store Paths in BigQuery

Create functions to upload images to Google Cloud Storage and store their paths in BigQuery:

def upload_image_to_gcs(image_file, bucket_name):
   from google.cloud import storage
   client = storage.Client()
   bucket = client.bucket(bucket_name)
   blob = bucket.blob(image_file.name)
   blob.upload_from_string(image_file.read(), content_type=image_file.type)
   return blob.public_url

def store_image_path_in_bigquery(image_path):
   table_id = "your_project.your_dataset.image_data"
   rows_to_insert = [
       {u"image_path": image_path, u"processed": False}
   ]
   errors = bq_client.insert_rows_json(table_id, rows_to_insert)
   if errors == []:
       print("New rows have been added.")
   else:
       print("Encountered errors while inserting rows: {}".format(errors))

st.title("Image Upload and Classification")

uploaded_file = st.file_uploader("Choose an image...", type="jpg")

if uploaded_file is not None:
   image_path = upload_image_to_gcs(uploaded_file, "your_bucket_name")
   store_image_path_in_bigquery(image_path)
   st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
   st.write("Image uploaded and stored successfully!")

Classify Images Using Vertex AI

Add functionality to classify images using a model in Vertex AI:

def classify_images():
   query = """
   SELECT image_path FROM `your_project.your_dataset.image_data`
   WHERE processed = FALSE
   """
   rows = bq_client.query(query).result()

   model = aiplatform.Model(model_name='projects/your_project/locations/your_location/models/your_model_id')

   for row in rows:
       image_path = row['image_path']
       prediction = model.predict(image_path)

       # Update BigQuery with the prediction result
       bq_client.query(f"""
           UPDATE `your_project.your_dataset.image_data`
           SET prediction = '{prediction}', processed = TRUE
           WHERE image_path = '{image_path}'
       """).result()

if st.button("Classify Images"):
   classify_images()
   st.write("Images classified successfully!")

Step 3: Run Your Streamlit Application

To run your Streamlit application, use the following command:

streamlit run your_script.py

Conclusion

By following the steps outlined above, you can create a Streamlit application that integrates Google BigQuery and Vertex AI for continuous image analysis. This setup allows users to upload images, store them in Google Cloud Storage, save the image paths in BigQuery, and perform image classification using Vertex AI. This integration ensures real-time insights and efficient processing, leveraging the powerful capabilities of Google Cloud services. For more detailed information, refer to the Google Cloud documentation.

We're here to help!

Get the Semantic Layer Guide!

Everything that a data leader needs to understand and deploy metrics at scale

Download The Full Guide

Core Semantic Layer Concepts

Benefits and ROI

Implementation Steps

Get started with the next generation of data applications

Create an account to connect your business and elevate how your operate.

ABOUT THE AUTHOR
Britton Stamper

Britton is the CTO of Push.ai and oversees Product, Design, and Engineering. He's been a passionate builder, analyst and designer who loves all things data products and growth. You can find him reading books at a coffee shop or finding winning strategies in board games and board rooms.

Enjoyed this read?

Stay up to date with the latest product updates and insights sent straight to your inbox!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.