Spaces:
Runtime error
Runtime error
Sarah Yakum
commited on
Commit
•
11aec48
1
Parent(s):
b7bc15f
commit
Browse files
.bashrc
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
export PATH="$PATH:/c/Users/sarah/anaconda3:/c/Users/sarah/anaconda3/Scripts"
|
2 |
+
alias python="winpty python.exe"
|
app.py
CHANGED
@@ -1,22 +1,89 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
file_name = st.file_uploader("Upload a hot dog candidate image")
|
12 |
|
13 |
if file_name is not None:
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
st.title("Segmentation of Beauty Products")
|
4 |
+
|
5 |
+
file_name = st.file_uploader("Upload a a beauty product")
|
6 |
+
|
7 |
+
|
8 |
+
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
9 |
+
import torch
|
10 |
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
|
13 |
+
model_name = "nvidia/segformer-b5-finetuned-ade-640-640"
|
14 |
+
feature_extractor = SegformerImageProcessor.from_pretrained(model_name)
|
15 |
+
model = SegformerForSemanticSegmentation.from_pretrained(model_name)
|
16 |
+
model.to(device)
|
17 |
|
|
|
18 |
|
19 |
if file_name is not None:
|
20 |
+
image = Image.open(img_name)
|
21 |
+
image.show()
|
22 |
+
|
23 |
+
pixel_values = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
|
24 |
+
|
25 |
+
outputs = model(pixel_values)
|
26 |
+
logits = outputs.logits
|
27 |
+
|
28 |
+
def ade_palette():
|
29 |
+
return [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],
|
30 |
+
[4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],
|
31 |
+
[230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],
|
32 |
+
[150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],
|
33 |
+
[143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],
|
34 |
+
[0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],
|
35 |
+
[255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],
|
36 |
+
[255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],
|
37 |
+
[255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],
|
38 |
+
[224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],
|
39 |
+
[255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],
|
40 |
+
[6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],
|
41 |
+
[140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],
|
42 |
+
[255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],
|
43 |
+
[255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],
|
44 |
+
[11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],
|
45 |
+
[0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],
|
46 |
+
[255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],
|
47 |
+
[0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],
|
48 |
+
[173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],
|
49 |
+
[255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],
|
50 |
+
[255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],
|
51 |
+
[255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],
|
52 |
+
[0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],
|
53 |
+
[0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],
|
54 |
+
[143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],
|
55 |
+
[8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],
|
56 |
+
[255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],
|
57 |
+
[92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],
|
58 |
+
[163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],
|
59 |
+
[255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],
|
60 |
+
[255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],
|
61 |
+
[10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],
|
62 |
+
[255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],
|
63 |
+
[41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],
|
64 |
+
[71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],
|
65 |
+
[184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],
|
66 |
+
[102, 255, 0], [92, 0, 255]]
|
67 |
|
68 |
+
from torch import nn
|
69 |
+
import numpy as np
|
70 |
+
import matplotlib.pyplot as plt
|
71 |
|
72 |
+
logits = nn.functional.interpolate(outputs.logits.detach().cpu(),
|
73 |
+
size=image.size[::-1],
|
74 |
+
mode='bilinear',
|
75 |
+
align_corners=False)
|
76 |
+
seg = logits.argmax(dim=1)[0]
|
77 |
+
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
|
78 |
+
palette = np.array(ade_palette())
|
79 |
+
for label, color in enumerate(palette):
|
80 |
+
color_seg[seg == label, :] = color
|
81 |
+
|
82 |
+
color_seg = color_seg[..., ::-1]
|
83 |
+
|
84 |
+
img = np.array(image) * 0.5 + color_seg * 0.5
|
85 |
+
img = img.astype(np.uint8)
|
86 |
+
|
87 |
+
plt.figure(figsize=(15, 10))
|
88 |
+
plt.imshow(img)
|
89 |
+
plt.show()
|