Onur Bayramoglu
commited on
Commit
•
9269c5a
1
Parent(s):
c0c4828
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import tensorflow_hub as hub
|
5 |
+
import tensorflow as tf
|
6 |
+
import numpy as np
|
7 |
+
from tensorflow import keras
|
8 |
+
from tensorflow.keras.models import load_model
|
9 |
+
from tensorflow.keras import preprocessing
|
10 |
+
import time
|
11 |
+
fig = plt.figure()
|
12 |
+
|
13 |
+
with open("custom.css") as f:
|
14 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
15 |
+
|
16 |
+
st.title('Bag Classifier')
|
17 |
+
|
18 |
+
st.markdown("Welcome to this simple web application that classifies bags. The bags are classified into six different classes namely: Backpack, Briefcase, Duffle, Handbag and Purse.")
|
19 |
+
|
20 |
+
|
21 |
+
def main():
|
22 |
+
file_uploaded = st.file_uploader("Choose File", type=["png","jpg","jpeg"])
|
23 |
+
class_btn = st.button("Classify")
|
24 |
+
if file_uploaded is not None:
|
25 |
+
image = Image.open(file_uploaded)
|
26 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
27 |
+
|
28 |
+
if class_btn:
|
29 |
+
if file_uploaded is None:
|
30 |
+
st.write("Invalid command, please upload an image")
|
31 |
+
else:
|
32 |
+
with st.spinner('Model working....'):
|
33 |
+
plt.imshow(image)
|
34 |
+
plt.axis("off")
|
35 |
+
predictions = predict(image)
|
36 |
+
time.sleep(1)
|
37 |
+
st.success('Classified')
|
38 |
+
st.write(predictions)
|
39 |
+
st.pyplot(fig)
|
40 |
+
|
41 |
+
|
42 |
+
def predict(image):
|
43 |
+
classifier_model = "base_dir.h5"
|
44 |
+
IMAGE_SHAPE = (224, 224,3)
|
45 |
+
model = load_model(classifier_model, compile=False, custom_objects={'KerasLayer': hub.KerasLayer})
|
46 |
+
test_image = image.resize((224,224))
|
47 |
+
test_image = preprocessing.image.img_to_array(test_image)
|
48 |
+
test_image = test_image / 255.0
|
49 |
+
test_image = np.expand_dims(test_image, axis=0)
|
50 |
+
class_names = [
|
51 |
+
'Backpack',
|
52 |
+
'Briefcase',
|
53 |
+
'Duffle',
|
54 |
+
'Handbag',
|
55 |
+
'Purse']
|
56 |
+
predictions = model.predict(test_image)
|
57 |
+
scores = tf.nn.softmax(predictions[0])
|
58 |
+
scores = scores.numpy()
|
59 |
+
results = {
|
60 |
+
'Backpack': 0,
|
61 |
+
'Briefcase': 0,
|
62 |
+
'Duffle': 0,
|
63 |
+
'Handbag': 0,
|
64 |
+
'Purse': 0
|
65 |
+
}
|
66 |
+
|
67 |
+
|
68 |
+
result = f"{class_names[np.argmax(scores)]} with a { (100 * np.max(scores)).round(2) } % confidence."
|
69 |
+
return result
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
main()
|
83 |
+
|
84 |
+
|