Build an AI Image Generator app using Imagen on Vertex AI
Code
텍스트 프롬프트를 입력으로 받아 이미지를 생성한다.
import argparse
import vertexai
from vertexai.preview.vision_models import ImageGenerationModel
def generate_image(
project_id: str, location: str, output_file: str, prompt: str
) -> vertexai.preview.vision_models.ImageGenerationResponse:
"""Generate an image using a text prompt.
Args:
project_id: Google Cloud project ID, used to initialize Vertex AI.
location: Google Cloud region, used to initialize Vertex AI.
output_file: Local path to the output image file.
prompt: The text prompt describing what you want to see."""
vertexai.init(project=project_id, location=location)
# Load Model
model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-002")
# Generate images
images = model.generate_images(
prompt=prompt,
# Optional parameters
number_of_images=1,
seed=1,
add_watermark=False,
)
images[0].save(location=output_file)
return images
generate_image(
project_id='<project-id>',
location='<REGION>',
output_file='image.jpeg',
prompt='<Text Prompt>',
)
Result
Create an image of a cricket ground in the heart of Los Angeles
우산쓰고 있는 장화 신은 미피와 멜라니 이미지를 생성해줘. 색상은 Miffy Color Palette만 사용하도록 해.
전혀 다른 이미지가 생성되었다.
프롬프트를 명사를 영어로 다시 작성하였다.
우산 쓰고 있는 장화 신은 miffy와 melanie 이미지를 생성해줘. 색상은 Miffy Color Palette만 사용하도록 해.
미피는 잘 그렸지만 멜라니는 그게 아니라고.
create an image of a halloween miffy.
제법 잘 그려냈지만 아래 글씨는 뭉개졌다.
create an image of a christmas miffy.
실사화를 원한 것은 아니었는데.
축구장에서 컴퓨터하고 있는 배구선수 김연경 이미지를 생성해줘
앞선 프롬프트와 마찬가지로 같은 이미지가 생성되었고, 다시 실행했을 때는 여자로 바뀌고 강물이 생긴 것 말고는 숲속에 있는 사람 이미지를 생성했다. 울창한 숲을 걸어가는 사람에 대한 이미지가 편향되어있는 느낌이다.