astro21's picture
Update app.py
771690f verified
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()