Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# ✅ Load the trained model
|
| 8 |
+
model = load_model("lead_pipe_cnn.h5")
|
| 9 |
+
|
| 10 |
+
# ✅ Define the same image size as during training
|
| 11 |
+
IMG_SIZE = (256, 256)
|
| 12 |
+
|
| 13 |
+
# ✅ Class names (adjust to your dataset labels)
|
| 14 |
+
class_names = ['Lead', 'Non-Lead', 'Other']
|
| 15 |
+
|
| 16 |
+
def predict(img):
|
| 17 |
+
# Convert to array
|
| 18 |
+
img = img.resize(IMG_SIZE)
|
| 19 |
+
img_array = image.img_to_array(img)
|
| 20 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
| 21 |
+
|
| 22 |
+
# Make prediction
|
| 23 |
+
preds = model.predict(img_array)
|
| 24 |
+
pred_class = class_names[np.argmax(preds)]
|
| 25 |
+
confidence = np.max(preds) * 100
|
| 26 |
+
|
| 27 |
+
return {pred_class: float(confidence)}
|
| 28 |
+
|
| 29 |
+
# ✅ Gradio UI
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=predict,
|
| 32 |
+
inputs=gr.Image(type="pil", label="Upload Pipe Image"),
|
| 33 |
+
outputs=gr.Label(num_top_classes=3, label="Prediction"),
|
| 34 |
+
title="PipeSense CNN - Lead Detection",
|
| 35 |
+
description="Upload an image of a pipe to detect if it's Lead or Non-Lead."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|