-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add edit image with mask sample and test (#11275)
* add edit image with mask sample * Trigger Build
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Google Cloud Vertex AI sample for editing an image using a mask. The | ||
edit is applied to the masked area of the image and is saved to a new file. | ||
Example usage: | ||
python edit_image_mask.py --project_id <project-id> --location <location> \ | ||
--input_file <filepath> --mask_file <filepath> \ | ||
--output_file <filepath> --prompt <text> | ||
""" | ||
|
||
# [START aiplatform_imagen_edit_image_mask] | ||
|
||
import argparse | ||
|
||
import vertexai | ||
from vertexai.preview.vision_models import Image, ImageGenerationModel | ||
|
||
|
||
def edit_image_mask( | ||
project_id: str, | ||
location: str, | ||
input_file: str, | ||
mask_file: str, | ||
output_file: str, | ||
prompt: str, | ||
) -> vertexai.preview.vision_models.ImageGenerationResponse: | ||
"""Edit a local image using a mask. | ||
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. Image can be in PNG or JPEG format. | ||
mask_file: Local path to the mask file. Image must be in PNG format. | ||
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) | ||
|
||
model = ImageGenerationModel.from_pretrained("imagegeneration@002") | ||
base_img = Image.load_from_file(location=input_file) | ||
mask_img = Image.load_from_file(location=mask_file) | ||
|
||
images = model.edit_image( | ||
base_image=base_img, | ||
mask=mask_img, | ||
prompt=prompt, | ||
# Optional parameters | ||
seed=1, | ||
# Controls the strength of the prompt. | ||
# -- 0-9 (low strength), 10-20 (medium strength), 21+ (high strength) | ||
guidance_scale=21, | ||
number_of_images=1, | ||
) | ||
|
||
images[0].save(location=output_file) | ||
|
||
# Optional. View the edited image in a notebook. | ||
# images[0].show() | ||
|
||
print(f"Created output image using {len(images[0]._image_bytes)} bytes") | ||
|
||
return images | ||
|
||
|
||
# [END aiplatform_imagen_edit_image_mask] | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) | ||
parser.add_argument( | ||
"--location", | ||
help="The location in which to initialize Vertex AI.", | ||
default="us-central1", | ||
) | ||
parser.add_argument( | ||
"--input_file", | ||
help="The local path to the input file (e.g., 'my-input.png').", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--mask_file", | ||
help="The local path to the mask file (e.g., 'my-mask.png').", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--output_file", | ||
help="The local path to the output file (e.g., 'my-output.png').", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--prompt", | ||
help="The text prompt describing what you want to see (e.g., 'a dog').", | ||
required=True, | ||
) | ||
args = parser.parse_args() | ||
edit_image_mask( | ||
args.project_id, | ||
args.location, | ||
args.input_file, | ||
args.mask_file, | ||
args.output_file, | ||
args.prompt, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import backoff | ||
|
||
import edit_image_mask | ||
|
||
from google.api_core.exceptions import ResourceExhausted | ||
|
||
|
||
_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources") | ||
_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
_LOCATION = "us-central1" | ||
_INPUT_FILE = os.path.join(_RESOURCES, "dog_newspaper.png") | ||
_MASK_FILE = os.path.join(_RESOURCES, "dog_newspaper_mask.png") | ||
_OUTPUT_FILE = os.path.join(_RESOURCES, "dog_book.png") | ||
_PROMPT = "a big book" | ||
|
||
|
||
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60) | ||
def test_edit_image_mask() -> None: | ||
response = edit_image_mask.edit_image_mask( | ||
_PROJECT_ID, | ||
_LOCATION, | ||
_INPUT_FILE, | ||
_MASK_FILE, | ||
_OUTPUT_FILE, | ||
_PROMPT, | ||
) | ||
|
||
assert len(response[0]._image_bytes) > 1000 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.