File size: 1,462 Bytes
317248b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
755be3b
317248b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input, decode_predictions

model = MobileNetV2(weights='imagenet')

def preprocess_image(image):
    img = image.resize((224,224))
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)
    return img_array

def predict(image):
    img_array = preprocess_image(image)
    preds = model.predict(img_array)
    decoded_preds = decode_predictions(preds, top=1)[0]
    return decoded_preds

def main():
    st.set_page_config(page_title='Image Classification', page_icon=":camera_flash:")
    st.title('Image Classification with MobileNetV2')
    st.sidebar.title("Options")

    st.sidebar.write('Upload an image for classification')

    uploaded_file = st.sidebar.file_uploader("", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:

        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image', use_column_width=True)

        if st.button('Classify'):
            with st.spinner('Classifying...'):
                prediction = predict(image)
            st.success('Classification done!')

            st.write('*Prediction:*')
            imagenet_id, label, score = prediction[0]
            st.write(f"- *{label}* (Confidence: {score:2%})")

if __name__ == '__main__':
    main()