Update app.py
Browse files
app.py
CHANGED
@@ -17,13 +17,9 @@ from model.pipeline import CatVTONPipeline, CatVTONPix2PixPipeline
|
|
17 |
from model.flux.pipeline_flux_tryon import FluxTryOnPipeline
|
18 |
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
|
19 |
|
20 |
-
access_token = os.getenv('HF_ACCESS_TOKEN')
|
21 |
|
22 |
-
# dùng để phân tích các tham số từ dòng lệnh và trả về cấu hình cài đặt cho chương trình
|
23 |
def parse_args():
|
24 |
-
# Khởi tạo đối tượng để quản lý các tham số dòng lệnh.
|
25 |
parser = argparse.ArgumentParser(description="Simple example of a training script.")
|
26 |
-
|
27 |
parser.add_argument(
|
28 |
"--base_model_path",
|
29 |
type=str,
|
@@ -32,7 +28,14 @@ def parse_args():
|
|
32 |
"The path to the base model to use for evaluation. This can be a local path or a model identifier from the Model Hub."
|
33 |
),
|
34 |
)
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
parser.add_argument(
|
37 |
"--resume_path",
|
38 |
type=str,
|
@@ -93,24 +96,18 @@ def parse_args():
|
|
93 |
)
|
94 |
|
95 |
args = parser.parse_args()
|
96 |
-
|
97 |
-
# Xử lý tham số:
|
98 |
-
# Đảm bảo rằng local_rank (chỉ số GPU cục bộ khi chạy phân tán) được đồng bộ từ biến môi trường
|
99 |
-
# Khi chạy các tác vụ huấn luyện phân tán, hệ thống cần biết chỉ số GPU cục bộ để phân bổ tài nguyên.
|
100 |
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
|
101 |
if env_local_rank != -1 and env_local_rank != args.local_rank:
|
102 |
args.local_rank = env_local_rank
|
103 |
|
104 |
return args
|
105 |
|
106 |
-
# Hàm image_grid tạo một lưới ảnh (grid) từ danh sách các ảnh đầu vào, với số hàng (rows) và số cột (cols) được chỉ định.
|
107 |
def image_grid(imgs, rows, cols):
|
108 |
-
assert len(imgs) == rows * cols
|
109 |
|
110 |
w, h = imgs[0].size
|
111 |
-
grid = Image.new("RGB", size=(cols * w, rows * h))
|
112 |
|
113 |
-
#Duyệt qua các ảnh và ghép vào lưới
|
114 |
for i, img in enumerate(imgs):
|
115 |
grid.paste(img, box=(i % cols * w, i // cols * h))
|
116 |
return grid
|
@@ -120,88 +117,56 @@ args = parse_args()
|
|
120 |
|
121 |
# Mask-based CatVTON
|
122 |
catvton_repo = "zhengchong/CatVTON"
|
123 |
-
repo_path = snapshot_download(repo_id=catvton_repo)
|
124 |
-
|
125 |
-
# Pipeline thực hiện Virtual Try on (dùng mask)
|
126 |
pipeline = CatVTONPipeline(
|
127 |
-
base_ckpt=args.base_model_path,
|
128 |
-
attn_ckpt=repo_path,
|
129 |
attn_ckpt_version="mix",
|
130 |
-
weight_dtype=init_weight_dtype(args.mixed_precision),
|
131 |
-
use_tf32=args.allow_tf32,
|
132 |
-
device='cuda'
|
133 |
)
|
134 |
-
|
135 |
-
|
136 |
-
# VaeImageProcessor: Bộ xử lý hình ảnh được thiết kế để làm việc với các mô hình dựa trên VAE (Variational Autoencoder).
|
137 |
-
mask_processor = VaeImageProcessor(
|
138 |
-
vae_scale_factor=8, # Tỉ lệ nén hình ảnh khi xử lý bằng VAE. Ảnh sẽ được giảm kích thước theo tỉ lệ 1/8.
|
139 |
-
do_normalize=False, # Không thực hiện chuẩn hóa giá trị pixel (ví dụ: chuyển đổi giá trị về khoảng [0, 1]).
|
140 |
-
do_binarize=True, # Chuyển đổi hình ảnh thành nhị phân (chỉ chứa 2 giá trị: 0 hoặc 255). Quan trọng để tạo mặt nạ rõ ràng.
|
141 |
-
do_convert_grayscale=True
|
142 |
-
)
|
143 |
-
# AutoMasker: Công cụ tự động tạo mặt nạ dựa trên các mô hình dự đoán hình dạng cơ thể người và phân đoạn quần áo.
|
144 |
automasker = AutoMasker(
|
145 |
-
densepose_ckpt=os.path.join(repo_path, "DensePose"),
|
146 |
-
schp_ckpt=os.path.join(repo_path, "SCHP"),
|
147 |
device='cuda',
|
148 |
)
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
153 |
def submit_function(
|
154 |
person_image,
|
155 |
cloth_image,
|
156 |
-
cloth_type,
|
157 |
num_inference_steps,
|
158 |
guidance_scale,
|
159 |
seed,
|
160 |
-
show_type
|
161 |
):
|
162 |
-
|
163 |
-
|
164 |
-
mask
|
165 |
-
#person_image["layers"][0] # Lấy mặt nạ do người dùng vẽ (nếu có).
|
166 |
-
if len(person_image["layers"]) > 0:
|
167 |
-
# Nếu danh sách không rỗng, lấy phần tử đầu tiên
|
168 |
-
layer = person_image["layers"][0]
|
169 |
-
else:
|
170 |
-
# Nếu danh sách rỗng, thực hiện hành động thay thế hoặc thông báo lỗi
|
171 |
-
layer = None
|
172 |
-
print("Không có layers trong person_image.")
|
173 |
-
|
174 |
-
print("Loại của mask:", type(mask))
|
175 |
-
print("Giá trị của mask:", mask)
|
176 |
-
#mask = Image.open(mask).convert("L") # Chuyển mặt nạ thành ảnh thang độ xám
|
177 |
-
if mask is None:
|
178 |
-
raise ValueError("Tham số 'mask' bị rỗng.")
|
179 |
-
elif isinstance(mask, (str, bytes)) or hasattr(mask, "read"):
|
180 |
-
mask = Image.open(mask).convert("L")
|
181 |
-
else:
|
182 |
-
raise ValueError(f"Kiểu dữ liệu '{type(mask)}' của 'mask' không được hỗ trợ.")
|
183 |
-
|
184 |
-
|
185 |
-
if len(np.unique(np.array(mask))) == 1: # Nếu mặt nạ chỉ chứa một giá trị (ví dụ: toàn đen hoặc toàn trắng), thì không sử dụng mặt nạ (mask = None).
|
186 |
mask = None
|
187 |
else:
|
188 |
-
mask = np.array(mask)
|
189 |
-
mask[mask > 0] = 255
|
190 |
-
mask = Image.fromarray(mask)
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
result_save_path = os.path.join(tmp_folder, date_str[:8], date_str[8:] + ".png") # Đường dẫn đầy đủ để lưu ảnh kết quả.
|
196 |
if not os.path.exists(os.path.join(tmp_folder, date_str[:8])):
|
197 |
-
os.makedirs(os.path.join(tmp_folder, date_str[:8]))
|
198 |
|
199 |
-
# Xử lý seed ngẫu nhiên
|
200 |
generator = None
|
201 |
-
if seed != -1:
|
202 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
203 |
|
204 |
-
# Chuẩn hóa ảnh đầu vào
|
205 |
person_image = Image.open(person_image).convert("RGB")
|
206 |
cloth_image = Image.open(cloth_image).convert("RGB")
|
207 |
person_image = resize_and_crop(person_image, (args.width, args.height))
|
@@ -209,15 +174,14 @@ def submit_function(
|
|
209 |
|
210 |
# Process mask
|
211 |
if mask is not None:
|
212 |
-
mask = resize_and_crop(mask, (args.width, args.height))
|
213 |
else:
|
214 |
mask = automasker(
|
215 |
person_image,
|
216 |
cloth_type
|
217 |
-
)['mask']
|
218 |
-
mask = mask_processor.blur(mask, blur_factor=9)
|
219 |
|
220 |
-
# Suy luận mô hình: gán các tham số vô hàm tính toán, trả lại result là hình ảnh
|
221 |
# Inference
|
222 |
# try:
|
223 |
result_image = pipeline(
|
@@ -233,13 +197,10 @@ def submit_function(
|
|
233 |
# "An error occurred. Please try again later: {}".format(e)
|
234 |
# )
|
235 |
|
236 |
-
# Post-process
|
237 |
-
|
238 |
-
|
239 |
-
save_result_image = image_grid([person_image, masked_person, cloth_image, result_image], 1, 4) # Tạo một ảnh lưới chứa
|
240 |
save_result_image.save(result_save_path)
|
241 |
-
|
242 |
-
# Điều chỉnh hiển thị kết quả
|
243 |
if show_type == "result only":
|
244 |
return result_image
|
245 |
else:
|
@@ -250,15 +211,132 @@ def submit_function(
|
|
250 |
else:
|
251 |
condition_width = width // 3
|
252 |
conditions = image_grid([person_image, masked_person , cloth_image], 3, 1)
|
253 |
-
|
254 |
-
|
255 |
-
# conditions: Ảnh ghép ban đầu, được tạo từ các ảnh như ảnh người gốc, ảnh quần áo, và ảnh mặt nạ (tùy chọn).
|
256 |
-
# Tham số Image.NEAREST: Đây là phương pháp nội suy (interpolation) gần nhất, dùng để thay đổi kích thước ảnh mà không làm mờ hay mất chi tiết.
|
257 |
-
new_result_image = Image.new("RGB", (width + condition_width + 5, height)) # Image.new: Tạo một ảnh trống mới
|
258 |
new_result_image.paste(conditions, (0, 0))
|
259 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
260 |
return new_result_image
|
261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
def person_example_fn(image_path):
|
264 |
return image_path
|
@@ -269,10 +347,9 @@ HEADER = ""
|
|
269 |
def app_gradio():
|
270 |
with gr.Blocks(title="CatVTON") as demo:
|
271 |
gr.Markdown(HEADER)
|
272 |
-
with gr.Tab("Mask-based"):
|
273 |
with gr.Row():
|
274 |
with gr.Column(scale=1, min_width=350):
|
275 |
-
# Ảnh model (người)
|
276 |
with gr.Row():
|
277 |
image_path = gr.Image(
|
278 |
type="filepath",
|
@@ -283,7 +360,6 @@ def app_gradio():
|
|
283 |
interactive=True, label="Person Image", type="filepath"
|
284 |
)
|
285 |
|
286 |
-
# Ảnh quần áo
|
287 |
with gr.Row():
|
288 |
with gr.Column(scale=1, min_width=230):
|
289 |
cloth_image = gr.Image(
|
@@ -299,13 +375,12 @@ def app_gradio():
|
|
299 |
value="upper",
|
300 |
)
|
301 |
|
302 |
-
|
303 |
submit = gr.Button("Submit")
|
304 |
gr.Markdown(
|
305 |
'<center><span style="color: #FF0000">!!! Click only Once, Wait for Delay !!!</span></center>'
|
306 |
)
|
307 |
|
308 |
-
# Advance setting
|
309 |
gr.Markdown(
|
310 |
'<span style="color: #808080; font-size: small;">Advanced options can adjust details:<br>1. `Inference Step` may enhance details;<br>2. `CFG` is highly correlated with saturation;<br>3. `Random seed` may improve pseudo-shadow.</span>'
|
311 |
)
|
@@ -327,9 +402,7 @@ def app_gradio():
|
|
327 |
value="input & mask & result",
|
328 |
)
|
329 |
|
330 |
-
|
331 |
with gr.Column(scale=2, min_width=500):
|
332 |
-
# Result image
|
333 |
result_image = gr.Image(interactive=False, label="Result")
|
334 |
with gr.Row():
|
335 |
# Photo Examples
|
@@ -392,7 +465,6 @@ def app_gradio():
|
|
392 |
person_example_fn, inputs=image_path, outputs=person_image
|
393 |
)
|
394 |
|
395 |
-
# Function khi ấn nút submit
|
396 |
submit.click(
|
397 |
submit_function,
|
398 |
[
|
@@ -406,9 +478,9 @@ def app_gradio():
|
|
406 |
],
|
407 |
result_image,
|
408 |
)
|
|
|
409 |
|
410 |
demo.queue().launch(share=True, show_error=True)
|
411 |
-
#demo.queue().launch()
|
412 |
|
413 |
|
414 |
if __name__ == "__main__":
|
|
|
17 |
from model.flux.pipeline_flux_tryon import FluxTryOnPipeline
|
18 |
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
|
19 |
|
|
|
20 |
|
|
|
21 |
def parse_args():
|
|
|
22 |
parser = argparse.ArgumentParser(description="Simple example of a training script.")
|
|
|
23 |
parser.add_argument(
|
24 |
"--base_model_path",
|
25 |
type=str,
|
|
|
28 |
"The path to the base model to use for evaluation. This can be a local path or a model identifier from the Model Hub."
|
29 |
),
|
30 |
)
|
31 |
+
parser.add_argument(
|
32 |
+
"--p2p_base_model_path",
|
33 |
+
type=str,
|
34 |
+
default="timbrooks/instruct-pix2pix",
|
35 |
+
help=(
|
36 |
+
"The path to the base model to use for evaluation. This can be a local path or a model identifier from the Model Hub."
|
37 |
+
),
|
38 |
+
)
|
39 |
parser.add_argument(
|
40 |
"--resume_path",
|
41 |
type=str,
|
|
|
96 |
)
|
97 |
|
98 |
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
99 |
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
|
100 |
if env_local_rank != -1 and env_local_rank != args.local_rank:
|
101 |
args.local_rank = env_local_rank
|
102 |
|
103 |
return args
|
104 |
|
|
|
105 |
def image_grid(imgs, rows, cols):
|
106 |
+
assert len(imgs) == rows * cols
|
107 |
|
108 |
w, h = imgs[0].size
|
109 |
+
grid = Image.new("RGB", size=(cols * w, rows * h))
|
110 |
|
|
|
111 |
for i, img in enumerate(imgs):
|
112 |
grid.paste(img, box=(i % cols * w, i // cols * h))
|
113 |
return grid
|
|
|
117 |
|
118 |
# Mask-based CatVTON
|
119 |
catvton_repo = "zhengchong/CatVTON"
|
120 |
+
repo_path = snapshot_download(repo_id=catvton_repo)
|
121 |
+
# Pipeline
|
|
|
122 |
pipeline = CatVTONPipeline(
|
123 |
+
base_ckpt=args.base_model_path,
|
124 |
+
attn_ckpt=repo_path,
|
125 |
attn_ckpt_version="mix",
|
126 |
+
weight_dtype=init_weight_dtype(args.mixed_precision),
|
127 |
+
use_tf32=args.allow_tf32,
|
128 |
+
device='cuda'
|
129 |
)
|
130 |
+
# AutoMasker
|
131 |
+
mask_processor = VaeImageProcessor(vae_scale_factor=8, do_normalize=False, do_binarize=True, do_convert_grayscale=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
automasker = AutoMasker(
|
133 |
+
densepose_ckpt=os.path.join(repo_path, "DensePose"),
|
134 |
+
schp_ckpt=os.path.join(repo_path, "SCHP"),
|
135 |
device='cuda',
|
136 |
)
|
137 |
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
@spaces.GPU(duration=120)
|
142 |
def submit_function(
|
143 |
person_image,
|
144 |
cloth_image,
|
145 |
+
cloth_type,
|
146 |
num_inference_steps,
|
147 |
guidance_scale,
|
148 |
seed,
|
149 |
+
show_type
|
150 |
):
|
151 |
+
person_image, mask = person_image["background"], person_image["layers"][0]
|
152 |
+
mask = Image.open(mask).convert("L")
|
153 |
+
if len(np.unique(np.array(mask))) == 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
mask = None
|
155 |
else:
|
156 |
+
mask = np.array(mask)
|
157 |
+
mask[mask > 0] = 255
|
158 |
+
mask = Image.fromarray(mask)
|
159 |
+
|
160 |
+
tmp_folder = args.output_dir
|
161 |
+
date_str = datetime.now().strftime("%Y%m%d%H%M%S")
|
162 |
+
result_save_path = os.path.join(tmp_folder, date_str[:8], date_str[8:] + ".png")
|
|
|
163 |
if not os.path.exists(os.path.join(tmp_folder, date_str[:8])):
|
164 |
+
os.makedirs(os.path.join(tmp_folder, date_str[:8]))
|
165 |
|
|
|
166 |
generator = None
|
167 |
+
if seed != -1:
|
168 |
generator = torch.Generator(device='cuda').manual_seed(seed)
|
169 |
|
|
|
170 |
person_image = Image.open(person_image).convert("RGB")
|
171 |
cloth_image = Image.open(cloth_image).convert("RGB")
|
172 |
person_image = resize_and_crop(person_image, (args.width, args.height))
|
|
|
174 |
|
175 |
# Process mask
|
176 |
if mask is not None:
|
177 |
+
mask = resize_and_crop(mask, (args.width, args.height))
|
178 |
else:
|
179 |
mask = automasker(
|
180 |
person_image,
|
181 |
cloth_type
|
182 |
+
)['mask']
|
183 |
+
mask = mask_processor.blur(mask, blur_factor=9)
|
184 |
|
|
|
185 |
# Inference
|
186 |
# try:
|
187 |
result_image = pipeline(
|
|
|
197 |
# "An error occurred. Please try again later: {}".format(e)
|
198 |
# )
|
199 |
|
200 |
+
# Post-process
|
201 |
+
masked_person = vis_mask(person_image, mask)
|
202 |
+
save_result_image = image_grid([person_image, masked_person, cloth_image, result_image], 1, 4)
|
|
|
203 |
save_result_image.save(result_save_path)
|
|
|
|
|
204 |
if show_type == "result only":
|
205 |
return result_image
|
206 |
else:
|
|
|
211 |
else:
|
212 |
condition_width = width // 3
|
213 |
conditions = image_grid([person_image, masked_person , cloth_image], 3, 1)
|
214 |
+
conditions = conditions.resize((condition_width, height), Image.NEAREST)
|
215 |
+
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
|
|
|
|
|
|
216 |
new_result_image.paste(conditions, (0, 0))
|
217 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
218 |
return new_result_image
|
219 |
|
220 |
+
@spaces.GPU(duration=120)
|
221 |
+
def submit_function_p2p(
|
222 |
+
person_image,
|
223 |
+
cloth_image,
|
224 |
+
num_inference_steps,
|
225 |
+
guidance_scale,
|
226 |
+
seed):
|
227 |
+
person_image= person_image["background"]
|
228 |
+
|
229 |
+
tmp_folder = args.output_dir
|
230 |
+
date_str = datetime.now().strftime("%Y%m%d%H%M%S")
|
231 |
+
result_save_path = os.path.join(tmp_folder, date_str[:8], date_str[8:] + ".png")
|
232 |
+
if not os.path.exists(os.path.join(tmp_folder, date_str[:8])):
|
233 |
+
os.makedirs(os.path.join(tmp_folder, date_str[:8]))
|
234 |
+
|
235 |
+
generator = None
|
236 |
+
if seed != -1:
|
237 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
238 |
+
|
239 |
+
person_image = Image.open(person_image).convert("RGB")
|
240 |
+
cloth_image = Image.open(cloth_image).convert("RGB")
|
241 |
+
person_image = resize_and_crop(person_image, (args.width, args.height))
|
242 |
+
cloth_image = resize_and_padding(cloth_image, (args.width, args.height))
|
243 |
+
|
244 |
+
# Inference
|
245 |
+
try:
|
246 |
+
result_image = pipeline_p2p(
|
247 |
+
image=person_image,
|
248 |
+
condition_image=cloth_image,
|
249 |
+
num_inference_steps=num_inference_steps,
|
250 |
+
guidance_scale=guidance_scale,
|
251 |
+
generator=generator
|
252 |
+
)[0]
|
253 |
+
except Exception as e:
|
254 |
+
raise gr.Error(
|
255 |
+
"An error occurred. Please try again later: {}".format(e)
|
256 |
+
)
|
257 |
+
|
258 |
+
# Post-process
|
259 |
+
save_result_image = image_grid([person_image, cloth_image, result_image], 1, 3)
|
260 |
+
save_result_image.save(result_save_path)
|
261 |
+
return result_image
|
262 |
+
|
263 |
+
@spaces.GPU(duration=120)
|
264 |
+
def submit_function_flux(
|
265 |
+
person_image,
|
266 |
+
cloth_image,
|
267 |
+
cloth_type,
|
268 |
+
num_inference_steps,
|
269 |
+
guidance_scale,
|
270 |
+
seed,
|
271 |
+
show_type
|
272 |
+
):
|
273 |
+
|
274 |
+
# Process image editor input
|
275 |
+
person_image, mask = person_image["background"], person_image["layers"][0]
|
276 |
+
mask = Image.open(mask).convert("L")
|
277 |
+
if len(np.unique(np.array(mask))) == 1:
|
278 |
+
mask = None
|
279 |
+
else:
|
280 |
+
mask = np.array(mask)
|
281 |
+
mask[mask > 0] = 255
|
282 |
+
mask = Image.fromarray(mask)
|
283 |
+
|
284 |
+
# Set random seed
|
285 |
+
generator = None
|
286 |
+
if seed != -1:
|
287 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
288 |
+
|
289 |
+
# Process input images
|
290 |
+
person_image = Image.open(person_image).convert("RGB")
|
291 |
+
cloth_image = Image.open(cloth_image).convert("RGB")
|
292 |
+
|
293 |
+
# Adjust image sizes
|
294 |
+
person_image = resize_and_crop(person_image, (args.width, args.height))
|
295 |
+
cloth_image = resize_and_padding(cloth_image, (args.width, args.height))
|
296 |
+
|
297 |
+
# Process mask
|
298 |
+
if mask is not None:
|
299 |
+
mask = resize_and_crop(mask, (args.width, args.height))
|
300 |
+
else:
|
301 |
+
mask = automasker(
|
302 |
+
person_image,
|
303 |
+
cloth_type
|
304 |
+
)['mask']
|
305 |
+
mask = mask_processor.blur(mask, blur_factor=9)
|
306 |
+
|
307 |
+
# Inference
|
308 |
+
result_image = pipeline_flux(
|
309 |
+
image=person_image,
|
310 |
+
condition_image=cloth_image,
|
311 |
+
mask_image=mask,
|
312 |
+
width=args.width,
|
313 |
+
height=args.height,
|
314 |
+
num_inference_steps=num_inference_steps,
|
315 |
+
guidance_scale=guidance_scale,
|
316 |
+
generator=generator
|
317 |
+
).images[0]
|
318 |
+
|
319 |
+
# Post-processing
|
320 |
+
masked_person = vis_mask(person_image, mask)
|
321 |
+
|
322 |
+
# Return result based on show type
|
323 |
+
if show_type == "result only":
|
324 |
+
return result_image
|
325 |
+
else:
|
326 |
+
width, height = person_image.size
|
327 |
+
if show_type == "input & result":
|
328 |
+
condition_width = width // 2
|
329 |
+
conditions = image_grid([person_image, cloth_image], 2, 1)
|
330 |
+
else:
|
331 |
+
condition_width = width // 3
|
332 |
+
conditions = image_grid([person_image, masked_person, cloth_image], 3, 1)
|
333 |
+
|
334 |
+
conditions = conditions.resize((condition_width, height), Image.NEAREST)
|
335 |
+
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
336 |
+
new_result_image.paste(conditions, (0, 0))
|
337 |
+
new_result_image.paste(result_image, (condition_width + 5, 0))
|
338 |
+
return new_result_image
|
339 |
+
|
340 |
|
341 |
def person_example_fn(image_path):
|
342 |
return image_path
|
|
|
347 |
def app_gradio():
|
348 |
with gr.Blocks(title="CatVTON") as demo:
|
349 |
gr.Markdown(HEADER)
|
350 |
+
with gr.Tab("Mask-based & SD1.5"):
|
351 |
with gr.Row():
|
352 |
with gr.Column(scale=1, min_width=350):
|
|
|
353 |
with gr.Row():
|
354 |
image_path = gr.Image(
|
355 |
type="filepath",
|
|
|
360 |
interactive=True, label="Person Image", type="filepath"
|
361 |
)
|
362 |
|
|
|
363 |
with gr.Row():
|
364 |
with gr.Column(scale=1, min_width=230):
|
365 |
cloth_image = gr.Image(
|
|
|
375 |
value="upper",
|
376 |
)
|
377 |
|
378 |
+
|
379 |
submit = gr.Button("Submit")
|
380 |
gr.Markdown(
|
381 |
'<center><span style="color: #FF0000">!!! Click only Once, Wait for Delay !!!</span></center>'
|
382 |
)
|
383 |
|
|
|
384 |
gr.Markdown(
|
385 |
'<span style="color: #808080; font-size: small;">Advanced options can adjust details:<br>1. `Inference Step` may enhance details;<br>2. `CFG` is highly correlated with saturation;<br>3. `Random seed` may improve pseudo-shadow.</span>'
|
386 |
)
|
|
|
402 |
value="input & mask & result",
|
403 |
)
|
404 |
|
|
|
405 |
with gr.Column(scale=2, min_width=500):
|
|
|
406 |
result_image = gr.Image(interactive=False, label="Result")
|
407 |
with gr.Row():
|
408 |
# Photo Examples
|
|
|
465 |
person_example_fn, inputs=image_path, outputs=person_image
|
466 |
)
|
467 |
|
|
|
468 |
submit.click(
|
469 |
submit_function,
|
470 |
[
|
|
|
478 |
],
|
479 |
result_image,
|
480 |
)
|
481 |
+
|
482 |
|
483 |
demo.queue().launch(share=True, show_error=True)
|
|
|
484 |
|
485 |
|
486 |
if __name__ == "__main__":
|