Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
# Load your our trained Model
|
| 9 |
+
model = load_model('Crack-segmentation.h5')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def preprocess_image(image, target_size=(128, 128)):
|
| 13 |
+
# Convert the PIL image to a NumPy array
|
| 14 |
+
img_array = np.array(image)
|
| 15 |
+
# Resize the image using OpenCV
|
| 16 |
+
img_resized = cv2.resize(img_array, target_size)
|
| 17 |
+
# Normalize the image
|
| 18 |
+
img_resized = img_resized.astype('float32') / 255.0
|
| 19 |
+
# Expand dimensions to match the input shape for the model
|
| 20 |
+
img_resized = np.expand_dims(img_resized, axis=0)
|
| 21 |
+
return img_resized
|
| 22 |
+
|
| 23 |
+
def predict_mask(image):
|
| 24 |
+
preprocessed_image = preprocess_image(image)
|
| 25 |
+
# Make predictions
|
| 26 |
+
predictions = model.predict(preprocessed_image)
|
| 27 |
+
# Squeeze the prediction to remove the batch dimension
|
| 28 |
+
predicted_mask = predictions.squeeze()
|
| 29 |
+
# Normalize the mask to [0, 255] and convert to uint8
|
| 30 |
+
predicted_mask = (predicted_mask * 255).astype(np.uint8)
|
| 31 |
+
return predicted_mask
|
| 32 |
+
|
| 33 |
+
# Define the Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=predict_mask,
|
| 36 |
+
inputs=gr.Image(),
|
| 37 |
+
outputs=gr.Image(type="numpy",label='Segmented Image 🚀'),
|
| 38 |
+
title="Crack Segmentation",
|
| 39 |
+
description="Upload an Image 📥"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the Gradio interface
|
| 43 |
+
iface.launch()
|