mattritchey's picture
Update app.py
fbf56bb
raw
history blame
1.64 kB
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 17 08:43:48 2023
@author: mritchey
"""
import keras
import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
model_type = st.sidebar.selectbox(
'Select Model', ('VGG16', 'VGG19', 'ResNet50V2', 'MobileNetV2'))
models = {'VGG16': 'vgg16', 'VGG19': 'vgg16', 'ResNet50V2': 'resnet_v2',
'MobileNetV2': 'mobilenet_v2'}
model_type2 = models[model_type]
top_n = st.sidebar.selectbox('Number of Results', (3, 5, 10))
results = st.sidebar.selectbox('Display Summary', ('No','Yes'))
exec(f'from keras.applications.{model_type2} import {model_type}')
exec(
f'from keras.applications.{model_type2} import preprocess_input, decode_predictions')
model = eval(f'{model_type}(weights="imagenet")')
img_path = st.file_uploader("Upload Picture")
img = Image.open(img_path)
st.image(img)
img = img.resize((224, 224)) # Resize to match VGG16 input size
x = np.array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# Make predictions on the image
preds = model.predict(x)
# Convert the predictions to human-readable labels
decoded_preds = decode_predictions(preds, top=top_n)[0]
df = pd.DataFrame(decoded_preds)
df.columns = ['label', 'Object', 'Percent Certainty']
df.index = df.index+1
df = df[['Object', 'Percent Certainty']]
df['Percent Certainty'] = df['Percent Certainty'].apply(
lambda x: '{:.2%}'.format(x))
st.dataframe(df)
if results=='Yes':
stringlist = []
model.summary(print_fn=lambda x: stringlist.append(x))
short_model_summary = "\n".join(stringlist)
print(short_model_summary)
st.write(short_model_summary)