File size: 9,794 Bytes
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
5006716
 
 
e8c34ca
 
 
771690f
e8c34ca
 
2bcf521
 
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5470b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8c34ca
5470b62
 
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfacabc
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5006716
 
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
 
 
5470b62
 
e8c34ca
 
5470b62
 
 
 
e8c34ca
 
 
 
 
5006716
 
 
 
 
 
 
 
e8c34ca
 
 
 
 
 
 
5006716
e8c34ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00bf342
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import streamlit as st
from PIL import Image
import tensorflow as tf
import librosa
import numpy as np
import io
import os
import gc
import matplotlib.pyplot as plt
import librosa.display
from datetime import datetime
import random
from tensorflow.keras.preprocessing import image 
from tensorflow.keras.preprocessing.image import load_img, img_to_array

import matplotlib



# load assets

ppt = Image.open(r"posters/Avifauna Acoustics Detection.png")
dataset_img = Image.open("posters/dataset.png")
# about_img = Image.open("poster/about.png")


# Set Matplotlib backend
matplotlib.use("Agg")

# Define the classes/species mapping
class_labels = {
    0: 'affinis',
    1: 'asiaticus',
    2: 'indicus',
    3: 'mystery',
    4: 'smyrnensis',
    5: 'sonneratii',
    6: 'striata',
    7: 'sutorius',
    8: 'xanthornus'
}

# Function to preprocess audio for CNN models
N_FFT = 1024
HOP_SIZE = 1024
N_MELS = 128
WIN_SIZE = 1024
WINDOW_TYPE = "hann"
FEATURE = "mel"
FMIN = 0
def preprocess_and_return_spectrogram_from_signal(signal):
    # Plot the spectrogram
    plt.figure(figsize=(10, 4))
    D = librosa.amplitude_to_db(np.abs(librosa.stft(signal)), ref=np.max)
    librosa.display.specshow(D, y_axis="linear")
    plt.colorbar(format="%+2.0f dB")
    plt.title("Linear-frequency power spectrogram")

    # Create a BytesIO object to capture the image data
    image_buffer = io.BytesIO()

    # Save the spectrogram image to the BytesIO object
    plt.savefig(image_buffer, format="png", bbox_inches="tight")
    plt.close()

    # Reset the BytesIO object's position to the beginning
    image_buffer.seek(0)

    # Return the BytesIO object
    return image_buffer

def preprocess_and_save_spectrogram_from_signal(signal, image_path):
    # Plot the spectrogram and save it
    plt.figure(figsize=(10, 4))
    D = librosa.amplitude_to_db(np.abs(librosa.stft(signal)), ref=np.max)
    librosa.display.specshow(D, y_axis="linear")
    plt.colorbar(format="%+2.0f dB")
    plt.title("Linear-frequency power spectrogram")

    # Save the spectrogram image to the species folder
    plt.savefig(image_path, format="png", bbox_inches="tight")
    plt.close()


def preprocess_and_save_spectrogram_from_signal_v2(signal, image_path):
    fig, ax = plt.subplots(figsize=(10, 4))
    D = librosa.amplitude_to_db(np.abs(librosa.stft(signal)), ref=np.max)
    librosa.display.specshow(D, y_axis="linear")

    try:
        plt.savefig(image_path, format="png", bbox_inches="tight")

    except Exception as e:
        print(f"Error processing {image_path}: {e}")

        plt.close(fig)


# Streamlit app with two columns
st.set_page_config(layout="wide", page_title="Avifauna Acoustics Detection", page_icon=":bird:")

st.title('Avifauna Acoustics Detection')

