Spaces:
Runtime error
Runtime error
test examples
Browse files
app.py
CHANGED
@@ -22,29 +22,44 @@ 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",
|
33 |
42,
|
34 |
False,
|
35 |
-
0.
|
36 |
30
|
37 |
],
|
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.
|
48 |
30
|
49 |
]
|
50 |
]
|
|
|
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.85,
|
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.85,
|
63 |
30
|
64 |
]
|
65 |
]
|