VishnuEcoClim commited on
Commit
d50261b
·
1 Parent(s): 047c523

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -1,10 +1,10 @@
1
- import time
2
  import streamlit as st
3
  import numpy as np
4
  from PIL import Image
5
- import urllib.request
6
  import io
7
- from utils import *
 
 
8
 
9
  # Initialize labels and model
10
  labels = gen_labels()
@@ -26,36 +26,36 @@ st.markdown('''
26
  opt = st.selectbox("How do you want to upload the image for classification?",
27
  ('Please Select', 'Upload image via link', 'Upload image from device'))
28
 
29
- # Image processing based on user selection
30
- image = None
 
 
31
  if opt == 'Upload image from device':
32
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
33
  if file:
34
- try:
35
- image = Image.open(io.BytesIO(file.read())).resize((256, 256), Image.LANCZOS)
36
- except Exception as e:
37
- st.error(f"Error reading the file: {e}")
 
 
 
 
38
 
39
  elif opt == 'Upload image via link':
40
  img_url = st.text_input('Enter the Image Address')
41
  if st.button('Submit'):
42
  try:
43
  response = urllib.request.urlopen(img_url)
44
- image = Image.open(response).resize((256, 256), Image.LANCZOS)
45
- except ValueError:
46
- st.error("Please Enter a valid Image Address!")
47
-
48
- try:
49
- if image is not None:
50
- st.image(image, width = 300, caption = 'Uploaded Image')
51
- if st.button('Predict'):
52
- img = preprocess(image)
53
-
54
- model = model_arc()
55
- #model.load_weights("classify_model.h5")
56
-
57
- prediction = model.predict(img[np.newaxis, ...])
58
- st.info('Hey! The uploaded image has been classified as " {} waste " '.format(labels[np.argmax(prediction[0], axis=-1)]))
59
- except Exception as e:
60
- st.info(e)
61
- pass
 
 
1
  import streamlit as st
2
  import numpy as np
3
  from PIL import Image
 
4
  import io
5
+ import urllib.request
6
+ import tensorflow as tf
7
+ import matplotlib.pyplot as plt
8
 
9
  # Initialize labels and model
10
  labels = gen_labels()
 
26
  opt = st.selectbox("How do you want to upload the image for classification?",
27
  ('Please Select', 'Upload image via link', 'Upload image from device'))
28
 
29
+ # Streamlit UI
30
+ st.title("Image Classifier with Streamlit")
31
+ opt = st.selectbox("How do you want to upload the image?", ('Please Select', 'Upload image via link', 'Upload image from device'))
32
+
33
  if opt == 'Upload image from device':
34
  file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
35
  if file:
36
+ img = Image.open(io.BytesIO(file.read())).resize((256, 256), Image.LANCZOS)
37
+ st.image(img, width=300, caption='Uploaded Image')
38
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
39
+ img_array = np.expand_dims(img_array, axis=0)
40
+ prediction = model.predict(img_array)
41
+ confidence = np.max(prediction[0]) * 100
42
+ st.write(f"Predicted Label: {class_names[np.argmax(prediction[0])]}")
43
+ st.write(f"Confidence: {confidence:.2f}%")
44
 
45
  elif opt == 'Upload image via link':
46
  img_url = st.text_input('Enter the Image Address')
47
  if st.button('Submit'):
48
  try:
49
  response = urllib.request.urlopen(img_url)
50
+ img = Image.open(response).resize((256, 256), Image.LANCZOS)
51
+ st.image(img, width=300, caption='Uploaded Image')
52
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
53
+ img_array = np.expand_dims(img_array, axis=0)
54
+ prediction = model.predict(img_array)
55
+ confidence = np.max(prediction[0]) * 100
56
+ st.write(f"Predicted Label: {class_names[np.argmax(prediction[0])]}")
57
+ st.write(f"Confidence: {confidence:.2f}%")
58
+ except Exception as e:
59
+ st.error("Error processing the image. Please try again.")
60
+
61
+ # Add any other Streamlit components or functionalities as needed.