Spaces:
Running
Running
Commit
·
3d77e30
1
Parent(s):
ef7979f
Update app.py
Browse files
app.py
CHANGED
@@ -3,28 +3,16 @@ 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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
</div>
|
14 |
-
'''
|
15 |
-
st.markdown(html_temp, unsafe_allow_html=True)
|
16 |
|
17 |
-
|
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'])
|
@@ -32,26 +20,26 @@ if opt == 'Upload image from device':
|
|
32 |
image = Image.open(file).resize((256, 256), Image.LANCZOS)
|
33 |
|
34 |
elif opt == 'Upload image via link':
|
|
|
35 |
try:
|
36 |
-
|
37 |
-
image = Image.open(urllib.request.urlopen(img)).resize((256, 256), Image.LANCZOS)
|
38 |
except ValueError:
|
39 |
-
|
40 |
-
show = st.error("Please Enter a valid Image Address!")
|
41 |
-
time.sleep(4)
|
42 |
-
show.empty()
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
st.image(image, width = 300, caption = 'Uploaded Image')
|
47 |
-
if st.button('Predict'):
|
48 |
-
img = preprocess(image)
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
import urllib.request
|
6 |
+
from utils import * # Assuming the gen_labels() and preprocess() functions are in this module
|
7 |
|
8 |
+
# Load labels
|
9 |
labels = gen_labels()
|
10 |
|
11 |
+
# Streamlit app layout
|
12 |
+
st.markdown('<center><h1>Garbage Segregation</h1></center>', unsafe_allow_html=True)
|
13 |
+
st.markdown('<center><h3>Please upload Waste Image to find its Category</h3></center>', unsafe_allow_html=True)
|
|
|
|
|
|
|
14 |
|
15 |
+
opt = st.selectbox("How do you want to upload the image for classification?", ('Please Select', 'Upload image via link', 'Upload image from device'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
if opt == 'Upload image from device':
|
18 |
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
|
|
|
20 |
image = Image.open(file).resize((256, 256), Image.LANCZOS)
|
21 |
|
22 |
elif opt == 'Upload image via link':
|
23 |
+
img_url = st.text_input('Enter the Image Address')
|
24 |
try:
|
25 |
+
image = Image.open(urllib.request.urlopen(img_url)).resize((256, 256), Image.LANCZOS)
|
|
|
26 |
except ValueError:
|
27 |
+
st.error("Please Enter a valid Image Address!")
|
|
|
|
|
|
|
28 |
|
29 |
+
if 'image' in locals(): # Check if image variable exists
|
30 |
+
st.image(image, width=300, caption='Uploaded Image')
|
|
|
|
|
|
|
31 |
|
32 |
+
if st.button('Predict'):
|
33 |
+
try:
|
34 |
+
img_array = preprocess(image)
|
35 |
+
model = model_arc() # Assuming this function initializes and returns your model
|
36 |
+
prediction = model.predict(img_array[np.newaxis, ...])
|
37 |
+
|
38 |
+
# Get the predicted class name
|
39 |
+
predicted_class_index = np.argmax(prediction[0], axis=-1)
|
40 |
+
predicted_class_name = labels[predicted_class_index]
|
41 |
+
|
42 |
+
st.info('The uploaded image has been classified as "{}" waste.'.format(predicted_class_name))
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"An error occurred: {e}")
|