짧은 형식의 이미지 응답 받기

이 샘플은 Imagen 모델을 사용하여 질문하고 제공된 이미지에 대한 답변을 얻는 방법을 보여줍니다.

코드 샘플

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


import argparse

import vertexai
from vertexai.preview.vision_models import Image, ImageTextModel

def get_short_form_image_responses(
    project_id: str, location: str, input_file: str, question: str
) -> list:
    """Get short-form responses to a question about a local image.
    Args:
      project_id: Google Cloud project ID, used to initialize Vertex AI.
      location: Google Cloud region, used to initialize Vertex AI.
      input_file: Local path to the input image file.
      question: The question about the contents of the image."""

    vertexai.init(project=project_id, location=location)

    model = ImageTextModel.from_pretrained("imagetext@001")
    source_img = Image.load_from_file(location=input_file)

    answers = model.ask_question(
        image=source_img,
        question=question,
        # Optional parameters
        number_of_results=1,
    )

    print(answers)

    return answers

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.