Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model('unet_model.h5')
|
| 8 |
+
|
| 9 |
+
# Function to preprocess the images
|
| 10 |
+
def preprocess_images(old_image, current_image, img_size=128):
|
| 11 |
+
old_image_resized = cv2.resize(old_image, (img_size, img_size)) / 255.0
|
| 12 |
+
current_image_resized = cv2.resize(current_image, (img_size, img_size)) / 255.0
|
| 13 |
+
input_combined = np.concatenate([old_image_resized, current_image_resized], axis=-1)
|
| 14 |
+
input_combined = np.expand_dims(input_combined, axis=0)
|
| 15 |
+
return input_combined
|
| 16 |
+
|
| 17 |
+
# Prediction function
|
| 18 |
+
def predict_mask(old_image, current_image):
|
| 19 |
+
# Convert from PIL to numpy array
|
| 20 |
+
old_image_np = np.array(old_image)
|
| 21 |
+
current_image_np = np.array(current_image)
|
| 22 |
+
|
| 23 |
+
# Preprocess images
|
| 24 |
+
preprocessed_input = preprocess_images(old_image_np, current_image_np)
|
| 25 |
+
|
| 26 |
+
# Predict the mask
|
| 27 |
+
prediction = model.predict(preprocessed_input)
|
| 28 |
+
prediction_mask = (prediction[0] > 0.5).astype(np.uint8) * 255 # Binary mask
|
| 29 |
+
|
| 30 |
+
# Resize mask back to original size
|
| 31 |
+
mask_resized = cv2.resize(prediction_mask, (old_image_np.shape[1], old_image_np.shape[0]))
|
| 32 |
+
|
| 33 |
+
return mask_resized
|
| 34 |
+
|
| 35 |
+
# Gradio Interface
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=predict_mask,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Image(label="Old Image"),
|
| 40 |
+
gr.Image(label="Current Image")
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Image(label="Predicted Mask"),
|
| 43 |
+
title="Change Detection with U-Net",
|
| 44 |
+
description="Upload two images (old and current) to detect changes using a U-Net model."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
interface.launch()
|