ChangeD / app.py
Vinit710's picture
Create app.py
ca72a90 verified
import cv2
import numpy as np
import tensorflow as tf
import gradio as gr
# Load the trained model
model = tf.keras.models.load_model('unet_model.h5')
# Function to preprocess the images
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
# Prediction function
def predict_mask(old_image, current_image):
# Convert from PIL to numpy array
old_image_np = np.array(old_image)
current_image_np = np.array(current_image)
# Preprocess images
preprocessed_input = preprocess_images(old_image_np, current_image_np)
# Predict the mask
prediction = model.predict(preprocessed_input)
prediction_mask = (prediction[0] > 0.5).astype(np.uint8) * 255 # Binary mask
# Resize mask back to original size
mask_resized = cv2.resize(prediction_mask, (old_image_np.shape[1], old_image_np.shape[0]))
return mask_resized
# Gradio Interface
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()