Spaces:
Running
Running
File size: 2,209 Bytes
58bd1b2 c01fb47 58bd1b2 9dc35b4 cefd4c5 418cf06 6750728 418cf06 96aab6f 418cf06 96aab6f 418cf06 58bd1b2 3d77e30 58bd1b2 3d77e30 58bd1b2 f3fa2b6 58bd1b2 3d77e30 58bd1b2 f3fa2b6 94e1bda 3d77e30 58bd1b2 3d77e30 f434ef0 3d77e30 418cf06 3d77e30 96aab6f 3d77e30 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import time
import streamlit as st
import numpy as np
import PIL
import urllib.request
from utils import *
#from fastai.data.external import *
# Function to classify the garbage
def classify_garbage(img_path, model):
processed_img = preprocess(img_path)
prediction = model.predict(processed_img)
labels = gen_labels()
predicted_class = np.argmax(prediction, axis=1)[0]
classification_result = labels[predicted_class]
# Get the confidence (probability) of the predicted class
confidence = prediction[0][predicted_class] * 100 # Convert probability to percentage
return classification_result, confidence
# Streamlit app layout
st.markdown('<center><h1>Garbage Segregation</h1></center>', unsafe_allow_html=True)
st.markdown('<center><h3>Please upload Waste Image to find its Category</h3></center>', 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'))
if opt == 'Upload image from device':
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
if file is not None:
image = Image.open(file).resize((256, 256))#, Image.LANCZOS)
elif opt == 'Upload image via link':
img_url = st.text_input('Enter the Image Address')
try:
image = Image.open(urllib.request.urlopen(img_url)).resize((256, 256))#, Image.LANCZOS)
except ValueError:
st.error("Please Enter a valid Image Address!")
if 'image' in locals(): # Check if image variable exists
st.image(image, width=300, caption='Uploaded Image')
if st.button('Predict'):
try:
model = model_arc() # Initialize your model
# Ensure image shape is correct and add batch dimension
img_array = preprocess(image) # This should return an array of shape (1, 256, 256, 3)
predicted_class, confidence = classify_garbage(img_array, model)
st.info('The uploaded image has been classified as "{}" with {:.2f}% confidence.'.format(predicted_class, confidence))
except Exception as e:
st.error(f"An error occurred: {e}") |