File size: 1,366 Bytes
19f993d
 
 
 
 
 
48b5ec8
19f993d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
# -*- 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))

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)