Vineedhar commited on
Commit
6859e9b
·
verified ·
1 Parent(s): 50517b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
  import tensorflow as tf
5
- import matplotlib.pyplot as plt
6
 
7
  def iou(y_true, y_pred):
8
  def f(y_true, y_pred):
@@ -22,8 +22,8 @@ def dice_coef(y_true, y_pred):
22
  def dice_loss(y_true, y_pred):
23
  return 1.0 - dice_coef(y_true, y_pred)
24
 
25
- def read_image(file, target_size=(256, 256)):
26
- img = Image.open(file).convert('RGB')
27
  img = img.resize(target_size)
28
  x = np.array(img, dtype=np.float32)
29
  x = x / 255.0
@@ -37,25 +37,36 @@ def preprocess_image(img):
37
 
38
  def predict_image(model, img):
39
  pred = model.predict(img)
40
- return pred
41
-
42
- def visualize_prediction(img, pred):
43
- fig, axs = plt.subplots(1, 2, figsize=(10, 5))
44
- axs[0].imshow(img)
45
- axs[0].set_title('Original Image')
46
- axs[1].imshow(pred[0, ...], cmap='gray') # Assuming the prediction is a mask or similar
47
- axs[1].set_title('Predicted Image')
48
- plt.close(fig)
49
- return fig
50
 
51
- # Load the model with custom loss and metric
52
- model = tf.keras.models.load_model("oryx_road_segmentation_model.h5", custom_objects={'dice_coef': dice_coef, 'iou': iou})
 
 
53
 
54
  def process_image(image):
55
  img = read_image(image)
56
  img_preprocessed = preprocess_image(img)
57
- pred = predict_image(model, img_preprocessed)
58
- return visualize_prediction(img, pred)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- iface = gr.Interface(fn=process_image, inputs="file", outputs="plot", title="orYx Models - Road Segmentation")
61
- iface.launch()
 
2
  import numpy as np
3
  from PIL import Image
4
  import tensorflow as tf
5
+
6
 
7
  def iou(y_true, y_pred):
8
  def f(y_true, y_pred):
 
22
  def dice_loss(y_true, y_pred):
23
  return 1.0 - dice_coef(y_true, y_pred)
24
 
25
+ def read_image(file_path, target_size=(256, 256)):
26
+ img = Image.open(file_path)
27
  img = img.resize(target_size)
28
  x = np.array(img, dtype=np.float32)
29
  x = x / 255.0
 
37
 
38
  def predict_image(model, img):
39
  pred = model.predict(img)
40
+ return pred[0, ...] # Taking the first item in the batch
 
 
 
 
 
 
 
 
 
41
 
42
+ # Load the model with specific custom objects
43
+ loaded_model = tf.keras.models.load_model(
44
+ "oryx_road_segmentation_model.h5",
45
+ custom_objects={'dice_coef': dice_coef, 'iou': iou})
46
 
47
  def process_image(image):
48
  img = read_image(image)
49
  img_preprocessed = preprocess_image(img)
50
+ pred = predict_image(loaded_model, img_preprocessed)
51
+
52
+ # Convert single-channel image to RGB by duplicating the channel across RGB
53
+ pred_img = np.squeeze(pred) # Remove the singleton dimension
54
+ pred_img = np.clip(pred_img, 0, 1) # Ensure all values are between 0 and 1
55
+ pred_img_rgb = np.stack((pred_img,)*3, axis=-1) # Stack grayscale across three channels to mimic RGB
56
+ pred_img_rgb = (pred_img_rgb * 255).astype(np.uint8) # Scale to 0-255 and convert to uint8
57
+ return Image.fromarray(pred_img_rgb) # Now converting a proper 2D RGB array
58
+
59
+ # Sample images directory or paths
60
+ sample_images = ["234989_sat.jpg", "259157_sat.jpg", "751359_sat.jpg","168243_sat.jpg","877873_sat.jpg","836987_sat.jpg"]
61
+
62
+ # Gradio Interface
63
+ iface = gr.Interface(
64
+ fn=process_image,
65
+ inputs=gr.Image(type="filepath"),
66
+ outputs=gr.Image(type="pil", label="Predicted Image"),
67
+ title="orYx Models' - Road Segmentation Predictor",
68
+ description="Upload an image or choose a sample and view the model's segmentation for roads on different terrains.",
69
+ examples= sample_images
70
+ )
71
 
72
+ iface.launch(debug=True)