Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,124 +1,96 @@
|
|
1 |
-
from __future__ import annotations
|
2 |
-
import math
|
3 |
-
import random
|
4 |
import gradio as gr
|
|
|
|
|
5 |
import numpy as np
|
|
|
6 |
import torch
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from huggingface_hub import hf_hub_download
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
is_cosxl_edit=True,
|
17 |
-
vae=vae,
|
18 |
-
torch_dtype=torch.float16,
|
19 |
)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
torch_dtype=torch.float16,
|
28 |
-
use_safetensors=True,
|
29 |
-
variant="fp16"
|
30 |
)
|
31 |
-
refiner.to("cuda")
|
32 |
|
33 |
-
def
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
def
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
generator=generator,
|
61 |
-
output_type="latent",
|
62 |
-
).images
|
63 |
-
refine = refiner(
|
64 |
-
prompt=f"{instruction}, 4k, hd, high quality, masterpiece",
|
65 |
-
negative_prompt=negative_prompt,
|
66 |
-
guidance_scale=7.5,
|
67 |
-
num_inference_steps=steps,
|
68 |
-
image=output_image,
|
69 |
-
generator=generator,
|
70 |
-
).images[0]
|
71 |
-
return seed, refine
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
'''
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
["./red_car.png", "add some snow"],
|
84 |
-
]
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
with gr.Row():
|
89 |
-
instruction = gr.Textbox(lines=1, label="Instruction", interactive=True)
|
90 |
-
generate_button = gr.Button("Run", scale=0)
|
91 |
-
|
92 |
-
with gr.Row():
|
93 |
-
input_image = gr.Image(label="Image", type='filepath', interactive=True)
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
label="Negative prompt",
|
103 |
-
max_lines=1,
|
104 |
-
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, ugly, disgusting, blurry, amputation,(face asymmetry, eyes asymmetry, deformed eyes, open mouth)",
|
105 |
-
visible=True
|
106 |
-
)
|
107 |
-
with gr.Row():
|
108 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, interactive=True)
|
109 |
-
seed = gr.Number(value=2404, step=1, label="Seed", interactive=True)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
)
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
|
124 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio_imageslider import ImageSlider
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
import numpy as np
|
5 |
+
import cv2
|
6 |
import torch
|
7 |
+
from torchvision import transforms
|
8 |
+
from transformers import AutoModelForImageSegmentation
|
|
|
9 |
|
10 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
11 |
|
12 |
+
# Load BiRefNet model for background removal
|
13 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
14 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
|
|
|
|
|
|
15 |
)
|
16 |
+
birefnet.to("cuda")
|
17 |
+
transform_image = transforms.Compose(
|
18 |
+
[
|
19 |
+
transforms.Resize((1024, 1024)),
|
20 |
+
transforms.ToTensor(),
|
21 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
22 |
+
]
|
|
|
|
|
|
|
23 |
)
|
|
|
24 |
|
25 |
+
def load_img(image, output_type="numpy"):
|
26 |
+
if output_type == "pil":
|
27 |
+
return Image.open(image).convert("RGB")
|
28 |
+
else:
|
29 |
+
return np.array(Image.open(image).convert("RGB"))
|
30 |
+
|
31 |
+
def add_text_to_image(image, text, position, color, font_size):
|
32 |
+
img = Image.fromarray(image)
|
33 |
+
draw = ImageDraw.Draw(img)
|
34 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
35 |
+
draw.text(position, text, fill=color, font=font)
|
36 |
+
return np.array(img)
|
37 |
|
38 |
+
def inpaint_image(image, mask, inpaint_radius):
|
39 |
+
img_cv = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
40 |
+
mask_cv = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
|
41 |
+
result = cv2.inpaint(img_cv, mask_cv, inpaint_radius, cv2.INPAINT_TELEA)
|
42 |
+
return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
43 |
|
44 |
+
def background_removal(image):
|
45 |
+
im = load_img(image, output_type="pil")
|
46 |
+
im = im.convert("RGB")
|
47 |
+
image_size = im.size
|
48 |
+
origin = im.copy()
|
49 |
+
image = load_img(im)
|
50 |
+
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
51 |
+
with torch.no_grad():
|
52 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
53 |
+
pred = preds[0].squeeze()
|
54 |
+
pred_pil = transforms.ToPILImage()(pred)
|
55 |
+
mask = pred_pil.resize(image_size)
|
56 |
+
im.putalpha(mask)
|
57 |
+
return (im, origin)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
def update_image(image, text, color, font_size, mask_image, inpaint_radius):
|
60 |
+
img_with_text = add_text_to_image(image, text, (50, 50), color, font_size)
|
61 |
+
if mask_image is not None:
|
62 |
+
mask = np.array(mask_image)
|
63 |
+
img_with_text = inpaint_image(img_with_text, mask, inpaint_radius)
|
64 |
+
return img_with_text
|
|
|
65 |
|
66 |
+
def fn(image):
|
67 |
+
return background_removal(image)
|
|
|
|
|
68 |
|
69 |
+
slider1 = ImageSlider(label="Original Image", type="pil")
|
70 |
+
slider2 = ImageSlider(label="Processed Image", type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
image_input = gr.Image(label="Upload an image for background removal")
|
73 |
+
text_input = gr.Textbox(label="Enter Text to Add", placeholder="Your text here...")
|
74 |
+
color_input = gr.ColorPicker(label="Text Color")
|
75 |
+
font_size_input = gr.Slider(minimum=10, maximum=100, label="Font Size")
|
76 |
+
mask_input = gr.Image(type="numpy", label="Upload Mask Image (for Inpainting)", optional=True)
|
77 |
+
inpaint_radius_input = gr.Slider(minimum=1, maximum=50, value=3, label="Inpaint Radius")
|
78 |
|
79 |
+
bg_removal_interface = gr.Interface(
|
80 |
+
fn, inputs=image_input, outputs=slider1, examples=["chameleon.jpg"]
|
81 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
design_editing_interface = gr.Interface(
|
84 |
+
fn=lambda image, text, color, font_size, mask_image, inpaint_radius: update_image(image, text, color, font_size, mask_image, inpaint_radius),
|
85 |
+
inputs=[image_input, text_input, color_input, font_size_input, mask_input, inpaint_radius_input],
|
86 |
+
outputs=slider2
|
87 |
+
)
|
|
|
88 |
|
89 |
+
demo = gr.TabbedInterface(
|
90 |
+
[bg_removal_interface, design_editing_interface],
|
91 |
+
["Background Removal", "Design Editing"],
|
92 |
+
title="Advanced Image Editor"
|
93 |
+
)
|
94 |
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo.launch()
|