Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,512 +1,120 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
import cv2
|
| 4 |
-
|
| 5 |
-
import requests
|
| 6 |
import json
|
| 7 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import base64
|
| 9 |
-
import
|
| 10 |
-
import os
|
| 11 |
-
from diffusers import StableDiffusionInpaintPipeline, StableDiffusionPipeline
|
| 12 |
-
import torch
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
pipe_inpaint = None
|
| 17 |
-
pipe_txt2img = None
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"Casual Model A": "https://i.postimg.cc/T2b6PG2V/male-model2.png"
|
| 25 |
}
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
h, w, _ = img.shape
|
| 31 |
-
img = cv2.putText(img, 'AI Outfit Designer', (int(0.02*w), h-10),
|
| 32 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (128, 128, 128), 1, cv2.LINE_AA)
|
| 33 |
return img
|
| 34 |
|
| 35 |
-
def initialize_models():
|
| 36 |
-
global pipe_inpaint, pipe_txt2img
|
| 37 |
-
try:
|
| 38 |
-
# Load inpainting model for outfit editing
|
| 39 |
-
pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
|
| 40 |
-
"runwayml/stable-diffusion-inpainting",
|
| 41 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 42 |
-
safety_checker=None,
|
| 43 |
-
requires_safety_checker=False
|
| 44 |
-
).to(device)
|
| 45 |
-
|
| 46 |
-
# Load text-to-image model for outfit generation
|
| 47 |
-
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
|
| 48 |
-
"runwayml/stable-diffusion-v1-5",
|
| 49 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 50 |
-
safety_checker=None,
|
| 51 |
-
requires_safety_checker=False
|
| 52 |
-
).to(device)
|
| 53 |
-
|
| 54 |
-
if device == "cuda":
|
| 55 |
-
pipe_inpaint.enable_attention_slicing()
|
| 56 |
-
pipe_txt2img.enable_attention_slicing()
|
| 57 |
-
|
| 58 |
-
return True
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"Error loading models: {e}")
|
| 61 |
-
return False
|
| 62 |
-
|
| 63 |
-
def create_placeholder_image(text="Model Image", size=(400, 600)):
|
| 64 |
-
"""Create placeholder image for models"""
|
| 65 |
-
img = Image.new('RGB', size, color='lightgray')
|
| 66 |
-
draw = ImageDraw.Draw(img)
|
| 67 |
-
|
| 68 |
-
try:
|
| 69 |
-
font = ImageFont.truetype("arial.ttf", 20)
|
| 70 |
-
except:
|
| 71 |
-
font = ImageFont.load_default()
|
| 72 |
-
|
| 73 |
-
bbox = draw.textbbox((0, 0), text, font=font)
|
| 74 |
-
text_width = bbox[2] - bbox[0]
|
| 75 |
-
text_height = bbox[3] - bbox[1]
|
| 76 |
-
|
| 77 |
-
x = (size[0] - text_width) // 2
|
| 78 |
-
y = (size[1] - text_height) // 2
|
| 79 |
-
|
| 80 |
-
draw.text((x, y), text, fill='black', font=font)
|
| 81 |
-
return np.array(img)
|
| 82 |
|
| 83 |
-
def
|
| 84 |
-
"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if response.status_code == 200:
|
| 88 |
-
image = Image.open(io.BytesIO(response.content))
|
| 89 |
-
# Convert to RGB if needed
|
| 90 |
-
if image.mode != 'RGB':
|
| 91 |
-
image = image.convert('RGB')
|
| 92 |
-
# Resize to standard size
|
| 93 |
-
image = image.resize((400, 600), Image.Resampling.LANCZOS)
|
| 94 |
-
return np.array(image)
|
| 95 |
-
else:
|
| 96 |
-
return create_placeholder_image("Failed to load model")
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"Error loading model image: {e}")
|
| 99 |
-
return create_placeholder_image("Model loading error")
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
try:
|
| 104 |
-
# Determine which model to use
|
| 105 |
-
if model_image is not None:
|
| 106 |
-
# Use uploaded image
|
| 107 |
-
base_model = model_image
|
| 108 |
-
model_source = "uploaded photo"
|
| 109 |
-
elif selected_model and selected_model in MODEL_AVATARS:
|
| 110 |
-
# Load selected model from URL
|
| 111 |
-
base_model = load_model_image_from_url(MODEL_AVATARS[selected_model])
|
| 112 |
-
model_source = selected_model
|
| 113 |
-
else:
|
| 114 |
-
return create_placeholder_image("Please upload a photo or select a model")
|
| 115 |
-
|
| 116 |
-
# If no garments provided, generate based on style
|
| 117 |
-
if garment_top is None and garment_bottom is None and not style_prompt:
|
| 118 |
-
return create_placeholder_image("Please upload garments or describe a style")
|
| 119 |
-
|
| 120 |
-
if garment_top is None and garment_bottom is None and style_prompt:
|
| 121 |
-
# Generate outfit based on text description
|
| 122 |
-
if pipe_txt2img:
|
| 123 |
-
prompt = f"professional fashion photo of a person wearing {style_prompt}, high quality, detailed clothing, same person as reference"
|
| 124 |
-
negative_prompt = "blurry, low quality, distorted, nude, different person"
|
| 125 |
-
|
| 126 |
-
generated_image = pipe_txt2img(
|
| 127 |
-
prompt=prompt,
|
| 128 |
-
negative_prompt=negative_prompt,
|
| 129 |
-
num_inference_steps=25,
|
| 130 |
-
guidance_scale=7.5,
|
| 131 |
-
width=400,
|
| 132 |
-
height=600
|
| 133 |
-
).images[0]
|
| 134 |
-
|
| 135 |
-
result = np.array(generated_image)
|
| 136 |
-
else:
|
| 137 |
-
# Create styled placeholder showing the base model with text overlay
|
| 138 |
-
result = create_styled_placeholder(base_model, style_prompt)
|
| 139 |
-
else:
|
| 140 |
-
# Apply uploaded garments to the base model
|
| 141 |
-
result = simulate_tryon(base_model, garment_top, garment_bottom, model_source)
|
| 142 |
-
|
| 143 |
-
# Add watermark
|
| 144 |
-
final_result = add_watermark(result)
|
| 145 |
-
return final_result
|
| 146 |
-
|
| 147 |
-
except Exception as e:
|
| 148 |
-
print(f"Error in outfit generation: {e}")
|
| 149 |
-
return create_placeholder_image("Generation Error")
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
result = base_model.copy()
|
| 155 |
else:
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# Add style overlay
|
| 159 |
-
img_pil = Image.fromarray(result)
|
| 160 |
-
draw = ImageDraw.Draw(img_pil)
|
| 161 |
-
|
| 162 |
-
try:
|
| 163 |
-
font = ImageFont.truetype("arial.ttf", 18)
|
| 164 |
-
small_font = ImageFont.truetype("arial.ttf", 14)
|
| 165 |
-
except:
|
| 166 |
-
font = ImageFont.load_default()
|
| 167 |
-
small_font = ImageFont.load_default()
|
| 168 |
-
|
| 169 |
-
# Add semi-transparent background for text
|
| 170 |
-
overlay = Image.new('RGBA', img_pil.size, (0,0,0,0))
|
| 171 |
-
overlay_draw = ImageDraw.Draw(overlay)
|
| 172 |
-
overlay_draw.rectangle([10, 10, 390, 80], fill=(0,0,0,128))
|
| 173 |
-
|
| 174 |
-
img_pil = Image.alpha_composite(img_pil.convert('RGBA'), overlay).convert('RGB')
|
| 175 |
-
draw = ImageDraw.Draw(img_pil)
|
| 176 |
-
|
| 177 |
-
# Add text
|
| 178 |
-
draw.text((20, 20), "Generated Style:", fill='white', font=small_font)
|
| 179 |
-
draw.text((20, 45), style_text[:40] + "..." if len(style_text) > 40 else style_text,
|
| 180 |
-
fill='yellow', font=font)
|
| 181 |
-
|
| 182 |
-
return np.array(img_pil)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
else:
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
# Convert to PIL for drawing
|
| 192 |
-
img_pil = Image.fromarray(result)
|
| 193 |
-
draw = ImageDraw.Draw(img_pil)
|
| 194 |
-
|
| 195 |
-
try:
|
| 196 |
-
font = ImageFont.truetype("arial.ttf", 16)
|
| 197 |
-
small_font = ImageFont.truetype("arial.ttf", 12)
|
| 198 |
-
except:
|
| 199 |
-
font = ImageFont.load_default()
|
| 200 |
-
small_font = ImageFont.load_default()
|
| 201 |
-
|
| 202 |
-
# Add overlay showing what's being applied
|
| 203 |
-
overlay = Image.new('RGBA', img_pil.size, (0,0,0,0))
|
| 204 |
-
overlay_draw = ImageDraw.Draw(overlay)
|
| 205 |
-
overlay_draw.rectangle([10, 10, 390, 100], fill=(0,100,0,128))
|
| 206 |
-
|
| 207 |
-
img_pil = Image.alpha_composite(img_pil.convert('RGBA'), overlay).convert('RGB')
|
| 208 |
-
draw = ImageDraw.Draw(img_pil)
|
| 209 |
-
|
| 210 |
-
y_pos = 20
|
| 211 |
-
draw.text((20, y_pos), f"Base Model: {model_source}", fill='white', font=small_font)
|
| 212 |
-
y_pos += 25
|
| 213 |
-
|
| 214 |
-
if garment_top is not None:
|
| 215 |
-
draw.text((20, y_pos), "✓ Top garment applied", fill='lightgreen', font=font)
|
| 216 |
-
y_pos += 25
|
| 217 |
-
if garment_bottom is not None:
|
| 218 |
-
draw.text((20, y_pos), "✓ Bottom garment applied", fill='lightgreen', font=font)
|
| 219 |
-
|
| 220 |
-
return np.array(img_pil)
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
if current_image is None:
|
| 225 |
-
return None, "Please generate an outfit first!"
|
| 226 |
-
|
| 227 |
-
if not edit_prompt.strip():
|
| 228 |
-
return current_image, "Please enter an edit prompt!"
|
| 229 |
-
|
| 230 |
-
try:
|
| 231 |
-
# Convert to PIL if numpy array
|
| 232 |
-
if isinstance(current_image, np.ndarray):
|
| 233 |
-
image = Image.fromarray(current_image)
|
| 234 |
-
else:
|
| 235 |
-
image = current_image
|
| 236 |
-
|
| 237 |
-
# Resize if needed
|
| 238 |
-
image = image.resize((400, 600))
|
| 239 |
-
|
| 240 |
-
# Create a simple mask for clothing area
|
| 241 |
-
mask = Image.new('L', (400, 600), 0)
|
| 242 |
-
mask_draw = ImageDraw.Draw(mask)
|
| 243 |
-
mask_draw.rectangle([50, 150, 350, 450], fill=255) # Clothing area
|
| 244 |
-
|
| 245 |
-
if pipe_inpaint:
|
| 246 |
-
prompt = f"person wearing {edit_prompt}, high quality fashion photo"
|
| 247 |
-
negative_prompt = "blurry, low quality, distorted, nude"
|
| 248 |
-
|
| 249 |
-
edited_image = pipe_inpaint(
|
| 250 |
-
prompt=prompt,
|
| 251 |
-
image=image,
|
| 252 |
-
mask_image=mask,
|
| 253 |
-
negative_prompt=negative_prompt,
|
| 254 |
-
num_inference_steps=20,
|
| 255 |
-
strength=strength,
|
| 256 |
-
guidance_scale=7.5
|
| 257 |
-
).images[0]
|
| 258 |
-
|
| 259 |
-
return np.array(edited_image), f"✅ Outfit edited: {edit_prompt}"
|
| 260 |
-
else:
|
| 261 |
-
# Fallback: add text overlay
|
| 262 |
-
draw = ImageDraw.Draw(image)
|
| 263 |
-
draw.text((10, 10), f"Edit: {edit_prompt}", fill='red')
|
| 264 |
-
return np.array(image), f"✅ Edit applied: {edit_prompt}"
|
| 265 |
-
|
| 266 |
-
except Exception as e:
|
| 267 |
-
return current_image, f"❌ Error editing: {str(e)}"
|
| 268 |
|
| 269 |
-
# Initialize models
|
| 270 |
-
print("Loading AI models...")
|
| 271 |
-
models_loaded = initialize_models()
|
| 272 |
-
print(f"Models loaded: {models_loaded}")
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 281 |
<div>
|
| 282 |
-
<h1
|
| 283 |
-
<h4>
|
| 284 |
-
<
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
<
|
| 289 |
-
🎨 Text Editing
|
| 290 |
-
</span>
|
| 291 |
-
<span style="background: #FF9800; color: white; padding: 5px 10px; border-radius: 15px; margin: 5px;">
|
| 292 |
-
👗 Virtual Try-On
|
| 293 |
-
</span>
|
| 294 |
</div>
|
| 295 |
</div>
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
with gr.Row():
|
| 300 |
-
# Left Column - Model Selection
|
| 301 |
with gr.Column():
|
| 302 |
-
gr.
|
| 303 |
-
<div style="text-align: center;">
|
| 304 |
-
<h3>👤 Step 1: Choose Your Base Model</h3>
|
| 305 |
-
<p>Upload your own photo or select a base model to dress up</p>
|
| 306 |
-
</div>
|
| 307 |
-
""")
|
| 308 |
-
|
| 309 |
-
model_image = gr.Image(
|
| 310 |
-
sources=['upload', 'clipboard'],
|
| 311 |
-
type="numpy",
|
| 312 |
-
label="Upload Your Photo (Optional)",
|
| 313 |
-
height=400
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
-
# Model selection dropdown
|
| 317 |
-
model_selector = gr.Dropdown(
|
| 318 |
-
choices=list(MODEL_AVATARS.keys()),
|
| 319 |
-
value="Fashion Model A",
|
| 320 |
-
label="Or Choose a Base Model",
|
| 321 |
-
interactive=True
|
| 322 |
-
)
|
| 323 |
-
|
| 324 |
-
# Example models gallery (shows actual base model images)
|
| 325 |
-
gr.Markdown("### 📸 Available Base Models")
|
| 326 |
-
|
| 327 |
-
# Create a function to show model preview
|
| 328 |
-
def show_model_preview(model_name):
|
| 329 |
-
if model_name in MODEL_AVATARS:
|
| 330 |
-
return load_model_image_from_url(MODEL_AVATARS[model_name])
|
| 331 |
-
return create_placeholder_image("Select a model")
|
| 332 |
-
|
| 333 |
-
model_preview = gr.Image(label="Selected Base Model Preview", height=300, interactive=False)
|
| 334 |
|
| 335 |
-
# Update preview when dropdown changes
|
| 336 |
-
model_selector.change(fn=show_model_preview, inputs=[model_selector], outputs=[model_preview])
|
| 337 |
-
|
| 338 |
-
# Middle Column - Garments and Style
|
| 339 |
with gr.Column():
|
| 340 |
-
gr.HTML(
|
| 341 |
-
|
| 342 |
-
<
|
| 343 |
-
<
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
label="Describe Your Desired Style",
|
| 350 |
-
placeholder="e.g., 'casual jeans and t-shirt', 'elegant black dress', 'business suit'",
|
| 351 |
-
lines=2
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
with gr.Row():
|
| 355 |
-
garment_top = gr.Image(
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
height=200
|
| 360 |
-
)
|
| 361 |
-
garment_bottom = gr.Image(
|
| 362 |
-
sources='upload',
|
| 363 |
-
type="numpy",
|
| 364 |
-
label="Bottom Garment (Optional)",
|
| 365 |
-
height=200
|
| 366 |
-
)
|
| 367 |
-
|
| 368 |
-
# Quick style buttons
|
| 369 |
-
gr.Markdown("### 🎯 Quick Style Options")
|
| 370 |
-
with gr.Row():
|
| 371 |
-
casual_btn = gr.Button("👕 Casual", size="sm")
|
| 372 |
-
business_btn = gr.Button("💼 Business", size="sm")
|
| 373 |
-
formal_btn = gr.Button("🎩 Formal", size="sm")
|
| 374 |
-
trendy_btn = gr.Button("✨ Trendy", size="sm")
|
| 375 |
-
|
| 376 |
-
generate_btn = gr.Button("🎨 Generate Outfit", variant="primary", size="lg")
|
| 377 |
-
|
| 378 |
-
# Right Column - Results
|
| 379 |
-
with gr.Column():
|
| 380 |
-
gr.HTML("""
|
| 381 |
-
<div style="text-align: center;">
|
| 382 |
-
<h3>✨ Your Generated Outfit</h3>
|
| 383 |
-
</div>
|
| 384 |
-
""")
|
| 385 |
-
|
| 386 |
-
result_image = gr.Image(label="Generated Outfit", height=400)
|
| 387 |
-
|
| 388 |
-
generation_status = gr.Markdown("Upload a photo or select a model, then click Generate!")
|
| 389 |
-
|
| 390 |
-
# Separator
|
| 391 |
-
gr.HTML("<hr style='margin: 30px 0; border: 1px solid #ddd;'>")
|
| 392 |
-
|
| 393 |
-
# Bottom Section - Text Editor
|
| 394 |
-
gr.HTML("""
|
| 395 |
-
<div style="text-align: center; margin: 20px;">
|
| 396 |
-
<h2>✏️ Step 3: Edit Your Outfit with AI</h2>
|
| 397 |
-
<p>Use natural language to modify your generated outfit</p>
|
| 398 |
-
</div>
|
| 399 |
-
""")
|
| 400 |
-
|
| 401 |
-
with gr.Row():
|
| 402 |
-
with gr.Column():
|
| 403 |
-
edit_prompt = gr.Textbox(
|
| 404 |
-
label="Describe Your Edits",
|
| 405 |
-
placeholder="e.g., 'change shirt to red', 'add a jacket', 'make it more colorful'",
|
| 406 |
-
lines=3
|
| 407 |
-
)
|
| 408 |
-
|
| 409 |
-
with gr.Row():
|
| 410 |
-
edit_strength = gr.Slider(
|
| 411 |
-
minimum=0.3,
|
| 412 |
-
maximum=1.0,
|
| 413 |
-
value=0.7,
|
| 414 |
-
step=0.1,
|
| 415 |
-
label="Edit Strength"
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
-
edit_btn = gr.Button("🖌️ Apply Edit", variant="secondary", size="lg")
|
| 419 |
|
| 420 |
-
# Quick edit options
|
| 421 |
-
gr.Markdown("### ⚡ Quick Edits")
|
| 422 |
-
with gr.Row():
|
| 423 |
-
add_jacket_btn = gr.Button("Add Jacket", size="sm")
|
| 424 |
-
change_color_btn = gr.Button("Change Colors", size="sm")
|
| 425 |
-
make_formal_btn = gr.Button("Make Formal", size="sm")
|
| 426 |
-
add_accessories_btn = gr.Button("Add Accessories", size="sm")
|
| 427 |
-
|
| 428 |
with gr.Column():
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
### 🎯 Style Prompt Examples:
|
| 440 |
-
- "Casual weekend outfit with jeans"
|
| 441 |
-
- "Professional business attire"
|
| 442 |
-
- "Elegant evening dress for dinner"
|
| 443 |
-
- "Sporty athleisure wear"
|
| 444 |
-
- "Vintage 1950s inspired look"
|
| 445 |
-
""")
|
| 446 |
-
|
| 447 |
-
with gr.Column():
|
| 448 |
-
gr.Markdown("""
|
| 449 |
-
### ✏️ Edit Prompt Examples:
|
| 450 |
-
- "Change the shirt to a red color"
|
| 451 |
-
- "Add a denim jacket over the outfit"
|
| 452 |
-
- "Make the dress shorter and more casual"
|
| 453 |
-
- "Add gold jewelry and accessories"
|
| 454 |
-
- "Change to a winter coat and boots"
|
| 455 |
-
""")
|
| 456 |
-
|
| 457 |
-
# Store current image for editing
|
| 458 |
-
current_generated = gr.State(None)
|
| 459 |
-
|
| 460 |
-
# Event Handlers
|
| 461 |
-
def generate_outfit(model_img, garment1, garment2, style_text, selected_model):
|
| 462 |
-
result = get_outfit_tryon_result(model_img, garment1, garment2, style_text, selected_model)
|
| 463 |
-
|
| 464 |
-
if model_img is not None:
|
| 465 |
-
model_info = "uploaded photo"
|
| 466 |
-
elif selected_model:
|
| 467 |
-
model_info = selected_model
|
| 468 |
-
else:
|
| 469 |
-
model_info = "no model selected"
|
| 470 |
-
|
| 471 |
-
status = f"✅ Generated outfit on {model_info}"
|
| 472 |
-
if style_text:
|
| 473 |
-
status += f" with style: {style_text}"
|
| 474 |
-
if garment1 is not None or garment2 is not None:
|
| 475 |
-
status += f" using uploaded garments"
|
| 476 |
-
|
| 477 |
-
return result, result, status
|
| 478 |
-
|
| 479 |
-
def apply_edit(current_img, edit_text, strength):
|
| 480 |
-
if current_img is None:
|
| 481 |
-
return None, "❌ Please generate an outfit first!"
|
| 482 |
-
|
| 483 |
-
edited_img, status = edit_outfit_with_text(current_img, edit_text, strength)
|
| 484 |
-
return edited_img, status
|
| 485 |
-
|
| 486 |
-
# Connect events
|
| 487 |
-
generate_btn.click(
|
| 488 |
-
fn=generate_outfit,
|
| 489 |
-
inputs=[model_image, garment_top, garment_bottom, style_prompt, model_selector],
|
| 490 |
-
outputs=[result_image, current_generated, generation_status]
|
| 491 |
-
)
|
| 492 |
-
|
| 493 |
-
edit_btn.click(
|
| 494 |
-
fn=apply_edit,
|
| 495 |
-
inputs=[current_generated, edit_prompt, edit_strength],
|
| 496 |
-
outputs=[edited_result, edit_status]
|
| 497 |
-
)
|
| 498 |
-
|
| 499 |
-
# Quick style buttons
|
| 500 |
-
casual_btn.click(lambda: "casual jeans and t-shirt outfit", outputs=[style_prompt])
|
| 501 |
-
business_btn.click(lambda: "professional business suit", outputs=[style_prompt])
|
| 502 |
-
formal_btn.click(lambda: "elegant formal evening wear", outputs=[style_prompt])
|
| 503 |
-
trendy_btn.click(lambda: "trendy fashionable modern outfit", outputs=[style_prompt])
|
| 504 |
-
|
| 505 |
-
# Quick edit buttons
|
| 506 |
-
add_jacket_btn.click(lambda: "add a stylish jacket", outputs=[edit_prompt])
|
| 507 |
-
change_color_btn.click(lambda: "change colors to be more vibrant", outputs=[edit_prompt])
|
| 508 |
-
make_formal_btn.click(lambda: "make the outfit more formal and elegant", outputs=[edit_prompt])
|
| 509 |
-
add_accessories_btn.click(lambda: "add fashionable accessories and jewelry", outputs=[edit_prompt])
|
| 510 |
|
| 511 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 512 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
|
|
|
| 2 |
import cv2
|
| 3 |
+
import numpy as np
|
|
|
|
| 4 |
import json
|
| 5 |
import random
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
+
import asyncio
|
| 8 |
+
|
| 9 |
+
import requests
|
| 10 |
import base64
|
| 11 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
machine_number = 0
|
| 14 |
+
model = os.path.join(os.path.dirname(__file__), "models/female_model.png")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
MODEL_MAP = {
|
| 17 |
+
"AI Model female_model": 'models/female_model.png',
|
| 18 |
+
"AI Model female_model1": 'models/female_model1.png',
|
| 19 |
+
"AI Model male_model": 'models/male_model.png',
|
| 20 |
+
"AI Model male_model2": 'models/male_model2.png',
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
+
def add_waterprint(img):
|
| 24 |
+
h, w, _ = img.shape
|
| 25 |
+
img = cv2.putText(img, 'Powered by OutfitAnyone', (int(0.3*w), h-20), cv2.FONT_HERSHEY_PLAIN, 2, (128, 128, 128), 2, cv2.LINE_AA)
|
|
|
|
|
|
|
|
|
|
| 26 |
return img
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
| 30 |
+
# model_name = "AI Model " + model_name.split("\\")[-1].split(".")[0] # windows
|
| 31 |
+
model_name = "AI Model " + model_name.split("/")[-1].split(".")[0] # linux
|
| 32 |
+
print(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
encoded_garment1 = cv2.imencode('.jpg', garment1)[1].tobytes()
|
| 35 |
+
encoded_garment1 = base64.b64encode(encoded_garment1).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
if garment2 is not None:
|
| 38 |
+
encoded_garment2 = cv2.imencode('.jpg', garment2)[1].tobytes()
|
| 39 |
+
encoded_garment2 = base64.b64encode(encoded_garment2).decode('utf-8')
|
|
|
|
| 40 |
else:
|
| 41 |
+
encoded_garment2 = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
url = os.environ['OA_IP_ADDRESS']
|
| 44 |
+
headers = {'Content-Type': 'application/json'}
|
| 45 |
+
seed = random.randint(0, 1222222222)
|
| 46 |
+
data = {
|
| 47 |
+
"garment1": encoded_garment1,
|
| 48 |
+
"garment2": encoded_garment2,
|
| 49 |
+
"model_name": model_name,
|
| 50 |
+
"seed": seed
|
| 51 |
+
}
|
| 52 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 53 |
+
print("response code", response.status_code)
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
result = response.json()
|
| 56 |
+
result = base64.b64decode(result['images'][0])
|
| 57 |
+
result_np = np.frombuffer(result, np.uint8)
|
| 58 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 59 |
else:
|
| 60 |
+
print('server error!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
final_img = add_waterprint(result_img)
|
| 63 |
+
return final_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
with gr.Blocks(css = ".output-image, .input-image, .image-preview {height: 400px !important} ") as demo:
|
| 67 |
+
gr.HTML(
|
| 68 |
+
"""
|
| 69 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 70 |
+
<a href="https://github.com/HumanAIGC/OutfitAnyone" style="margin-right: 20px; text-decoration: none; display: flex; align-items: center;">
|
| 71 |
+
</a>
|
|
|
|
| 72 |
<div>
|
| 73 |
+
<h1 >Outfit Anyone: Ultra-high quality virtual try-on for Any Clothing and Any Person</h1>
|
| 74 |
+
<h4 >v0.9</h4>
|
| 75 |
+
<h5 style="margin: 0;">OutfitAnyone plus version is now online with any model and any cloth: https://www.outfitanyone.life/</h5>
|
| 76 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center;>
|
| 77 |
+
<a href="https://arxiv.org/abs/2407.16224"><img src="https://img.shields.io/badge/Arxiv-2407.16224-red"></a>
|
| 78 |
+
<a href='https://humanaigc.github.io/outfit-anyone/'><img src='https://img.shields.io/badge/Project_Page-OutfitAnyone-green' alt='Project Page'></a>
|
| 79 |
+
<a href='https://github.com/HumanAIGC/OutfitAnyone'><img src='https://img.shields.io/badge/Github-Repo-blue'></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
</div>
|
| 81 |
</div>
|
| 82 |
+
</div>
|
| 83 |
+
""")
|
|
|
|
| 84 |
with gr.Row():
|
|
|
|
| 85 |
with gr.Column():
|
| 86 |
+
init_image = gr.Image(sources='clipboard', type="filepath", label="model", value=model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
with gr.Column():
|
| 89 |
+
gr.HTML(
|
| 90 |
+
"""
|
| 91 |
+
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 92 |
+
<div>
|
| 93 |
+
<h3>Models are fixed and cannot be uploaded or modified; we only support users uploading their own garments.</h3>
|
| 94 |
+
<h4 style="margin: 0;">For a one-piece dress or coat, you only need to upload the image to the 'top garment' section and leave the 'lower garment' section empty.</h4>
|
| 95 |
+
</div>
|
| 96 |
+
</div>
|
| 97 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
with gr.Row():
|
| 99 |
+
garment_top = gr.Image(sources='upload', type="numpy", label="top garment")
|
| 100 |
+
garment_down = gr.Image(sources='upload', type="numpy", label="lower garment")
|
| 101 |
+
|
| 102 |
+
run_button = gr.Button(value="Run")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
with gr.Column():
|
| 105 |
+
gallery = gr.Image()
|
| 106 |
+
|
| 107 |
+
run_button.click(fn=get_tryon_result,
|
| 108 |
+
inputs=[
|
| 109 |
+
init_image,
|
| 110 |
+
garment_top,
|
| 111 |
+
garment_down,
|
| 112 |
+
],
|
| 113 |
+
outputs=[gallery],
|
| 114 |
+
concurrency_limit=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
|
| 118 |
+
print("ip address alibaba", ip)
|
| 119 |
+
demo.queue(max_size=10)
|
| 120 |
demo.launch()
|