Spaces:
Running
on
Zero
Running
on
Zero
test examples
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from typing import Tuple
|
2 |
|
|
|
3 |
import random
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
@@ -20,14 +21,42 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
20 |
IMAGE_SIZE = 1024
|
21 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
EXAMPLES = [
|
24 |
[
|
25 |
{
|
26 |
-
"background": "https://media.roboflow.com/spaces/doge-2-image.
|
27 |
-
"layers": ["https://media.roboflow.com/spaces/doge-2-mask.png"],
|
28 |
-
"composite":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
},
|
30 |
-
"
|
31 |
42,
|
32 |
False,
|
33 |
0.9,
|
@@ -76,18 +105,18 @@ def process(
|
|
76 |
):
|
77 |
if not input_text:
|
78 |
gr.Info("Please enter a text prompt.")
|
79 |
-
return None
|
80 |
|
81 |
image = input_image_editor['background']
|
82 |
mask = input_image_editor['layers'][0]
|
83 |
|
84 |
if not image:
|
85 |
gr.Info("Please upload an image.")
|
86 |
-
return None
|
87 |
|
88 |
if not mask:
|
89 |
gr.Info("Please draw a mask on the image.")
|
90 |
-
return None
|
91 |
|
92 |
width, height = resize_image_dimensions(original_resolution_wh=image.size)
|
93 |
resized_image = image.resize((width, height), Image.LANCZOS)
|
@@ -168,10 +197,10 @@ with gr.Blocks() as demo:
|
|
168 |
)
|
169 |
with gr.Column():
|
170 |
output_image_component = gr.Image(
|
171 |
-
type='pil', image_mode='RGB', label='Generated image')
|
172 |
with gr.Accordion("Debug", open=False):
|
173 |
output_mask_component = gr.Image(
|
174 |
-
type='pil', image_mode='RGB', label='Input mask')
|
175 |
with gr.Row():
|
176 |
gr.Examples(
|
177 |
fn=process,
|
|
|
1 |
from typing import Tuple
|
2 |
|
3 |
+
import requests
|
4 |
import random
|
5 |
import numpy as np
|
6 |
import gradio as gr
|
|
|
21 |
IMAGE_SIZE = 1024
|
22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
|
24 |
+
|
25 |
+
def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
26 |
+
image = image.convert("RGBA")
|
27 |
+
data = image.getdata()
|
28 |
+
new_data = []
|
29 |
+
for item in data:
|
30 |
+
avg = sum(item[:3]) / 3
|
31 |
+
if avg < threshold:
|
32 |
+
new_data.append((0, 0, 0, 0))
|
33 |
+
else:
|
34 |
+
new_data.append(item)
|
35 |
+
|
36 |
+
image.putdata(new_data)
|
37 |
+
return image
|
38 |
+
|
39 |
+
|
40 |
EXAMPLES = [
|
41 |
[
|
42 |
{
|
43 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
44 |
+
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
|
45 |
+
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
46 |
+
},
|
47 |
+
"little lion",
|
48 |
+
42,
|
49 |
+
False,
|
50 |
+
0.9,
|
51 |
+
30
|
52 |
+
],
|
53 |
+
[
|
54 |
+
{
|
55 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
56 |
+
"layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
|
57 |
+
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
58 |
},
|
59 |
+
"tattoos",
|
60 |
42,
|
61 |
False,
|
62 |
0.9,
|
|
|
105 |
):
|
106 |
if not input_text:
|
107 |
gr.Info("Please enter a text prompt.")
|
108 |
+
return None, None
|
109 |
|
110 |
image = input_image_editor['background']
|
111 |
mask = input_image_editor['layers'][0]
|
112 |
|
113 |
if not image:
|
114 |
gr.Info("Please upload an image.")
|
115 |
+
return None, None
|
116 |
|
117 |
if not mask:
|
118 |
gr.Info("Please draw a mask on the image.")
|
119 |
+
return None, None
|
120 |
|
121 |
width, height = resize_image_dimensions(original_resolution_wh=image.size)
|
122 |
resized_image = image.resize((width, height), Image.LANCZOS)
|
|
|
197 |
)
|
198 |
with gr.Column():
|
199 |
output_image_component = gr.Image(
|
200 |
+
type='pil', image_mode='RGB', label='Generated image', format="png")
|
201 |
with gr.Accordion("Debug", open=False):
|
202 |
output_mask_component = gr.Image(
|
203 |
+
type='pil', image_mode='RGB', label='Input mask', format="png")
|
204 |
with gr.Row():
|
205 |
gr.Examples(
|
206 |
fn=process,
|