def home_page():
    st.title("Avifauna Acoustics Detection")
    with st.container():
        st.subheader("Introduction")
        st.write(
            "Avifauna Acoustics Detection is a field in ornithology and machine learning that involves identifying different bird species based on their vocalizations and physical characteristics. It plays a crucial role in bird conservation, ecology, and birdwatching. Bird vocalizations, such as songs and calls, are unique to each species and serve as an essential way to distinguish them. Additionally, machine learning and computer vision techniques are used to identify birds based on their physical attributes, including plumage, beak shape, and size. Avifauna Acoustics Detection can be valuable for tracking migration patterns, studying bird behavior, and monitoring populations.")

        st.write("---")
        st.subheader("Key Features")
        st.write(
            """
            - Our Avifauna Acoustics Detection application is designed to identify various bird species from audio recordings and images.
            - The application utilizes machine learning models trained on extensive datasets of bird songs, calls, and images.
            - We have employed deep learning models, including Convolutional Neural Networks (CNNs) for image recognition and recurrent neural networks (RNNs) for audio analysis.
            - Users can either upload bird audio recordings or bird images to get predictions about the species.
            - The application can provide information about the bird's common habitats, migration patterns, and conservation status, enhancing the user's birdwatching experience.""")
        st.write("---")
        st.subheader("Problem Statement")
        st.write(
            "Our goal is to develop a reliable tool for bird enthusiasts, ornithologists, and conservationists to "
            "easily identify bird species from their vocalizations and visual characteristics. We aim to address the "
            "challenge of accurate Avifauna Acoustics Detection by leveraging advanced machine learning techniques. "
            "This can aid in monitoring bird populations, understanding their behavior, and contributing to "
            "conservation efforts.")

        st.write("---")
        st.subheader("Future Scope")
        st.write(
            "Avifauna Acoustics Detection can have a profound impact on bird conservation and environmental research. "
            "In the future, we envision expanding the application's capabilities by incorporating real-time "
            "recognition using smartphones. This can assist in on-the-fly bird identification during birdwatching "
            "excursions. Additionally, we can collaborate with researchers to collect more extensive datasets and "
            "improve the accuracy of our models. Such advancements can play a vital role in preserving bird "
            "biodiversity and understanding their role in ecosystems.")

        with st.container():
            st.write("---")
            st.write("##")
            with st.container():
                st.write("---")
                st.write("##")
                image_column, text_column = st.columns((1, 2))
                with image_column:
                    st.image(ppt, use_column_width=True)
                with text_column:
                    st.subheader("Avifauna Acoustics Detection - PPT")
                    st.write(
                        """
                        This PPT explains the overall project in brief. 
                        """
                    )
                    link_str = "https://www.canva.com/design/DAFqmtxqCeU/d77pL4cFeSGat4rWpexioQ/view?utm_content=DAFqmtxqCeU&utm_campaign=designshare&utm_medium=link&utm_source=publishsharelink"
                    # link_str2 = "https://drive.google.com/drive/folders/1cFb_WIXBSvzkGFMEtjxAtnz502aEXSM4?usp=sharing"
                    st.markdown(f"[View]({link_str})")

            with st.container():
                st.write("---")
                st.write("##")
                image_column, text_column = st.columns((1, 2))
                with image_column:
                    st.image(dataset_img, use_column_width=True)
                with text_column:
                    st.subheader("Dataset - Xeno-canto")
                    st.write(
                        """
                        """
                    )
                    link_str = "https://xeno-canto.org/"
                    st.markdown(f"[View]({link_str})")

def model():
    # Create a sidebar for model selection
    with st.sidebar:
        st.write('Model Selection')
        model_files = [f for f in os.listdir('model') if f.endswith('.h5')]
        selected_model = st.selectbox('Select a model', model_files)

    # Create two columns
    col1, col2 = st.columns(2)

    # Upload an MP3 audio file
    with col1:
        audio_file = st.file_uploader('Upload an MP3 audio file', type=['mp3'])
        dropdown = st.selectbox('Select Actual Bird Species', class_labels.values())


        if audio_file is not None:
            st.write('Processing...')

            # Load the audio for playback
            y, sr = librosa.load(audio_file)

            # Play the uploaded audio using st.audio()
            st.audio(audio_file, format="audio/mp3", start_time=0)

            # Predict button
            if st.button('Predict'):
                st.write('Predicting using', selected_model)

               
                


                image_buffer = preprocess_and_return_spectrogram_from_signal(y)
                image = Image.open(image_buffer)
                st.image(image, caption='Spectrogram', use_column_width=True)
                
                # Load the selected model
                model_path = os.path.join('model', selected_model)
                model = tf.keras.models.load_model(model_path)

                # Load and preprocess the image
                # Load the image directly from the BytesIO object
                image = load_img(image_buffer, target_size=(224, 224))
                
                # Convert the image to a NumPy array
                img_array = img_to_array(image)
                
                # Expand the dimensions to make it compatible with your model
                img = np.expand_dims(img_array, axis=0)

                # Make a prediction
                predicted_class_index = model.predict(img, verbose=1)
                print(predicted_class_index)
                predicted_class = class_labels[predicted_class_index.argmax()]

                # Display the predicted bird species
                st.write('Actual Bird Species:', dropdown)
                st.write('Predicted Bird Species:', predicted_class)

# create a Streamlit app
# def about_us():
#     # st.image(about_img)


def app():
    tab1, tab2 = st.tabs(["Our Project", "Model"])
    with tab1:
        home_page()
    with tab2:
        model()
    # with tab3:
    #     # about_us()


app()