lily-hust commited on
Commit
a38bee7
1 Parent(s): 7e772ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -40
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
- st.title('Jacaranda Identification')
 
 
 
 
 
13
 
14
- st.markdown("This is a Deep Learning application to identify if a satellite image clip contains Jacaranda trees.\n")
15
- st.markdown('The predicting result will be "Jacaranda", or "Others".')
16
- st.markdown('You can click "Browse files" multiple times until adding all images before generating prediction.\n')
 
 
 
 
17
 
18
- img_height = 224
19
- img_width = 224
20
- class_names = ['Jacaranda', 'Others']
 
 
 
 
 
 
 
 
 
 
21
 
22
- model = tf.keras.models.load_model('model')
 
 
23
 
24
- state = st.session_state
25
- if 'file_uploader_key' not in state:
26
- state['file_uploader_key'] = 0
27
 
28
- if "uploaded_files" not in state:
29
- state["uploaded_files"] = []
 
30
 
31
- uploaded_file = st.file_uploader(
32
- "Upload images",
33
- type="jpg" or 'jpeg' or 'bmp' or 'png' or 'tif',
34
- accept_multiple_files=True,
35
- key=state['file_uploader_key'])
 
 
36
 
37
- if uploaded_file is not None:
38
- st.image(uploaded_file, width=100)
39
- state["uploaded_files"] = uploaded_file
40
 
41
- if st.button("Clear uploaded images"):
42
- state["file_uploader_key"] += 1
43
- st.empty()
44
- time.sleep(.5)
45
- st.experimental_rerun()
46
-
47
- if st.button("Generate prediction"):
48
- for file in uploaded_file:
49
- img = Image.open(file)
50
- img_array = img_to_array(img)
51
- img_array = tf.expand_dims(img_array, axis = 0) # Create a batch
52
- processed_image = preprocess_input(img_array)
53
-
54
- predictions = model.predict(processed_image)
55
- score = predictions[0]
56
- st.markdown("Predicted class of the image {} is : {}".format(file, class_names[np.argmax(score)]))
 
 
 
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()