Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,036 Bytes
b213d84 16c2627 b213d84 afadbd4 16c2627 afadbd4 b213d84 16c2627 b213d84 16c2627 b213d84 16c2627 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import numpy as np
from PIL import Image
from huggingface_hub import snapshot_download
from leffa.transform import LeffaTransform
from leffa.model import LeffaModel
from leffa.inference import LeffaInference
from utils.garment_agnostic_mask_predictor import AutoMasker
from utils.densepose_predictor import DensePosePredictor
import gradio as gr
# Download checkpoints
snapshot_download(repo_id="franciszzj/Leffa", local_dir="./")
def leffa_predict(src_image_path, ref_image_path, control_type):
assert control_type in [
"virtual_tryon", "pose_transfer"], "Invalid control type: {}".format(control_type)
src_image = Image.open(src_image_path)
ref_image = Image.open(ref_image_path)
src_image_array = np.array(src_image)
ref_image_array = np.array(ref_image)
# Mask
if control_type == "virtual_tryon":
automasker = AutoMasker(
densepose_path="./ckpts/densepose",
schp_path="./ckpts/schp",
)
src_image = src_image.convert("RGB")
mask = automasker(src_image, "upper")["mask"]
elif control_type == "pose_transfer":
mask = Image.fromarray(np.ones_like(src_image_array) * 255)
# DensePose
densepose_predictor = DensePosePredictor(
config_path="./ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml",
weights_path="./ckpts/densepose/model_final_162be9.pkl",
)
src_image_iuv_array = densepose_predictor.predict_iuv(src_image_array)
src_image_seg_array = densepose_predictor.predict_seg(src_image_array)
src_image_iuv = Image.fromarray(src_image_iuv_array)
src_image_seg = Image.fromarray(src_image_seg_array)
if control_type == "virtual_tryon":
densepose = src_image_seg
elif control_type == "pose_transfer":
densepose = src_image_iuv
# Leffa
transform = LeffaTransform()
if control_type == "virtual_tryon":
pretrained_model_name_or_path = "./ckpts/stable-diffusion-inpainting"
pretrained_model = "./ckpts/virtual_tryon.pth"
elif control_type == "pose_transfer":
pretrained_model_name_or_path = "./ckpts/stable-diffusion-xl-1.0-inpainting-0.1"
pretrained_model = "./ckpts/pose_transfer.pth"
model = LeffaModel(
pretrained_model_name_or_path=pretrained_model_name_or_path,
pretrained_model=pretrained_model,
)
inference = LeffaInference(model=model)
data = {
"src_image": [src_image],
"ref_image": [ref_image],
"mask": [mask],
"densepose": [densepose],
}
data = transform(data)
output = inference(data)
gen_image = output["generated_image"][0]
# gen_image.save("gen_image.png")
return np.array(gen_image)
if __name__ == "__main__":
# import sys
# src_image_path = sys.argv[1]
# ref_image_path = sys.argv[2]
# control_type = sys.argv[3]
# leffa_predict(src_image_path, ref_image_path, control_type)
with gr.Blocks().queue() as demo:
gr.Markdown(
"## Leffa: Learning Flow Fields in Attention for Controllable Person Image Generation")
gr.Markdown("Leffa is a unified framework for controllable person image generation that enables precise manipulation of both appearance (i.e., virtual try-on) and pose (i.e., pose transfer).")
with gr.Row():
with gr.Column():
src_image = gr.Image(
sources=["upload"],
type="filepath",
label="Source Person Image",
width=384,
height=512,
)
with gr.Row():
control_type = gr.Dropdown(
["virtual_tryon", "pose_transfer"], label="Control Type")
example = gr.Examples(
inputs=src_image,
examples_per_page=10,
examples=["./examples/14684_00_person.jpg",
"./examples/14092_00_person.jpg"],
)
with gr.Column():
ref_image = gr.Image(
sources=["upload"],
type="filepath",
label="Reference Image",
width=384,
height=512,
)
with gr.Row():
gen_button = gr.Button("Generate")
example = gr.Examples(
inputs=ref_image,
examples_per_page=10,
examples=["./examples/04181_00_garment.jpg",
"./examples/14684_00_person.jpg"],
)
with gr.Column():
gen_image = gr.Image(
label="Generated Person Image",
width=384,
height=512,
)
gen_button.click(fn=leffa_predict, inputs=[
src_image, ref_image, control_type], outputs=[gen_image])
demo.launch(share=True, server_port=7860)
|