-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (65 loc) · 2.35 KB
/
Copy pathapp.py
File metadata and controls
80 lines (65 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tensorflow as tf
from PIL import Image
import numpy as np
import streamlit as st
@st.cache_resource
def load_model():
model = tf.keras.models.load_model('/Users/shimodi/Documents/SolarDeepLearning/trained_effnet_hpo.keras')
return model
def preprocess_image(image):
img = image.resize((224, 224))
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
return img_array
def predict_defect(model, img):
preprocessed = preprocess_image(img)
prediction = model.predict(preprocessed)
return prediction
st.set_page_config(
page_title="Solar Panel Classifier",
page_icon="🌞",
layout="centered"
)
st.title("Solar Panel Defect Detection App🌤️")
st.write("Upload an image of a solar panel to detect defects.")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption="Uploaded Image", width="stretch")
st.write("Analyzing...")
model = load_model()
prediction = predict_defect(model, image)
classes = ['Bird-drop','Clean','Dusty','Electrical-damage','Physical-Damage','Snow-Covered']
class_idx = np.argmax(prediction)
defect = classes[class_idx]
confidence = 100*np.max(prediction)
st.subheader("Detection Result")
if defect == "Clean":
st.success("The panel appears to be in good condition!!")
else:
st.warning("A defect or contamination has been detected!!")
st.write(f"Detected Defect: **{defect}** with Confidence: **{confidence:.2f}**%")
# -------------------------------
# Threshold-based probability highlighting
# -------------------------------
st.divider()
st.subheader("Detailed Class Probabilities")
threshold = st.slider(
"Highlight classes above confidence (%)",
min_value=10,
max_value=90,
value=50,
step=5
)
probs = prediction[0] * 100 # convert to %
predicted_class = classes[class_idx]
for cls, prob in zip(classes, probs):
label = f"{cls}: {prob:.2f}%"
if cls == predicted_class:
st.info(f"⭐ {label}")
elif prob >= threshold:
st.success(label)
elif prob >= threshold / 2:
st.warning(label)
else:
st.error(label)