vision_service.py 807 B

1234567891011121314151617
  1. import os
  2. import vertexai
  3. from vertexai.vision_models import Image, MultiModalEmbeddingModel
  4. from typing import List
  5. class VertexVisionService:
  6. def __init__(self, project_id: str, location: str):
  7. # Ensure credentials are set before init if using service account key
  8. # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "gemini-service-key.json"
  9. vertexai.init(project=project_id, location=location)
  10. self.model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
  11. def get_image_embedding(self, image_path: str) -> List[float]:
  12. image = Image.load_from_file(image_path)
  13. # Standardizing to 1408 dimensions for consistency
  14. embeddings = self.model.get_embeddings(image=image, dimension=1408)
  15. return embeddings.image_embedding