|
|
import cv2 |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('unet_model.h5') |
|
|
|
|
|
|
|
|
def preprocess_images(old_image, current_image, img_size=128): |
|
|
old_image_resized = cv2.resize(old_image, (img_size, img_size)) / 255.0 |
|
|
current_image_resized = cv2.resize(current_image, (img_size, img_size)) / 255.0 |
|
|
input_combined = np.concatenate([old_image_resized, current_image_resized], axis=-1) |
|
|
input_combined = np.expand_dims(input_combined, axis=0) |
|
|
return input_combined |
|
|
|
|
|
|
|
|
def predict_mask(old_image, current_image): |
|
|
|
|
|
old_image_np = np.array(old_image) |
|
|
current_image_np = np.array(current_image) |
|
|
|
|
|
|
|
|
preprocessed_input = preprocess_images(old_image_np, current_image_np) |
|
|
|
|
|
|
|
|
prediction = model.predict(preprocessed_input) |
|
|
prediction_mask = (prediction[0] > 0.5).astype(np.uint8) * 255 |
|
|
|
|
|
|
|
|
mask_resized = cv2.resize(prediction_mask, (old_image_np.shape[1], old_image_np.shape[0])) |
|
|
|
|
|
return mask_resized |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=predict_mask, |
|
|
inputs=[ |
|
|
gr.Image(label="Old Image"), |
|
|
gr.Image(label="Current Image") |
|
|
], |
|
|
outputs=gr.Image(label="Predicted Mask"), |
|
|
title="Change Detection with U-Net", |
|
|
description="Upload two images (old and current) to detect changes using a U-Net model." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |
|
|
|