Aryan-EcoClim commited on
Commit
b85395e
·
verified ·
1 Parent(s): 003f748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -41
app.py CHANGED
@@ -4,19 +4,19 @@ from PIL import Image
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(
@@ -76,48 +76,34 @@ st.markdown(
76
  unsafe_allow_html=True,
77
  )
78
 
79
-
80
  # Logo
81
  st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.png?resize=48%2C48")
82
 
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
93
- st.header("Train the model with a new image and label")
94
 
95
- # Image upload section
96
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
97
  if file:
98
- try:
99
- image = preprocess_image(file)
100
- st.image(image, width=256, caption='Uploaded Image')
101
-
102
- # Label input
103
- user_label = st.selectbox("Select the correct label", labels)
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.")
123
-
 
 
 
 
 
 
 
4
  import tensorflow as tf
5
  from utils import preprocess_image
6
 
7
+
 
 
 
 
8
 
9
  # Initialize labels and model
10
  labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
11
  model = tf.keras.models.load_model('classify_model.h5')
12
 
13
+ # Customized Streamlit layout
14
+ st.set_page_config(
15
+ page_title="EcoIdentify by EcoClim Solutions",
16
+ page_icon="https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.png?resize=48%2C48",
17
+ layout="wide",
18
+ initial_sidebar_state="expanded",
19
+ )
20
 
21
  # Customized Streamlit styles
22
  st.markdown(
 
76
  unsafe_allow_html=True,
77
  )
78
 
 
79
  # Logo
80
  st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.png?resize=48%2C48")
81
 
82
  # Page title
83
  st.title("EcoIdentify by EcoClim Solutions")
84
 
85
+ # Subheader
86
+ st.header("Upload a waste image to find its category")
87
+
88
+ # Note
89
+ st.markdown("* Please note that our dataset is trained primarily with images that contain a white background. Therefore, images with white background would produce maximum accuracy *")
90
 
91
+ # Image upload section
92
+ opt = st.selectbox("How do you want to upload the image for classification?", ("Please Select", "Upload image from device"))
93
 
94
+ image = None
 
 
95
 
96
+ if opt == 'Upload image from device':
97
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
98
  if file:
99
+ image = preprocess_image(file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ try:
102
+ if image is not None:
103
+ st.image(image, width=256, caption='Uploaded Image')
104
+ if st.button('Predict'):
105
+ prediction = model.predict(image[np.newaxis, ...])
106
+ st.success(f'Prediction: {labels[np.argmax(prediction[0], axis=-1)]}')
107
+ except Exception as e:
108
+ st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
109
+