Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
import json
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
model = tf.keras.models.load_model("keras_model.h5", compile=False)
|
| 9 |
with open("metadata.json") as f:
|
| 10 |
metadata = json.load(f)
|
| 11 |
labels = metadata["labels"]
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Prediction function
|
| 14 |
-
def predict_image(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
fn=predict_image,
|
| 29 |
inputs=gr.Image(type="pil", label="Upload Solar Panel Image"),
|
| 30 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 31 |
title="π Solar Panel Condition Classifier",
|
| 32 |
description="Upload a solar panel image to detect if it is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered.",
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import json
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import tensorflowjs as tfjs
|
| 7 |
|
| 8 |
+
# Load metadata (class labels)
|
|
|
|
| 9 |
with open("metadata.json") as f:
|
| 10 |
metadata = json.load(f)
|
| 11 |
labels = metadata["labels"]
|
| 12 |
|
| 13 |
+
# Load TensorFlow.js model
|
| 14 |
+
model = tfjs.converters.load_keras_model("model.json")
|
| 15 |
+
|
| 16 |
# Prediction function
|
| 17 |
+
def predict_image(img):
|
| 18 |
+
try:
|
| 19 |
+
# Preprocess image
|
| 20 |
+
img = img.convert("RGB").resize((224, 224))
|
| 21 |
+
img_array = np.array(img) / 255.0
|
| 22 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 23 |
+
|
| 24 |
+
# Make prediction
|
| 25 |
+
predictions = model.predict(img_array)[0]
|
| 26 |
+
pred_idx = np.argmax(predictions)
|
| 27 |
+
pred_label = labels[pred_idx]
|
| 28 |
+
accuracy = float(predictions[pred_idx]) * 100
|
| 29 |
+
|
| 30 |
+
return f"β
Condition: {pred_label}", f"π― Accuracy: {accuracy:.2f}%"
|
| 31 |
+
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"β Error: {str(e)}", ""
|
| 34 |
+
|
| 35 |
+
# UI
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
fn=predict_image,
|
| 38 |
inputs=gr.Image(type="pil", label="Upload Solar Panel Image"),
|
| 39 |
+
outputs=[
|
| 40 |
+
gr.Textbox(label="Prediction"),
|
| 41 |
+
gr.Textbox(label="Accuracy")
|
| 42 |
+
],
|
| 43 |
title="π Solar Panel Condition Classifier",
|
| 44 |
description="Upload a solar panel image to detect if it is Clean, Dusty, Bird-drop, Electrical Damage, Physical Damage, or Snow-Covered.",
|
| 45 |
)
|
| 46 |
|
| 47 |
+
interface.launch()
|