Spaces:
Sleeping
Sleeping
Commit
·
19f993d
1
Parent(s):
384d08e
Upload 2 files
Browse files- app.py +49 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Mon Apr 17 08:43:48 2023
|
| 4 |
+
|
| 5 |
+
@author: mritchey
|
| 6 |
+
"""
|
| 7 |
+
# import keras
|
| 8 |
+
import streamlit as st
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
model_type = st.sidebar.selectbox(
|
| 14 |
+
'Select Model', ('VGG16', 'VGG19', 'ResNet50V2', 'MobileNetV2'))
|
| 15 |
+
models = {'VGG16': 'vgg16', 'VGG19': 'vgg16', 'ResNet50V2': 'resnet_v2',
|
| 16 |
+
'MobileNetV2': 'mobilenet_v2'}
|
| 17 |
+
model_type2 = models[model_type]
|
| 18 |
+
|
| 19 |
+
top_n = st.sidebar.selectbox('Number of Results', (3, 5, 10))
|
| 20 |
+
|
| 21 |
+
exec(f'from keras.applications.{model_type2} import {model_type}')
|
| 22 |
+
exec(
|
| 23 |
+
f'from keras.applications.{model_type2} import preprocess_input, decode_predictions')
|
| 24 |
+
model = eval(f'{model_type}(weights="imagenet")')
|
| 25 |
+
|
| 26 |
+
img_path = st.file_uploader("Upload Picture")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
img = Image.open(img_path)
|
| 30 |
+
st.image(img)
|
| 31 |
+
|
| 32 |
+
img = img.resize((224, 224)) # Resize to match VGG16 input size
|
| 33 |
+
x = np.array(img)
|
| 34 |
+
x = np.expand_dims(x, axis=0)
|
| 35 |
+
x = preprocess_input(x)
|
| 36 |
+
|
| 37 |
+
# Make predictions on the image
|
| 38 |
+
preds = model.predict(x)
|
| 39 |
+
# Convert the predictions to human-readable labels
|
| 40 |
+
decoded_preds = decode_predictions(preds, top=top_n)[0]
|
| 41 |
+
|
| 42 |
+
df = pd.DataFrame(decoded_preds)
|
| 43 |
+
df.columns = ['label', 'Object', 'Percent Certainty']
|
| 44 |
+
df.index = df.index+1
|
| 45 |
+
df = df[['Object', 'Percent Certainty']]
|
| 46 |
+
df['Percent Certainty'] = df['Percent Certainty'].apply(
|
| 47 |
+
lambda x: '{:.2%}'.format(x))
|
| 48 |
+
|
| 49 |
+
st.dataframe(df)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
Pillow
|
| 4 |
+
streamlit
|
| 5 |
+
keras
|