VishnuEcoClim commited on
Commit
baf7aa0
·
1 Parent(s): 7b908dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -38
app.py CHANGED
@@ -1,58 +1,57 @@
1
  import time
2
  import streamlit as st
3
  import numpy as np
4
- import PIL
5
  import urllib.request
6
  from utils import *
7
- #from fastai.data.external import *
8
 
9
- # Function to classify the garbage
10
- def classify_garbage(img_path, model):
11
- processed_img = preprocess(img_path)
12
- prediction = model.predict(processed_img)
13
 
14
- labels = gen_labels()
15
- predicted_class = np.argmax(prediction, axis=1)[0]
16
- classification_result = labels[predicted_class]
 
 
 
17
 
18
- # Get the confidence (probability) of the predicted class
19
- confidence = prediction[0][predicted_class] * 100 # Convert probability to percentage
 
 
 
 
 
20
 
21
- return classification_result, confidence
22
 
23
- # Streamlit app layout
24
- st.markdown('<center><h1>Garbage Segregation</h1></center>', unsafe_allow_html=True)
25
- st.markdown('<center><h3>Please upload Waste Image to find its Category</h3></center>', unsafe_allow_html=True)
26
-
27
- opt = st.selectbox("How do you want to upload the image for classification?", ('Please Select', 'Upload image via link', 'Upload image from device'))
28
 
29
  if opt == 'Upload image from device':
30
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
31
  if file is not None:
32
- pil_image = Image.open(file)
33
- image = preprocess(pil_image)
34
 
35
  elif opt == 'Upload image via link':
36
- img_url = st.text_input('Enter the Image Address')
37
  try:
38
- pil_image = Image.open(urllib.request.urlopen(img_url))
39
- image = preprocess(pil_image)
40
  except ValueError:
41
- st.error("Please Enter a valid Image Address!")
 
 
 
 
 
 
 
 
 
42
 
43
- if 'image' in locals(): # Check if image variable exists
44
- st.image(image, width=300, caption='Uploaded Image')
45
 
46
- if st.button('Predict'):
47
- try:
48
- model = model_arc() # Initialize your model
49
-
50
- # Ensure image shape is correct and add batch dimension
51
- img_array = preprocess(image) # This should return an array of shape (1, 256, 256, 3)
52
-
53
- predicted_class, confidence = classify_garbage(img_array, model)
54
-
55
- st.info('The uploaded image has been classified as "{}" with {:.2f}% confidence.'.format(predicted_class, confidence))
56
-
57
- except Exception as e:
58
- st.error(f"An error occurred: {e}")
 
1
  import time
2
  import streamlit as st
3
  import numpy as np
4
+ from PIL import Image
5
  import urllib.request
6
  from utils import *
 
7
 
8
+ labels = gen_labels()
 
 
 
9
 
10
+ html_temp = '''
11
+ <div style="padding-bottom: 20px; padding-top: 20px; padding-left: 5px; padding-right: 5px">
12
+ <center><h1>Garbage Segregation</h1></center>
13
+ </div>
14
+ '''
15
+ st.markdown(html_temp, unsafe_allow_html=True)
16
 
17
+ html_temp = '''
18
+ <div>
19
+ <h2></h2>
20
+ <center><h3>Please upload Waste Image to find its Category</h3></center>
21
+ </div>
22
+ '''
23
+ st.markdown(html_temp, unsafe_allow_html=True)
24
 
25
+ opt = st.selectbox("How do you want to upload the image for classification?\n", ('Please Select', 'Upload image via link', 'Upload image from device'))
26
 
27
+ image = None # Initialize image variable
 
 
 
 
28
 
29
  if opt == 'Upload image from device':
30
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
31
  if file is not None:
32
+ image = Image.open(file).resize((256, 256), Image.LANCZOS)
 
33
 
34
  elif opt == 'Upload image via link':
 
35
  try:
36
+ img = st.text_input('Enter the Image Address')
37
+ image = Image.open(urllib.request.urlopen(img)).resize((256, 256), Image.LANCZOS)
38
  except ValueError:
39
+ if st.button('Submit'):
40
+ show = st.error("Please Enter a valid Image Address!")
41
+ time.sleep(4)
42
+ show.empty()
43
+
44
+ try:
45
+ if image is not None:
46
+ st.image(image, width = 300, caption = 'Uploaded Image')
47
+ if st.button('Predict'):
48
+ img = preprocess(image)
49
 
50
+ model = model_arc()
51
+ #model.load_weights("classify_model.h5")
52
 
53
+ prediction = model.predict(img[np.newaxis, ...])
54
+ st.info('Hey! The uploaded image has been classified as " {} waste " '.format(labels[np.argmax(prediction[0], axis=-1)]))
55
+ except Exception as e:
56
+ st.info(e)
57
+ pass