Spaces:
Runtime error
Runtime error
ferdmartin
commited on
Commit
•
ed5cf1f
1
Parent(s):
9c300d3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!streamlit/bin/python
|
2 |
+
import streamlit as st
|
3 |
+
from pathlib import Path
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
+
from PIL import Image
|
8 |
+
from io import BytesIO
|
9 |
+
import json
|
10 |
+
#from GDownload import download_file_from_google_drive
|
11 |
+
|
12 |
+
@st.cache(allow_output_mutation=True)
|
13 |
+
def load_model():
|
14 |
+
# if selected_model == 'PVAN-Stanford':
|
15 |
+
# model_location = '1-q1R5dLfIFW7BbzKuYTjolAoqpjVClsb'
|
16 |
+
# save_dest = Path('saved_model')
|
17 |
+
# save_dest.mkdir(exist_ok=True)
|
18 |
+
# saved_model = Path("saved_model/FerNet_EfficientNet.h5")
|
19 |
+
|
20 |
+
# elif selected_model == 'PVAN-Tsinghua':
|
21 |
+
# model_location = '1-q1R5dLfIFW7BbzKuYTjolAoqpjVClsb'
|
22 |
+
# save_dest = Path('saved_model')
|
23 |
+
# save_dest.mkdir(exist_ok=True)
|
24 |
+
# saved_model = Path("saved_model/FerNet_EfficientNet.h5")
|
25 |
+
|
26 |
+
# if not saved_model.exists():
|
27 |
+
# download_file_from_google_drive(model_location, saved_model)
|
28 |
+
saved_model = str(Path().parent.absolute())+"/saved_models/FerNetEfficientNetB2"
|
29 |
+
saved_model = tf.keras.models.load_model(saved_model)
|
30 |
+
return saved_model
|
31 |
+
|
32 |
+
@st.cache
|
33 |
+
def load_classes():
|
34 |
+
with open(str(Path().parent.absolute())+'/App/classes_dict.json') as classes:
|
35 |
+
class_names = json.load(classes)
|
36 |
+
return class_names
|
37 |
+
|
38 |
+
def load_and_prep_image(filename, img_shape=260):
|
39 |
+
#img = tf.io.read_file(filename)
|
40 |
+
img = np.array(filename)#tf.io.decode_image(filename, channels=3)
|
41 |
+
# Resize our image
|
42 |
+
img = tf.image.resize(img, [img_shape,img_shape])
|
43 |
+
# Scale
|
44 |
+
return img # don't need to resclae images for EfficientNet models in Tensorflow
|
45 |
+
|
46 |
+
if __name__ == '__main__':
|
47 |
+
|
48 |
+
hide_st_style = """
|
49 |
+
<style>
|
50 |
+
footer {visibility: hidden;}
|
51 |
+
header {visibility: hidden;}
|
52 |
+
</style>
|
53 |
+
"""
|
54 |
+
st.markdown(hide_st_style, unsafe_allow_html=True)
|
55 |
+
|
56 |
+
st.title("Dog Breeds Detector")
|
57 |
+
|
58 |
+
options = ['PVAN-Stanford', 'PVAN-Tsinghua']
|
59 |
+
selected_model = st.selectbox('Select a model to use (Default: PVAN-Stanford):', options)
|
60 |
+
|
61 |
+
saved_model = load_model()
|
62 |
+
class_names = load_classes()
|
63 |
+
|
64 |
+
st.write("Choose any dog image and get the corresponding breed:")
|
65 |
+
|
66 |
+
uploaded_image = st.file_uploader("Choose an image...")
|
67 |
+
|
68 |
+
if uploaded_image:
|
69 |
+
uploaded_image = Image.open(uploaded_image)
|
70 |
+
# try:
|
71 |
+
uploaded_image = uploaded_image.convert("RGB")
|
72 |
+
membuf = BytesIO()
|
73 |
+
uploaded_image.save(membuf, format="jpeg")
|
74 |
+
uploaded_image = Image.open(membuf)
|
75 |
+
# finally:
|
76 |
+
|
77 |
+
|
78 |
+
image_for_the_model = load_and_prep_image(uploaded_image)
|
79 |
+
prediction = saved_model.predict(tf.expand_dims(image_for_the_model, axis=0), verbose=0)
|
80 |
+
|
81 |
+
top_k_proba, top_k_indices = tf.nn.top_k(prediction,k=5)
|
82 |
+
top_5_classes = {top_n+1:class_names[str(top_k)] for top_n, top_k in enumerate(list(tf.squeeze(top_k_indices).numpy()))}
|
83 |
+
top_k_proba = tf.squeeze(top_k_proba).numpy()
|
84 |
+
top_5_classes = pd.DataFrame({"Top-k":top_5_classes.keys(), "Dog Breed": top_5_classes.values(), "Probability": top_k_proba})
|
85 |
+
#top_5_classes.set_index("Top-k", inplace=True)
|
86 |
+
|
87 |
+
print(tf.argmax(prediction, axis=1).numpy())
|
88 |
+
predicted_breed = class_names[str(tf.argmax(prediction, axis=1).numpy()[0])]
|
89 |
+
predicted_breed = ' '.join(predicted_breed.split('_'))
|
90 |
+
predicted_breed = predicted_breed.title()
|
91 |
+
st.header(f'This dog looks like a {predicted_breed}')
|
92 |
+
|
93 |
+
col1, col2 = st.columns([1,2])
|
94 |
+
|
95 |
+
col1.image(uploaded_image,use_column_width=True)
|
96 |
+
col2.bar_chart(top_5_classes, x="Dog Breed", y="Probability")
|