Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionInpaintPipeline
|
4 |
-
|
5 |
-
# pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
6 |
-
# "stabilityai/stable-diffusion-2-inpainting",
|
7 |
-
# torch_dtype=torch.float16,
|
8 |
-
# )
|
9 |
from PIL import Image
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Placeholder function for processing
|
13 |
# Replace this with your actual processing logic
|
14 |
# For example, modifying the image based on the slider value and prompt
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
return processed_image
|
17 |
|
18 |
with gr.Blocks() as demo:
|
19 |
# Title at the top center
|
20 |
-
gr.Markdown("<h1 style='text-align: center;'>
|
21 |
|
22 |
with gr.Row():
|
23 |
with gr.Column(scale=1):
|
24 |
# Image upload on the left
|
25 |
image_input = gr.Image(type='pil', label='Upload Image')
|
26 |
# Slider below the image upload
|
27 |
-
slider = gr.Slider(minimum=1, maximum=4, step=
|
28 |
# Textbox for prompt
|
29 |
prompt_input = gr.Textbox(label='Enter Prompt')
|
30 |
# Submit button
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionInpaintPipeline
|
4 |
+
import cv2
|
|
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
|
7 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
8 |
+
"stabilityai/stable-diffusion-2-inpainting",
|
9 |
+
torch_dtype=torch.float16,
|
10 |
+
)
|
11 |
+
|
12 |
+
def zoom_out(image, n):
|
13 |
+
# Original dimensions
|
14 |
+
original_width, original_height = image.size
|
15 |
+
# Calculate new dimensions after shrinking
|
16 |
+
new_width = int(original_width // n)
|
17 |
+
new_height = int(original_height // n)
|
18 |
+
print(new_width,new_height)
|
19 |
+
# Resize the image (shrink the object)
|
20 |
+
resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)
|
21 |
+
|
22 |
+
# Create a new white canvas that is n times larger than the original
|
23 |
+
new_canvas_width = original_width
|
24 |
+
new_canvas_height = original_height
|
25 |
+
new_image = Image.new("RGB", (new_canvas_width, new_canvas_height), "white")
|
26 |
+
|
27 |
+
# Calculate the position to paste the resized object in the center
|
28 |
+
x_offset = (new_canvas_width - new_width) // 2
|
29 |
+
y_offset = (new_canvas_height - new_height) // 2
|
30 |
+
|
31 |
+
# Paste the resized image onto the center of the new canvas
|
32 |
+
new_image.paste(resized_image, (x_offset, y_offset))
|
33 |
+
return new_image
|
34 |
+
|
35 |
+
|
36 |
+
def create_contour_mask(image, tolerance=10):
|
37 |
+
# Convert the image to a numpy array
|
38 |
+
image_array = np.array(image)
|
39 |
+
|
40 |
+
# Define a tolerance range around white
|
41 |
+
lower_bound = np.array([255 - tolerance] * 3)
|
42 |
+
upper_bound = np.array([255] * 3)
|
43 |
+
|
44 |
+
# Create a mask: black for background, white for object
|
45 |
+
mask = np.ones((image_array.shape[0], image_array.shape[1]), dtype=np.uint8) * 255
|
46 |
+
non_white_pixels = np.any((image_array < lower_bound) | (image_array > upper_bound), axis=-1)
|
47 |
+
mask[non_white_pixels] = 0 # Non-white pixels become black in the mask
|
48 |
+
|
49 |
+
# Perform dilation to fill in any gaps (like white text within the object)
|
50 |
+
kernel = np.ones((2, 2), np.uint8) # You can adjust the size of the kernel as needed
|
51 |
+
dilated_mask = cv2.dilate(mask, kernel, iterations=2)
|
52 |
+
|
53 |
+
# Invert the dilated mask to get the final object mask
|
54 |
+
# object_mask = 255 - dilated_mask
|
55 |
+
|
56 |
+
# Convert to PIL image for easier visualization and further processing
|
57 |
+
mask_image = Image.fromarray(dilated_mask)
|
58 |
+
|
59 |
+
return mask_image
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
def process_image(image: Image.Image, prompt: str, slider_value: float) -> Image.Image:
|
64 |
# Placeholder function for processing
|
65 |
# Replace this with your actual processing logic
|
66 |
# For example, modifying the image based on the slider value and prompt
|
67 |
+
if slider_value != 1:
|
68 |
+
image = zoom_out(image,slider_value)
|
69 |
+
mask = create_contour_mask(image,10)
|
70 |
+
# processed_image = image.copy() # Just returning a copy for now
|
71 |
+
processed_image = pipe(prompt=prompt, image=image_big, mask_image=contour_mask).images[0]
|
72 |
return processed_image
|
73 |
|
74 |
with gr.Blocks() as demo:
|
75 |
# Title at the top center
|
76 |
+
gr.Markdown("<h1 style='text-align: center;'>Imagine Backgrounds</h1>")
|
77 |
|
78 |
with gr.Row():
|
79 |
with gr.Column(scale=1):
|
80 |
# Image upload on the left
|
81 |
image_input = gr.Image(type='pil', label='Upload Image')
|
82 |
# Slider below the image upload
|
83 |
+
slider = gr.Slider(minimum=1, maximum=4, step=0.2, value=1, label='Select Zoom')
|
84 |
# Textbox for prompt
|
85 |
prompt_input = gr.Textbox(label='Enter Prompt')
|
86 |
# Submit button
|