Aryan-EcoClim commited on
Commit
581f508
·
1 Parent(s): 92b77a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -83
app.py CHANGED
@@ -4,77 +4,12 @@ from PIL import Image
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(
23
- """
24
- <style>
25
- body {
26
- color: #333333;
27
- background-color: #f9f9f9;
28
- font-family: 'Helvetica', sans-serif;
29
- }
30
- .st-bb {
31
- padding: 0rem;
32
- }
33
- .st-ec {
34
- color: #666666;
35
- }
36
- .st-ef {
37
- color: #666666;
38
- }
39
- .st-ei {
40
- color: #333333;
41
- }
42
- .st-dh {
43
- font-size: 36px;
44
- font-weight: bold;
45
- color: #4CAF50;
46
- text-align: center;
47
- margin-bottom: 20px;
48
- }
49
- .st-gf {
50
- background-color: #4CAF50;
51
- color: white;
52
- padding: 15px 30px;
53
- font-size: 18px;
54
- border: none;
55
- border-radius: 8px;
56
- cursor: pointer;
57
- transition: background-color 0.3s;
58
- }
59
- .st-gf:hover {
60
- background-color: #45a049;
61
- }
62
- .st-gh {
63
- text-align: center;
64
- font-size: 24px;
65
- font-weight: bold;
66
- margin-bottom: 20px;
67
- }
68
- .st-logo {
69
- max-width: 100%;
70
- height: auto;
71
- margin: 20px auto;
72
- display: block;
73
- }
74
- </style>
75
- """,
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")
@@ -82,30 +17,71 @@ st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.pn
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
  try:
100
  image = preprocess_image(file)
 
 
 
 
 
 
 
 
 
 
 
 
101
  except Exception as e:
102
  st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
103
- try:
104
- if image is not None:
105
- st.image(image, width=256, caption='Uploaded Image')
106
- if st.button('Predict'):
107
- prediction = model.predict(image[np.newaxis, ...])
108
- st.success(f'Prediction: {labels[np.argmax(prediction[0], axis=-1)]}')
109
- except Exception as e:
110
- st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
111
-
 
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
  # Customized Streamlit layout
12
+ # (Your existing layout code remains unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Logo
15
  st.image("https://ecoclimsolutions.files.wordpress.com/2024/01/rmcai-removebg.png?resize=48%2C48")
 
17
  # Page title
18
  st.title("EcoIdentify by EcoClim Solutions")
19
 
20
+ # Mode selection
21
+ mode = st.selectbox("Select Mode", ["Predict Mode", "Train Mode"])
22
+
23
+ if mode == "Predict Mode":
24
+ # Subheader
25
+ st.header("Upload a waste image to find its category")
26
+
27
+ # Note
28
+ 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 *")
29
 
30
+ # Image upload section
31
+ opt = st.selectbox("How do you want to upload the image for classification?", ("Please Select", "Upload image from device"))
32
 
33
+ image = None
 
34
 
35
+ if opt == 'Upload image from device':
36
+ file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
37
+ if file:
38
+ try:
39
+ image = preprocess_image(file)
40
+ except Exception as e:
41
+ st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
42
+ try:
43
+ if image is not None:
44
+ st.image(image, width=256, caption='Uploaded Image')
45
+ if st.button('Predict'):
46
+ prediction = model.predict(image[np.newaxis, ...])
47
+ predicted_label = labels[np.argmax(prediction[0], axis=-1)]
48
+ st.success(f'Prediction: {predicted_label}')
49
 
50
+ # Ask user if the prediction is correct
51
+ user_feedback = st.radio("Is the prediction correct?", ["Yes", "No"])
52
+ if user_feedback == "No":
53
+ # Allow user to provide correct label
54
+ user_label = st.text_input("Enter the correct label:")
55
+ if user_label:
56
+ # Update the model with the user-provided image and label
57
+ image = preprocess_image(file) # preprocess the image again
58
+ target = np.zeros(len(labels))
59
+ target[labels.index(user_label)] = 1
60
+ model.train_on_batch(image[np.newaxis, ...], target)
61
+ st.success(f'Thank you for providing feedback. Model has been updated with the new label.')
62
+ except Exception as e:
63
+ st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")
64
+
65
+ elif mode == "Train Mode":
66
+ # Train the model with a new image and label
67
+ st.header("Train the model with a new image and label")
68
+
69
+ # Image upload section
70
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
71
  if file:
72
  try:
73
  image = preprocess_image(file)
74
+ st.image(image, width=256, caption='Uploaded Image')
75
+
76
+ # Label input
77
+ user_label = st.selectbox("Select the correct label", labels)
78
+
79
+ # Train button
80
+ if st.button('Train Model'):
81
+ # Update the model with the user-provided image and label
82
+ target = np.zeros(len(labels))
83
+ target[labels.index(user_label)] = 1
84
+ model.train_on_batch(image[np.newaxis, ...], target)
85
+ st.success(f'Model has been trained with the new image and label.')
86
  except Exception as e:
87
  st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")