Spaces:
Running
on
Zero
Running
on
Zero
重新优化界面
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ 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)
|
|
@@ -121,61 +121,53 @@ pipe = TryonPipeline.from_pretrained(
|
|
| 121 |
)
|
| 122 |
pipe.unet_encoder = UNet_Encoder
|
| 123 |
|
|
|
|
|
|
|
| 124 |
@spaces.GPU
|
| 125 |
-
def
|
|
|
|
| 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 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 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 |
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 164 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 165 |
-
|
| 166 |
-
|
| 167 |
|
| 168 |
args = apply_net.create_argument_parser().parse_args(('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'))
|
| 169 |
# verbosity = getattr(args, "verbosity", None)
|
| 170 |
pose_img = args.func(args,human_img_arg)
|
| 171 |
pose_img = pose_img[:,:,::-1]
|
| 172 |
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
with torch.no_grad():
|
| 175 |
# Extract the images
|
| 176 |
with torch.cuda.amp.autocast():
|
| 177 |
with torch.no_grad():
|
| 178 |
-
prompt = "model is wearing "
|
| 179 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 180 |
with torch.inference_mode():
|
| 181 |
(
|
|
@@ -189,125 +181,117 @@ def start_tryon(dict,garm_img,garment_des,is_checked,is_checked_crop,denoise_ste
|
|
| 189 |
do_classifier_free_guidance=True,
|
| 190 |
negative_prompt=negative_prompt,
|
| 191 |
)
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
prompt = [prompt] * 1
|
| 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 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 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 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
|
| 274 |
-
inputs=
|
| 275 |
-
examples_per_page=
|
| 276 |
-
examples=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
)
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
with gr.Column():
|
| 280 |
-
|
| 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 |
-
try_button = gr.Button(value="Try-on")
|
| 300 |
-
with gr.Accordion(label="Advanced Settings", open=False):
|
| 301 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 302 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
| 303 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
image_blocks.launch()
|
| 313 |
-
|
|
|
|
| 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 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 28 |
|
| 29 |
def pil_to_binary_mask(pil_image, threshold=0):
|
| 30 |
np_image = np.array(pil_image)
|
|
|
|
| 121 |
)
|
| 122 |
pipe.unet_encoder = UNet_Encoder
|
| 123 |
|
| 124 |
+
progress=gr.Progress()
|
| 125 |
+
|
| 126 |
@spaces.GPU
|
| 127 |
+
def infer(person,garment,denoise_steps,seed):
|
| 128 |
+
progress(0,desc="Starting")
|
| 129 |
device = "cuda"
|
| 130 |
+
|
| 131 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
| 132 |
pipe.to(device)
|
| 133 |
pipe.unet_encoder.to(device)
|
| 134 |
|
| 135 |
+
human_img = person.convert("RGB").resize((768,1024))
|
| 136 |
+
garm_img= garment.convert("RGB").resize((768,1024))
|
| 137 |
+
|
| 138 |
+
progress(0.1,desc="Mask generating")
|
| 139 |
|
| 140 |
+
keypoints = openpose_model(human_img.resize((384,512)))
|
| 141 |
+
model_parse, _ = parsing_model(human_img.resize((384,512)))
|
| 142 |
+
mask, mask_gray = get_mask_location('hd', "upper_body", model_parse, keypoints)
|
| 143 |
+
mask = mask.resize((768,1024))
|
| 144 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
mask_gray = (1-transforms.ToTensor()(mask)) * tensor_transfrom(human_img)
|
| 146 |
mask_gray = to_pil_image((mask_gray+1.0)/2.0)
|
| 147 |
|
| 148 |
+
progress(0.3,desc="DensePose processing")
|
| 149 |
|
| 150 |
human_img_arg = _apply_exif_orientation(human_img.resize((384,512)))
|
| 151 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
|
|
|
|
|
|
| 152 |
|
| 153 |
args = apply_net.create_argument_parser().parse_args(('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'))
|
| 154 |
# verbosity = getattr(args, "verbosity", None)
|
| 155 |
pose_img = args.func(args,human_img_arg)
|
| 156 |
pose_img = pose_img[:,:,::-1]
|
| 157 |
pose_img = Image.fromarray(pose_img).resize((768,1024))
|
| 158 |
+
|
| 159 |
+
progress(0.5,desc="Image generating")
|
| 160 |
+
|
| 161 |
+
def callback(pipe, step, timestep, callback_kwargs):
|
| 162 |
+
progress_value = 0.5 + ((step+1.0)/denoise_steps)*(0.5/1.0)
|
| 163 |
+
progress(progress_value, desc=f"Image generating, {step + 1}/{denoise_steps} steps")
|
| 164 |
+
return callback_kwargs
|
| 165 |
|
| 166 |
with torch.no_grad():
|
| 167 |
# Extract the images
|
| 168 |
with torch.cuda.amp.autocast():
|
| 169 |
with torch.no_grad():
|
| 170 |
+
prompt = "model is wearing clothing"
|
| 171 |
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 172 |
with torch.inference_mode():
|
| 173 |
(
|
|
|
|
| 181 |
do_classifier_free_guidance=True,
|
| 182 |
negative_prompt=negative_prompt,
|
| 183 |
)
|
| 184 |
+
|
| 185 |
+
prompt = "a photo of clothing"
|
| 186 |
+
negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 187 |
+
if not isinstance(prompt, List):
|
| 188 |
prompt = [prompt] * 1
|
| 189 |
+
if not isinstance(negative_prompt, List):
|
| 190 |
+
negative_prompt = [negative_prompt] * 1
|
| 191 |
+
with torch.inference_mode():
|
| 192 |
+
(
|
| 193 |
+
prompt_embeds_c,
|
| 194 |
+
_,
|
| 195 |
+
_,
|
| 196 |
+
_,
|
| 197 |
+
) = pipe.encode_prompt(
|
| 198 |
+
prompt,
|
| 199 |
+
num_images_per_prompt=1,
|
| 200 |
+
do_classifier_free_guidance=False,
|
| 201 |
+
negative_prompt=negative_prompt,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
pose_img = tensor_transfrom(pose_img).unsqueeze(0).to(device,torch.float16)
|
| 205 |
+
garm_tensor = tensor_transfrom(garm_img).unsqueeze(0).to(device,torch.float16)
|
| 206 |
+
generator = torch.Generator(device).manual_seed(seed) if seed is not None else None
|
| 207 |
+
images = pipe(
|
| 208 |
+
prompt_embeds=prompt_embeds.to(device,torch.float16),
|
| 209 |
+
negative_prompt_embeds=negative_prompt_embeds.to(device,torch.float16),
|
| 210 |
+
pooled_prompt_embeds=pooled_prompt_embeds.to(device,torch.float16),
|
| 211 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(device,torch.float16),
|
| 212 |
+
num_inference_steps=denoise_steps,
|
| 213 |
+
generator=generator,
|
| 214 |
+
strength = 1.0,
|
| 215 |
+
pose_img = pose_img.to(device,torch.float16),
|
| 216 |
+
text_embeds_cloth=prompt_embeds_c.to(device,torch.float16),
|
| 217 |
+
cloth = garm_tensor.to(device,torch.float16),
|
| 218 |
+
mask_image=mask,
|
| 219 |
+
image=human_img,
|
| 220 |
+
height=1024,
|
| 221 |
+
width=768,
|
| 222 |
+
ip_adapter_image = garm_img.resize((768,1024)),
|
| 223 |
+
guidance_scale=2.0,
|
| 224 |
+
callback_on_step_end=callback
|
| 225 |
+
)[0]
|
| 226 |
+
progress(1,desc="Complete")
|
| 227 |
+
return images[0]
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
title = "## IDM-VTON"
|
| 231 |
+
description = "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)"
|
| 232 |
+
|
| 233 |
+
example_path = os.path.join(os.path.dirname(__file__), 'example')
|
| 234 |
+
person_list = os.listdir(os.path.join(example_path,"human"))
|
| 235 |
+
person_images = [os.path.join(example_path,"human",person) for person in person_list]
|
| 236 |
+
|
| 237 |
+
garment_list = os.listdir(os.path.join(example_path,"cloth"))
|
| 238 |
+
garment_images = [os.path.join(example_path,"cloth",garment) for garment in garment_list]
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
with gr.Blocks().queue() as demo:
|
| 242 |
+
gr.Markdown(title)
|
| 243 |
+
gr.Markdown(description)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
with gr.Row():
|
| 245 |
with gr.Column():
|
| 246 |
+
gr.Markdown("#### Person Image")
|
| 247 |
+
person_image = gr.Image(
|
| 248 |
+
sources=["upload"],
|
| 249 |
+
type="pil",
|
| 250 |
+
label="Person Image",
|
| 251 |
+
width=512,
|
| 252 |
+
height=512,
|
| 253 |
+
)
|
| 254 |
|
| 255 |
+
gr.Examples(
|
| 256 |
+
inputs=person_image,
|
| 257 |
+
examples_per_page=20,
|
| 258 |
+
examples=person_images,
|
| 259 |
+
)
|
| 260 |
+
with gr.Column():
|
| 261 |
+
gr.Markdown("#### Garment Image")
|
| 262 |
+
garment_image = gr.Image(
|
| 263 |
+
sources=["upload"],
|
| 264 |
+
type="pil",
|
| 265 |
+
label="Garment Image",
|
| 266 |
+
width=512,
|
| 267 |
+
height=512,
|
| 268 |
)
|
| 269 |
|
| 270 |
+
gr.Examples(
|
| 271 |
+
inputs=garment_image,
|
| 272 |
+
examples_per_page=20,
|
| 273 |
+
examples=garment_images,
|
| 274 |
+
)
|
| 275 |
with gr.Column():
|
| 276 |
+
gr.Markdown("#### Generated Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
+
gen_image = gr.Image(
|
| 279 |
+
label="Generated Image",
|
| 280 |
+
width=512,
|
| 281 |
+
height=512,
|
| 282 |
+
)
|
| 283 |
|
|
|
|
|
|
|
|
|
|
| 284 |
with gr.Row():
|
| 285 |
+
gen_button = gr.Button("Generate")
|
| 286 |
+
|
| 287 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 288 |
denoise_steps = gr.Number(label="Denoising Steps", minimum=20, maximum=40, value=30, step=1)
|
| 289 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
| 290 |
+
|
| 291 |
+
gen_button.click(
|
| 292 |
+
fn=infer,
|
| 293 |
+
inputs=[person_image, garment_image, denoise_steps, seed],
|
| 294 |
+
outputs=[gen_image]
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
demo.launch()
|
|
|
|
|
|