|
| 1 | +from diffsynth import ModelManager, FluxImagePipeline, download_customized_models |
| 2 | +from diffsynth.data.video import crop_and_resize |
| 3 | +from modelscope import dataset_snapshot_download |
| 4 | +from examples.EntityControl.utils import visualize_masks |
| 5 | +from PIL import Image |
| 6 | +import numpy as np |
| 7 | +import torch |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +def build_pipeline(): |
| 12 | + model_manager = ModelManager(torch_dtype=torch.bfloat16, device="cuda", model_id_list=["FLUX.1-dev"]) |
| 13 | + model_manager.load_lora( |
| 14 | + download_customized_models( |
| 15 | + model_id="DiffSynth-Studio/Eligen", |
| 16 | + origin_file_path="model_bf16.safetensors", |
| 17 | + local_dir="models/lora/entity_control" |
| 18 | + ), |
| 19 | + lora_alpha=1 |
| 20 | + ) |
| 21 | + model_manager.load_lora( |
| 22 | + download_customized_models( |
| 23 | + model_id="iic/In-Context-LoRA", |
| 24 | + origin_file_path="visual-identity-design.safetensors", |
| 25 | + local_dir="models/lora/In-Context-LoRA" |
| 26 | + ), |
| 27 | + lora_alpha=1 |
| 28 | + ) |
| 29 | + pipe = FluxImagePipeline.from_model_manager(model_manager) |
| 30 | + return pipe |
| 31 | + |
| 32 | + |
| 33 | +def generate(pipe: FluxImagePipeline, logo_image, target_image, mask, height, width, prompt, logo_prompt, image_save_path, mask_save_path): |
| 34 | + mask = Image.fromarray(np.concatenate([ |
| 35 | + np.ones((height, width, 3), dtype=np.uint8) * 0, |
| 36 | + np.array(crop_and_resize(mask, height, width)), |
| 37 | + ], axis=1)) |
| 38 | + |
| 39 | + input_image = Image.fromarray(np.concatenate([ |
| 40 | + np.array(crop_and_resize(logo_image, height, width)), |
| 41 | + np.array(crop_and_resize(target_image, height, width)), |
| 42 | + ], axis=1)) |
| 43 | + |
| 44 | + image = pipe( |
| 45 | + prompt=prompt, |
| 46 | + input_image=input_image, |
| 47 | + cfg_scale=3.0, |
| 48 | + negative_prompt="", |
| 49 | + num_inference_steps=50, |
| 50 | + embedded_guidance=3.5, |
| 51 | + seed=0, |
| 52 | + height=height, |
| 53 | + width=width * 2, |
| 54 | + eligen_entity_prompts=[logo_prompt], |
| 55 | + eligen_entity_masks=[mask], |
| 56 | + enable_eligen_on_negative=False, |
| 57 | + enable_eligen_inpaint=True, |
| 58 | + ) |
| 59 | + image.save(image_save_path) |
| 60 | + visualize_masks(image, [mask], [logo_prompt], mask_save_path) |
| 61 | + |
| 62 | + |
| 63 | +pipe = build_pipeline() |
| 64 | + |
| 65 | +dataset_snapshot_download(dataset_id="DiffSynth-Studio/examples_in_diffsynth", local_dir="./", allow_file_pattern="data/examples/eligen/logo_transfer*") |
| 66 | +logo_image = Image.open("data/examples/eligen/logo_transfer_logo.png") |
| 67 | +target_image = Image.open("data/examples/eligen/logo_transfer_target_image.png") |
| 68 | + |
| 69 | +prompt="The two-panel image showcases the joyful identity, with the left panel showing a rabbit graphic; [LEFT] while the right panel translates the design onto a shopping tote with the rabbit logo in black, held by a person in a market setting, emphasizing the brand's approachable and eco-friendly vibe." |
| 70 | +logo_prompt="a rabbit logo" |
| 71 | + |
| 72 | +mask = Image.open("data/examples/eligen/logo_transfer_mask_1.png") |
| 73 | +generate( |
| 74 | + pipe, logo_image, target_image, mask, |
| 75 | + height=1024, width=736, |
| 76 | + prompt=prompt, logo_prompt=logo_prompt, |
| 77 | + image_save_path="entity_transfer_1.png", |
| 78 | + mask_save_path="entity_transfer_with_mask_1.png" |
| 79 | +) |
| 80 | + |
| 81 | +mask = Image.open("data/examples/eligen/logo_transfer_mask_2.png") |
| 82 | +generate( |
| 83 | + pipe, logo_image, target_image, mask, |
| 84 | + height=1024, width=736, |
| 85 | + prompt=prompt, logo_prompt=logo_prompt, |
| 86 | + image_save_path="entity_transfer_2.png", |
| 87 | + mask_save_path="entity_transfer_with_mask_2.png" |
| 88 | +) |
0 commit comments