Hantr commited on
Commit
cd9d2d6
·
1 Parent(s): ef65312
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -105,11 +105,36 @@ def sepia(input_img):
105
  return fig
106
 
107
 
108
- iface = gr.Interface(fn=sepia,
109
- inputs=gr.inputs.Image(type="pil", label="Upload an image"),
110
- outputs=gr.outputs.Image(type='pil', label="Segmentation result"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  examples=["city-1.jpg", "city-2.jpg", "city-3.jpg", "city-4.jpg", "city-5.jpg"],
112
  allow_flagging='never')
113
 
114
 
115
- iface.launch()
 
105
  return fig
106
 
107
 
108
+ def compare_images(input_img):
109
+ input_img = Image.fromarray(input_img)
110
+
111
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
112
+ outputs = model(**inputs)
113
+ logits = outputs.logits
114
+
115
+ logits = tf.transpose(logits, [0, 2, 3, 1])
116
+ logits = tf.image.resize(
117
+ logits, input_img.size[::-1]
118
+ )
119
+ seg = tf.math.argmax(logits, axis=-1)[0]
120
+
121
+ color_seg = np.zeros(
122
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
123
+ )
124
+ for label, color in enumerate(colormap):
125
+ color_seg[seg.numpy() == label, :] = color
126
+
127
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
128
+ pred_img = pred_img.astype(np.uint8)
129
+
130
+ return input_img, pred_img
131
+
132
+
133
+ demo = gr.Interface(fn=compare_images,
134
+ inputs=gr.Image(shape=(1024, 1024)),
135
+ outputs=["image", "image"],
136
  examples=["city-1.jpg", "city-2.jpg", "city-3.jpg", "city-4.jpg", "city-5.jpg"],
137
  allow_flagging='never')
138
 
139
 
140
+ demo.launch()