Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,20 @@ from PIL import Image
|
|
| 4 |
import tensorflow as tf
|
| 5 |
from utils import preprocess_image
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Initialize labels and model
|
| 8 |
labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 9 |
model = tf.keras.models.load_model('classify_model.h5')
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
# Customized Streamlit styles
|
| 13 |
st.markdown(
|
| 14 |
"""
|
|
@@ -74,47 +83,10 @@ st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.pn
|
|
| 74 |
# Page title
|
| 75 |
st.title("EcoIdentify by EcoClim Solutions")
|
| 76 |
|
| 77 |
-
# Mode selection
|
| 78 |
mode = st.selectbox("Select Mode", ["Predict Mode", "Train Mode"])
|
| 79 |
|
| 80 |
if mode == "Predict Mode":
|
| 81 |
-
#
|
| 82 |
-
st.header("Upload a waste image to find its category")
|
| 83 |
-
|
| 84 |
-
# Note
|
| 85 |
-
st.markdown("* Please note that our dataset is trained primarily with images that contain a white background. Therefore, images with a white background would produce maximum accuracy *")
|
| 86 |
-
|
| 87 |
-
# Image upload section
|
| 88 |
-
opt = st.selectbox("How do you want to upload the image for classification?", ("Please Select", "Upload image from device"))
|
| 89 |
-
|
| 90 |
-
image = None
|
| 91 |
-
|
| 92 |
-
if opt == 'Upload image from device':
|
| 93 |
-
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
|
| 94 |
-
if file:
|
| 95 |
-
try:
|
| 96 |
-
image = preprocess_image(file)
|
| 97 |
-
except Exception as e:
|
| 98 |
-
st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
|
| 99 |
-
try:
|
| 100 |
-
if image is not None:
|
| 101 |
-
st.image(image, width=256, caption='Uploaded Image')
|
| 102 |
-
if st.button('Predict'):
|
| 103 |
-
prediction = model.predict(image[np.newaxis, ...])
|
| 104 |
-
predicted_label = labels[np.argmax(prediction[0], axis=-1)]
|
| 105 |
-
st.success(f'Prediction: {predicted_label}')
|
| 106 |
-
|
| 107 |
-
# Ask user if the prediction is correct
|
| 108 |
-
user_feedback = st.radio("Is the prediction correct?", ["Yes", "No"])
|
| 109 |
-
if user_feedback == "No":
|
| 110 |
-
# Allow user to provide correct label
|
| 111 |
-
user_label = st.text_input("Enter the correct label:")
|
| 112 |
-
if user_label:
|
| 113 |
-
st.success(f'Thank you for providing feedback. Please switch to "Train Mode" to update the model.')
|
| 114 |
-
|
| 115 |
-
except Exception as e:
|
| 116 |
-
st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
|
| 117 |
-
|
| 118 |
|
| 119 |
elif mode == "Train Mode":
|
| 120 |
# Train the model with a new image and label
|
|
@@ -132,11 +104,19 @@ elif mode == "Train Mode":
|
|
| 132 |
|
| 133 |
# Train button
|
| 134 |
if st.button('Train Model'):
|
| 135 |
-
|
| 136 |
-
image = image[np.newaxis, ...] # Add a batch dimension
|
| 137 |
label_index = labels.index(user_label)
|
| 138 |
label_one_hot = tf.one_hot(label_index, len(labels))
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
|
|
|
|
| 4 |
import tensorflow as tf
|
| 5 |
from utils import preprocess_image
|
| 6 |
|
| 7 |
+
import numpy as np
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import tensorflow as tf
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from utils import preprocess_image
|
| 12 |
+
|
| 13 |
# Initialize labels and model
|
| 14 |
labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 15 |
model = tf.keras.models.load_model('classify_model.h5')
|
| 16 |
|
| 17 |
+
# Accumulate images and labels for training
|
| 18 |
+
accumulated_images = []
|
| 19 |
+
accumulated_labels = []
|
| 20 |
+
|
| 21 |
# Customized Streamlit styles
|
| 22 |
st.markdown(
|
| 23 |
"""
|
|
|
|
| 83 |
# Page title
|
| 84 |
st.title("EcoIdentify by EcoClim Solutions")
|
| 85 |
|
|
|
|
| 86 |
mode = st.selectbox("Select Mode", ["Predict Mode", "Train Mode"])
|
| 87 |
|
| 88 |
if mode == "Predict Mode":
|
| 89 |
+
# ... [same code for Predict Mode] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
elif mode == "Train Mode":
|
| 92 |
# Train the model with a new image and label
|
|
|
|
| 104 |
|
| 105 |
# Train button
|
| 106 |
if st.button('Train Model'):
|
| 107 |
+
accumulated_images.append(image[np.newaxis, ...])
|
|
|
|
| 108 |
label_index = labels.index(user_label)
|
| 109 |
label_one_hot = tf.one_hot(label_index, len(labels))
|
| 110 |
+
accumulated_labels.append(label_one_hot)
|
| 111 |
+
|
| 112 |
+
if len(accumulated_images) >= 5: # Example threshold
|
| 113 |
+
X_train = np.vstack(accumulated_images)
|
| 114 |
+
y_train = np.vstack(accumulated_labels)
|
| 115 |
+
model.fit(X_train, y_train, epochs=2, batch_size=1)
|
| 116 |
+
st.success(f'Model has been trained with the accumulated images and labels.')
|
| 117 |
+
# Clear accumulated data
|
| 118 |
+
accumulated_images.clear()
|
| 119 |
+
accumulated_labels.clear()
|
| 120 |
+
|
| 121 |
except Exception as e:
|
| 122 |
st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
|