EUNSEO56 commited on
Commit
cebfdfe
·
1 Parent(s): b00abaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -71,7 +71,7 @@ def label_to_color_image(label):
71
  raise ValueError("label value too large.")
72
  return colormap[label]
73
 
74
- def draw_plot(pred_img, seg):
75
  fig = plt.figure(figsize=(20, 15))
76
 
77
  grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
@@ -85,14 +85,19 @@ def draw_plot(pred_img, seg):
85
 
86
  unique_labels = np.unique(seg.numpy().astype("uint8"))
87
  ax = plt.subplot(grid_spec[1])
88
- plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
 
 
 
 
 
89
  ax.yaxis.tick_right()
90
  plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
91
  plt.xticks([], [])
92
  ax.tick_params(width=0.0, labelsize=25)
93
  return fig
94
 
95
- def sepia(input_img):
96
  input_img = Image.fromarray(input_img)
97
 
98
  inputs = feature_extractor(images=input_img, return_tensors="tf")
@@ -102,29 +107,17 @@ def sepia(input_img):
102
  logits = tf.transpose(logits, [0, 2, 3, 1])
103
  logits = tf.image.resize(
104
  logits, input_img.size[::-1]
105
- ) # We reverse the shape of `image` because `image.size` returns width and height.
106
  seg = tf.math.argmax(logits, axis=-1)[0]
107
 
108
- color_seg = np.zeros(
109
- (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
110
- ) # height, width, 3
111
- for label, color in enumerate(colormap):
112
- color_seg[seg.numpy() == label, :] = color
113
-
114
- # Show image + mask
115
- pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
116
- pred_img = pred_img.astype(np.uint8)
117
-
118
- fig = draw_plot(pred_img, seg)
119
-
120
-
121
  return fig
122
 
123
  demo = gr.Interface(fn=sepia,
124
- inputs=gr.Image(shape=(400, 600)),
125
- outputs=['plot'],
126
- examples=["side-1.jpg", "side-2.jpg", "side-3.jpg", "side-4.jpg"],
 
127
  allow_flagging='never')
128
 
129
-
130
  demo.launch()
 
71
  raise ValueError("label value too large.")
72
  return colormap[label]
73
 
74
+ def draw_plot(pred_img, seg, cursor_pos):
75
  fig = plt.figure(figsize=(20, 15))
76
 
77
  grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
 
85
 
86
  unique_labels = np.unique(seg.numpy().astype("uint8"))
87
  ax = plt.subplot(grid_spec[1])
88
+ cursor_x, cursor_y = cursor_pos
89
+
90
+ mask = seg.numpy() == seg.numpy()[cursor_x, cursor_y]
91
+ mask_image = FULL_COLOR_MAP[mask].reshape(pred_img.shape)
92
+
93
+ plt.imshow(mask_image.astype(np.uint8), interpolation="nearest")
94
  ax.yaxis.tick_right()
95
  plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
96
  plt.xticks([], [])
97
  ax.tick_params(width=0.0, labelsize=25)
98
  return fig
99
 
100
+ def sepia(input_img, cursor_pos):
101
  input_img = Image.fromarray(input_img)
102
 
103
  inputs = feature_extractor(images=input_img, return_tensors="tf")
 
107
  logits = tf.transpose(logits, [0, 2, 3, 1])
108
  logits = tf.image.resize(
109
  logits, input_img.size[::-1]
110
+ )
111
  seg = tf.math.argmax(logits, axis=-1)[0]
112
 
113
+ fig = draw_plot(np.array(input_img), seg, cursor_pos)
 
 
 
 
 
 
 
 
 
 
 
 
114
  return fig
115
 
116
  demo = gr.Interface(fn=sepia,
117
+ inputs=["image", "canvas"],
118
+ outputs="plot",
119
+ examples=[["side-1.jpg", [200, 300]], ["side-2.jpg", [150, 250]], ["side-3.jpg", [100, 200]], ["side-4.jpg", [250, 400]]],
120
+ live=True,
121
  allow_flagging='never')
122
 
 
123
  demo.launch()