Spaces:
Runtime error
Runtime error
Bismay commited on
Commit ·
adc77ba
1
Parent(s): 15fe318
Rollback changes
Browse files
app.py
CHANGED
|
@@ -244,7 +244,7 @@ class ClothingInpainter:
|
|
| 244 |
|
| 245 |
return images_output
|
| 246 |
|
| 247 |
-
def process_segmentation(image,
|
| 248 |
try:
|
| 249 |
if image is None:
|
| 250 |
raise gr.Error("Please upload an image")
|
|
@@ -260,25 +260,13 @@ def process_segmentation(image, clothing_parts=None, prompt=None, dilation_itera
|
|
| 260 |
if not all_masks:
|
| 261 |
logger.error("No clothing detected in the image")
|
| 262 |
raise gr.Error("No clothing detected in the image. Please try a different image.")
|
| 263 |
-
|
| 264 |
inpainter.last_mask = all_masks
|
| 265 |
-
|
| 266 |
-
# If clothing parts are selected and prompt is provided, generate variations
|
| 267 |
-
if clothing_parts and prompt:
|
| 268 |
-
# Convert selected_parts to lowercase/dash format
|
| 269 |
-
selected_parts = [p.lower() for p in clothing_parts]
|
| 270 |
-
prompt_dict = {'pos': prompt}
|
| 271 |
-
|
| 272 |
-
# Generate inpainted images
|
| 273 |
-
images = inpainter.inpaint(prompt_dict, image, selected_parts, dilation_iterations)
|
| 274 |
-
return images
|
| 275 |
-
|
| 276 |
-
# Otherwise, just show the segmentation visualization
|
| 277 |
main_parts = ['upper-clothes', 'dress', 'coat', 'pants', 'skirt']
|
| 278 |
masks = {k: v for k, v in all_masks.items() if k in main_parts}
|
| 279 |
-
vis_image = inpainter.visualize_segmentation(image, masks, selected_parts=
|
| 280 |
-
|
| 281 |
-
|
| 282 |
except gr.Error as e:
|
| 283 |
raise e
|
| 284 |
except Exception as e:
|
|
@@ -362,110 +350,76 @@ def update_selected_parts(image, selected_parts, dilation_iterations):
|
|
| 362 |
# Initialize the model
|
| 363 |
init()
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
value=2,
|
| 397 |
-
step=1,
|
| 398 |
-
label="Mask Dilation (adjust for better blending)"
|
| 399 |
-
)
|
| 400 |
-
|
| 401 |
-
# Prompt Section
|
| 402 |
-
gr.Markdown("### Describe the New Clothing")
|
| 403 |
-
prompt = gr.Textbox(
|
| 404 |
-
label="Enter your prompt",
|
| 405 |
-
placeholder="e.g., A stylish black leather jacket with silver zippers",
|
| 406 |
-
lines=2
|
| 407 |
-
)
|
| 408 |
-
|
| 409 |
-
# Generate Button
|
| 410 |
-
generate_btn = gr.Button("Generate Variations", variant="primary")
|
| 411 |
-
|
| 412 |
-
with gr.Column(scale=1):
|
| 413 |
-
# Output Section
|
| 414 |
-
gr.Markdown("### Results")
|
| 415 |
-
with gr.Group():
|
| 416 |
-
output_gallery = gr.Gallery(
|
| 417 |
-
label="Generated Variations",
|
| 418 |
-
show_label=True,
|
| 419 |
-
elem_id="gallery",
|
| 420 |
-
columns=3,
|
| 421 |
-
height="auto",
|
| 422 |
-
object_fit="contain"
|
| 423 |
-
)
|
| 424 |
-
|
| 425 |
-
# Example Image Loading
|
| 426 |
-
def load_example():
|
| 427 |
-
example_path = "example.jpg"
|
| 428 |
-
if os.path.exists(example_path):
|
| 429 |
-
return Image.open(example_path)
|
| 430 |
-
return None
|
| 431 |
-
|
| 432 |
-
example_btn.click(
|
| 433 |
-
fn=load_example,
|
| 434 |
-
outputs=input_image
|
| 435 |
-
)
|
| 436 |
-
|
| 437 |
-
# Main Processing
|
| 438 |
-
generate_btn.click(
|
| 439 |
-
fn=process_segmentation,
|
| 440 |
-
inputs=[input_image, clothing_parts, prompt, dilation_slider],
|
| 441 |
-
outputs=output_gallery
|
| 442 |
-
)
|
| 443 |
-
|
| 444 |
-
# Input Image Change Handler
|
| 445 |
-
input_image.change(
|
| 446 |
-
fn=process_segmentation,
|
| 447 |
-
inputs=[input_image, clothing_parts, prompt, dilation_slider],
|
| 448 |
-
outputs=output_gallery
|
| 449 |
-
)
|
| 450 |
-
|
| 451 |
-
# Clothing Parts Change Handler
|
| 452 |
-
clothing_parts.change(
|
| 453 |
-
fn=process_segmentation,
|
| 454 |
-
inputs=[input_image, clothing_parts, prompt, dilation_slider],
|
| 455 |
-
outputs=output_gallery
|
| 456 |
-
)
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
| 465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
if __name__ == "__main__":
|
| 468 |
-
demo = create_interface()
|
| 469 |
demo.launch(share=True)
|
| 470 |
|
| 471 |
|
|
|
|
| 244 |
|
| 245 |
return images_output
|
| 246 |
|
| 247 |
+
def process_segmentation(image, dilation_iterations=2):
|
| 248 |
try:
|
| 249 |
if image is None:
|
| 250 |
raise gr.Error("Please upload an image")
|
|
|
|
| 260 |
if not all_masks:
|
| 261 |
logger.error("No clothing detected in the image")
|
| 262 |
raise gr.Error("No clothing detected in the image. Please try a different image.")
|
|
|
|
| 263 |
inpainter.last_mask = all_masks
|
| 264 |
+
# Only show main clothing parts for selection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
main_parts = ['upper-clothes', 'dress', 'coat', 'pants', 'skirt']
|
| 266 |
masks = {k: v for k, v in all_masks.items() if k in main_parts}
|
| 267 |
+
vis_image = inpainter.visualize_segmentation(image, masks, selected_parts=None)
|
| 268 |
+
detected_parts = [k for k in masks.keys()]
|
| 269 |
+
return vis_image, gr.update(choices=detected_parts, value=[])
|
| 270 |
except gr.Error as e:
|
| 271 |
raise e
|
| 272 |
except Exception as e:
|
|
|
|
| 350 |
# Initialize the model
|
| 351 |
init()
|
| 352 |
|
| 353 |
+
# Create Gradio interface
|
| 354 |
+
with gr.Blocks(title="ClothQuill - AI Clothing Inpainting") as demo:
|
| 355 |
+
gr.Markdown("# ClothQuill - AI Clothing Inpainting")
|
| 356 |
+
gr.Markdown("Upload an image to see segmented clothing parts, then select parts to modify and describe your changes")
|
| 357 |
+
|
| 358 |
+
with gr.Row():
|
| 359 |
+
with gr.Column():
|
| 360 |
+
input_image = gr.Image(
|
| 361 |
+
type="pil",
|
| 362 |
+
label="Upload Image",
|
| 363 |
+
scale=1, # This ensures the image maintains its aspect ratio
|
| 364 |
+
height=None # Allow dynamic height based on content
|
| 365 |
+
)
|
| 366 |
+
dilation_slider = gr.Slider(
|
| 367 |
+
minimum=0,
|
| 368 |
+
maximum=5,
|
| 369 |
+
value=2,
|
| 370 |
+
step=1,
|
| 371 |
+
label="Mask Dilation",
|
| 372 |
+
info="Adjust the mask dilation to control the area of modification"
|
| 373 |
+
)
|
| 374 |
+
selected_parts = gr.CheckboxGroup(
|
| 375 |
+
choices=[],
|
| 376 |
+
label="Select parts to modify",
|
| 377 |
+
value=[]
|
| 378 |
+
)
|
| 379 |
+
prompt = gr.Textbox(
|
| 380 |
+
label="Describe the clothing you want to generate",
|
| 381 |
+
placeholder="e.g., A stylish black leather jacket"
|
| 382 |
+
)
|
| 383 |
+
generate_btn = gr.Button("Generate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
+
with gr.Column():
|
| 386 |
+
gallery = gr.Gallery(
|
| 387 |
+
label="Generated Results",
|
| 388 |
+
show_label=False,
|
| 389 |
+
columns=2,
|
| 390 |
+
height=None, # Allow dynamic height
|
| 391 |
+
object_fit="contain" # Maintain aspect ratio
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
# Add event handler for image upload
|
| 395 |
+
input_image.upload(
|
| 396 |
+
fn=process_segmentation,
|
| 397 |
+
inputs=[input_image, dilation_slider],
|
| 398 |
+
outputs=[input_image, selected_parts]
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
# Add event handler for dilation changes
|
| 402 |
+
dilation_slider.change(
|
| 403 |
+
fn=update_dilation,
|
| 404 |
+
inputs=[input_image, selected_parts,dilation_slider],
|
| 405 |
+
outputs=input_image
|
| 406 |
+
)
|
| 407 |
|
| 408 |
+
# Add event handler for generation
|
| 409 |
+
generate_btn.click(
|
| 410 |
+
fn=process_image,
|
| 411 |
+
inputs=[prompt, input_image, selected_parts, dilation_slider],
|
| 412 |
+
outputs=gallery
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
# Add event handler for part selection changes
|
| 416 |
+
selected_parts.change(
|
| 417 |
+
fn=update_selected_parts,
|
| 418 |
+
inputs=[input_image, selected_parts, dilation_slider],
|
| 419 |
+
outputs=input_image
|
| 420 |
+
)
|
| 421 |
|
| 422 |
if __name__ == "__main__":
|
|
|
|
| 423 |
demo.launch(share=True)
|
| 424 |
|
| 425 |
|