forked from AIAnytime/Waste-Classifier-Sustainability-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
182 lines (144 loc) · 7.8 KB
/
app.py
File metadata and controls
182 lines (144 loc) · 7.8 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# app.py
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image, ImageOps
import pandas as pd
# Set page config first
st.set_page_config(layout='wide', page_title="Solar Panel Fault Detection")
def load_model():
"""Load the TFLite model"""
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()
return interpreter
def process_image(image):
"""Process image to match Teachable Machine's requirements"""
# Resize the image to match what Teachable Machine expects
size = (224, 224)
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
# Convert image to numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
# Create the array of the right shape
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
data[0] = normalized_image_array
return data
def predict(interpreter, image_data):
"""Run prediction on processed image data"""
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'], image_data)
interpreter.invoke()
return interpreter.get_tensor(output_details[0]['index'])[0]
def format_condition(condition):
"""Format the condition text for display"""
if condition == "Physical":
return "Physical Damage"
elif condition == "Electrical":
return "Electrical Damage"
return condition
def main():
st.title("Solar Panel Fault Detection")
try:
# Load model and labels
interpreter = load_model()
with open("labels.txt", "r") as f:
labels = [line.strip() for line in f.readlines()]
# Create tabs for different modes
tab1, tab2 = st.tabs(["Single Image Analysis", "Batch Analysis"])
with tab1:
uploaded_file = st.file_uploader("Upload a solar panel image", type=['jpg', 'png', 'jpeg'])
if uploaded_file:
# Display image and analysis side by side
col1, col2 = st.columns(2)
with col1:
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption="Uploaded Image", use_container_width=True)
with col2:
if st.button("Analyze Image"):
with st.spinner("Analyzing..."):
# Process and predict
processed_image = process_image(image)
predictions = predict(interpreter, processed_image)
# Get the results
class_index = np.argmax(predictions)
confidence = predictions[class_index]
condition = labels[class_index].split(' ')[1] # Get condition after the number
condition_display = format_condition(condition)
# Display results with appropriate styling
if condition == "Clean":
st.success(f"✅ Panel Status: {condition_display}")
elif condition == "Physical":
st.warning(f"⚠️ Panel Status: {condition_display}")
else:
st.error(f"🚨 Panel Status: {condition_display}")
# Show confidence score
st.metric("Confidence", f"{confidence * 100:.1f}%")
# Show confidence bars for all classes
st.subheader("Detailed Analysis")
for idx, score in enumerate(predictions):
label = labels[idx].split(' ')[1] # Get condition after the number
label_display = format_condition(label)
st.write(f"{label_display}: {score * 100:.1f}%")
st.progress(float(score))
with tab2:
uploaded_files = st.file_uploader("Upload multiple images",
type=['jpg', 'png', 'jpeg'],
accept_multiple_files=True)
if uploaded_files:
if st.button("Analyze All Images"):
results = []
progress = st.progress(0)
status = st.empty()
for idx, file in enumerate(uploaded_files):
status.text(f"Processing image {idx + 1} of {len(uploaded_files)}")
try:
# Process image
image = Image.open(file).convert('RGB')
processed_image = process_image(image)
predictions = predict(interpreter, processed_image)
# Get prediction
class_index = np.argmax(predictions)
confidence = predictions[class_index]
condition = labels[class_index].split(' ')[1]
condition_display = format_condition(condition)
results.append({
'Image': file.name,
'Condition': condition_display,
'Confidence': f"{confidence:.1%}"
})
except Exception as e:
results.append({
'Image': file.name,
'Condition': 'Error',
'Confidence': str(e)
})
progress.progress((idx + 1) / len(uploaded_files))
status.text("Analysis Complete!")
# Show summary
df = pd.DataFrame(results)
col1, col2, col3 = st.columns(3)
total = len(df)
problems = len(df[df['Condition'].isin(['Physical Damage', 'Electrical Damage'])])
col1.metric("Total Panels", total)
col2.metric("Issues Found", problems)
col3.metric("Clean Panels", total - problems)
# Show detailed results
st.subheader("Detailed Results")
st.dataframe(df, use_container_width=True)
# Download option
csv = df.to_csv(index=False)
st.download_button(
"Download Results (CSV)",
csv,
"solar_panel_analysis.csv",
"text/csv"
)
except FileNotFoundError:
st.error("Model or labels file not found!")
st.info("Please ensure 'converted_model.tflite' and 'labels.txt' are in the app directory")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()