Spaces:
Sleeping
Sleeping
fixing image input processing
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import os
|
|
8 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
9 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
10 |
|
|
|
|
|
11 |
def cityscapes_palette():
|
12 |
"""Cityscapes palette for external use."""
|
13 |
return [[128, 64, 128], [244, 35, 232], [70, 70, 70], [102, 102, 156],
|
@@ -42,13 +44,19 @@ def annotation(image:ImageDraw, color_seg:np.array):
|
|
42 |
sub_square_seg = reduced_seg[ y:y+step_size, x:x+step_size]
|
43 |
# print(f"{sub_square_seg.shape=}, {sub_square_seg.sum()}")
|
44 |
|
45 |
-
if (sub_square_seg.sum() >
|
46 |
print("light found at square ", x, y)
|
47 |
draw.rectangle([(x, y), (x + step_size, y + step_size)], outline=128, width=3)
|
48 |
|
49 |
-
def call(image:
|
50 |
-
|
|
|
|
|
51 |
print(f"{np.array(resized_image).shape=}") # 1024, 1024, 3
|
|
|
|
|
|
|
|
|
52 |
inputs = feature_extractor(images=resized_image, return_tensors="pt")
|
53 |
|
54 |
outputs = model(**inputs)
|
@@ -58,7 +66,7 @@ def call(image: Image):
|
|
58 |
# First, rescale logits to original image size
|
59 |
interpolated_logits = nn.functional.interpolate(
|
60 |
outputs.logits,
|
61 |
-
size=resized_image.size[::-1], # (height, width)
|
62 |
mode='bilinear',
|
63 |
align_corners=False)
|
64 |
print(f"{interpolated_logits.shape=}, {outputs.logits.shape=}") # 1, 19, 1024, 1024
|
@@ -85,8 +93,8 @@ def call(image: Image):
|
|
85 |
|
86 |
return out_im_file
|
87 |
|
88 |
-
original_image = Image.open("./examples/1.jpg")
|
89 |
-
print(f"{np.array(original_image).shape=}") # eg 729, 1000, 3
|
90 |
|
91 |
# out = call(original_image)
|
92 |
# out.save("out2.jpeg")
|
@@ -101,7 +109,11 @@ iface = gr.Interface(fn=call,
|
|
101 |
description=description,
|
102 |
examples=[
|
103 |
os.path.join(os.path.dirname(__file__), "examples/1.jpg"),
|
104 |
-
os.path.join(os.path.dirname(__file__), "examples/2.jpg")
|
|
|
|
|
|
|
|
|
105 |
],
|
106 |
thumbnail="thumbnail.webp")
|
107 |
iface.launch()
|
|
|
8 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
9 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
10 |
|
11 |
+
# https://github.com/NielsRogge/Transformers-Tutorials/blob/master/SegFormer/Segformer_inference_notebook.ipynb
|
12 |
+
|
13 |
def cityscapes_palette():
|
14 |
"""Cityscapes palette for external use."""
|
15 |
return [[128, 64, 128], [244, 35, 232], [70, 70, 70], [102, 102, 156],
|
|
|
44 |
sub_square_seg = reduced_seg[ y:y+step_size, x:x+step_size]
|
45 |
# print(f"{sub_square_seg.shape=}, {sub_square_seg.sum()}")
|
46 |
|
47 |
+
if (sub_square_seg.sum() > 100000):
|
48 |
print("light found at square ", x, y)
|
49 |
draw.rectangle([(x, y), (x + step_size, y + step_size)], outline=128, width=3)
|
50 |
|
51 |
+
def call(image): #nparray
|
52 |
+
|
53 |
+
resized = Image.fromarray(image).resize((1024,1024))
|
54 |
+
resized_image = np.array(resized)
|
55 |
print(f"{np.array(resized_image).shape=}") # 1024, 1024, 3
|
56 |
+
|
57 |
+
# resized_image = Image.fromarray(resized_image_np)
|
58 |
+
# print(f"{resized_image=}")
|
59 |
+
|
60 |
inputs = feature_extractor(images=resized_image, return_tensors="pt")
|
61 |
|
62 |
outputs = model(**inputs)
|
|
|
66 |
# First, rescale logits to original image size
|
67 |
interpolated_logits = nn.functional.interpolate(
|
68 |
outputs.logits,
|
69 |
+
size=[1024, 1024], #resized_image.size[::-1], # (height, width)
|
70 |
mode='bilinear',
|
71 |
align_corners=False)
|
72 |
print(f"{interpolated_logits.shape=}, {outputs.logits.shape=}") # 1, 19, 1024, 1024
|
|
|
93 |
|
94 |
return out_im_file
|
95 |
|
96 |
+
# original_image = Image.open("./examples/1.jpg")
|
97 |
+
# print(f"{np.array(original_image).shape=}") # eg 729, 1000, 3
|
98 |
|
99 |
# out = call(original_image)
|
100 |
# out.save("out2.jpeg")
|
|
|
109 |
description=description,
|
110 |
examples=[
|
111 |
os.path.join(os.path.dirname(__file__), "examples/1.jpg"),
|
112 |
+
os.path.join(os.path.dirname(__file__), "examples/2.jpg"),
|
113 |
+
os.path.join(os.path.dirname(__file__), "examples/3.jpg"),
|
114 |
+
os.path.join(os.path.dirname(__file__), "examples/4.jpg"),
|
115 |
+
os.path.join(os.path.dirname(__file__), "examples/5.jpg"),
|
116 |
+
os.path.join(os.path.dirname(__file__), "examples/6.jpg"),
|
117 |
],
|
118 |
thumbnail="thumbnail.webp")
|
119 |
iface.launch()
|