Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from torchvision import models, transforms
|
| 6 |
+
|
| 7 |
+
# Load the models
|
| 8 |
+
text_to_image_pipeline = pipeline("text-to-image-generation", model="CompVis/stable-diffusion-v1-4")
|
| 9 |
+
segmentation_model = models.segmentation.deeplabv3_resnet101(pretrained=True)
|
| 10 |
+
segmentation_model.eval()
|
| 11 |
+
|
| 12 |
+
# Define transformation for the segmentation model
|
| 13 |
+
preprocess = transforms.Compose([
|
| 14 |
+
transforms.Resize(256),
|
| 15 |
+
transforms.CenterCrop(224),
|
| 16 |
+
transforms.ToTensor(),
|
| 17 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 18 |
+
])
|
| 19 |
+
|
| 20 |
+
# Helper function to segment clothing area
|
| 21 |
+
def segment_clothing(image):
|
| 22 |
+
input_tensor = preprocess(image)
|
| 23 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 24 |
+
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
output = segmentation_model(input_batch)['out'][0]
|
| 27 |
+
output_predictions = output.argmax(0)
|
| 28 |
+
mask = output_predictions.byte().cpu().numpy()
|
| 29 |
+
|
| 30 |
+
return mask
|
| 31 |
+
|
| 32 |
+
# Function to generate base image
|
| 33 |
+
def generate_base_image(base_prompt_part1, base_prompt_color, base_prompt_clothing):
|
| 34 |
+
# Combine the parts to create the full base prompt
|
| 35 |
+
base_prompt = f"{base_prompt_part1} {base_prompt_color} {base_prompt_clothing}"
|
| 36 |
+
|
| 37 |
+
# Generate base clothing image
|
| 38 |
+
base_image = text_to_image_pipeline(base_prompt)[0]
|
| 39 |
+
base_image = Image.fromarray(base_image)
|
| 40 |
+
|
| 41 |
+
return base_image
|
| 42 |
+
|
| 43 |
+
# Define the function to generate design and paste it on the clothing
|
| 44 |
+
def generate_and_paste_design(base_image, design_prompt):
|
| 45 |
+
# Generate design
|
| 46 |
+
generated_image = text_to_image_pipeline(design_prompt)[0]
|
| 47 |
+
generated_design = Image.fromarray(generated_image)
|
| 48 |
+
|
| 49 |
+
# Segment the clothing area
|
| 50 |
+
clothing_mask = segment_clothing(base_image)
|
| 51 |
+
|
| 52 |
+
# Ensure the generated design fits within the clothing area
|
| 53 |
+
generated_design = generated_design.resize(base_image.size)
|
| 54 |
+
|
| 55 |
+
# Paste the design onto the clothing area
|
| 56 |
+
clothing_area = Image.composite(generated_design, base_image, Image.fromarray(clothing_mask*255))
|
| 57 |
+
|
| 58 |
+
return clothing_area
|
| 59 |
+
|
| 60 |
+
# Create the Gradio interface
|
| 61 |
+
base_prompt_part1_input = gr.inputs.Textbox(lines=1, placeholder="Enter 'a single plain'")
|
| 62 |
+
base_prompt_color_input = gr.inputs.Textbox(lines=1, placeholder="Enter color type")
|
| 63 |
+
base_prompt_clothing_input = gr.inputs.Textbox(lines=1, placeholder="Enter clothing type")
|
| 64 |
+
design_prompt_input = gr.inputs.Textbox(lines=1, placeholder="Enter design prompt")
|
| 65 |
+
output_image = gr.outputs.Image(type="pil")
|
| 66 |
+
|
| 67 |
+
def full_process(base_prompt_part1, base_prompt_color, base_prompt_clothing, design_prompt):
|
| 68 |
+
# Generate the base image
|
| 69 |
+
base_image = generate_base_image(base_prompt_part1, base_prompt_color, base_prompt_clothing)
|
| 70 |
+
|
| 71 |
+
# Generate and paste the design on the base image
|
| 72 |
+
final_image = generate_and_paste_design(base_image, design_prompt)
|
| 73 |
+
|
| 74 |
+
return final_image
|
| 75 |
+
|
| 76 |
+
gr.Interface(
|
| 77 |
+
fn=full_process,
|
| 78 |
+
inputs=[base_prompt_part1_input, base_prompt_color_input, base_prompt_clothing_input, design_prompt_input],
|
| 79 |
+
outputs=output_image,
|
| 80 |
+
title="Design and Paste on Clothing",
|
| 81 |
+
description="Generate a base clothing image from the given prompts and paste the generated design onto it."
|
| 82 |
+
).launch()
|