Image Generation and Analysis with Google Vertex AI

January 20, 2025

Setting Up Vertex AI

First, install the required dependencies:

pip install google-cloud-aiplatform pillow

Image Generation

Using Vertex AI's ImageGenerationModel, we can create custom images from text prompts. Here's how to generate a bouquet image:

from vertexai.preview.vision_models import ImageGenerationModel
import vertexai

def generate_bouquet_image(project_id, location, output_file, prompt):
    vertexai.init(project=project_id, location=location)
    model = ImageGenerationModel.from_pretrained("imagegeneration@002")
    
    images = model.generate_images(
        prompt=prompt,
        number_of_images=1,
        seed=1,
        add_watermark=False,
    )
    images[0].save(location=output_file)

Image Analysis with Gemini Pro Vision

After generating the image, we can analyze it using Gemini Pro Vision model:

from vertexai.generative_models import GenerativeModel, Image

def analyze_bouquet_image(image_path):
    image = Image.load_from_file(image_path)
    multimodal_model = GenerativeModel("gemini-pro-vision")
    
    prompt = "generate birthday wishes based on the image passed"
    contents = [image, prompt]
    responses = multimodal_model.generate_content(contents)
    return responses

Usage Example

Here's how to use both functions together:

project_id = "your-project-id"
location = "us-central1"
output_file = "image.jpeg"
prompt = "Create an image containing a bouquet of 2 sunflowers and 3 roses"

# Generate image
generate_bouquet_image(project_id, location, output_file, prompt)

# Analyze generated image
result = analyze_bouquet_image(output_file)

Key Features

  • Text-to-image generation using Vertex AI
  • Image analysis with Gemini Pro Vision
  • Support for custom prompts and multiple images
  • Local image saving and processing
  • Integration with Google Cloud Platform

Reference:

#VertexAI #Python #MachineLearning #GoogleCloud