Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.applications.imagenet_utils import decode_predictions
|
4 |
+
import cv2
|
5 |
+
from PIL import Image, ImageOps
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
@st.cache(allow_output_mutation=True)
|
9 |
+
def load_model():
|
10 |
+
model=tf.keras.models.load_model('keras.h5')
|
11 |
+
return model
|
12 |
+
with st.spinner('Model is being loaded..'):
|
13 |
+
model=load_model()
|
14 |
+
|
15 |
+
st.write("""
|
16 |
+
# Image Classification
|
17 |
+
"""
|
18 |
+
)
|
19 |
+
|
20 |
+
file = st.file_uploader("Upload the image to be classified U0001F447", type=["jpg", "png"])
|
21 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
22 |
+
|
23 |
+
def upload_predict(upload_image, model):
|
24 |
+
|
25 |
+
size = (180,180)
|
26 |
+
image = ImageOps.fit(upload_image, size, Image.ANTIALIAS)
|
27 |
+
image = np.asarray(image)
|
28 |
+
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
29 |
+
img_resize = cv2.resize(img, dsize=(224, 224),interpolation=cv2.INTER_CUBIC)
|
30 |
+
|
31 |
+
img_reshape = img_resize[np.newaxis,...]
|
32 |
+
|
33 |
+
prediction = model.predict(img_reshape)
|
34 |
+
pred_class=decode_predictions(prediction,top=1)
|
35 |
+
|
36 |
+
return pred_class
|
37 |
+
if file is None:
|
38 |
+
st.text("Please upload an image file")
|
39 |
+
else:
|
40 |
+
image = Image.open(file)
|
41 |
+
st.image(image, use_column_width=True)
|
42 |
+
predictions = upload_predict(image, model)
|
43 |
+
image_class = str(predictions[0][0][1])
|
44 |
+
score=np.round(predictions[0][0][2])
|
45 |
+
st.write("The image is classified as",image_class)
|
46 |
+
st.write("The similarity score is approximately",score)
|
47 |
+
print("The image is classified as ",image_class, "with a similarity score of",score)
|