가상 착용 이미지 생성

가상 테스트를 사용하면 의류 제품을 가상으로 입어보는 사람의 이미지를 생성할 수 있습니다. 사람 이미지와 의류 제품 이미지를 제공하면 가상 체험에서 해당 제품을 착용한 사람의 이미지를 생성합니다.

Colab에서 가상 테스트 사용해 보기

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. 환경에 대한 인증을 설정하세요.

    Select the tab for how you plan to use the samples on this page:

    Python

    로컬 개발 환경에서 이 페이지의 Python 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치합니다.

      외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Google Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    REST

    로컬 개발 환경에서 이 페이지의 REST API 샘플을 사용하려면 gcloud CLI에 제공한 사용자 인증 정보를 사용합니다.

      Google Cloud CLI를 설치합니다.

      외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

    자세한 내용은 Google Cloud 인증 문서의 REST 사용을 위한 인증을 참조하세요.

    이미지 생성

    Python

    설치

    pip install --upgrade google-genai

    자세한 내용은 SDK 참고 문서를 참조하세요.

    Vertex AI에서 Gen AI SDK를 사용하도록 환경 변수를 설정합니다.

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    from google import genai
    from google.genai.types import RecontextImageSource, ProductImage
    
    client = genai.Client()
    
    # TODO(developer): Update and un-comment below line
    # output_file = "output-image.png"
    
    image = client.models.recontext_image(
        model="virtual-try-on-preview-08-04",
        source=RecontextImageSource(
            person_image=Image.from_file(location="test_resources/man.png"),
            product_images=[
                ProductImage(product_image=Image.from_file(location="test_resources/sweater.jpg"))
            ],
        ),
    )
    
    image.generated_images[0].image.save(output_file)
    
    print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    요청 데이터를 사용하기 전에 다음을 바꿉니다.

    • REGION: 프로젝트가 있는 리전입니다. 지원되는 리전에 대한 자세한 내용은 Vertex AI의 생성형 AI 위치를 참고하세요.
    • PROJECT_ID: Google Cloud 프로젝트 ID
    • BASE64_PERSON_IMAGE: 사람 이미지의 Base64 인코딩 이미지입니다.
    • BASE64_PRODUCT_IMAGE: 제품 이미지의 Base64 인코딩 이미지입니다.
    • IMAGE_COUNT: 생성할 이미지 수입니다. 허용되는 값의 범위는 1~4입니다.
    • GCS_OUTPUT_PATH: 가상 체험 출력을 저장할 Cloud Storage 경로입니다.

    HTTP 메서드 및 URL:

    POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict

    JSON 요청 본문:

    {
      "instances": [
        {
          "personImage": {
            "image": {
              "bytesBase64Encoded": "BASE64_PERSON_IMAGE"
            }
          },
          "productImages": [
            {
              "image": {
                "bytesBase64Encoded": "BASE64_PRODUCT_IMAGE"
              }
            }
          ]
        }
      ],
      "parameters": {
        "sampleCount": IMAGE_COUNT,
        "storageUri": "GCS_OUTPUT_PATH"
      }
    }
    

    요청을 보내려면 다음 옵션 중 하나를 선택합니다.

    curl

    요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict"

    PowerShell

    요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict" | Select-Object -Expand Content
    요청은 이미지 객체를 반환합니다. 이 예에서는 base64로 인코딩된 이미지로 두 개의 예측 객체와 함께 두 개의 이미지 객체가 반환됩니다.
    {
      "predictions": [
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        },
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        }
      ]
    }