Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
import time
|
3 |
-
|
4 |
-
import cv2
|
5 |
import pandas
|
6 |
from PIL import Image
|
7 |
import numpy as np
|
@@ -9,49 +6,76 @@ import tensorflow as tf
|
|
9 |
from tensorflow.keras.applications.resnet50 import preprocess_input
|
10 |
from tensorflow.keras.preprocessing.image import img_to_array
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
st.
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
if 'file_uploader_key' not in state:
|
26 |
-
state['file_uploader_key'] = 0
|
27 |
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
if
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
import pandas
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
6 |
from tensorflow.keras.applications.resnet50 import preprocess_input
|
7 |
from tensorflow.keras.preprocessing.image import img_to_array
|
8 |
|
9 |
+
def main():
|
10 |
+
st.title('Jacaranda Identification')
|
11 |
+
|
12 |
+
st.markdown("This is a Deep Learning application to identify if a satellite image clip contains Jacaranda trees.\n")
|
13 |
+
st.markdown('The predicting result will be "Jacaranda", or "Others".')
|
14 |
+
st.markdown('You can click "Browse files" multiple times until adding all images before generating prediction.\n')
|
15 |
|
16 |
+
run_the_app()
|
17 |
+
|
18 |
+
@st.cache_resource()#(allow_output_mutation=True)
|
19 |
+
def load_model():
|
20 |
+
# Load the network. Because this is cached it will only happen once.
|
21 |
+
model = tf.keras.models.load_model('model')
|
22 |
+
return model
|
23 |
|
24 |
+
@st.cache_data()
|
25 |
+
def generate_df():
|
26 |
+
dict = {'Image file name':[],
|
27 |
+
'Class name': []
|
28 |
+
}
|
29 |
+
df = pd.DataFrame(dict)
|
30 |
+
return df
|
31 |
+
|
32 |
+
@st.cache_data()
|
33 |
+
def write_df(df, file, cls):
|
34 |
+
rec = {'Image file name': file,
|
35 |
+
'Class name': cls}
|
36 |
+
df.append(rec, ignore_index = True)
|
37 |
|
38 |
+
@st.cache_data()
|
39 |
+
def convert_df(df):
|
40 |
+
return df.to_csv(index=False, encoding='utf-8')
|
41 |
|
42 |
+
def run_the_app():
|
|
|
|
|
43 |
|
44 |
+
class_names = ['Jacaranda', 'Others']
|
45 |
+
model = load_model()
|
46 |
+
df = generate_df()
|
47 |
|
48 |
+
uploaded_files = st.file_uploader(
|
49 |
+
"Upload images",
|
50 |
+
type="jpg" or 'jpeg' or 'bmp' or 'png' or 'tif',
|
51 |
+
accept_multiple_files=True)
|
52 |
+
|
53 |
+
if uploaded_files:
|
54 |
+
st.image(uploaded_files, width=100)
|
55 |
|
56 |
+
if st.button("Clear uploaded images"):
|
57 |
+
st.empty()
|
58 |
+
st.experimental_rerun()
|
59 |
|
60 |
+
if st.button("Generate prediction"):
|
61 |
+
for file in uploaded_files:
|
62 |
+
img = Image.open(file)
|
63 |
+
img_array = img_to_array(img)
|
64 |
+
img_array = tf.expand_dims(img_array, axis = 0) # Create a batch
|
65 |
+
processed_image = preprocess_input(img_array)
|
66 |
+
|
67 |
+
predictions = model.predict(processed_image)
|
68 |
+
score = predictions[0]
|
69 |
+
cls = class_names[np.argmax(score)]
|
70 |
+
|
71 |
+
st.markdown("Predicted class of the image {} is : {}".format(file, cls))
|
72 |
+
|
73 |
+
write_df(df, file, cls)
|
74 |
+
|
75 |
+
csv = convert_df(df)
|
76 |
+
st.download_button("Download the results as CSV",
|
77 |
+
data = csv,
|
78 |
+
file_name = "jacaranda_identification.csv")
|
79 |
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|