Spaces:
Running
Running
Commit
·
d50261b
1
Parent(s):
047c523
Update app.py
Browse files
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 |
-
|
|
|
|
|
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 |
-
#
|
30 |
-
|
|
|
|
|
31 |
if opt == 'Upload image from device':
|
32 |
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
|
33 |
if file:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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.
|
|
|
|
|
|
|
|
|
|
|
|