EcoIdentify / app.py
Aryan-EcoClim's picture
Update app.py
72125ff
raw
history blame
3.16 kB
import time
import streamlit as st
import numpy as np
from PIL import Image
import urllib.request
import io
from utils import *
# Initialize labels and model
labels = gen_labels()
model = model_arc() # Assuming this function initializes and returns a trained model
# Streamlit UI
st.markdown('''
<div style="padding-bottom: 20px; padding-top: 20px; padding-left: 5px; padding-right: 5px">
<center><h1>EcoIdentify (Test)</h1></center>
</div>
''', unsafe_allow_html=True)
st.markdown('''
<div>
<center><h3>Please upload Waste Image to find its Category</h3></center>
</div>
''', unsafe_allow_html=True)
opt = st.selectbox("How do you want to upload the image for classification?",
('Please Select', 'Upload image via link', 'Upload image from device'))
# Image processing based on user selection
image = None
if opt == 'Upload image from device':
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
if file:
try:
image = Image.open(io.BytesIO(file.read())).resize((256, 256), Image.LANCZOS)
except Exception as e:
st.error(f"Error reading the file: {e}")
elif opt == 'Upload image via link':
img_url = st.text_input('Enter the Image Address')
if st.button('Submit'):
try:
response = urllib.request.urlopen(img_url)
image = Image.open(response).resize((256, 256), Image.LANCZOS)
except ValueError:
st.error("Please Enter a valid Image Address!")
try:
if image is not None:
st.image(image, width = 256, caption = 'Uploaded Image')
if st.button('Predict'):
img = preprocess(image)
model = model_arc()
#model.load_weights("classify_model.h5")
print("---------------img-array---------------------")
print(img[np.newaxis, ...])
prediction = model.predict(img[np.newaxis, ...])
print("------------summary------------------------")
print(model.summary())
print("------------------------------------")
print(prediction)
st.info('Hey! The uploaded image has been classified as " {} waste " '.format(labels[np.argmax(prediction[0], axis=-1)]) + )
def
if img == 'paper' or 'cardboard' or 'metal' or 'glass':
return(" therefore your item is recyclable. Please refer to https://www.wm.com/us/en/drop-off-locations to find a drop-off location near you.")
elif img == 'plastic':
return(' therefore you item may have a chance of being recyclable. Since this model has yet to recognize types of plastics, please refer to https://www.bing.com/ck/a?!&&p=c1474e95017548dfJmltdHM9MTcwMzcyMTYwMCZpZ3VpZD0xNmNjOTFiOS1hMDgwLTY5MmItMzBmNi04MmE1YTE3ODY4NDImaW5zaWQ9NTIyMA&ptn=3&ver=2&hsh=3&fclid=16cc91b9-a080-692b-30f6-82a5a1786842&psq=what+type+of+plastic+can+be+recycled&u=a1aHR0cHM6Ly93d3cucGxhc3RpY3Nmb3JjaGFuZ2Uub3JnL2Jsb2cvd2hpY2gtcGxhc3RpYy1jYW4tYmUtcmVjeWNsZWQ&ntb=1 to check if this item can be recycled.')
else:
return('Your item is not recyclable. Please discard it safely.')
except Exception as e:
st.info(e)
pass