aagoluoglu commited on
Commit
6d6e3fa
·
1 Parent(s): 83840f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -32,7 +32,7 @@ app_ui = ui.page_fillable(
32
  shinyswatch.theme.minty(),
33
  ui.layout_sidebar(
34
  ui.sidebar(
35
- ui.input_file("tile_image", "Choose TIFF File", accept=[".tif", ".tiff", ".png"], multiple=False),
36
  # Artwork by @allison_horst
37
  ui.input_selectize(
38
  "xvar",
@@ -53,7 +53,7 @@ app_ui = ui.page_fillable(
53
  ui.input_switch("by_species", "Show species", value=True),
54
  ui.input_switch("show_margins", "Show marginal plots", value=True),
55
  ),
56
- ui.output_image("uploaded_image"), # display the uploaded TIFF sidewalk tile image
57
  ui.output_plot("prediction_plots", fill=True),
58
  ui.output_ui("value_boxes"),
59
  ui.output_plot("scatter", fill=True),
@@ -66,9 +66,9 @@ app_ui = ui.page_fillable(
66
  )
67
 
68
  ### HELPER FUNCTIONS ###
69
- def tif_bytes_to_pil_image(tif_bytes):
70
- # Create a BytesIO object from the TIFF bytes
71
- bytes_io = io.BytesIO(tif_bytes)
72
 
73
  # Open the BytesIO object as an Image, crop to square, resize to 256
74
  image = Image.open(bytes_io).convert("RGB")
@@ -126,7 +126,7 @@ def server(input: Inputs, output: Outputs, session: Session):
126
  """Displays the uploaded image"""
127
  img_src = uploaded_image_path()
128
  if img_src:
129
- img: ImgData = {"src": str(img_src), "width": "100px"}
130
  print("IMAGE", img)
131
  return img
132
  else:
@@ -187,7 +187,7 @@ def server(input: Inputs, output: Outputs, session: Session):
187
  image_bytes = f.read()
188
 
189
  # Convert the image bytes to a PIL Image
190
- image = tif_bytes_to_pil_image(image_bytes)
191
 
192
  """ Prepare Inputs """
193
  # get input points prompt (grid of points)
@@ -234,7 +234,7 @@ def server(input: Inputs, output: Outputs, session: Session):
234
  if image is None or prob is None or prediction is None:
235
  # Return a placeholder plot or message
236
  fig, ax = plt.subplots()
237
- ax.text(0.5, 0.5, "Upload a square image to see predictions. Predictions will take a few moments to load.", fontsize=12, ha="center")
238
  ax.axis("off") # Hide axis
239
  plt.tight_layout()
240
  return fig
@@ -252,14 +252,14 @@ def server(input: Inputs, output: Outputs, session: Session):
252
  axes[1].imshow(prob)
253
  axes[1].set_title("Probability Map")
254
 
255
- # # Plot the prediction image on the right
256
- # axes[2].imshow(prediction)
257
- # axes[2].set_title("Prediction")
258
 
259
- # # Plot the predicted mask on the right
260
- # axes[3].imshow(image)
261
- # show_mask(prediction, axes[3])
262
- # axes[3].set_title("Predicted Mask")
263
 
264
  # Hide axis ticks and labels
265
  for ax in axes:
 
32
  shinyswatch.theme.minty(),
33
  ui.layout_sidebar(
34
  ui.sidebar(
35
+ ui.input_file("tile_image", "Choose an Image", accept=[".tif", ".tiff", ".png"], multiple=False),
36
  # Artwork by @allison_horst
37
  ui.input_selectize(
38
  "xvar",
 
53
  ui.input_switch("by_species", "Show species", value=True),
54
  ui.input_switch("show_margins", "Show marginal plots", value=True),
55
  ),
56
+ ui.output_image("uploaded_image"), # display the uploaded sidewalk tile image
57
  ui.output_plot("prediction_plots", fill=True),
58
  ui.output_ui("value_boxes"),
59
  ui.output_plot("scatter", fill=True),
 
66
  )
67
 
68
  ### HELPER FUNCTIONS ###
69
+ def bytes_to_pil_image(bytes):
70
+ # Create a BytesIO object from the bytes
71
+ bytes_io = io.BytesIO(bytes)
72
 
73
  # Open the BytesIO object as an Image, crop to square, resize to 256
74
  image = Image.open(bytes_io).convert("RGB")
 
126
  """Displays the uploaded image"""
127
  img_src = uploaded_image_path()
128
  if img_src:
129
+ img: ImgData = {"src": str(img_src), "width": "200px"}
130
  print("IMAGE", img)
131
  return img
132
  else:
 
187
  image_bytes = f.read()
188
 
189
  # Convert the image bytes to a PIL Image
190
+ image = bytes_to_pil_image(image_bytes)
191
 
192
  """ Prepare Inputs """
193
  # get input points prompt (grid of points)
 
234
  if image is None or prob is None or prediction is None:
235
  # Return a placeholder plot or message
236
  fig, ax = plt.subplots()
237
+ ax.text(0.5, 0.5, "Upload an image to see predictions. Predictions will take a few moments to load.", fontsize=12, ha="center")
238
  ax.axis("off") # Hide axis
239
  plt.tight_layout()
240
  return fig
 
252
  axes[1].imshow(prob)
253
  axes[1].set_title("Probability Map")
254
 
255
+ # Plot the prediction image on the right
256
+ axes[2].imshow(prediction)
257
+ axes[2].set_title("Prediction")
258
 
259
+ # Plot the predicted mask on the right
260
+ axes[3].imshow(image)
261
+ show_mask(prediction, axes[3])
262
+ axes[3].set_title("Predicted Mask")
263
 
264
  # Hide axis ticks and labels
265
  for ax in axes: