File size: 2,924 Bytes
cf19c0f
018eb81
89c7879
 
 
 
 
 
f399f98
 
a38bee7
 
 
233cf7e
a38bee7
 
f399f98
2fb6cdd
a38bee7
 
 
 
 
 
 
91bfda6
a38bee7
 
 
 
 
 
 
 
 
 
1818d34
a38bee7
1818d34
 
89c7879
a38bee7
 
 
89c7879
a38bee7
7e772ad
f399f98
 
 
a38bee7
 
 
7e772ad
a38bee7
 
 
 
 
 
 
7e772ad
a38bee7
 
 
7e772ad
a38bee7
 
 
 
 
 
 
 
 
 
 
 
 
214e0e3
a38bee7
 
 
 
 
7e772ad
a38bee7
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import streamlit as st
import pandas as pd
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input 
from tensorflow.keras.preprocessing.image import img_to_array    

from datasets import load_dataset

def main():
    st.title('Jacaranda Identification')
    
    st.markdown("This is a Deep Learning application to identify if a aerial image clip contains Jacaranda trees.\n")
    st.markdown('The predicting result will be "Jacaranda", or "Others".')
    st.markdown('You can click "Browse files" multiple times until adding all images before generating prediction.\n')
    st.markdown('The image clips can look like these examples. The image size can be arbitrary but better not include too much contents.')

    run_the_app()
    
@st.cache_resource()#(allow_output_mutation=True)
def load_model():
    # Load the network. Because this is cached it will only happen once.
    model = tf.keras.models.load_model('model')
    return model

@st.cache_data()
def generate_df():
    dict = {'Image file name':[],
            'Class name': []
            }
    df = pd.DataFrame(dict)
    return df
   
@st.cache_data()
def write_df(df, file, cls):
    rec = {'Image file name': file.name,
           'Class name': cls}
    df = pd.concat([df, pd.DataFrame([rec])], ignore_index=True)
    return df

@st.cache_data()
def convert_df(df):
    return df.to_csv(index=False, encoding='utf-8')

def run_the_app():

    dataset = load_dataset('jacaranda', split='train')
    st.image(dataset[0:5]['image'])

    class_names = ['Jacaranda', 'Others']
    model = load_model()
    df = generate_df()

    uploaded_files = st.file_uploader(
            "Upload images", 
            type="jpg" or 'jpeg' or 'bmp' or 'png' or 'tif', 
            accept_multiple_files=True)
    
    if uploaded_files:
        st.image(uploaded_files, width=100)

        if st.button("Clear uploaded images"):
            st.empty()
            st.experimental_rerun()

        if st.button("Generate prediction"):
            for file in uploaded_files:
                img = Image.open(file)
                img_array = img_to_array(img)
                img_array = tf.expand_dims(img_array, axis = 0) # Create a batch
                processed_image = preprocess_input(img_array)
                        
                predictions = model.predict(processed_image)
                score = predictions[0]
                cls = class_names[np.argmax(score)]
                
                st.markdown("Predicted class of the image {} is : {}".format(file, cls))
                
                df = write_df(df, file, cls)

            csv = convert_df(df)
            st.download_button("Download the results as CSV", 
                           data = csv, 
                           file_name = "jacaranda_identification.csv")

if __name__ == "__main__":
    main()