Spaces:
Runtime error
Runtime error
examples
Browse files
app.py
CHANGED
@@ -22,11 +22,26 @@ IMAGE_SIZE = 1024
|
|
22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
EXAMPLES = [
|
26 |
[
|
27 |
{
|
28 |
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
29 |
-
"layers": [Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw)],
|
30 |
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
31 |
},
|
32 |
"little lion",
|
@@ -38,10 +53,10 @@ EXAMPLES = [
|
|
38 |
[
|
39 |
{
|
40 |
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
41 |
-
"layers": [Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw)],
|
42 |
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
43 |
},
|
44 |
-
"tattoos",
|
45 |
42,
|
46 |
False,
|
47 |
0.85,
|
@@ -203,7 +218,7 @@ with gr.Blocks() as demo:
|
|
203 |
output_mask_component
|
204 |
],
|
205 |
run_on_click=True,
|
206 |
-
cache_examples=
|
207 |
)
|
208 |
|
209 |
submit_button_component.click(
|
|
|
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",
|
|
|
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 |
+
"tribal tattoos",
|
60 |
42,
|
61 |
False,
|
62 |
0.85,
|
|
|
218 |
output_mask_component
|
219 |
],
|
220 |
run_on_click=True,
|
221 |
+
cache_examples=True
|
222 |
)
|
223 |
|
224 |
submit_button_component.click(
|