aagoluoglu commited on
Commit
cd92a82
·
verified ·
1 Parent(s): b8d3e14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -70,9 +70,12 @@ 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, resize to 256
74
  image = Image.open(bytes_io).convert("RGB")
75
- #image = image.resize((256, 256))
 
 
 
76
 
77
  return image
78
 
@@ -94,7 +97,7 @@ def load_model():
94
 
95
  return model, processor, device
96
 
97
- def show_mask(mask, ax, width, height, random_color=False):
98
  if random_color:
99
  color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
100
  else:
@@ -229,7 +232,7 @@ def server(input: Inputs, output: Outputs, session: Session):
229
  if image is None or prob is None or prediction is None:
230
  # Return a placeholder plot or message
231
  fig, ax = plt.subplots()
232
- ax.text(0.5, 0.5, "Upload an image to see predictions", fontsize=12, ha="center")
233
  ax.axis("off") # Hide axis
234
  plt.tight_layout()
235
  return fig
@@ -238,25 +241,22 @@ def server(input: Inputs, output: Outputs, session: Session):
238
 
239
  # Extract the image data
240
  #image_data = image.cpu().detach().numpy()
241
-
242
- # Get the dimensions (width and height) of the image
243
- width, height = image.size
244
 
245
  # Plot the first image on the left
246
  axes[0].imshow(image)
247
  axes[0].set_title("Image")
248
 
249
  # Plot the probability map on the right
250
- axes[1].imshow(prob, extent=(0, width, height, 0)) # Setting extent to match original image dimensions
251
- axes[1].set_title(f"Probability Map {prob.shape}")
252
 
253
  # Plot the prediction image on the right
254
- axes[2].imshow(prediction, extent=(0, width, height, 0)) # Setting extent to match original image dimensions
255
  axes[2].set_title("Prediction")
256
 
257
  # Plot the predicted mask on the right
258
  axes[3].imshow(image)
259
- show_mask(prediction, axes[3], width, height)
260
  axes[3].set_title("Predicted Mask")
261
 
262
  # Hide axis ticks and labels
 
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")
75
+ w, h = image.size
76
+ dim = min(w, h)
77
+ image = image.crop(0, 0, dim, dim)
78
+ image = image.resize((256, 256))
79
 
80
  return image
81
 
 
97
 
98
  return model, processor, device
99
 
100
+ def show_mask(mask, ax, random_color=False):
101
  if random_color:
102
  color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
103
  else:
 
232
  if image is None or prob is None or prediction is None:
233
  # Return a placeholder plot or message
234
  fig, ax = plt.subplots()
235
+ ax.text(0.5, 0.5, "Upload a square image to see predictions. If the image is not a square, the image will be cropped to a square, taking the top left portion of the image. Predictions will take a few moments to load.", fontsize=12, ha="center")
236
  ax.axis("off") # Hide axis
237
  plt.tight_layout()
238
  return fig
 
241
 
242
  # Extract the image data
243
  #image_data = image.cpu().detach().numpy()
 
 
 
244
 
245
  # Plot the first image on the left
246
  axes[0].imshow(image)
247
  axes[0].set_title("Image")
248
 
249
  # Plot the probability map on the right
250
+ axes[1].imshow(prob)
251
+ axes[1].set_title("Probability Map")
252
 
253
  # Plot the prediction image on the right
254
+ axes[2].imshow(prediction)
255
  axes[2].set_title("Prediction")
256
 
257
  # Plot the predicted mask on the right
258
  axes[3].imshow(image)
259
+ show_mask(prediction, axes[3])
260
  axes[3].set_title("Predicted Mask")
261
 
262
  # Hide axis ticks and labels