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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import numpy as np
2
  import streamlit as st
3
  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(
@@ -86,7 +87,7 @@ st.title("EcoIdentify by EcoClim Solutions")
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"))
@@ -102,8 +103,14 @@ 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
-
 
1
  import numpy as np
2
  import streamlit as st
3
  from PIL import Image
4
+ import torch
5
+ import torch.nn as nn
6
+ import torchvision.transforms as transforms
7
  from utils import preprocess_image
8
 
 
 
9
  # Initialize labels and model
10
  labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
11
+ model = torch.load('classify_model.pth')
12
+ model.eval()
13
 
14
  # Customized Streamlit layout
15
  st.set_page_config(
 
87
  st.header("Upload a waste image to find its category")
88
 
89
  # Note
90
+ 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 *")
91
 
92
  # Image upload section
93
  opt = st.selectbox("How do you want to upload the image for classification?", ("Please Select", "Upload image from device"))
 
103
  if image is not None:
104
  st.image(image, width=256, caption='Uploaded Image')
105
  if st.button('Predict'):
106
+ transform = transforms.Compose([
107
+ transforms.Resize((256, 256)),
108
+ transforms.ToTensor(),
109
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
110
+ ])
111
+ image = transform(image).unsqueeze(0)
112
+ with torch.no_grad():
113
+ prediction = model(image)
114
+ st.success(f'Prediction: {labels[torch.argmax(prediction, dim=1).item()]}')
115
  except Exception as e:
116
+ st.error(f"An error occurred: {e}. Please contact us EcoClim Solutions at EcoClimSolutions.wordpress.com.")