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.