Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,96 @@
|
|
1 |
-
import spaces
|
2 |
-
import gradio as gr
|
3 |
import torch
|
|
|
4 |
from PIL import Image
|
5 |
-
|
6 |
-
import
|
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 |
-
example_image_path = "example0.webp" # Replace with the actual path to the example image
|
53 |
-
example_prompt = """A Jelita Sukawati speaker is captured mid-speech. She has long, dark brown hair that cascades over her shoulders, framing her radiant, smiling face. Her Latina features are highlighted by warm, sun-kissed skin and bright, expressive eyes. She gestures with her left hand, displaying a delicate ring on her pinky finger, as she speaks passionately.
|
54 |
-
The woman is wearing a colorful, patterned dress with a green lanyard featuring multiple badges and logos hanging around her neck. The lanyard prominently displays the "CagliostroLab" text.
|
55 |
-
Behind her, there is a blurred background with a white banner containing logos and text, indicating a professional or conference setting. The overall scene captures the energy and vibrancy of her presentation."""
|
56 |
-
example_cfg_scale = 3.2
|
57 |
-
example_steps = 32
|
58 |
-
example_width = 1152
|
59 |
-
example_height = 896
|
60 |
-
example_seed = 3981632454
|
61 |
-
example_lora_scale = 0.85
|
62 |
-
|
63 |
-
def load_example():
|
64 |
-
# Load example image from file
|
65 |
-
example_image = Image.open(example_image_path)
|
66 |
-
return example_prompt, example_cfg_scale, example_steps, False, example_seed, example_width, example_height, example_lora_scale, example_image
|
67 |
-
|
68 |
-
with gr.Blocks() as app:
|
69 |
-
gr.Markdown("# Flux RealismLora Image Generator")
|
70 |
-
with gr.Row():
|
71 |
-
with gr.Column(scale=3):
|
72 |
-
prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
|
73 |
-
generate_button = gr.Button("Generate")
|
74 |
-
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
|
75 |
-
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
|
76 |
-
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
|
77 |
-
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
|
78 |
-
randomize_seed = gr.Checkbox(False, label="Randomize seed")
|
79 |
-
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
|
80 |
-
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
|
81 |
-
with gr.Column(scale=1):
|
82 |
-
result = gr.Image(label="Generated Image")
|
83 |
-
gr.Markdown("Generate images using RealismLora and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]")
|
84 |
-
|
85 |
-
# Automatically load example data and image when the interface is launched
|
86 |
-
app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import torchvision.transforms as transforms
|
3 |
from PIL import Image
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Load MiDaS model
|
9 |
+
midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
|
10 |
+
midas.eval()
|
11 |
+
|
12 |
+
# Preprocessing function
|
13 |
+
def preprocess_image(image):
|
14 |
+
transform = transforms.Compose([
|
15 |
+
transforms.Resize(384),
|
16 |
+
transforms.CenterCrop(384),
|
17 |
+
transforms.ToTensor(),
|
18 |
+
transforms.Normalize(
|
19 |
+
mean=[0.485, 0.456, 0.406],
|
20 |
+
std=[0.229, 0.224, 0.225],
|
21 |
+
),
|
22 |
+
])
|
23 |
+
return transform(image).unsqueeze(0)
|
24 |
+
|
25 |
+
# Function to generate the displacement map
|
26 |
+
def generate_displacement_map(image_a):
|
27 |
+
input_batch = preprocess_image(image_a)
|
28 |
+
|
29 |
+
with torch.no_grad():
|
30 |
+
depth_map = midas(input_batch)
|
31 |
+
|
32 |
+
depth_map = depth_map.squeeze().cpu().numpy()
|
33 |
+
depth_map = cv2.resize(depth_map, (image_a.width, image_a.height))
|
34 |
+
|
35 |
+
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
36 |
+
displacement_map = depth_map * 30
|
37 |
+
return displacement_map
|
38 |
+
|
39 |
+
# Function to warp and fit Image-B onto Image-A
|
40 |
+
def fit_and_warp_design(image_a, image_b, design_bbox):
|
41 |
+
displacement_map = generate_displacement_map(image_a)
|
42 |
+
|
43 |
+
# Extract bounding box coordinates
|
44 |
+
top_left = (int(design_bbox[0]), int(design_bbox[1]))
|
45 |
+
bottom_right = (int(design_bbox[2]), int(design_bbox[3]))
|
46 |
+
|
47 |
+
# Resize the design to fit within the specified bounding box
|
48 |
+
design_width = bottom_right[0] - top_left[0]
|
49 |
+
design_height = bottom_right[1] - top_left[1]
|
50 |
+
image_b = image_b.resize((design_width, design_height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Create a blank canvas with the same size as Image-A
|
53 |
+
canvas = Image.new('RGBA', (displacement_map.shape[1], displacement_map.shape[0]), (0, 0, 0, 0))
|
54 |
+
canvas.paste(image_b, top_left, image_b)
|
55 |
+
canvas_np = np.array(canvas)
|
56 |
+
|
57 |
+
h, w = displacement_map.shape
|
58 |
+
y_indices, x_indices = np.indices((h, w), dtype=np.float32)
|
59 |
+
x_displacement = (x_indices + displacement_map).astype(np.float32)
|
60 |
+
y_displacement = (y_indices + displacement_map).astype(np.float32)
|
61 |
+
|
62 |
+
x_displacement = np.clip(x_displacement, 0, w - 1)
|
63 |
+
y_displacement = np.clip(y_displacement, 0, h - 1)
|
64 |
+
|
65 |
+
warped_canvas = cv2.remap(canvas_np, x_displacement, y_displacement, cv2.INTER_LINEAR, borderMode=cv2.BORDER_TRANSPARENT)
|
66 |
+
|
67 |
+
image_a_rgba = image_a.convert("RGBA")
|
68 |
+
image_a_np = np.array(image_a_rgba)
|
69 |
+
|
70 |
+
non_transparent_pixels = warped_canvas[..., 3] > 0
|
71 |
+
image_a_np[non_transparent_pixels] = warped_canvas[non_transparent_pixels]
|
72 |
+
|
73 |
+
final_image = Image.fromarray(image_a_np)
|
74 |
+
return final_image
|
75 |
+
|
76 |
+
# Gradio interface function
|
77 |
+
def process_images(image_a, image_b, design_bbox):
|
78 |
+
result = fit_and_warp_design(image_a, image_b, design_bbox)
|
79 |
+
return result
|
80 |
+
|
81 |
+
# Gradio UI components
|
82 |
+
image_input_a = gr.inputs.Image(label="Upload Clothing Image", type="pil")
|
83 |
+
image_input_b = gr.inputs.Image(label="Upload Design Image", type="pil")
|
84 |
+
design_bbox_input = gr.inputs.Image(tool="select", label="Adjust Design Position and Size")
|
85 |
+
|
86 |
+
# Define the Gradio interface
|
87 |
+
iface = gr.Interface(
|
88 |
+
fn=process_images,
|
89 |
+
inputs=[image_input_a, design_bbox_input],
|
90 |
+
outputs="image",
|
91 |
+
title="Clothing Design Fitting with Drag-and-Drop",
|
92 |
+
description="Upload a clothing image and a design image. Drag and resize the design onto the clothing using the cursor.",
|
93 |
+
)
|
94 |
+
|
95 |
+
# Launch the interface
|
96 |
+
iface.launch()
|