[アップデート] Amazon Nova Canvas で virtual try-on と style options がサポートされました
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
Amazon Nova Canvas で virtual try-on と style options がサポートされました。
virtual try-on
virtual try-on を直訳すると、仮想試着でしょうか。
その名の通り、衣類や靴など、試着した状態の画像生成を行うモードです。人物や空間を示す画像と、商品を示す画像の 2 つの画像アップロードし、画像生成を行います。
衣類や靴以外にも、家具など仮想試着できるようです。
Virtual try-on is an image-guided use case of inpainting in which the contents of a reference image are superimposed into a source image based on the guidance of a mask image. Amazon Nova Canvas has been tuned for garments, accessories, furniture, and related objects. The model also generalizes well to other cases, such as adding a logo or text into an image.
ドキュメントのイメージですが、すごい精度でおもしろいですね。
Visual Styles
style options はドキュメントでは Visual Styles と紹介されていました。
TEXT_TO_IMAGE
で利用可能で、次のスタイルを選択し、画像を生成するようです。
3D_ANIMATED_FAMILY_FILM
DESIGN_SKETCH
FLAT_VECTOR_ILLUSTRATION
GRAPHIC_NOVEL_ILLUSTRATION
MAXIMALISM
MIDCENTURY_RETRO
PHOTOREALISM
SOFT_DIGITAL_PAINTING
やってみた
Virtual try-on
実際にやってみましょう。まずは Virtual try-on から行います。Virtual try-on はマネジメントコンソールで提供されていたため、そちらに乗っかります。
Sub-action は次の 2 つがあります。
Replace garment
- ベース画像の服や靴を、ガーメント画像の服や靴に置き換え
Insert object
- 家具やグラフィックなどのオブジェクトを挿入して、ベース画像を修正
家具を用意できなかったため、今回は Replace garment
を選択します。
Base image が人物の画像、Garment image が服や靴になります。Base image, Garment image に以下を選んでみました。
Base image
Garment image
生成された画像は以下になります。すごいですね。
確かに金色ジャケットを見に纏うことができました。(ジャケットの下に何も着ていないように見えますがw)
Visual Styles
続いて Visual Styles です。
こちらは、執筆時点でマネジメントコンソールから機能を利用できなかったため、boto3 を利用して実行します。
プロンプトはちょうど日比谷オフィスに来ていたため、「オフィスの窓側から見える東京タワーの風景
」としてみました。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate an image from a text prompt with the Amazon Nova Canvas model (on demand).
"""
import base64
import io
import json
import logging
import boto3
from PIL import Image
from botocore.config import Config
from botocore.exceptions import ClientError
class ImageError(Exception):
"Custom exception for errors returned by Amazon Nova Canvas"
def __init__(self, message):
self.message = message
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def generate_image(model_id, body):
"""
Generate an image using Amazon Nova Canvas model on demand.
Args:
model_id (str): The model ID to use.
body (str) : The request body to use.
Returns:
image_bytes (bytes): The image generated by the model.
"""
logger.info(
"Generating image with Amazon Nova Canvas model %s", model_id)
bedrock = boto3.client(
service_name='bedrock-runtime',
config=Config(read_timeout=300)
)
accept = "application/json"
content_type = "application/json"
response = bedrock.invoke_model(
body=body, modelId=model_id, accept=accept, contentType=content_type
)
response_body = json.loads(response.get("body").read())
base64_image = response_body.get("images")[0]
base64_bytes = base64_image.encode('ascii')
image_bytes = base64.b64decode(base64_bytes)
finish_reason = response_body.get("error")
if finish_reason is not None:
raise ImageError(f"Image generation error. Error is {finish_reason}")
logger.info(
"Successfully generated image with Amazon Nova Canvas model %s", model_id)
return image_bytes
def main():
"""
Entrypoint for Amazon Nova Canvas example.
"""
logging.basicConfig(level=logging.INFO,
format="%(levelname)s: %(message)s")
model_id = 'amazon.nova-canvas-v1:0'
prompt = """オフィスの窓側から見える東京タワーの風景"""
styles = [
"3D_ANIMATED_FAMILY_FILM",
"DESIGN_SKETCH",
"FLAT_VECTOR_ILLUSTRATION",
"GRAPHIC_NOVEL_ILLUSTRATION",
"MAXIMALISM",
"MIDCENTURY_RETRO",
"PHOTOREALISM",
"SOFT_DIGITAL_PAINTING"
]
for style in styles:
logger.info(f"Generating image with style: {style}")
body = json.dumps({
"taskType": "TEXT_IMAGE",
"textToImageParams": {
"text": prompt,
"style": style
},
"imageGenerationConfig": {
"numberOfImages": 1,
"height": 1024,
"width": 1024,
"cfgScale": 8.0,
"seed": 0
}
})
try:
image_bytes = generate_image(
model_id=model_id,
body=body
)
image = Image.open(io.BytesIO(image_bytes))
# Save image with style-based filename
filename = f"{style}.png"
image.save(filename)
logger.info(f"Saved image as {filename}")
except ClientError as err:
message = err.response["Error"]["Message"]
logger.error(f"A client error occurred for style {style}:", message)
print(f"A client error occured for style {style}: " + format(message))
except ImageError as err:
logger.error(f"Error for style {style}: {err.message}")
print(f"Error for style {style}: {err.message}")
else:
print(
f"Finished generating image with style {style} using model {model_id}.")
if __name__ == "__main__":
main()
次のコード例を元に作ってみました。サンプルがあるのはありがたいですね。
生成された画像は以下のとおりです。PHOTOREALISM が、かなりリアルですごいなと思いました。(小並感)
個人的には SOFT_DIGITAL_PAINTING が好きです。
3D_ANIMATED_FAMILY_FILM
DESIGN_SKETCH
FLAT_VECTOR_ILLUSTRATION
GRAPHIC_NOVEL_ILLUSTRATION
MAXIMALISM
MIDCENTURY_RETRO
PHOTOREALISM
SOFT_DIGITAL_PAINTING
まとめ
以上、「Amazon Nova Canvas で virtual try-on と style options がサポートされました。」でした!
とくに virtual try-on は、かなりリアルな再現度で服や人物を認識しており圧巻でした。
このブログがどなたかの参考になれば幸いです。クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!