Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,8 @@ from transformers import (
|
|
| 9 |
CLIPTextModel,
|
| 10 |
CLIPTextModelWithProjection,
|
| 11 |
)
|
| 12 |
-
from diffusers import DDPMScheduler,AutoencoderKL
|
| 13 |
from typing import List
|
| 14 |
-
|
| 15 |
import torch
|
| 16 |
import os
|
| 17 |
from transformers import AutoTokenizer
|
|
@@ -22,293 +21,205 @@ from torchvision import transforms
|
|
| 22 |
import apply_net
|
| 23 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 24 |
from preprocess.openpose.run_openpose import OpenPose
|
| 25 |
-
from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
|
| 26 |
from torchvision.transforms.functional import to_pil_image
|
| 27 |
|
| 28 |
|
|
|
|
| 29 |
def pil_to_binary_mask(pil_image, threshold=0):
|
| 30 |
-
np_image = np.array(pil_image)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
mask = np.zeros(binary_mask.shape, dtype=np.uint8)
|
| 34 |
-
for i in range(binary_mask.shape[0]):
|
| 35 |
-
for j in range(binary_mask.shape[1]):
|
| 36 |
-
if binary_mask[i,j] == True :
|
| 37 |
-
mask[i,j] = 1
|
| 38 |
-
mask = (mask*255).astype(np.uint8)
|
| 39 |
-
output_mask = Image.fromarray(mask)
|
| 40 |
-
return output_mask
|
| 41 |
|
| 42 |
|
|
|
|
| 43 |
base_path = 'yisol/IDM-VTON'
|
| 44 |
example_path = os.path.join(os.path.dirname(__file__), 'example')
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
subfolder="unet",
|
| 49 |
-
torch_dtype=torch.float16,
|
| 50 |
-
)
|
| 51 |
unet.requires_grad_(False)
|
| 52 |
-
tokenizer_one = AutoTokenizer.from_pretrained(
|
| 53 |
-
base_path,
|
| 54 |
-
subfolder="tokenizer",
|
| 55 |
-
revision=None,
|
| 56 |
-
use_fast=False,
|
| 57 |
-
)
|
| 58 |
-
tokenizer_two = AutoTokenizer.from_pretrained(
|
| 59 |
-
base_path,
|
| 60 |
-
subfolder="tokenizer_2",
|
| 61 |
-
revision=None,
|
| 62 |
-
use_fast=False,
|
| 63 |
-
)
|
| 64 |
-
noise_scheduler = DDPMScheduler.from_pretrained(base_path, subfolder="scheduler")
|
| 65 |
-
|
| 66 |
-
text_encoder_one = CLIPTextModel.from_pretrained(
|
| 67 |
-
base_path,
|
| 68 |
-
subfolder="text_encoder",
|
| 69 |
-
torch_dtype=torch.float16,
|
| 70 |
-
)
|
| 71 |
-
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(
|
| 72 |
-
base_path,
|
| 73 |
-
subfolder="text_encoder_2",
|
| 74 |
-
torch_dtype=torch.float16,
|
| 75 |
-
)
|
| 76 |
-
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
| 77 |
-
base_path,
|
| 78 |
-
subfolder="image_encoder",
|
| 79 |
-
torch_dtype=torch.float16,
|
| 80 |
-
)
|
| 81 |
-
vae = AutoencoderKL.from_pretrained(base_path,
|
| 82 |
-
subfolder="vae",
|
| 83 |
-
torch_dtype=torch.float16,
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
| 93 |
parsing_model = Parsing(0)
|
| 94 |
openpose_model = OpenPose(0)
|
| 95 |
|
|
|
|
| 96 |
UNet_Encoder.requires_grad_(False)
|
| 97 |
image_encoder.requires_grad_(False)
|
| 98 |
vae.requires_grad_(False)
|
| 99 |
unet.requires_grad_(False)
|
| 100 |
text_encoder_one.requires_grad_(False)
|
| 101 |
text_encoder_two.requires_grad_(False)
|
| 102 |
-
tensor_transfrom = transforms.Compose(
|
| 103 |
-
[
|
| 104 |
-
transforms.ToTensor(),
|
| 105 |
-
transforms.Normalize([0.5], [0.5]),
|
| 106 |
-
]
|
| 107 |
-
)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
pipe = TryonPipeline.from_pretrained(
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
)
|
| 122 |
pipe.unet_encoder = UNet_Encoder
|
| 123 |
|
| 124 |
-
@spaces.GPU
|
| 125 |
-
def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_steps,seed):
|
| 126 |
-
device = "cuda"
|
| 127 |
-
|
| 128 |
-
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 129 |
-
pipe.to(device)
|
| 130 |
-
pipe.unet_encoder.to(device)
|
| 131 |
-
|
| 132 |
-
garm_img= garm_img.convert("RGB").resize((768,1024))
|
| 133 |
-
human_img_orig = dict["background"].convert("RGB")
|
| 134 |
-
|
| 135 |
-
if is_checked_crop:
|
| 136 |
-
width, height = human_img_orig.size
|
| 137 |
-
target_width = int(min(width, height * (3 / 4)))
|
| 138 |
-
target_height = int(min(height, width * (4 / 3)))
|
| 139 |
-
left = (width - target_width) / 2
|
| 140 |
-
top = (height - target_height) / 2
|
| 141 |
-
right = (width + target_width) / 2
|
| 142 |
-
bottom = (height + target_height) / 2
|
| 143 |
-
cropped_img = human_img_orig.crop((left, top, right, bottom))
|
| 144 |
-
crop_size = cropped_img.size
|
| 145 |
-
human_img = cropped_img.resize((768,1024))
|
| 146 |
-
else:
|
| 147 |
-
human_img = human_img_orig.resize((768,1024))
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
if is_checked:
|
| 151 |
-
keypoints = openpose_model(human_img.resize((384,512)))
|
| 152 |
-
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 153 |
-
mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
|
| 154 |
-
mask = mask.resize((768,1024))
|
| 155 |
-
else:
|
| 156 |
-
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
| 157 |
-
# mask = transforms.ToTensor()(mask)
|
| 158 |
-
# mask = mask.unsqueeze(0)
|
| 159 |
-
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 160 |
-
mask_gray = to_pil_image((mask_gray+1.0)/2.0)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
prompt = "model is wearing " + garment_des
|
| 179 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
if is_checked_crop:
|
| 237 |
-
out_img = images[0].resize(crop_size)
|
| 238 |
-
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 239 |
-
return human_img_orig, mask_gray
|
| 240 |
-
else:
|
| 241 |
-
return images[0], mask_gray
|
| 242 |
-
# return images[0], mask_gray
|
| 243 |
-
|
| 244 |
-
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
| 245 |
-
garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list]
|
| 246 |
-
|
| 247 |
-
human_list = os.listdir(os.path.join(example_path,"human"))
|
| 248 |
-
human_list_path = [os.path.join(example_path,"human",human) for human in human_list]
|
| 249 |
-
|
| 250 |
-
human_ex_list = []
|
| 251 |
-
for ex_human in human_list_path:
|
| 252 |
-
ex_dict= {}
|
| 253 |
-
ex_dict['background'] = ex_human
|
| 254 |
-
ex_dict['layers'] = None
|
| 255 |
-
ex_dict['composite'] = None
|
| 256 |
-
human_ex_list.append(ex_dict)
|
| 257 |
-
|
| 258 |
-
##default human
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
image_blocks = gr.Blocks().queue()
|
| 262 |
-
with image_blocks as demo:
|
| 263 |
-
gr.Markdown("## IDM-VTON 👕👔👚")
|
| 264 |
-
# gr.Markdown("Virtual Try-on with your image and garment image. Check out the [source codes](https://github.com/yisol/IDM-VTON) and the [model](https://huggingface.co/yisol/IDM-VTON)")
|
| 265 |
-
with gr.Row():
|
| 266 |
-
with gr.Column():
|
| 267 |
-
imgs = gr.ImageEditor(sources='upload', type="pil", label='Human. Mask with pen or use auto-masking', interactive=True)
|
| 268 |
-
with gr.Row():
|
| 269 |
-
is_checked = gr.Checkbox(label="Yes", info="Use auto-generated mask (Takes 5 seconds)",value=True)
|
| 270 |
-
with gr.Row():
|
| 271 |
-
is_checked_crop = gr.Checkbox(label="Yes", info="Use auto-crop & resizing",value=False)
|
| 272 |
-
|
| 273 |
-
example = gr.Examples(
|
| 274 |
-
inputs=imgs,
|
| 275 |
-
examples_per_page=10,
|
| 276 |
-
examples=human_ex_list
|
| 277 |
-
)
|
| 278 |
-
|
| 279 |
-
with gr.Column():
|
| 280 |
-
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
| 281 |
-
with gr.Row(elem_id="prompt-container"):
|
| 282 |
-
with gr.Row():
|
| 283 |
-
prompt = gr.Textbox(placeholder="Description of garment ex) Short Sleeve Round Neck T-shirts", show_label=False, elem_id="prompt")
|
| 284 |
-
example = gr.Examples(
|
| 285 |
-
inputs=garm_img,
|
| 286 |
-
examples_per_page=8,
|
| 287 |
-
examples=garm_list_path)
|
| 288 |
-
with gr.Column():
|
| 289 |
-
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 290 |
-
masked_img = gr.Image(label="Masked image output", elem_id="masked-img",show_share_button=False)
|
| 291 |
-
with gr.Column():
|
| 292 |
-
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
| 293 |
-
image_out = gr.Image(label="Output", elem_id="output-img",show_share_button=False)
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
with gr.Column():
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
|
| 313 |
image_blocks.launch(server_name="0.0.0.0", server_port=7860)
|
| 314 |
-
|
|
|
|
| 9 |
CLIPTextModel,
|
| 10 |
CLIPTextModelWithProjection,
|
| 11 |
)
|
| 12 |
+
from diffusers import DDPMScheduler, AutoencoderKL
|
| 13 |
from typing import List
|
|
|
|
| 14 |
import torch
|
| 15 |
import os
|
| 16 |
from transformers import AutoTokenizer
|
|
|
|
| 21 |
import apply_net
|
| 22 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 23 |
from preprocess.openpose.run_openpose import OpenPose
|
| 24 |
+
from detectron2.data.detection_utils import convert_PIL_to_numpy, _apply_exif_orientation
|
| 25 |
from torchvision.transforms.functional import to_pil_image
|
| 26 |
|
| 27 |
|
| 28 |
+
# Function to convert a PIL image to a binary mask
|
| 29 |
def pil_to_binary_mask(pil_image, threshold=0):
|
| 30 |
+
np_image = np.array(pil_image.convert("L"))
|
| 31 |
+
mask = (np_image > threshold).astype(np.uint8) * 255
|
| 32 |
+
return Image.fromarray(mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
+
# Base paths for pre-trained models and examples
|
| 36 |
base_path = 'yisol/IDM-VTON'
|
| 37 |
example_path = os.path.join(os.path.dirname(__file__), 'example')
|
| 38 |
|
| 39 |
+
# Load the UNet model for try-on
|
| 40 |
+
unet = UNet2DConditionModel.from_pretrained(base_path, subfolder="unet", torch_dtype=torch.float16)
|
|
|
|
|
|
|
|
|
|
| 41 |
unet.requires_grad_(False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Load tokenizers and other required models
|
| 44 |
+
tokenizer_one = AutoTokenizer.from_pretrained(base_path, subfolder="tokenizer", use_fast=False)
|
| 45 |
+
tokenizer_two = AutoTokenizer.from_pretrained(base_path, subfolder="tokenizer_2", use_fast=False)
|
| 46 |
+
noise_scheduler = DDPMScheduler.from_pretrained(base_path, subfolder="scheduler")
|
| 47 |
+
text_encoder_one = CLIPTextModel.from_pretrained(base_path, subfolder="text_encoder", torch_dtype=torch.float16)
|
| 48 |
+
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(base_path, subfolder="text_encoder_2", torch_dtype=torch.float16)
|
| 49 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(base_path, subfolder="image_encoder", torch_dtype=torch.float16)
|
| 50 |
+
vae = AutoencoderKL.from_pretrained(base_path, subfolder="vae", torch_dtype=torch.float16)
|
| 51 |
+
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(base_path, subfolder="unet_encoder", torch_dtype=torch.float16)
|
| 52 |
|
| 53 |
+
# Load parsing and openpose models
|
| 54 |
parsing_model = Parsing(0)
|
| 55 |
openpose_model = OpenPose(0)
|
| 56 |
|
| 57 |
+
# Freeze the parameters of the models to avoid gradients
|
| 58 |
UNet_Encoder.requires_grad_(False)
|
| 59 |
image_encoder.requires_grad_(False)
|
| 60 |
vae.requires_grad_(False)
|
| 61 |
unet.requires_grad_(False)
|
| 62 |
text_encoder_one.requires_grad_(False)
|
| 63 |
text_encoder_two.requires_grad_(False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Image transformation function
|
| 66 |
+
tensor_transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5], [0.5])])
|
| 67 |
+
|
| 68 |
+
# Initialize the pipeline for try-on
|
| 69 |
pipe = TryonPipeline.from_pretrained(
|
| 70 |
+
base_path,
|
| 71 |
+
unet=unet,
|
| 72 |
+
vae=vae,
|
| 73 |
+
feature_extractor=CLIPImageProcessor(),
|
| 74 |
+
text_encoder=text_encoder_one,
|
| 75 |
+
text_encoder_2=text_encoder_two,
|
| 76 |
+
tokenizer=tokenizer_one,
|
| 77 |
+
tokenizer_2=tokenizer_two,
|
| 78 |
+
scheduler=noise_scheduler,
|
| 79 |
+
image_encoder=image_encoder,
|
| 80 |
+
torch_dtype=torch.float16,
|
| 81 |
)
|
| 82 |
pipe.unet_encoder = UNet_Encoder
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# Main function for try-on with error handling
|
| 86 |
+
@spaces.GPU
|
| 87 |
+
def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed):
|
| 88 |
+
try:
|
| 89 |
+
device = "cuda"
|
| 90 |
+
|
| 91 |
+
# Prepare the device and models for computation
|
| 92 |
+
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 93 |
+
pipe.to(device)
|
| 94 |
+
pipe.unet_encoder.to(device)
|
| 95 |
+
|
| 96 |
+
# Prepare the images
|
| 97 |
+
garm_img = garm_img.convert("RGB").resize((768, 1024))
|
| 98 |
+
human_img_orig = dict["background"].convert("RGB")
|
| 99 |
+
|
| 100 |
+
# Handle cropping if needed
|
| 101 |
+
if is_checked_crop:
|
| 102 |
+
width, height = human_img_orig.size
|
| 103 |
+
target_width = int(min(width, height * (3 / 4)))
|
| 104 |
+
target_height = int(min(height, width * (4 / 3)))
|
| 105 |
+
left = (width - target_width) / 2
|
| 106 |
+
top = (height - target_height) / 2
|
| 107 |
+
right = (width + target_width) / 2
|
| 108 |
+
bottom = (height + target_height) / 2
|
| 109 |
+
cropped_img = human_img_orig.crop((left, top, right, bottom))
|
| 110 |
+
crop_size = cropped_img.size
|
| 111 |
+
human_img = cropped_img.resize((768, 1024))
|
| 112 |
+
else:
|
| 113 |
+
human_img = human_img_orig.resize((768, 1024))
|
| 114 |
+
|
| 115 |
+
# Apply masking if selected
|
| 116 |
+
if is_checked:
|
| 117 |
+
keypoints = openpose_model(human_img.resize((384, 512)))
|
| 118 |
+
model_parse, _ = parsing_model(human_img.resize((384, 512)))
|
| 119 |
+
mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
|
| 120 |
+
mask = mask.resize((768, 1024))
|
| 121 |
+
else:
|
| 122 |
+
mask = pil_to_binary_mask(dict['layers'][0].convert("RGB").resize((768, 1024)))
|
| 123 |
+
|
| 124 |
+
mask_gray = (1 - transforms.ToTensor()(mask)) * tensor_transform(human_img)
|
| 125 |
+
mask_gray = to_pil_image((mask_gray + 1.0) / 2.0)
|
| 126 |
+
|
| 127 |
+
# Apply pose estimation
|
| 128 |
+
human_img_arg = _apply_exif_orientation(human_img.resize((384, 512)))
|
| 129 |
+
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 130 |
+
|
| 131 |
+
args = apply_net.create_argument_parser().parse_args(
|
| 132 |
+
('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda')
|
| 133 |
+
)
|
| 134 |
+
pose_img = args.func(args, human_img_arg)
|
| 135 |
+
pose_img = pose_img[:, :, ::-1]
|
| 136 |
+
pose_img = Image.fromarray(pose_img).resize((768, 1024))
|
| 137 |
+
|
| 138 |
+
# Generate the try-on image
|
| 139 |
+
with torch.no_grad():
|
| 140 |
+
with torch.cuda.amp.autocast():
|
| 141 |
prompt = "model is wearing " + garment_des
|
| 142 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 143 |
+
prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = pipe.encode_prompt(
|
| 144 |
+
prompt, num_images_per_prompt=1, do_classifier_free_guidance=True, negative_prompt=negative_prompt
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Cloth prompt embedding
|
| 148 |
+
prompt = "a photo of " + garment_des
|
| 149 |
+
prompt_embeds_c, _, _, _ = pipe.encode_prompt(
|
| 150 |
+
prompt, num_images_per_prompt=1, do_classifier_free_guidance=False, negative_prompt=negative_prompt
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Convert pose image and garment to tensors
|
| 154 |
+
pose_img = tensor_transform(pose_img).unsqueeze(0).to(device, torch.float16)
|
| 155 |
+
garm_tensor = tensor_transform(garm_img).unsqueeze(0).to(device, torch.float16)
|
| 156 |
+
generator = torch.Generator(device).manual_seed(seed) if seed is not None else None
|
| 157 |
+
|
| 158 |
+
# Run the pipeline
|
| 159 |
+
images = pipe(
|
| 160 |
+
prompt_embeds=prompt_embeds.to(device, torch.float16),
|
| 161 |
+
negative_prompt_embeds=negative_prompt_embeds.to(device, torch.float16),
|
| 162 |
+
pooled_prompt_embeds=pooled_prompt_embeds.to(device, torch.float16),
|
| 163 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(device, torch.float16),
|
| 164 |
+
num_inference_steps=denoise_steps,
|
| 165 |
+
generator=generator,
|
| 166 |
+
strength=1.0,
|
| 167 |
+
pose_img=pose_img.to(device, torch.float16),
|
| 168 |
+
text_embeds_cloth=prompt_embeds_c.to(device, torch.float16),
|
| 169 |
+
cloth=garm_tensor.to(device, torch.float16),
|
| 170 |
+
mask_image=mask,
|
| 171 |
+
image=human_img,
|
| 172 |
+
height=1024,
|
| 173 |
+
width=768,
|
| 174 |
+
ip_adapter_image=garm_img.resize((768, 1024)),
|
| 175 |
+
guidance_scale=2.0,
|
| 176 |
+
)[0]
|
| 177 |
+
|
| 178 |
+
if is_checked_crop:
|
| 179 |
+
out_img = images[0].resize(crop_size)
|
| 180 |
+
human_img_orig.paste(out_img, (int(left), int(top)))
|
| 181 |
+
return human_img_orig, mask_gray
|
| 182 |
+
else:
|
| 183 |
+
return images[0], mask_gray
|
| 184 |
+
|
| 185 |
+
except Exception as e:
|
| 186 |
+
print(f"Error during try-on: {e}")
|
| 187 |
+
return None, None
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# Gradio interface setup
|
| 191 |
+
garm_list = os.listdir(os.path.join(example_path, "cloth"))
|
| 192 |
+
garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_list]
|
| 193 |
+
human_list = os.listdir(os.path.join(example_path, "human"))
|
| 194 |
+
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
| 195 |
+
human_ex_list = [{"background": ex_human, "layers": None, "composite": None} for ex_human in human_list_path]
|
| 196 |
+
|
| 197 |
+
# Gradio blocks UI
|
| 198 |
+
with gr.Blocks() as image_blocks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
with gr.Column():
|
| 200 |
+
with gr.Row():
|
| 201 |
+
imgs = gr.Image(source='upload', type="pil", label='Person Image')
|
| 202 |
+
is_checked = gr.Checkbox(label="Check if mask needed")
|
| 203 |
+
is_checked_crop = gr.Checkbox(label="Check to crop")
|
| 204 |
+
ex_img = gr.Examples(inputs=imgs, examples_per_page=9, examples=human_ex_list)
|
| 205 |
+
with gr.Column():
|
| 206 |
+
garm_img = gr.Image(source='upload', type="pil", label='Cloth')
|
| 207 |
+
garment_des = gr.Textbox(label="Garment Description", value='garment,shirt')
|
| 208 |
+
ex_garm = gr.Examples(inputs=garm_img, examples_per_page=9, examples=garm_list_path)
|
| 209 |
+
with gr.Row():
|
| 210 |
+
denoise_steps = gr.Slider(label="denoise steps", minimum=1, maximum=50, step=1, value=25)
|
| 211 |
+
seed = gr.Slider(label="Seed (for reproducible results)", minimum=0, maximum=2147483647, step=1)
|
| 212 |
+
with gr.Row():
|
| 213 |
+
try_button = gr.Button("Try it on")
|
| 214 |
+
with gr.Row():
|
| 215 |
+
out_img = gr.Image(label="Generated tryon output")
|
| 216 |
+
masked_img = gr.Image(label="Mask")
|
| 217 |
+
|
| 218 |
+
try_button.click(
|
| 219 |
+
start_tryon,
|
| 220 |
+
inputs=[imgs, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed],
|
| 221 |
+
outputs=[out_img, masked_img]
|
| 222 |
+
)
|
| 223 |
|
| 224 |
+
# Launch Gradio app
|
| 225 |
image_blocks.launch(server_name="0.0.0.0", server_port=7860)
|
|
|