EcoIdentify / app.py
Aryan-EcoClim's picture
Update app.py
003f748 verified
raw
history blame
3.58 kB
import numpy as np
import streamlit as st
from PIL import Image
import tensorflow as tf
from utils import preprocess_image
import numpy as np
import streamlit as st
import tensorflow as tf
from PIL import Image
from utils import preprocess_image
# Initialize labels and model
labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
model = tf.keras.models.load_model('classify_model.h5')
# Accumulate images and labels for training
accumulated_images = []
accumulated_labels = []
# Customized Streamlit styles
st.markdown(
"""
<style>
body {
color: #333333;
background-color: #f9f9f9;
font-family: 'Helvetica', sans-serif;
}
.st-bb {
padding: 0rem;
}
.st-ec {
color: #666666;
}
.st-ef {
color: #666666;
}
.st-ei {
color: #333333;
}
.st-dh {
font-size: 36px;
font-weight: bold;
color: #4CAF50;
text-align: center;
margin-bottom: 20px;
}
.st-gf {
background-color: #4CAF50;
color: white;
padding: 15px 30px;
font-size: 18px;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s;
}
.st-gf:hover {
background-color: #45a049;
}
.st-gh {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-bottom: 20px;
}
.st-logo {
max-width: 100%;
height: auto;
margin: 20px auto;
display: block;
}
</style>
""",
unsafe_allow_html=True,
)
# Logo
st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.png?resize=48%2C48")
# Page title
st.title("EcoIdentify by EcoClim Solutions")
mode = st.selectbox("Select Mode", ["Predict Mode", "Train Mode"])
if mode == "Predict Mode":
# ... [same code for Predict Mode] ...
elif mode == "Train Mode":
# Train the model with a new image and label
st.header("Train the model with a new image and label")
# Image upload section
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
if file:
try:
image = preprocess_image(file)
st.image(image, width=256, caption='Uploaded Image')
# Label input
user_label = st.selectbox("Select the correct label", labels)
# Train button
if st.button('Train Model'):
accumulated_images.append(image[np.newaxis, ...])
label_index = labels.index(user_label)
label_one_hot = tf.one_hot(label_index, len(labels))
accumulated_labels.append(label_one_hot)
if len(accumulated_images) >= 5: # Example threshold
X_train = np.vstack(accumulated_images)
y_train = np.vstack(accumulated_labels)
model.fit(X_train, y_train, epochs=2, batch_size=1)
st.success(f'Model has been trained with the accumulated images and labels.')
# Clear accumulated data
accumulated_images.clear()
accumulated_labels.clear()
except Exception as e:
st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")