S.Sai Yashasvini commited on
Commit
317248b
·
verified ·
1 Parent(s): 42a4b20

create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input, decode_predictions
6
+
7
+ model = MobileNetV2(weights='imagenet')
8
+
9
+ def preprocess_image(image):
10
+ img = image.resize((224,224))
11
+ img_array = np.array(img)
12
+ img_array = np.expand_dims(img_array, axis=0)
13
+ img_array = preprocess_input(img_array)
14
+ return img_array
15
+
16
+ def predict(image):
17
+ img_array = preprocess_image(image)
18
+ preds = model.predict(img_array)
19
+ decoded_preds = decode_predictions(preds, top=1)[0]
20
+ return decoded_preds
21
+
22
+ def main():
23
+ st.set_page_config(page_title='Image Classification', page_icon=":camera_flash:")
24
+ st.title('Image Classification with MobileNetV2')
25
+ st.sidebar.title("Options")
26
+
27
+ st.sidebar.write('Upload an image for classification')
28
+
29
+ uploaded_file = st.sidebar.file_uploader("", type=["jpg", "jpeg", "png"])
30
+
31
+ if uploaded_file is not None:
32
+
33
+ image = Image.open(uploaded_file)
34
+ st.image(image, caption='Uploaded Image', use_column_width=True)
35
+
36
+ if st.button('Classify'):
37
+ with st.spinner('Classifying...'):
38
+ prediction = predict(image)
39
+ st.success('Classification done!')
40
+
41
+ st.write('*Prediction:*')
42
+ imagenet_id, label, score = prediction[0]
43
+ st.write(f"- *{label}* (Confidence: {score:2%})")
44
+
45
+ if _name_ == '_main_':
46
+ main()